Package: microsoft-opentelemetry-exporter-azuremonitor
Microsoft Opentelemetry exporter for Azure MonitorThe 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.Source code|Package (PyPi)|API reference documentation|Product documentation|Samples|ChangelogNOTE: This is the preview for the next major version ofopentelemetry-azure-monitor-python.Getting startedInstall the packageInstall the Microsoft Opentelemetry exporter for Azure Monitor withpip:pipinstallmicrosoft-opentelemetry-exporter-azuremonitor--prePrerequisites:To use this package, you must have:Azure subscription -Create a free accountAzure Monitor -How to use application insightsOpentelemetry SDK -Opentelemtry SDK for PythonPython 3.5 or later -Install PythonAuthenticate the clientInteraction with Azure monitor exporter starts with an instance of theAzureMonitorSpanExporterclass. You will need aconnection_stringto instantiate the object.
Please find the samples linked below for demonstration as to how to authenticate using a connection string.Create Exporter from connection stringfrommicrosoft.opentelemetry.exporter.azuremonitorimportAzureMonitorSpanExporterexporter=AzureMonitorSpanExporter(connection_string=os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING "])Key conceptsSome 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 aTracerfor use by the given instrumentation library.Span Processor: A span processor allows hooks for SDK'sSpanstart 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.AzureMonitorSpanExporter: This is the class that is initialized to send tracing related telemetry to Azure Monitor.Exporter Options: Options to configure Azure exporters. Includes connection_string, instrumentation_key, proxies, timeout etc.For more information about these resources, seeWhat is Azure Monitor?.ExamplesThe following sections provide several code snippets covering some of the most common tasks, including:Exporting a custom spanModifying spansUsing an instrumentation to track a libraryExport Hello World Traceimportosfromopentelemetryimporttracefromopentelemetry.sdk.traceimportTracerProviderfromopentelemetry.sdk.trace.exportimportBatchExportSpanProcessorfrommicrosoft.opentelemetry.exporter.azuremonitorimportAzureMonitorSpanExporterexporter=AzureMonitorSpanExporter(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)withtracer.start_as_current_span("hello"):print("Hello, World!")Modifying TracesYou can pass a callback function to the exporter to process telemetry before it is exported.Your callback function can return False if you do not want this envelope exported.Your callback function must accept an envelope data type as its parameter.You can see the schema for Azure Monitor data types in the envelopes here.The AzureMonitorSpanExporter handles Data data types.frommicrosoft.opentelemetry.exporter.azuremonitorimportAzureMonitorSpanExporterfromopentelemetryimporttracefromopentelemetry.sdk.traceimportTracerProviderfromopentelemetry.sdk.trace.exportimportBatchExportSpanProcessor# Callback function to add os_type: linux to span propertiesdefcallback_function(envelope):envelope.data.baseData.properties['os_type']='linux'returnTrueexporter=AzureMonitorSpanExporter(connection_string='InstrumentationKey=<your-ikey-here>')# This line will modify telemetryexporter.add_telemetry_processor(callback_function)trace.set_tracer_provider(TracerProvider())tracer=trace.get_tracer(__name__)span_processor=BatchExportSpanProcessor(exporter)trace.get_tracer_provider().add_span_processor(span_processor)withtracer.start_as_current_span('hello'):print('Hello World!')Instrumentation with requests libraryOpenTelemetry also supports several instrumentations which allows to instrument with third party libraries.This example shows how to instrument with therequestslibrary.Install the requests integration package using pip install opentelemetry-instrumentation-requests.importosimportrequestsfromopentelemetryimporttracefromopentelemetry.instrumentation.requestsimportRequestsInstrumentorfromopentelemetry.sdk.traceimportTracerProviderfromopentelemetry.sdk.trace.exportimportBatchExportSpanProcessorfrommicrosoft.opentelemetry.exporter.azuremonitorimportAzureMonitorSpanExportertrace.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(AzureMonitorSpanExporter(connection_string=os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING "]))trace.get_tracer_provider().add_span_processor(span_processor)RequestsInstrumentor().instrument()# This request will be tracedresponse=requests.get(url="https://azure.microsoft.com/")TroubleshootingThe exporter raises exceptions defined inAzure Core.Next stepsMore sample codePlease find further examples in thesamplesdirectory demonstrating common scenarios.Additional documentationFor more extensive documentation on the Azure Monitor service, see theAzure Monitor documentationon docs.microsoft.com.For detailed overview of Opentelemetry, visit theiroverviewpage.ContributingThis 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, visithttps://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.This project has adopted theMicrosoft Open Source Code of Conduct.
For more information see theCode of Conduct FAQor
contactopencode@microsoft.comwith any additional questions or comments.