David Y.
—I would like to create a custom middleware class for FastAPI that logs the duration of each request, for performance profiling purposes. This middleware will ultimately form part of a shared library between different FastAPI projects, so I would also like to avoid tying it to the code of any individual FastAPI project, as appears to be the approach in the FastAPI middleware documentation.
How do I accomplish this? Is middleware the right approach?
In FastAPI, middleware processes requests before they’re sent to specific path operations and processes responses before they’re returned. This makes it ideal for generic operations we want to do on every request and response, such as logging the time between a request and its response.
To create a custom middleware, we must define a class that adheres to the ASGI (Asynchronous Server Gateway Interface) specification. Here’s an example of a middleware class for our timing operation, which we might store in a self-contained file named timing_middleware.py
:
import time class TimingMiddleware: def __init__(self, app): self.app = app async def __call__(self, scope, receive, send): start_time = time.time() await self.app(scope, receive, send) duration = time.time() - start_time print(f"Request duration: {duration:.2f} seconds")
The important part of this class is the __call__
method, which implements the ASGI application specification. It is an async
function that takes the parameters scope
, receive
and send
. These parameters are needed for the FastAPI request. In this simple middleware, we just pass them through to our FastAPI app in the line await self.app(scope, receive, send)
. We can think of __call__
as a wrapper around our app’s requests.
Since this is a generic ASGI middleware class, we can use it not just with different FastAPI projects, but also with projects using other Python ASGI servers, such as Starlette, on which FastAPI is built.
Here’s how we might use this class in a FastAPI project:
from fastapi import FastAPI from timing_middleware import TimingMiddleware # import middleware class app = FastAPI() app.add_middleware(TimingMiddleware) # add middleware to FastAPI app @app.get("/hello") async def greeter(): return {"Hello": "World"} @app.get("/goodbye") async def farewell(): return {"Goodbye": "World"}
Now our middleware will run every time we request one of the API’s defined routes. We should keep this in mind when extending the middleware’s capabilities – if our middleware’s __call__
function contains too much processing, it will slow down our entire application.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at [email protected].
If you are a California resident, see our Supplemental notice.