FastAPI application monitoring made easy
Understand how your API is being used and how it's performing without deploying, configuring and maintaining complex monitoring tools.
Let’s assume your team has just launched a new API built with FastAPI. Now that it’s live you’re looking for a way to track the API’s adoption and performance. You probably have questions like:
- How many requests is the API handling?
- Who is using the API?
- Which endpoints are being used the most?
- What kinds of errors are occurring, and why?
- Is the API performance satisfactory?
There are many different ways to approach this. In this post, we’ll explore a couple of options.
Prometheus and Grafana
If you search around the internet, you will likely find recommendations for setting up Prometheus and Grafana to monitor your application. These are widely adopted open-source tools for monitoring all sorts of things.
This approach can be powerful and flexible, allowing you to implement features like custom metrics and infrastructure monitoring within the same platform, while tailoring everything exactly to your needs.
However, Prometheus and Grafana aren’t exactly plug-and-play solutions. You will need to invest a fair bit of time to learn and set things up, especially if you haven’t used them before. Some of the steps include deploying and configuring both tools, implementing metrics logging using the Prometheus client in your application and then building dashboards in Grafana.
Chances are you’re looking for a simpler solution that doesn’t require you to become an expert in monitoring tools.
Apitally
An easier option is to use a purpose-built API monitoring tool, such as Apitally, which provides API traffic, error and performance insights out of the box.
Apitally was specifically designed to be easy to use and it automatically captures the most important API metrics for FastAPI applications. It also includes an intuitive API monitoring dashboard, saving you the trouble of having to build one.
Some of the insights you can expect to see in Apitally include:
- Number of requests handled, per endpoint and per consumer
- Number of unique API consumers
- Error rates and types
- Validation error details (from pydantic)
- Server error details (optionally with Sentry integration)
- Response times / latencies
- Request and response payload sizes
Apitally syncs this data from your application every minute, so you can easily keep track of those API metrics in near real-time using the Apitally dashboard. And as a bonus, Apitally will also monitor the uptime & availability of your API and send you alerts when something is wrong.
To set this up, you only need to install a small dependency and add a couple of lines of code to your project, as shown in the example below.
from fastapi import FastAPI
from apitally.fastapi import ApitallyMiddleware
app = FastAPI()
app.add_middleware(
ApitallyMiddleware,
client_id="your-client-id", # provided by Apitally
env="prod", # or "dev" etc.
)
The Apitally middleware works asynchronously and does not impact the performance of your application. It also doesn’t capture request and response headers and bodies, which means you don’t have to worry about masking sensitive information that may be present in your API payloads.
Identifying consumers
You might find it useful to analyze API requests by consumer, so you can understand the different usage patterns of
individual consumers. To allow that, you can optionally associate requests with consumers in your application by setting
the apitally_consumer
property on the request.state
object.
As an example, if your application is using authentication, you could set the apitally_consumer
based on the
authenticated user.
You could do this anywhere within your existing authentication code, or in a dedicated function, like in the example below.
def identify_consumer(
request: Request,
current_user: Annotated[User, Depends(get_current_user)],
):
request.state.apitally_consumer = current_user.username
app = FastAPI(dependencies=[Depends(identify_consumer)])
Conclusion
While not as versatile and customizable as managing your own monitoring stack, using a tool like Apitally can help you gain valuable API insights with a minimal amount of effort. And sometimes a simple solution is all you need.
If you’d like to try Apitally out for your FastAPI project, you can follow the detailed setup guide for FastAPI. You should be good to go in less than 5 minutes.
Happy monitoring!