FastAPI application monitoring made easy
Tags: api, monitoring, fastapi
Published: 2024-01-06 13:05:52

Let's assume your team has just shipped a new REST API using FastAPI. Now that it's live you want to know how it's being used and how it's performing. You may have questions like:

- Is the API working as expected?
- How many requests is the API handling, by how many unique consumers?
- Which features of the API are being used the most, and by whom?
- Are there any errors faced by consumers, and what types of errors?
- What does the performance look like?
- Is the API up & available to users at all times?

Out of the box, FastAPI doesn't provide any means for you to answer those kinds of questions. And it shouldn't. It's a web framework, not a monitoring solution, after all. There's many different ways to approach this, but let's dive into a couple of options.

## The hard way

You might find recommendations on the internet for setting up Prometheus and Grafana to monitor your application. These are widely adopted open-source tools for monitoring all sorts of things, and they can certainly help you achieve what you're after.

This approach can be powerful and versatile, allowing you to implement things like custom metrics and infrastructure monitoring all within the same platform while tailoring everything exactly to your needs.

However, Prometheus and Grafana aren't exactly plug-and-play solutions. If you're new to them, you might need to invest a fair bit of time to get the answers you're looking for. You'll need to deploy and configure both tools, implement metrics logging using the Prometheus client in your application and then build dashboards in Grafana. Also, remember to consider the ongoing costs of hosting and maintaining these tools.

You may have good reasons for going down this path, and if you do, I highly recommend [this article](https://dev.to/ken_mwaura1/getting-started-monitoring-a-fastapi-app-with-grafana-and-prometheus-a-step-by-step-guide-3fbn) to get you started. On the other hand, if you'd prefer a simpler approach, read on.

## The easy way

By using a purpose-built API monitoring solution, such as [Apitally](https://apitally.io), you can gain insights into how your API is being used and how it's performing within a matter of minutes. This is possible because Apitally provides seamless integration with FastAPI via a simple middleware that captures relevant metadata about all handled requests and responses. This includes:

- HTTP method and path of requests
- Response status codes
- Response times / latencies
- Consumer identifiers *(if configured)*

This data syncs from your application to Apitally every minute and is then available for analysis on an intuitive dashboard that allows you to easily keep track of API requests, error rates and response times and provides insights into the usage and performance of each endpoint.

![Screenshot of Apitally traffic dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v8vggk0cca5mamkpofts.png)

All you need to do is install a [small dependency](https://pypi.org/project/apitally/) and add a couple of lines of code to your project, as shown in the example below.

```python
from fastapi import FastAPI
from apitally.fastapi import ApitallyMiddleware

app = FastAPI()
app.add_middleware(
    ApitallyMiddleware,
    client_id="your-client-id",  # provided by Apitally
    env="default",  # or "dev", "prod" etc. (optional)
)
```

The 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.

```python
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 provide valuable insights into how your API is being used a lot quicker. 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](https://docs.apitally.io/frameworks/fastapi). You should be good to go in less than 5 minutes.

Happy monitoring! Feel free to ask any questions or let me know what you think in the comments below.