Microsoft Azure Monitor Opentelemetry Exporter Trace Python Samples
These code samples show common champion scenario operations with the AzureMonitorTraceExporter.
Span Event: sample_span_event.py
Trace: sample_trace.py
Collector: sample_collector.py
Jaeger: sample_jaeger.py
Django instrumentation: manage.py
Flask instrumentation: sample_flask.py
Requests instrumentation: sample_requests.py
Psycopg2 instrumentation: sample_psycopg2.py
Emitting metrics with instrumentations: sample_metrics.py
Azure AppConfig Add Config Setting: sample_app_config.py
Azure Communication Chat Create Client/Thread: sample_comm_chat.py
Azure Communication Phone Numbers List Purchased Numbers: sample_comm_phone.py
Azure Communication SMS Send Message: sample_comm_sms.py
Azure CosmosDb Create Db/Container: sample_cosmos.py
Azure EventHub Send EventData: sample_event_hub.py
Azure EventHub Blob Storage Checkpoint Store: sample_blob_checkpoint.py
Azure EventGrid Send Event: sample_event_grid.py
Azure Form Recognizer Analyze Document: sample_form_recognizer.py
Azure KeyVault Create Certificate: sample_key_cert.py
Azure KeyVault Set Secret: sample_key_secret.py
Azure KeyVault Create Keys: sample_key_keys.py
Azure Service Bus Send: sample_servicebus_send.py
Azure Service Bus Receive: sample_servicebus_receive.py
Azure Storage Blob Create Container: sample_storage_blob.py
Azure Azure Text Analytics Extract Key Phrases: sample_text_analytics.py
Installation
$ pip install azure-monitor-opentelemetry-exporter --pre
Run the Applications
Trace
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableRun the sample
$ # from this directory
$ python sample_trace.py
Span Event
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableRun the sample
$ # from this directory
$ python sample_span_event.py
Django instrumentation
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableRun the sample
$ pip install opentelemetry-instrumentation-django
$ # from the django directory
$ python manage.py runserver
- Open http://localhost:8000/
Flask instrumentation
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableRun the sample
$ pip install opentelemetry-instrumentation-flask
$ # from this directory
$ python sample_flask.py
- Open http://localhost:8080/
Requests instrumentation
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableRun the sample
$ pip install opentelemetry-instrumentation-requests
$ # from this directory
$ python sample_requests.py
Psycopg2 instrumentation
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableEnsure you have a database named
test
with a table namedtest_table
, make suretest_table
has a field calledtest_field
Supply your
user
andpassword
for your database if neededRun the sample
$ pip install opentelemetry-instrumentation-psycopg2
$ # from this directory
$ python sample_psycopg2.py
Emitting metrics with instrumentations
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableRun the sample
$ pip install opentelemetry-instrumentation-flask
$ pip install opentelemetry-instrumentation-requests
$ # from this directory
$ python sample_metrics.py
Calls made with the underlying instrumented libraries will track metrics information in the metrics explorer view in Application Insights. Only certain instrumentations support metrics collection, refer to https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/instrumentation/README.md for the full list. This also assumes you are using the latest versions of the instrumentation libraries.
Collector
Start the Collector locally to see how the Collector works in practice.
From the same folder as collector/otel-collector-config.yaml and collector/docker-compose.yml, start the Docker container.
$ docker-compose up
- Install the OpenTelemetry OTLP Exporter
$ pip install opentelemetry-exporter-otlp
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableRun the sample
# from collector directory
$ python sample_collector.py
- You should be able to see your traces in the Zipkin backend as well as Azure Monitor Application Insights backend.
Jaeger
- The Jaeger project provides an all-in-one Docker container with a UI, database, and consumer. Run the following command to start Jaeger:
$ docker run -p 16686:16686 -p 6831:6831/udp jaegertracing/all-in-one
This command starts Jaeger locally on port 16686 and exposes the Jaeger thrift agent on port 6831. You can visit Jaeger at http://localhost:16686.
Install the OpenTelemetry Jaeger Exporter
$ pip install opentelemetry-exporter-jaeger
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableRun the sample
$ # from this directory
$ python sample_jaeger.py
- You should be able to see your traces in the Jaeger backend as well as Azure Monitor Application Insights backend.
Azure AppConfig Add Config Setting
The following sample assumes that you have setup an Azure App Configuration Store.
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableRun the sample
$ # azure-appconfiguration library
$ pip install azure-appconfiguration
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_app_config.py
Azure Communication Chat Create Client/Thread
The following sample assumes that you have setup an Azure Communication Services resource.
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableRun the sample
$ # azure-communication-chat library
$ pip install azure-communication-chat
$ # azure-communication-identity library for authentication
$ pip install azure-communication-identity
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_comm_chat.py
Azure Communication Phone Numbers List Purchased Numbers
The following sample assumes that you have setup an Azure Communication Services resource.
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableRun the sample
$ # azure-communication-phonenumbers library
$ pip install azure-communication-phonenumbers
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_comm_phone.py
Azure Communication SMS Send Message
The following sample assumes that you have setup an Azure Communication Services resource.
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableRun the sample
$ # azure-communication-sms library
$ pip install azure-communication-sms
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_comm_sms.py
Azure CosmosDb Create Db/Container
The following sample assumes that you have setup Azure CosmosDb account.
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableUpdate
ACCOUNT_URI
environment variableUpdate
ACCOUNT_KEY
environment variableRun the sample
$ # azure-cosmos library
$ pip install azure-cosmos
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_cosmos.py
Azure EventHub Send EventData
The following sample assumes that you have setup an Azure EventHubs namespace and EventHub.
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableUpdate
EVENT_HUB_CONN_STR
environment variableUpdate
EVENT_HUB_NAME
environment variableRun the sample
$ # azure-eventhub library
$ pip install azure-eventhub
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_event_hub.py
Azure EventHub Blob Storage Checkpoint Store
The following sample assumes that you have setup an Azure EventHubs namespace, EventHub and Azure Blob storage.
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableUpdate
EVENT_HUB_CONN_STR
environment variableUpdate
EVENT_HUB_NAME
environment variableUpdate
AZURE_STORAGE_CONN_STR
environment variableRun the sample
$ # azure-eventhub-checkpointstoreblob library
$ pip install azure-eventhub-checkpointstoreblob
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_blob_checkpoint.py
Azure EventGrid Send Event
The following sample assumes that you have setup an Azure Event Grid Topic.
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableUpdate
EG_ACCESS_KEY
environment variableUpdate
EG_TOPIC_HOSTNAME
environment variableRun the sample
$ # azure-azure-eventgrid library
$ pip install azure-eventgrid
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_event_grid.py
Azure Form Recognizer Analyze Document
The following sample assumes that you have setup an Azure Form Recognizer Resource.
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableRun the sample
$ # azure-ai-formrecognizer library
$ pip install azure-ai-formrecognizer
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_form_recognizer.py
Azure KeyVault Create Certificate
The following sample assumes that you have setup an Azure Key Vault resource and a service principal for authentication.
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableRun the sample
$ # azure-keyvault-certificates library
$ pip install azure-keyvault-certificates
$ # azure-identity library for authentication
$ pip install azure-identity
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_key_cert.py
Azure KeyVault Set Secret
The following sample assumes that you have setup an Azure Key Vault resource and a service principal for authentication.
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableRun the sample
$ # azure-keyvault-secrets library
$ pip install azure-keyvault-secrets
$ # azure-identity library for authentication
$ pip install azure-identity
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_key_secret.py
Azure KeyVault Create Keys
The following sample assumes that you have setup an Azure Key Vault resource and a service principal for authentication.
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableRun the sample
$ # azure-keyvault-keys library
$ pip install azure-keyvault-keys
$ # azure-identity library for authentication
$ pip install azure-identity
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_key_keys.py
Azure Service Bus Send
The following sample assumes that you have setup an Azure Service Bus namespace.
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableUpdate
SERVICE_BUS_CONN_STR
environment variableUpdate
SERVICE_BUS_QUEUE_NAME
environment variableRun the sample
$ # azure-servicebus library
$ pip install azure-servicebus
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_servicebus_send.py
Azure Service Bus Receive
The following sample assumes that you have setup an Azure Service Bus namespace.
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableUpdate
SERVICE_BUS_CONN_STR
environment variableUpdate
SERVICE_BUS_QUEUE_NAME
environment variableRun the sample
$ # azure-servicebus library
$ pip install azure-servicebus
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_servicebus_receive.py
Azure Storage Blob Create Container
The following sample assumes that you have setup Azure Blob storage.
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableUpdate
AZURE_STORAGE_CONNECTION_STRING
environment variableUpdate
AZURE_STORAGE_BLOB_CONTAINER_NAME
environment variableRun the sample
$ # azure-storage-blob library
$ pip install azure-storage-blob
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_storage_blob.py
Azure Text Analytics Extract Key Phrases
The following sample assumes that you have setup an Azure Cognitive Services Resource.
Update
APPLICATIONINSIGHTS_CONNECTION_STRING
environment variableRun the sample
$ # azure-ai-textanalytics library
$ pip install azure-ai-textanalytics
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_text_analytics.py
Explore the data
After running the applications, data would be available in Azure