Google Cloud Trace
The Google Cloud Trace API client library for Python. This library primarily provides an OpenTelemetry Span Exporter to send traces from Python applications to Google Cloud Trace. It is part of the larger `google-cloud-python` monorepo, with frequent updates across various Google Cloud services. The current version is 1.19.0.
Warnings
- gotcha The `google-cloud-trace` library primarily functions as an OpenTelemetry exporter. You should use OpenTelemetry for instrumentation (creating spans, propagating context) and then configure `CloudTraceSpanExporter` to send those traces to Google Cloud Trace, rather than directly using legacy `google.cloud.trace.Client` methods for instrumentation.
- gotcha Ensure your Google Cloud Project ID is correctly configured. It can be passed explicitly to `CloudTraceSpanExporter(project_id='your-project-id')` or automatically discovered if the `GOOGLE_CLOUD_PROJECT` environment variable is set or if running on GCP infrastructure.
- gotcha Authentication to Google Cloud Trace requires proper Google Cloud credentials. This typically involves setting the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to a service account key file, or relying on Application Default Credentials when running on GCP (e.g., GCE, Cloud Run, GKE).
- breaking If migrating from older OpenCensus-based tracing to OpenTelemetry, significant code changes are required. OpenCensus is deprecated in favor of OpenTelemetry, and `google-cloud-trace` no longer directly supports OpenCensus exporters.
Install
-
pip install google-cloud-trace opentelemetry-sdk opentelemetry-api
Imports
- CloudTraceSpanExporter
from google.cloud.trace_v2.exporters import CloudTraceSpanExporter
- TracerProvider
from opentelemetry.sdk.trace import TracerProvider
- trace
from opentelemetry import trace
Quickstart
import os
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.resources import Resource
from google.cloud.trace_v2.exporters import CloudTraceSpanExporter
# Set the Google Cloud project ID (can also be picked up from GOOGLE_CLOUD_PROJECT environment variable)
project_id = os.environ.get('GOOGLE_CLOUD_PROJECT', 'your-gcp-project-id')
# 1. Configure OpenTelemetry TracerProvider with a Resource (important for service naming in Trace)
resource = Resource.create({"service.name": "my-python-app", "service.version": "1.0.0"})
provider = TracerProvider(resource=resource)
# 2. Add the Google Cloud Trace Span Exporter
# You can specify project_id here or rely on default credentials if GOOGLE_CLOUD_PROJECT is set
cloud_trace_exporter = CloudTraceSpanExporter(project_id=project_id)
provider.add_span_processor(SimpleSpanProcessor(cloud_trace_exporter))
# 3. Set the global tracer provider
trace.set_tracer_provider(provider)
# 4. Get a tracer and create spans
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span('my-application-span'):
print('Doing some work...')
with tracer.start_as_current_span('child-operation') as child_span:
child_span.set_attribute('custom_key', 'custom_value')
print('Doing child work...')
print('Work finished.')
# In a real application, spans are typically sent asynchronously.
# For a short script, ensure exporter has time to send or explicitly shut down.
# provider.shutdown() # Uncomment in production to ensure all spans are exported before process exits.