pip install opentelemetry-azure-monitor
The online documentation is available at https://opentelemetry-azure-monitor-python.readthedocs.io/.
The Azure Monitor Span Exporter allows you to export OpenTelemetry traces to Azure Monitor.
This example shows how to send a span "hello" to Azure Monitor.
- Create an Azure Monitor resource and get the instrumentation key, more information can be found here.
- Place your instrumentation key in a
connection string
and directly into your code. - Alternatively, you can specify your
connection string
in an environment variableAPPLICATIONINSIGHTS_CONNECTION_STRING
.
from azure_monitor import AzureMonitorSpanExporter
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor
trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
# SpanExporter receives the spans and send them to the target location
exporter = AzureMonitorSpanExporter(
connection_string='InstrumentationKey=<your-ikey-here>',
)
span_processor = BatchExportSpanProcessor(exporter)
trace.get_tracer_provider().add_span_processor(span_processor)
with tracer.start_as_current_span('hello'):
print('Hello World!')
OpenTelemetry also supports several integrations which allows to integrate with third party libraries.
This example shows how to integrate with the requests_ library.
- Create an Azure Monitor resource and get the instrumentation key, more information can be found here.
- Install the
requests
integration package usingpip install opentelemetry-ext-http-requests
. - Place your instrumentation key in a
connection string
and directly into your code. - Alternatively, you can specify your
connection string
in an environment variableAPPLICATIONINSIGHTS_CONNECTION_STRING
.
import requests
from azure_monitor import AzureMonitorSpanExporter
from opentelemetry import trace
from opentelemetry.ext.requests import RequestsInstrumentor
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor
trace.set_tracer_provider(TracerProvider())
tracer_provider = trace.get_tracer_provider()
exporter = AzureMonitorSpanExporter(
connection_string='InstrumentationKey=<your-ikey-here>',
)
span_processor = BatchExportSpanProcessor(exporter)
tracer_provider.add_span_processor(span_processor)
RequestsInstrumentor().instrument()
# This request will be traced
response = requests.get(url="https://azure.microsoft.com/")
- You 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
handlesData
data types.
from azure_monitor import AzureMonitorSpanExporter
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor
# Callback function to add os_type: linux to span properties
def callback_function(envelope):
envelope.data.baseData.properties['os_type'] = 'linux'
return True
exporter = AzureMonitorSpanExporter(
connection_string='InstrumentationKey=<your-ikey-here>'
)
# This line will modify telemetry
exporter.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)
with tracer.start_as_current_span('hello'):
print('Hello World!')
The Azure Monitor Metrics Exporter allows you to export metrics to Azure Monitor.
This example shows how to track a counter metric and send it as telemetry every export interval.
- Create an Azure Monitor resource and get the instrumentation key, more information can be found here.
- Place your instrumentation key in a
connection string
and directly into your code. - Alternatively, you can specify your
connection string
in an environment variableAPPLICATIONINSIGHTS_CONNECTION_STRING
.
from azure_monitor import AzureMonitorMetricsExporter
from opentelemetry import metrics
from opentelemetry.sdk.metrics import Counter, MeterProvider
from opentelemetry.sdk.metrics.export.controller import PushController
metrics.set_meter_provider(MeterProvider())
meter = metrics.get_meter(__name__)
exporter = AzureMonitorMetricsExporter(
connection_string='InstrumentationKey=<your-ikey-here>'
)
controller = PushController(meter, exporter, 5)
requests_counter = meter.create_metric(
name="requests",
description="number of requests",
unit="1",
value_type=int,
metric_type=Counter,
label_keys=("environment",),
)
testing_labels = {"environment": "testing"}
requests_counter.add(25, testing_labels)
input("...")