Jaeger Python Client

4.8.0 · active · verified Thu Apr 16

The `jaeger-client` library provides a Python implementation for OpenTracing tracers, enabling applications to send traces to a Jaeger backend. It is currently at version 4.8.0, actively maintained, and primarily used for distributed tracing and observability. Releases typically occur every few months, aligning with new features or Python version updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Jaeger tracer using `Config`, create root and child spans, and log key-value pairs. It leverages environment variables for agent host/port, falling back to defaults. Crucially, `tracer.close()` is called to ensure all buffered spans are sent to the Jaeger agent.

import os
from jaeger_client import Config

# Configure Jaeger client
config = Config(
    config={
        'sampler': {
            'type': 'const',
            'param': 1,
        },
        'logging': True,
        'local_agent': {
            'reporting_host': os.environ.get('JAEGER_AGENT_HOST', 'localhost'),
            'reporting_port': int(os.environ.get('JAEGER_AGENT_PORT', 6831)),
        },
    },
    service_name='my-python-app',
    validate=True,
)

# Initialize tracer (it's recommended to do this once per application lifetime)
tracer = config.initialize_tracer()

# Create a root span
with tracer.start_active_span('my-root-operation') as scope:
    scope.span.log_kv({'event': 'root_span_started'})
    print(f"Root Span Trace ID: {scope.span.trace_id:x}")

    # Create a child span automatically inheriting from the active span
    with tracer.start_active_span('my-child-operation'):
        print("Doing some work in a child span...")
        scope.span.log_kv({'event': 'child_work_done'})

print("Spans created and ready to be flushed.")

# It is crucial to close the tracer to ensure all buffered spans are flushed.
# In a long-running application, this should be done during graceful shutdown.
tracer.close()

view raw JSON →