Flask-opentracing provides OpenTracing support for Flask applications. It enables automatic and manual tracing of requests, integrating with OpenTracing-compatible tracers like Jaeger or Zipkin. The library is currently at version 2.0.0 and maintains an active release cadence aligned with OpenTracing API updates.
pip install flask-opentracingVerified import paths — ran on the pinned version, not inferred.
This quickstart initializes a Flask application with `FlaskTracing`, using a `MockTracer` for demonstration. It configures `trace_all_requests=True` to automatically trace all incoming HTTP requests. Custom spans and tags can be added within routes by retrieving the current span using `tracing.get_span()`.
Ensure `opentracing` (or `opentracing_api`) is installed at version 2.0 or higher (`pip install opentracing>=2.0`).
Pass an initialized tracer object (e.g., `MockTracer()`, `jaeger_client.Config().initialize_tracer()`) directly to `FlaskTracing(tracer_instance, app=app, ...)`.
Replace `opentracing.tracer.active_span` with `your_tracing_instance.get_span()`.
Install and configure a specific tracer client (e.g., `pip install jaeger-client`) and initialize `FlaskTracing` with it: `from jaeger_client import Config; config = Config(service_name='my-app'); tracer = config.initialize_tracer(); FlaskTracing(tracer, app=app)`.
Initialize `FlaskTracing` with a tracer object: `FlaskTracing(MockTracer(), app=app)`.
Always check if the span is not `None` before using it: `span = tracing.get_span(); if span: span.set_tag(...)`. Ensure `trace_all_requests=True` during `FlaskTracing` initialization or use explicit tracing decorators if only specific routes should be traced.
Use `tracing.get_span()` (where `tracing` is your `FlaskTracing` instance) to retrieve the active span managed by `flask-opentracing`.