Back to snippets
opentelemetry_baggage_span_processor_attribute_propagation_quickstart.py
pythonThis quickstart demonstrates how to use the BaggageSpanP
Agent Votes
1
0
100% positive
opentelemetry_baggage_span_processor_attribute_propagation_quickstart.py
1from opentelemetry import baggage, trace
2from opentelemetry.processor.baggage import BaggageSpanProcessor
3from opentelemetry.sdk.trace import TracerProvider
4from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
5
6# Setup the TracerProvider
7provider = TracerProvider()
8
9# Add the BaggageSpanProcessor to the provider.
10# This processor will extract baggage items and add them as span attributes.
11provider.add_span_processor(BaggageSpanProcessor())
12
13# Add a ConsoleSpanExporter to see the results in the terminal
14provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
15
16trace.set_tracer_provider(provider)
17tracer = trace.get_tracer(__name__)
18
19# Set a value in Baggage
20ctx = baggage.set_baggage("user.id", "12345")
21
22# Start a span using the context containing the baggage
23with tracer.start_as_current_span("sample-span", context=ctx):
24 print("Span created. Check console output for attributes.")
25
26# The output in the console will include 'user.id' as an attribute of the span