Google Cloud Trace Exporter for OpenTelemetry

1.11.0 · active · verified Thu Apr 09

The `opentelemetry-exporter-gcp-trace` library provides an OpenTelemetry `SpanExporter` implementation to send trace data to Google Cloud Trace. It is part of the larger `opentelemetry-operations-python` project maintained by Google, currently at version 1.11.0, with regular updates (typically monthly or bi-monthly) to maintain compatibility with the OpenTelemetry SDK and Google Cloud services.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `GcpTraceExporter` and integrate it with the OpenTelemetry SDK to send traces to Google Cloud Trace. It includes the recommended `GcpResourceDetector` for automatic resource attribute population and highlights the importance of `provider.shutdown()` for complete trace export.

import os
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.gcp_trace import GcpTraceExporter
from opentelemetry_resourcedetector_gcp import GcpResourceDetector

# Configure GCP authentication:
# This exporter uses Application Default Credentials (ADC).
# Ensure GOOGLE_APPLICATION_CREDENTIALS environment variable points to a service account key
# or run on a GCP environment (VM, Cloud Run, GKE, etc.) with appropriate permissions.
# For example, to use a local service account file:
# export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-account-key.json"

# 1. Detect GCP resources automatically (highly recommended)
# This helps populate project ID, instance ID, etc., which are crucial for Cloud Trace.
resource = GcpResourceDetector().detect().merge(
    Resource.create({"service.name": "my-gcp-python-app"})
)

# 2. Set up the TracerProvider with the detected resource
provider = TracerProvider(resource=resource)
trace.set_tracer_provider(provider)

# 3. Instantiate the GCP Trace Exporter
exporter = GcpTraceExporter()

# 4. Set up a BatchSpanProcessor to send traces efficiently
# Spans are batched and sent asynchronously.
span_processor = BatchSpanProcessor(exporter)
provider.add_span_processor(span_processor)

# 5. Get a tracer and create spans
tracer = trace.get_tracer(__name__)

with tracer.start_as_current_span("parent-operation-gcp"):
    print("Performing some work that will be traced in GCP...")
    with tracer.start_as_current_span("child-operation-db"):
        print("Simulating a database call...")
    with tracer.start_as_current_span("child-operation-api"):
        print("Simulating an external API call...")

# 6. Crucially, shut down the provider to ensure all batched spans are exported
# In short-lived scripts, this step is vital. In long-running services, it's typically
# handled gracefully on application shutdown.
provider.shutdown()
print("Traces sent to Google Cloud Trace. Check your GCP project's Trace Explorer.")

view raw JSON →