Microsoft Opentelemetry exporter for Azure Monitor¶
The exporter for Azure Monitor allows you to export tracing data utilizing the OpenTelemetry SDK and send telemetry data to Azure Monitor for applications written in Python.
Install the package¶
Install the Microsoft Opentelemetry exporter for Azure Monitor with pip:
pip install azure-opentelemetry-exporter-azuremonitor --pre
To use this package, you must have:
Authenticate the client¶
Interaction with Azure monitor exporter starts with an instance of the
AzureMonitorTraceExporter class. You will need a connection_string to instantiate the object.
Please find the samples linked below for demonstration as to how to authenticate using a connection string.
Some of the key concepts for the Azure monitor exporter include:
Opentelemetry: Opentelemetry is a set of libraries used to collect and export telemetry data (metrics, logs, and traces) for analysis in order to understand your software’s performance and behavior.
Instrumentation: The ability to call the opentelemetry API directly by any application is facilitated by instrumentaton. A library that enables OpenTelemetry observability for another library is called an Instrumentation Library.
Trace: Trace refers to distributed tracing. It can be thought of as a directed acyclic graph (DAG) of Spans, where the edges between Spans are defined as parent/child relationship.
Tracer Provider: Provides a
Tracerfor use by the given instrumentation library.
Span Processor: A span processor allows hooks for SDK’s
Spanstart and end method invocations. Follow the link for more information.
Sampling: Sampling is a mechanism to control the noise and overhead introduced by OpenTelemetry by reducing the number of samples of traces collected and sent to the backend.
AzureMonitorTraceExporter: This is the class that is initialized to send tracing related telemetry to Azure Monitor.
Exporter Options: Options to configure Azure exporters. Currently only includes connection_string.
For more information about these resources, see What is Azure Monitor?.
The following sections provide several code snippets covering some of the most common tasks, including:
Export Hello World Trace¶
import os from opentelemetry import trace from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchExportSpanProcessor from azure.opentelemetry.exporter.azuremonitor import AzureMonitorTraceExporter exporter = AzureMonitorTraceExporter( connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING "] ) trace.set_tracer_provider(TracerProvider()) tracer = trace.get_tracer(__name__) span_processor = BatchExportSpanProcessor(exporter) trace.get_tracer_provider().add_span_processor(span_processor) with tracer.start_as_current_span("hello"): print("Hello, World!")
Instrumentation with requests library¶
OpenTelemetry also supports several instrumentations which allows to instrument with third party libraries.
This example shows how to instrument with the requests library.
Install the requests integration package using pip install opentelemetry-instrumentation-requests.
import os import requests from opentelemetry import trace from opentelemetry.instrumentation.requests import RequestsInstrumentor from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchExportSpanProcessor from azure.opentelemetry.exporter.azuremonitor import AzureMonitorTraceExporter trace.set_tracer_provider(TracerProvider()) tracer = trace.get_tracer(__name__) # This line causes your calls made with the requests library to be tracked. RequestsInstrumentor().instrument() span_processor = BatchExportSpanProcessor( AzureMonitorTraceExporter( connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING "] ) ) trace.get_tracer_provider().add_span_processor(span_processor) RequestsInstrumentor().instrument() # This request will be traced response = requests.get(url="https://azure.microsoft.com/")
More sample code¶
Please find further examples in the samples directory demonstrating common scenarios.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.