Azure Resource Detector for OpenTelemetry
The `opentelemetry-resource-detector-azure` library provides an OpenTelemetry resource detector that automatically gathers Azure-specific metadata (e.g., cloud provider, region, resource ID) and adds it as attributes to exported telemetry data. It's part of the OpenTelemetry Python Contrib repository, currently at version 0.1.5, and follows the release cadence of the wider OpenTelemetry Python project, often receiving updates alongside core SDK releases.
Warnings
- gotcha The Azure Resource Detector requires appropriate Azure credentials and permissions (e.g., Contributor role on the resource group or specific Reader permissions) to query Azure Resource Manager APIs. Without correct authentication (e.g., Managed Identity or `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET` environment variables), the detector will fail to gather metadata, resulting in missing or incomplete resource attributes.
- gotcha Resource attributes are collected at SDK initialization time and are immutable throughout the application's lifecycle. Any changes to the Azure environment or the running resource's metadata after the application starts will not be reflected in the telemetry emitted by the current application instance.
- gotcha The Azure Resource Detector makes network calls to Azure APIs during application startup to fetch resource metadata. This can introduce latency during the initialization phase of your application. Ensure robust network connectivity and consider the performance implications in latency-sensitive applications.
Install
-
pip install opentelemetry-sdk opentelemetry-resource-detector-azure
Imports
- AzureResourceDetector
from opentelemetry.sdk.extension.resource.azure import AzureResourceDetector
- Resource
from opentelemetry.sdk.resources import Resource
- TracerProvider
from opentelemetry.sdk.trace import TracerProvider
- ConsoleSpanExporter
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
- SimpleSpanProcessor
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
Quickstart
import os
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
from opentelemetry.sdk.extension.resource.azure import AzureResourceDetector
# Configure Azure credentials via environment variables for testing purposes
# In a real Azure environment, Managed Identity is often preferred.
# For local testing, ensure these are set:
# os.environ['AZURE_CLIENT_ID'] = os.environ.get('AZURE_CLIENT_ID', '')
# os.environ['AZURE_TENANT_ID'] = os.environ.get('AZURE_TENANT_ID', '')
# os.environ['AZURE_CLIENT_SECRET'] = os.environ.get('AZURE_CLIENT_SECRET', '')
# Instantiate and register the Azure Resource Detector
# The detector makes network calls to Azure APIs, potentially adding startup latency.
# Resource attributes are collected at SDK initialization and are immutable.
resource = Resource.create([AzureResourceDetector()])
# Set up a TracerProvider with the detected resource
provider = TracerProvider(resource=resource)
processor = SimpleSpanProcessor(ConsoleSpanExporter())
provider.add_span_processor(processor)
trace.set_tracer_provider(provider)
# Get a tracer and create a simple span
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("azure-resource-test-span") as span:
span.set_attribute("my.custom.attribute", "hello")
print("Span created. Check console for resource attributes from Azure.")
# Ensure all spans are processed and exported on shutdown
provider.shutdown()