Skip to main content

Microsoft Azure Monitor Opentelemetry Exporter Client Library for Python

Project description

Microsoft Opentelemetry exporter for Azure Monitor

Gitter chat

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.

Source code | Package (PyPi) | API reference documentation | Product documentation | Samples | Changelog

Getting started

Install the package

Install the Microsoft Opentelemetry exporter for Azure Monitor with pip:

pip install azure-monitor-opentelemetry-exporter --pre

Prerequisites:

To use this package, you must have:

Instantiate 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 construct the exporter using a connection string.

Create Exporter from connection string

from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
exporter = AzureMonitorTraceExporter.from_connection_string(
    conn_str = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
)

You can also instantiate the exporter directly via the constructor. In this case, the connection string will be automatically populated from the APPLICATIONINSIGHTS_CONNECTION_STRING environment variable.

from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
exporter = AzureMonitorTraceExporter()

Key concepts

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 instrumentation. 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 Tracer for use by the given instrumentation library.

  • Span Processor: A span processor allows hooks for SDK's Span start 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.

For more information about these resources, see What is Azure Monitor?.

Examples

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 BatchSpanProcessor
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter

exporter = AzureMonitorTraceExporter.from_connection_string(
    connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING "]
)

trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
span_processor = BatchSpanProcessor(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 BatchSpanProcessor

from azure.monitor.opentelemetry.exporter 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 = BatchSpanProcessor(
    AzureMonitorTraceExporter.from_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/")

Troubleshooting

The exporter raises exceptions defined in Azure Core.

Next steps

More sample code

Please find further examples in the samples directory demonstrating common scenarios.

Additional documentation

For more extensive documentation on the Azure Monitor service, see the Azure Monitor documentation on docs.microsoft.com.

For detailed overview of Opentelemetry, visit their overview page.

Contributing

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.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

File details

Details for the file azure-monitor-opentelemetry-exporter-1.0.0b5.zip.

File metadata

  • Download URL: azure-monitor-opentelemetry-exporter-1.0.0b5.zip
  • Upload date:
  • Size: 82.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for azure-monitor-opentelemetry-exporter-1.0.0b5.zip
Algorithm Hash digest
SHA256 13d42f3f44200dabcc7875cacf38b837448041907ce37f45cb780b38064bd02d
MD5 d3ccfa3c6a2f8797b1cb47c0b4b84fdc
BLAKE2b-256 ad6d26ba501a49484341a56635e9378eb2a0beb4f0f7e3a9ab3ebe97fc5e4138

See more details on using hashes here.

File details

Details for the file azure_monitor_opentelemetry_exporter-1.0.0b5-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for azure_monitor_opentelemetry_exporter-1.0.0b5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 31a8404af630211001f66f22e332f76ea5407fb0920ba5e47780429b0dc29d92
MD5 22b6ef628536722c64cc7c90efe68484
BLAKE2b-256 2795e63501a426c44f5284890dbbb0a1c25a8eee2bcacb3ccd49505aa7af75ec

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page