Monitor your Fast API applications
![Monitor your Fast API applications](/api-monitoring.png)
When developing an API, we always want to know how our API is performing for all our customers. Monitoring your API is crucial to ensure it's current performance and reliability. However setting up the infrastructure to do this can be tedious and often we result to performing queries on logs.
Most of the time we just want to know at a high-level the current status. This could be:
- Average and maximum processing time today, this week or over a month
- Number of 5xx status codes
- Current CPU or memory utilization
Trckrspace can help you solve this problem by providing a service for you to store metrics about your own API. Trckrspace is easy to integrate with your current setup and can remove the need for building a custom solution by providing users with interactive custom dashboards.
To follow along with this guide, first create a trckrspace account by clicking here.
Getting setup
- Log into your dashboard at clicking here.
- Create an API key - you can find more info in the quick start guide by clicking here
Tutorial
In Fast API we can add trckrspace integration in just a few lines of code by
utilising the middleware
feature.
"A ‘middleware’ is a function that works with every request before it is processed by any specific path operation. And also with every response before returning it." — FastAPI docs
We can use the middleware
feature to save an event to trckrspace with all our
metrics after each request.
Here's how to do this:
import os
import time
from fastapi import FastAPI
import requests
app = FastAPI(
title='My API',
)
def save_event(event: dict):
api_key = os.environ['trckrspace_api_key']
headers = {X-Api-Key': api_key}
requests.post(
'https://api.trckrspace.com/usage', headers=headers, json=event
)
return None
@app.middleware("http")
async def middleware(request: Request, call_next):
start_time = time.time()
user = request.state.user_uuid
response = await call_next(request)
event = {
'status_code': response.status_code,
'endpoint': request.url,
'processing_time': time.time() - start_time,
'user_uuid': user_uuid,
}
save_event(event)
return response
Further improvements
We can further improve this solution by using background tasks. A background task in FastAPI is an async task which runs after a response has been returned. Using this would mean that our API has no additional request time that could be incurred by saving the trckrspace event.
from starlette.background import BackgroundTask
@app.middleware("http")
async def middleware(request: Request, call_next):
start_time = time.time()
user = request.state.user_uuid
response = await call_next(request)
event = {
'status_code': response.status_code,
'endpoint': request.url,
'processing_time': time.time() - start_time,
'user_uuid': user_uuid,
}
response.background = BackgroundTask(save_event, event)
return response
Viewing Data
To view your data log back into the trckrspace dashboard and create a new dashboard.
You can filter components on the dashboard by category
and any other
key-value pairs, for example here status_code=200
. After that you can
aggregate your data by or just see the raw events.
Following the getting started guide to learn more.
Summary
This guide showed you how easy it is to integrate trckrspace to monitor your FastAPI. As well as API monitoring, trckrspace can be used for many more use-cases. Checkout more examples or signup for free now to try it out yourself.