Monitor FastAPI with OpenTelemetry

FastAPI is a modern, high-performance web framework for building APIs with Python based on standard type hints

In this article you will learn how to monitor and optimize FastAPI performance using OpenTelemetry observability framework.

What is OpenTelemetry?

OpenTelemetry is a vendor-neutral standard on how to collect telemetry data for applications and their supporting infrastructures. OpenTelemetry was created via the merger of OpenCensus and OpenTracing projects.

OpenTelemetry aims to standardize how you collect and send telemetry data to backend platforms: distributed tracingopen in new window and OpenTelemetry metricsopen in new window.

FastAPI instrumentation

To install OpenTelemetry instrumentation for FastAPI:

pip install opentelemetry-instrumentation-fastapi

Usage

To instrument FastAPI application:

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

app = FastAPI()
FastAPIInstrumentor.instrument_app(app)

Also see OpenTelemetry FastAPI exampleopen in new window at GitHub.

What is Uptrace?

Uptrace is a source-available APMopen in new window powered by OpenTelemetry and ClickHouse. It allows you to identify and fix bugs in production faster knowing what conditions lead to which errors.

You can get startedopen in new window with Uptrace by downloading a DEB/RPM package or a pre-compiled Go binary.

What is next?

Next, instrumentopen in new window more operations, for example, database queries and network calls. You can also learn about OpenTelemetry Python Tracing APIopen in new window to create your own instrumentations.

Popular instrumentations:

Last Updated: