Monitor Falcon with OpenTelemetry

Falcon is a minimalist web framework for building RESTful APIs in Python. It is lightweight, fast, and designed to be easy to use.

By integrating OpenTelemetry into your Falcon application, you can capture and export telemetry data such as traces, metrics, and logs. You can use that data for monitoring, troubleshooting, and analyzing the behavior and performance of your application.

What is OpenTelemetry?

OpenTelemetryopen in new window is an open-source observability framework that aims to standardize and simplify the collection, processing, and export of telemetry data from applications and systems.

OpenTelemetry supports multiple programming languages and platforms, making it suitable for a wide range of applications and environments.

OpenTelemetry enables developers to instrument their code and collect telemetry data, which can then be exported to various OpenTelemetry backendsopen in new window or observability platforms for analysis and visualization.

OpenTelemetry Falcon

To instrument Falcon app, you need to install the corresponding OpenTelemetry Falcon instrumentation:

pip install opentelemetry-instrumentation-falcon

Usage

Use the following code to integrate OpenTelemetry to capture telemetry data from your Falcon application:

from falcon import API
from opentelemetry.instrumentation.falcon import FalconInstrumentor

FalconInstrumentor().instrument()

app = falcon.API()

class HelloWorldResource(object):
    def on_get(self, req, resp):
        resp.body = 'Hello World'

app.add_route('/hello', HelloWorldResource())

You can exclude certain URLs from being tracked using environment variables:

# excludes requests such as `https://site/client/123/info` and `https://site/xyz/healthcheck`
export OTEL_PYTHON_FALCON_EXCLUDED_URLS="client/.*/info,healthcheck"

You can also extract certain attributes from Falcon’s request object and use them as span attributes:

export OTEL_PYTHON_FALCON_TRACED_REQUEST_ATTRS='query_string,uri_template'

See documentationopen in new window for more details.

Hooks

You can specify request and response hooks:

# the hook is called right after a Span is created
def request_hook(span, req):
    pass

# the hook is called right before the span is finished
def response_hook(span, req, resp):
    pass

FalconInstrumentation().instrument(request_hook=request_hook, response_hook=response_hook)

What is Uptrace?

Uptrace is a distributed tracing toolopen in new window that supports distributed tracing, metrics, and logs. You can use it to monitor applications and troubleshoot issues.

Uptrace Overview

Uptrace comes with an intuitive query builder, rich dashboards, alerting rules with notifications, and integrations for most languages and frameworks.

Uptrace can process billions of spans and metrics on a single server and allows you to monitor your applications at 10x lower cost.

In just a few minutes, you can try Uptrace by visiting the cloud demoopen in new window (no login required) or running it locally with Dockeropen in new window. The source code is available on GitHubopen in new window.

What's next?

Next, instrument more operations to get a more detailed picture. Try to prioritize network calls, disk operations, database queries, error and logs.

You can also create your own instrumentations using OpenTelemetry Python Tracing APIopen in new window.

Last Updated: