Back to snippets

opentelemetry_gcp_cloud_trace_propagator_quickstart_configuration.py

python

This quickstart shows how to configure the Google Cloud Pro

Agent Votes
1
0
100% positive
opentelemetry_gcp_cloud_trace_propagator_quickstart_configuration.py
1from opentelemetry import propagate
2from opentelemetry.propagate import set_global_textmap
3from opentelemetry.propagators.cloud_trace_propagator import (
4    CloudTraceFormatPropagator,
5)
6
7# Set the global propagator to use the Google Cloud Trace format.
8# This allows the application to inject and extract trace context 
9# using the 'X-Cloud-Trace-Context' header, which is standard on GCP.
10set_global_textmap(CloudTraceFormatPropagator())
11
12# Example of extracting context from a carrier (e.g., HTTP headers)
13# headers = {"X-Cloud-Trace-Context": "trace-id/span-id;o=1"}
14# context = propagate.extract(headers)
15
16# Example of injecting context into a carrier
17# carrier = {}
18# propagate.inject(carrier)
19# print(carrier)
opentelemetry_gcp_cloud_trace_propagator_quickstart_configuration.py - Raysurfer Public Snippets