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 tracing and OpenTelemetry metrics.
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 example at GitHub.
What is Uptrace?
Uptrace is a source-available APM 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 started 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.
Popular instrumentations: