OpenTelemetry Falcon Instrumentation
OpenTelemetry Falcon instrumentation provides automatic tracing for HTTP requests in Falcon web applications. It captures request flow, uses Falcon resource and method names as span names, and records errors. Currently at version 0.62b0, this library is part of the broader opentelemetry-python-contrib project, which often releases beta versions in sync with the core OpenTelemetry Python SDK's release cadence, indicating active development.
Warnings
- gotcha The OpenTelemetry SDK (including instrumentors) must be initialized *before* any instrumented library (like Falcon) is imported. If `falcon` is imported first, instrumentation hooks may not be applied, leading to missing telemetry data.
- gotcha Running the Falcon application in reloader/hot-reload mode (e.g., with `gunicorn --reload` or similar development server options) can interfere with OpenTelemetry's instrumentation process, leading to broken or incomplete tracing.
- gotcha To exclude specific URLs (e.g., health check endpoints) from being traced, set the environment variables `OTEL_PYTHON_FALCON_EXCLUDED_URLS` or the more general `OTEL_PYTHON_EXCLUDED_URLS` to a comma-delimited string of regular expressions. Incorrectly configuring these can lead to noisy or costly telemetry data.
- deprecated Falcon 3.x deprecated `falcon.API` in favor of `falcon.App`. While the instrumentation currently supports both, relying on the deprecated `falcon.API` might lead to issues or missed instrumentation in future versions if the alias is removed or not fully covered.
- gotcha As a beta package (indicated by `b0` suffix), `opentelemetry-instrumentation-falcon` may undergo more frequent breaking changes to its API, internal implementation, or the semantic conventions it uses for generated telemetry data compared to stable OpenTelemetry components. This may require more frequent updates to your application or observability backend configuration.
- gotcha For ASGI (Asynchronous Server Gateway Interface) Falcon applications, you should use `opentelemetry-instrumentation-asgi` in conjunction with `opentelemetry-instrumentation-falcon` to ensure proper tracing of the ASGI layer.
Install
-
pip install opentelemetry-instrumentation-falcon opentelemetry-sdk
Imports
- FalconInstrumentor
from opentelemetry.instrumentation.falcon import FalconInstrumentor
Quickstart
import falcon
from opentelemetry.instrumentation.falcon import FalconInstrumentor
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
# Configure OpenTelemetry SDK
resource = Resource.create({"service.name": "my-falcon-app"})
provider = TracerProvider(resource=resource)
span_processor = SimpleSpanProcessor(ConsoleSpanExporter())
provider.add_span_processor(span_processor)
trace.set_tracer_provider(provider)
# Instrument Falcon BEFORE creating the app instance
FalconInstrumentor().instrument()
class HelloResource:
def on_get(self, req, resp):
resp.status = falcon.HTTP_200
resp.media = {'message': 'Hello World'}
app = falcon.App()
app.add_route('/hello', HelloResource())
# To run this app (e.g., using Gunicorn):
# gunicorn -b 127.0.0.1:8000 your_module_name:app
# Then access http://127.0.0.1:8000/hello