Registry / observability / flask-opentracing

flask-opentracing

JSON →
library2.0.0pypypi✓ verified 86d ago

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-opentracing
INSTALL
IMPORT
SIG · FLASK-OPENTRACING
F
flask-opentracing
observabilitypythonv2.0.0
Install
3.3s avg
Import
490ms
Disk
22MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.0.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.503s · 24.3MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 3.3s · import 0.476s · 25MB
22MB installed
● package 22MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

FlaskTracing
from flask_opentracing import FlaskTracing

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()`.

from flask import Flask from flask_opentracing import FlaskTracing from opentracing.mocktracer import MockTracer app = Flask(__name__) # Initialize FlaskTracing with an OpenTracing-compatible tracer. # For production, replace MockTracer with a real tracer like Jaeger or Zipkin. # e.g., from jaeger_client import Config; tracer = Config(service_name='my-app').initialize_tracer() # Use os.environ.get for dynamic configuration if needed. tracing = FlaskTracing(MockTracer(), app=app, trace_all_requests=True) @app.route('/') def hello(): # Get the current active span for manual instrumentation span = tracing.get_span() if span: span.set_tag('hello-to', 'world') return "Hello World!" @app.route('/greet/<name>') def greet(name): span = tracing.get_span() if span: span.set_tag('greeting-name', name) return f"Hello, {name}!" if __name__ == '__main__': app.run(debug=True)
Debug
Known issues
breaking`flask-opentracing` v1.0.0 and newer versions require `opentracing-api` v2.0 or higher. If upgrading from `flask-opentracing` 0.x, ensure your `opentracing-api` dependency is also updated to v2.x to avoid compatibility issues.
fix
Ensure `opentracing` (or `opentracing_api`) is installed at version 2.0 or higher (`pip install opentracing>=2.0`).
affects: >=1.0.0
breakingAs of `flask-opentracing` v1.0.0, the `FlaskTracing` constructor explicitly requires an `opentracing.Tracer` instance as its first argument. The older implicit use of `opentracing.tracer` (from `opentracing-api` v1.x) is no longer supported.
fix
Pass an initialized tracer object (e.g., `MockTracer()`, `jaeger_client.Config().initialize_tracer()`) directly to `FlaskTracing(tracer_instance, app=app, ...)`.
affects: >=1.0.0
gotchaWhen trying to access the current span, always use `tracing.get_span()` (where `tracing` is your `FlaskTracing` instance). Do not use `opentracing.tracer.active_span` or similar global access patterns, which are deprecated or removed in `opentracing-api` v2.x and not compatible with `flask-opentracing` v1.0.0+.
fix
Replace `opentracing.tracer.active_span` with `your_tracing_instance.get_span()`.
affects: >=1.0.0
gotchaFor production deployments, `MockTracer` should be replaced with a real distributed tracing client (e.g., Jaeger, Zipkin). `MockTracer` only collects spans in memory and does not send them to a tracing backend.
fix
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)`.
affects: all
Errors
Common errors & fixes
TypeError: __init__() missing 1 required positional argument: 'tracer'
Attempting to initialize `FlaskTracing` without providing an `opentracing.Tracer` instance as the first argument, which became mandatory in v1.0.0+.
fix
Initialize `FlaskTracing` with a tracer object: `FlaskTracing(MockTracer(), app=app)`.
AttributeError: 'NoneType' object has no attribute 'set_tag'
You are trying to call methods like `set_tag()` on a span object that is `None`. This typically happens if `tracing.get_span()` returns `None`, meaning no active span was found for the current request.
fix
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.
AttributeError: 'MockTracer' object has no attribute 'active_span'
This error occurs when trying to access `active_span` directly on an `opentracing.Tracer` object (like `MockTracer`) or via `opentracing.tracer.active_span` with `opentracing-api` v2.x. `flask-opentracing` v1.0.0+ is designed for `opentracing-api` v2.x, which changed how active spans are managed.
fix
Use `tracing.get_span()` (where `tracing` is your `FlaskTracing` instance) to retrieve the active span managed by `flask-opentracing`.
Upgrade
Version history
2.0.0latest on PyPI · released Feb 29, 2024
Audit
Dependencies
flaskrequiredCore web framework integration.
opentracingrequiredOpenTracing API specification.
opentracing_apirequiredPython API for OpenTracing. Note: flask-opentracing v1.0.0+ requires opentracing-api v2.0+.
Agent activity
14 hits · last 30 days
node
12
OpenAI (training)
2
Resources