{"id":9751,"library":"flask-opentracing","title":"Flask OpenTracing","description":"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.","status":"active","version":"2.0.0","language":"en","source_language":"en","source_url":"https://github.com/opentracing-contrib/python-flask","tags":["flask","opentracing","observability","tracing","distributed-tracing"],"install":[{"cmd":"pip install flask-opentracing","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core web framework integration.","package":"flask","optional":false},{"reason":"OpenTracing API specification.","package":"opentracing","optional":false},{"reason":"Python API for OpenTracing. Note: flask-opentracing v1.0.0+ requires opentracing-api v2.0+.","package":"opentracing_api","optional":false}],"imports":[{"symbol":"FlaskTracing","correct":"from flask_opentracing import FlaskTracing"}],"quickstart":{"code":"from flask import Flask\nfrom flask_opentracing import FlaskTracing\nfrom opentracing.mocktracer import MockTracer\n\napp = Flask(__name__)\n\n# Initialize FlaskTracing with an OpenTracing-compatible tracer.\n# For production, replace MockTracer with a real tracer like Jaeger or Zipkin.\n# e.g., from jaeger_client import Config; tracer = Config(service_name='my-app').initialize_tracer()\n# Use os.environ.get for dynamic configuration if needed.\ntracing = FlaskTracing(MockTracer(), app=app, trace_all_requests=True)\n\n@app.route('/')\ndef hello():\n    # Get the current active span for manual instrumentation\n    span = tracing.get_span()\n    if span:\n        span.set_tag('hello-to', 'world')\n    return \"Hello World!\"\n\n@app.route('/greet/<name>')\ndef greet(name):\n    span = tracing.get_span()\n    if span:\n        span.set_tag('greeting-name', name)\n    return f\"Hello, {name}!\"\n\nif __name__ == '__main__':\n    app.run(debug=True)","lang":"python","description":"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()`."},"warnings":[{"fix":"Ensure `opentracing` (or `opentracing_api`) is installed at version 2.0 or higher (`pip install opentracing>=2.0`).","message":"`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.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Pass an initialized tracer object (e.g., `MockTracer()`, `jaeger_client.Config().initialize_tracer()`) directly to `FlaskTracing(tracer_instance, app=app, ...)`.","message":"As 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.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Replace `opentracing.tracer.active_span` with `your_tracing_instance.get_span()`.","message":"When 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+.","severity":"gotcha","affected_versions":">=1.0.0"},{"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)`.","message":"For 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.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Initialize `FlaskTracing` with a tracer object: `FlaskTracing(MockTracer(), app=app)`.","cause":"Attempting to initialize `FlaskTracing` without providing an `opentracing.Tracer` instance as the first argument, which became mandatory in v1.0.0+.","error":"TypeError: __init__() missing 1 required positional argument: 'tracer'"},{"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.","cause":"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.","error":"AttributeError: 'NoneType' object has no attribute 'set_tag'"},{"fix":"Use `tracing.get_span()` (where `tracing` is your `FlaskTracing` instance) to retrieve the active span managed by `flask-opentracing`.","cause":"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.","error":"AttributeError: 'MockTracer' object has no attribute 'active_span'"}]}