Zipkin JSON Exporter for OpenTelemetry

1.41.0 · active · verified Fri Apr 10

This library allows to export tracing data to Zipkin using JSON for serialization. It is part of the OpenTelemetry Python project, which releases frequently (typically monthly or bi-monthly). The current version is 1.41.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up the OpenTelemetry Python SDK with the Zipkin JSON Exporter. It initializes a TracerProvider, configures the ZipkinExporter to send traces to a default or environment-variable-defined endpoint, and uses a BatchSpanProcessor for efficient trace export. Finally, it creates a simple span and ensures proper shutdown.

import os
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.zipkin.json import ZipkinExporter

# Configure a TracerProvider with a service name
resource = Resource.create({"service.name": "my-zipkin-service"})
tracer_provider = TracerProvider(resource=resource)
trace.set_tracer_provider(tracer_provider)

# Create a ZipkinExporter
# The default endpoint is http://localhost:9411/api/v2/spans
# You can override it with environment variable OTEL_EXPORTER_ZIPKIN_ENDPOINT
# or by passing the 'endpoint' argument to ZipkinExporter
zipkin_exporter = ZipkinExporter(
    endpoint=os.environ.get("OTEL_EXPORTER_ZIPKIN_ENDPOINT", "http://localhost:9411/api/v2/spans")
)

# Add the exporter to a BatchSpanProcessor
span_processor = BatchSpanProcessor(zipkin_exporter)
tracer_provider.add_span_processor(span_processor)

# Get a tracer and create a span
tracer = trace.get_tracer(__name__)

with tracer.start_as_current_span("my-span") as span:
    span.set_attribute("event.name", "hello")
    print("Hello, OpenTelemetry with Zipkin!")

# Ensure all spans are exported before exiting
tracer_provider.shutdown()

view raw JSON →