Share via

Microsoft Azure Monitor Opentelemetry Exporter Trace Python Samples

These code samples show common champion scenario operations with the AzureMonitorTraceExporter.

Installation

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

Run the Applications

Trace

  • Update APPLICATIONINSIGHTS_CONNECTION_STRING environment variable

  • Run the sample

$ # from this directory
$ python sample_trace.py

Span Event

  • Update APPLICATIONINSIGHTS_CONNECTION_STRING environment variable

  • Run the sample

$ # from this directory
$ python sample_span_event.py

Django instrumentation

  • Update APPLICATIONINSIGHTS_CONNECTION_STRING environment variable

  • Run 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 variable

  • Run 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 variable

  • Run the sample

$ pip install opentelemetry-instrumentation-requests
$ # from this directory
$ python sample_requests.py

Psycopg2 instrumentation

  • Update APPLICATIONINSIGHTS_CONNECTION_STRING environment variable

  • Ensure you have a database named test with a table named test_table, make sure test_table has a field called test_field

  • Supply your user and password for your database if needed

  • Run the sample

$ pip install opentelemetry-instrumentation-psycopg2
$ # from this directory
$ python sample_psycopg2.py

Emitting metrics with instrumentations

  • Update APPLICATIONINSIGHTS_CONNECTION_STRING environment variable

  • Run 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 variable

  • Run 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 variable

  • Run 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 variable

  • Run 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 variable

  • Run 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 variable

  • Run 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 variable

  • Run 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 variable

  • Update ACCOUNT_URI environment variable

  • Update ACCOUNT_KEY environment variable

  • Run 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 variable

  • Update EVENT_HUB_CONN_STR environment variable

  • Update EVENT_HUB_NAME environment variable

  • Run 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 variable

  • Update EVENT_HUB_CONN_STR environment variable

  • Update EVENT_HUB_NAME environment variable

  • Update AZURE_STORAGE_CONN_STR environment variable

  • Run 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 variable

  • Update EG_ACCESS_KEY environment variable

  • Update EG_TOPIC_HOSTNAME environment variable

  • Run 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 variable

  • Run 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 variable

  • Run 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 variable

  • Run 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 variable

  • Run 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 variable

  • Update SERVICE_BUS_CONN_STR environment variable

  • Update SERVICE_BUS_QUEUE_NAME environment variable

  • Run 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 variable

  • Update SERVICE_BUS_CONN_STR environment variable

  • Update SERVICE_BUS_QUEUE_NAME environment variable

  • Run 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 variable

  • Update AZURE_STORAGE_CONNECTION_STRING environment variable

  • Update AZURE_STORAGE_BLOB_CONTAINER_NAME environment variable

  • Run 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 variable

  • Run 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