with one click
opentelemetry-python-sdk
// Initialize and configure the OpenTelemetry Python SDK — TracerProvider, BatchSpanProcessor, ConsoleSpanExporter, and propagation format setup.
// Initialize and configure the OpenTelemetry Python SDK — TracerProvider, BatchSpanProcessor, ConsoleSpanExporter, and propagation format setup.
[HINT] Download the complete skill directory including SKILL.md and all related files
| name | opentelemetry-python-sdk |
| description | Initialize and configure the OpenTelemetry Python SDK — TracerProvider, BatchSpanProcessor, ConsoleSpanExporter, and propagation format setup. |
| tech_stack | ["opentelemetry"] |
| language | ["python"] |
| capability | ["observability"] |
| version | OpenTelemetry Python (Traces Stable, Metrics Stable, Logs Development) |
| collected_at | "2025-12-03T00:00:00.000Z" |
Source: https://opentelemetry.io/docs/languages/python/instrumentation/, https://opentelemetry.io/docs/languages/python/
Bootstrap the OpenTelemetry SDK in Python applications. Covers installing packages, creating and registering a global TracerProvider, wiring up span processors and exporters, acquiring tracers, and configuring propagation formats. This is the foundational skill — every other OpenTelemetry Python skill depends on this initialization pattern.
ConsoleSpanExporter# pip install opentelemetry-api opentelemetry-sdk
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
provider = TracerProvider()
processor = BatchSpanProcessor(ConsoleSpanExporter())
provider.add_span_processor(processor)
trace.set_tracer_provider(provider)
tracer = trace.get_tracer("my.service.name")
After this, any tracer.start_as_current_span(...) call will produce spans that flow through the processor chain and appear on the console.
with tracer.start_as_current_span("operation-name") as span:
span.set_attribute("key", "value")
# ... business logic ...
# span auto-closes on block exit
# pip install opentelemetry-propagator-b3
from opentelemetry.propagate import set_global_textmap
from opentelemetry.propagators.b3 import B3Format
set_global_textmap(B3Format())
Or via environment variable: OTEL_PROPAGATORS="b3". Environment variables override code settings.
| API | Role |
|---|---|
TracerProvider() | Create the provider that holds span processor chain |
BatchSpanProcessor(exporter) | Accumulate spans and flush in batches (production) |
SimpleSpanProcessor(exporter) | Export each span immediately (debug/short-lived processes) |
provider.add_span_processor(sp) | Register a processor with the provider |
trace.set_tracer_provider(provider) | Register as the global default |
trace.get_tracer("name") | Acquire a named tracer from the global provider |
tracer.start_as_current_span("name") | Context manager that creates and auto-closes the current span |
tracer.start_span("name") | Create a span without making it current (caller must .end()) |
span.set_attribute(k, v) | Attach key/value metadata (str, int, float, bool, or homogeneous list) |
span.set_status(Status(StatusCode.ERROR)) | Mark span as errored |
span.record_exception(ex) | Record exception with stack trace on the span |
span.add_event("message") | Attach a timestamped event log line |
set_global_textmap(propagator) | Change the propagation format in code |
OTEL_PROPAGATORS env var | Comma-separated list: tracecontext, baggage, b3, b3multi, jaeger, xray, ottrace, none |
OTEL_PROPAGATORS always overrides set_global_textmap().SimpleSpanProcessor or explicitly call provider.shutdown() before exit.start_span(), get_current_span() will not see it — and auto-instrumentation libraries won't either.@tracer.start_as_current_span("name") needs tracer defined at module scope, not inside a function.ConsoleSpanExporter with OTLPSpanExporter (see opentelemetry-python-otlp).tracer instance used by opentelemetry-python-manual-span.FastAPIInstrumentor.instrument_app() attaches to the global provider.opentelemetry-python-propagation.resource=Resource.create({"service.name": "my-svc"}) to TracerProvider() to set service identity early.