{"id":1610,"library":"opentelemetry-resource-detector-azure","title":"Azure Resource Detector for OpenTelemetry","description":"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.","status":"active","version":"0.1.5","language":"en","source_language":"en","source_url":"https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/resource/opentelemetry-resource-detector-azure","tags":["opentelemetry","observability","azure","telemetry","resource-detector"],"install":[{"cmd":"pip install opentelemetry-sdk opentelemetry-resource-detector-azure","lang":"bash","label":"Install core SDK and Azure detector"}],"dependencies":[{"reason":"Core OpenTelemetry SDK components are required for any detector.","package":"opentelemetry-sdk","optional":false},{"reason":"Required for authenticating to Azure Resource Manager to fetch metadata.","package":"azure-identity","optional":false}],"imports":[{"symbol":"AzureResourceDetector","correct":"from opentelemetry.sdk.extension.resource.azure import AzureResourceDetector"},{"symbol":"Resource","correct":"from opentelemetry.sdk.resources import Resource"},{"symbol":"TracerProvider","correct":"from opentelemetry.sdk.trace import TracerProvider"},{"symbol":"ConsoleSpanExporter","correct":"from opentelemetry.sdk.trace.export import ConsoleSpanExporter"},{"symbol":"SimpleSpanProcessor","correct":"from opentelemetry.sdk.trace.export import SimpleSpanProcessor"}],"quickstart":{"code":"import os\nfrom opentelemetry import trace\nfrom opentelemetry.sdk.resources import Resource\nfrom opentelemetry.sdk.trace import TracerProvider\nfrom opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor\nfrom opentelemetry.sdk.extension.resource.azure import AzureResourceDetector\n\n# Configure Azure credentials via environment variables for testing purposes\n# In a real Azure environment, Managed Identity is often preferred.\n# For local testing, ensure these are set:\n# os.environ['AZURE_CLIENT_ID'] = os.environ.get('AZURE_CLIENT_ID', '')\n# os.environ['AZURE_TENANT_ID'] = os.environ.get('AZURE_TENANT_ID', '')\n# os.environ['AZURE_CLIENT_SECRET'] = os.environ.get('AZURE_CLIENT_SECRET', '')\n\n# Instantiate and register the Azure Resource Detector\n# The detector makes network calls to Azure APIs, potentially adding startup latency.\n# Resource attributes are collected at SDK initialization and are immutable.\nresource = Resource.create([AzureResourceDetector()])\n\n# Set up a TracerProvider with the detected resource\nprovider = TracerProvider(resource=resource)\nprocessor = SimpleSpanProcessor(ConsoleSpanExporter())\nprovider.add_span_processor(processor)\ntrace.set_tracer_provider(provider)\n\n# Get a tracer and create a simple span\ntracer = trace.get_tracer(__name__)\n\nwith tracer.start_as_current_span(\"azure-resource-test-span\") as span:\n    span.set_attribute(\"my.custom.attribute\", \"hello\")\n    print(\"Span created. Check console for resource attributes from Azure.\")\n\n# Ensure all spans are processed and exported on shutdown\nprovider.shutdown()","lang":"python","description":"This quickstart demonstrates how to initialize the `AzureResourceDetector` and integrate it with the OpenTelemetry Python SDK. The detector automatically enriches telemetry with Azure-specific attributes. It requires Azure credentials (via environment variables or Managed Identity) to function correctly. The output on the console will include the automatically detected Azure resource attributes."},"warnings":[{"fix":"Ensure the application's identity has the necessary permissions to read Azure resource metadata. For local development, set `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, and `AZURE_CLIENT_SECRET` environment variables. For Azure deployments, prefer Managed Identities.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Understand that resource attributes are static for a given application run. If dynamic changes are needed, consider custom attributes on spans/metrics or restarting the application to re-initialize the SDK and re-detect resources.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Monitor application startup times. While usually negligible, in environments with strict startup time requirements or slow network egress, this could be a factor. The detector typically caches some results, but the initial fetch is network-dependent.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}