0% found this document useful (0 votes)
126 views15 pages

### Promethus Counter. Adding Prometheus To A FastAPI App - Python - by Carlos Armando Marcano Vargas - Python in Plain English

Download as pdf or txt
0% found this document useful (0 votes)
126 views15 pages

### Promethus Counter. Adding Prometheus To A FastAPI App - Python - by Carlos Armando Marcano Vargas - Python in Plain English

Download as pdf or txt
Download as pdf or txt
You are on page 1/ 15

31/10/2023, 11:14 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

Adding Prometheus to a FastAPI app | Python


Carlos Armando Marcano Vargas · Follow
Published in Python in Plain English
5 min read · Aug 21

Listen Share More

Photo by Joshua Aragon on Unsplash

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

Creating the FastAPI server


Now we are going to build the FastAPI server. We need to install FastAPI, Uvicorn
and the Prometheus client.

pip install fastapi uvicorn prometheus-client

main.py

from fastapi import FastAPI

from prometheus_client import make_asgi_app

app = FastAPI()

Open in app
metrics_app = make_asgi_app()
app.mount("/metrics", metrics_app)
Search
@app.get("/")
def index():
return "Hello, world!"

In this file, we import make_asgi_app from prometheus_client to create a Prometheus


metrics app. We pass that registry to make_asgi_app() to create the metrics app. We
mount that metrics app at the /metrics route using app.mount("/metrics",

metrics_app) .

We start the server and navigate to localhost:8000/metrics . We should see the


following response in our web browser.

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

from fastapi import FastAPI, Request


from prometheus_client import make_asgi_app, Counter

app = FastAPI()

index_counter = Counter('index_counter', 'Description of counter')

metrics_app = make_asgi_app()
app.mount("/metrics", metrics_app)

@app.get("/")
def index():
index_counter.inc()

return "Hello, world!"

Next, we create a counter. For every request to the route “/”, the counter will
increment by 1.

We start the server. And navigate to localhost:8000/ and then to


localhost:8000/metrics , we should see the following response.

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

from fastapi import FastAPI, Request


from prometheus_client import make_asgi_app, Counter

app = FastAPI()

all_requests = Counter('all_requests', 'A counter of the all requests made')

...

@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"

# A scrape configuration containing exactly one endpoint to scrape:


# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped
- job_name: "prometheus"

# metrics_path defaults to '/metrics'


# scheme defaults to 'http'.

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.

Thank you for taking the time to read this article.

If you have any recommendations about other packages, architectures, how to


improve my code, my English, or anything; please leave a comment or contact me
through Twitter, or LinkedIn.

The source code is here.

Resources
Instrumenting HTTP server is written in Go

Prometheus documentation.

Grafana documentation.

Originally published at https://carlosmv.hashnode.dev.

In Plain English
Thank you for being a part of our community! Before you go:

Be sure to clap and follow the writer! 👏


You can find even more content at PlainEnglish.io 🚀
Sign up for our free weekly newsletter. 🗞️
Follow us on Twitter, LinkedIn, YouTube, and Discord.

Python Fastapi Prometheus Programming Software Development

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

Written by Carlos Armando Marcano Vargas


194 Followers · Writer for Python in Plain English

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

Carlos Armando Marcano Vargas

How to Build an Application Monitoring System with FastAPI and


RabbitMQ | Python
In this article, we will build a service monitoring system using FastAPI and RabbitMQ. The
system will monitor the web traffic of various…

9 min read · Jul 24

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

Builescu Daniel in 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…

· 21 min read · Aug 21

2.2K 10

Builescu Daniel in Python in Plain English

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

10 Python Projects You Can Start Today and Monetize Tomorrow


🚀 Dive into 10 Python projects with HUGE potential! Turn your code into cash. 💰 Ready to
unlock the magic? 🔗

· 20 min read · Aug 10

739 10

Carlos Armando Marcano Vargas in DevOps.dev

Monitoring Your Go Application with Prometheus and Grafana: A Step-


by-Step Guide
Prometheus and Grafana are open-source tools for monitoring and observability of systems.
This step-by-step guide explains how to install…

5 min read · Apr 10

37

See all from Carlos Armando Marcano Vargas

See all from Python in Plain English

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

Recommended from Medium

dboost.me

Dependency Injection in Python (DIY)


Let’s think about dependency injection. In comparison with the java ecosystem, dependency
injection is not commonly used in the python…

3 min read · Aug 8

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

Dmytro Parfeniuk in Python in Plain English

🐍 Python Backend Project Advanced Setup (FastAPI Example)


👋 Hola! You might know something about Python if you are here. Especially about Python web
frameworks. There is one thing that really…

11 min read · Jul 23

429 2

Lists

Coding & Development


11 stories · 247 saves

General Coding Knowledge


20 stories · 507 saves

It's never too late or early to start something


15 stories · 185 saves

Stories to Help You Grow as a Software Developer


19 stories · 501 saves

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…

6 min read · Aug 6

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

Stop using Integer ID’s in your Database


I’ve seen this over and over for the last 30 years, people let the database set the ID or Primary
Key of a table from the database, at…

· 3 min read · May 22

2K 135

Virinchi T in Fournine Cloud

PostgreSQL 16: What’s New and Notable features 2/2


In the previous post (part 1) I have covered highlevel details about what is coming in this new
release.

9 min read · Sep 18

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…

3 min read · May 4

264 6

See more recommendations

https://python.plainenglish.io/adding-prometheus-to-a-fastapi-app-python-e038bccdd502 15/15

You might also like