DEV Community

Cover image for Monitor Falcon with OpenTelemetry
Alexandr Bandurchin for Uptrace

Posted on • Originally published at uptrace.dev

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?

OpenTelemetry 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 backends or observability platforms for analysis and visualization. The OpenTelemetry Operator can automatically inject instrumentation into your applications when running in Kubernetes, reducing manual configuration overhead.

OpenTelemetry Falcon

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

pip install opentelemetry-instrumentation-falcon
Enter fullscreen mode Exit fullscreen mode

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())
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

See documentation 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)
Enter fullscreen mode Exit fullscreen mode

What is Uptrace?

Uptrace is a distributed tracing tool 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 demo (no login required) or running it locally with Docker. The source code is available on GitHub.

What's next?

Falcon applications now have complete observability with OpenTelemetry instrumentation for API monitoring and distributed tracing. For alternative Python frameworks, explore FastAPI for async support or Django for full-featured web applications.

Top comments (0)