{"library":"opentelemetry-resourcedetector-gcp","code":"import os\nfrom opentelemetry import trace\nfrom opentelemetry.sdk.resources import get_aggregated_resources\nfrom opentelemetry.sdk.trace import TracerProvider\nfrom opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor\nfrom opentelemetry.resourcedetector.gcp_resource_detector import GoogleCloudResourceDetector\n\n# The detector automatically queries the GCP metadata server.\n# Ensure your application is running in a GCP environment for full detection.\nresource = get_aggregated_resources(\n    [GoogleCloudResourceDetector(raise_on_error=True)]\n)\n\n# Create a TracerProvider with the detected resource\ntracer_provider = TracerProvider(resource=resource)\ntrace.set_tracer_provider(tracer_provider)\n\n# For demonstration, export to console. In a real application, use a GCP exporter.\nspan_processor = SimpleSpanProcessor(ConsoleSpanExporter())\ntracer_provider.add_span_processor(span_processor)\n\ntracer = trace.get_tracer(__name__)\n\nwith tracer.start_as_current_span(\"my-gcp-operation\") as span:\n    span.set_attribute(\"custom.attribute\", \"value\")\n    print(f\"Span created with detected resource attributes: {resource.attributes}\")\n    # In a real application, this would be exported to Cloud Trace or another backend\n\nprint(\"Resource detection example complete.\")","lang":"python","description":"This quickstart demonstrates how to initialize the OpenTelemetry `TracerProvider` with `GoogleCloudResourceDetector`. The detector runs automatically when `get_aggregated_resources` is called, querying the GCP metadata server to identify the environment and populate standard OpenTelemetry resource attributes. Ensure the code runs within a Google Cloud environment (e.g., GCE, GKE, Cloud Run) for effective detection. The example then creates a basic span, which will inherit the detected resource attributes.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}