Zipkin JSON Exporter for OpenTelemetry
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
- gotcha Missing 'service.name' Resource Attribute: If the 'service.name' resource attribute is not explicitly set, traces may appear as 'unknown_service' in the Zipkin UI, making it difficult to identify the originating service.
- gotcha Silent Export Failures: OpenTelemetry exporters, including Zipkin, often fail silently if there are configuration errors (e.g., incorrect endpoint URL, network issues, or timeouts). Traces might not reach Zipkin without any obvious error message.
- gotcha Unended Spans Not Exported: Spans created but not explicitly ended (e.g., `span.end()` or exiting a `with` block) will remain in memory and will not be exported. Additionally, avoid using `SimpleSpanProcessor` in production as it blocks the application thread; `BatchSpanProcessor` is recommended for performance.
- gotcha Incorrect Zipkin Endpoint Configuration: The Zipkin exporter defaults to `http://localhost:9411/api/v2/spans`. A common mistake is using an incorrect host, port, or omitting the `/api/v2/spans` path, which can lead to connection refused or 404 errors at the Zipkin collector.
Install
-
pip install opentelemetry-exporter-zipkin-json opentelemetry-sdk requests
Imports
- ZipkinExporter
from opentelemetry.exporter.zipkin.json import ZipkinExporter
Quickstart
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()