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?

OpenTelemetryopen in new window 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 tracesopen in new window, logsopen in new window, and metricsopen in new window.

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 is available for most programming languages and provides interoperability across different languages and environments.

Installation

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 an open source and blazingly fast distributed tracing tool 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, instrument more operations, for example, database queries and network calls. You can also learn about OpenTelemetry Python Tracing API to create your own instrumentations.

Last Updated: