### Promethus Counter. Adding Prometheus To A FastAPI App - Python - by Carlos Armando Marcano Vargas - Python in Plain English
### Promethus Counter. Adding Prometheus To A FastAPI App - Python - by Carlos Armando Marcano Vargas - Python in Plain English
Get unlimited access to the best of Medium for less than $1/week. Become a member
In this article, we are going to learn how to add Prometheus to a FastAPI server. This
article will show a simple demonstration and code examples of how to create a
request counter to count the number of requests made to a route. And a request
counter for all the requests.
https://python.plainenglish.io/adding-prometheus-to-a-fastapi-app-python-e038bccdd502 1/15
31/10/2023, 11:14 Adding Prometheus to a FastAPI app | Python | by Carlos Armando Marcano Vargas | Python in Plain English
Requirements
Python installed
Pip installed
Prometheus
Prometheus is an open-source systems monitoring and alerting toolkit originally
built at SoundCloud. Since its inception in 2012, many companies and organizations
have adopted Prometheus, and the project has a very active developer and user
community.
Installing Prometheus
To install Prometheus, you can use a Docker Image or download a precompiled
binary. We will use a precompiled binary, to download it, we have to go to this site.
The prometheus server will start on port 9090 . So we go to localhost:9090 to see its
UI.
https://python.plainenglish.io/adding-prometheus-to-a-fastapi-app-python-e038bccdd502 2/15
31/10/2023, 11:14 Adding Prometheus to a FastAPI app | Python | by Carlos Armando Marcano Vargas | Python in Plain English
main.py
app = FastAPI()
Open in app
metrics_app = make_asgi_app()
app.mount("/metrics", metrics_app)
Search
@app.get("/")
def index():
return "Hello, world!"
metrics_app) .
https://python.plainenglish.io/adding-prometheus-to-a-fastapi-app-python-e038bccdd502 3/15
31/10/2023, 11:14 Adding Prometheus to a FastAPI app | Python | by Carlos Armando Marcano Vargas | Python in Plain English
Creating a counter
app = FastAPI()
metrics_app = make_asgi_app()
app.mount("/metrics", metrics_app)
@app.get("/")
def index():
index_counter.inc()
Next, we create a counter. For every request to the route “/”, the counter will
increment by 1.
Now, we can create a middleware, to count every request made to the server.
https://python.plainenglish.io/adding-prometheus-to-a-fastapi-app-python-e038bccdd502 4/15
31/10/2023, 11:14 Adding Prometheus to a FastAPI app | Python | by Carlos Armando Marcano Vargas | Python in Plain English
app = FastAPI()
...
@app.middleware("tracing")
def tracing(request: Request, call_next):
all_requests.inc()
response = call_next(request)
return response
Here, we create a FastAPI middleware and create a counter to count all the requests.
We create the tracing function and add the all_request.inc() function which will
increment by 1 for every request made to the server.
prometheus.yml
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default i
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is ev
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
https://python.plainenglish.io/adding-prometheus-to-a-fastapi-app-python-e038bccdd502 5/15
31/10/2023, 11:14 Adding Prometheus to a FastAPI app | Python | by Carlos Armando Marcano Vargas | Python in Plain English
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evalu
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
static_configs:
- targets: ["localhost:9090"]
- job_name: fastapi-server
static_configs:
- targets: ["localhost:8000"]
prometheus.yml is the configuration file for the Prometheus server. We add another
job_name to scrape the metrics of our Gin server and define it as fastapi-server .
We have to specify the port where the exporter is listening, in our case, 8000. Now,
we can go to localhost:9090 and execute a query to test if Prometheus is collecting
any metric.
We write all_requests_total to see how many requests were made to the server, and
click execute.
Conclusion
Prometheus is a powerful open-source monitoring system that can be easily
integrated with FastAPI applications. In this article, we saw how to set up
Prometheus monitoring for a FastAPI app. We installed the Prometheus client
library and exposed metrics endpoints. We then configured Prometheus to scrape
those endpoints and started visualizing the metrics in the Prometheus UI.
https://python.plainenglish.io/adding-prometheus-to-a-fastapi-app-python-e038bccdd502 6/15
31/10/2023, 11:14 Adding Prometheus to a FastAPI app | Python | by Carlos Armando Marcano Vargas | Python in Plain English
In this article, we create a request counter. But, this is not all we can do with
Prometheus. We can monitor response time, and request latency. We can set up
alerts based on these metrics to notify us of any issues. Prometheus gives us
visibility into the performance and health of our application, this feature helps us to
debug issues and optimize the performance of our applications.
Resources
Instrumenting HTTP server is written in Go
Prometheus documentation.
Grafana documentation.
In Plain English
Thank you for being a part of our community! Before you go:
https://python.plainenglish.io/adding-prometheus-to-a-fastapi-app-python-e038bccdd502 7/15
31/10/2023, 11:14 Adding Prometheus to a FastAPI app | Python | by Carlos Armando Marcano Vargas | Python in Plain English
Follow
I'm self taught developer. I like to write about what I'm learning, and building. Always writting about Python,
Go and Rust.
More from Carlos Armando Marcano Vargas and Python in Plain English
83 1
https://python.plainenglish.io/adding-prometheus-to-a-fastapi-app-python-e038bccdd502 8/15
31/10/2023, 11:14 Adding Prometheus to a FastAPI app | Python | by Carlos Armando Marcano Vargas | Python in Plain English
Wanna Code Like a Google Engineer? Let’s Dive into Advanced Python
Together!
Unlock the secrets of advanced Python, straight from an Ex-Googler! Dive into syntax, efficient
looping, magical libraries, and more. If…
2.2K 10
https://python.plainenglish.io/adding-prometheus-to-a-fastapi-app-python-e038bccdd502 9/15
31/10/2023, 11:14 Adding Prometheus to a FastAPI app | Python | by Carlos Armando Marcano Vargas | Python in Plain English
739 10
37
https://python.plainenglish.io/adding-prometheus-to-a-fastapi-app-python-e038bccdd502 10/15
31/10/2023, 11:14 Adding Prometheus to a FastAPI app | Python | by Carlos Armando Marcano Vargas | Python in Plain English
dboost.me
https://python.plainenglish.io/adding-prometheus-to-a-fastapi-app-python-e038bccdd502 11/15
31/10/2023, 11:14 Adding Prometheus to a FastAPI app | Python | by Carlos Armando Marcano Vargas | Python in Plain English
429 2
Lists
https://python.plainenglish.io/adding-prometheus-to-a-fastapi-app-python-e038bccdd502 12/15
31/10/2023, 11:14 Adding Prometheus to a FastAPI app | Python | by Carlos Armando Marcano Vargas | Python in Plain English
Darya Plotnikova
I‘d been writing for 1 year on FastAPI after 5-year experience on Django.
And here what I’ve found
For whom this article is written — for people who just started their way in backend development
and are deciding now what a framework to…
288 8
Tom Jay
https://python.plainenglish.io/adding-prometheus-to-a-fastapi-app-python-e038bccdd502 13/15
31/10/2023, 11:14 Adding Prometheus to a FastAPI app | Python | by Carlos Armando Marcano Vargas | Python in Plain English
2K 135
17
https://python.plainenglish.io/adding-prometheus-to-a-fastapi-app-python-e038bccdd502 14/15
31/10/2023, 11:14 Adding Prometheus to a FastAPI app | Python | by Carlos Armando Marcano Vargas | Python in Plain English
Tony
Python: Replace your .env file with this awesome tool at scale
When developing applications, handling sensitive information like API keys, database
credentials, and configuration settings is crucial…
264 6
https://python.plainenglish.io/adding-prometheus-to-a-fastapi-app-python-e038bccdd502 15/15