Monitoring FastAPI with OpenTelemetry
Tags: monitoring, python, fastapi
Published: 2023-09-14 14:33:20

By integrating OpenTelemetry with FastAPI, you can gain valuable insight into the performance, behavior and dependencies of your API. You can monitor and troubleshoot issues, optimize performance, and ensure the reliability of your FastAPI applications.

## What is FastAPI?

FastAPI is a modern, high-performance web framework for building APIs with Python. It is designed to be easy to use, highly efficient, and able to handle high loads.

FastAPI's combination of performance, productivity, and modern features has made it popular among developers building APIs with Python.

## What is OpenTelemetry?

[OpenTelemetry](https://uptrace.dev/opentelemetry/) is an open source observability framework hosted by Cloud Native Computing Foundation. It is a merger of OpenCensus and OpenTracing projects.

OpenTelemetry aims to provide a single standard across all types of observability signals such as [OpenTemetry logs](https://uptrace.dev/opentelemetry/logs.html), [distributed tracing](https://uptrace.dev/opentelemetry/distributed-tracing.html), and [metrics](https://uptrace.dev/opentelemetry/metrics.html).

OpenTelemetry specifies how to collect and send telemetry data to backend platforms. With OpenTelemetry, you can instrument your application once and then add or change vendors without changing the instrumentation.

OpenTelemetry provides detailed instrumentation, enabling you to monitor and measure the performance of your FastAPI applications in real-time. You can identify bottlenecks, optimize code, and ensure your application runs smoothly even under heavy traffic.

## FastAPI instrumentation

To install OpenTelemetry instrumentation for FastAPI:

```shell
pip install opentelemetry-instrumentation-fastapi
```

## Usage

To instrument FastAPI application:

```python
from fastapi import FastAPI
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor

app = FastAPI()
FastAPIInstrumentor.instrument_app(app)
```

Also see [OpenTelemetry FastAPI example](https://github.com/uptrace/uptrace-python/tree/master/example/fastapi) at GitHub.

OpenTelemetry allows you to trace requests as they flow through your FastAPI application, providing a clear picture of how different components and services interact. This end-to-end tracing helps diagnose issues quickly and streamline troubleshooting.

Once your FastAPI application is instrumented with OpenTelemetry, you can use the observability data in a variety of ways. You can visualize distributed traces, analyze performance metrics, and gain insight into the behavior of your API using [OpenTelemetry backends](https://uptrace.dev/blog/opentelemetry-backend.html) such as Uptrace, Jaeger, Prometheus, or Grafana.

## What is Uptrace?

Uptrace is an [open source APM](https://uptrace.dev/get/open-source-apm.html) for OpenTelemetry that supports distributed tracing, metrics, and logs. You can use it to monitor applications and troubleshoot issues.

![Uptrace home](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2mi5d6fddabxq84q4nxi.png)

Uptrace comes with an intuitive query builder, rich dashboards, alerting rules, 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](https://app.uptrace.dev/play) (no login required) or running it locally with [Docker](https://github.com/uptrace/uptrace/tree/master/example/docker). The source code is available on [GitHub](https://github.com/uptrace/uptrace).

### uvicorn

If you are using uvicorn with Gunicorn, you need to initialize OpenTelemetry in the post-fork hook:

```python
import uptrace

def post_fork(server, worker):
    uptrace.configure_opentelemetry(...)

workers = 4
worker_class = "uvicorn.workers.UvicornWorker"
```

See [Application servers](https://uptrace.dev/get/opentelemetry-python.html#application-servers) for details.

## Conclusion

With insights from OpenTelemetry, you can make informed decisions about scaling your FastAPI application. Load balancing strategies can be adjusted based on real-time data to handle traffic spikes effectively.

OpenTelemetry also allows you to instrument specific parts of your code for custom telemetry collection. You can use [OpenTelemetry Python APIs](https://uptrace.dev/opentelemetry/python-tracing.html) to manually create spans and add custom attributes, events, or metrics to capture additional information.

- [OpenTelemetry Flask](https://uptrace.dev/get/instrument/opentelemetry-flask.html)
- [OpenTelemetry Pyramid](https://uptrace.dev/get/instrument/opentelemetry-pyramid.html)