Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Carlos Armando Marcano Vargas
Carlos Armando Marcano Vargas

Posted on • Originally published atcarlosmv.hashnode.dev on

     

Adding Prometheus to a FastAPI app | Python

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.

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

Theprometheus server will start on port9090. So we go tolocalhost:9090 to see its UI.

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
Enter fullscreen modeExit fullscreen mode

main.py

from fastapi import FastAPIfrom prometheus_client import make_asgi_appapp = FastAPI()metrics_app = make_asgi_app()app.mount("/metrics", metrics_app)@app.get("/")def index():    return "Hello, world!"
Enter fullscreen modeExit fullscreen mode

In this file, we importmake_asgi_app fromprometheus_client to create a Prometheus metrics app. We pass that registry tomake_asgi_app() to create the metrics app. We mount that metrics app at the/metrics route usingapp.mount("/metrics", metrics_app).

We start the server and navigate tolocalhost:8000/metrics. We should see the following response in our web browser.

Creating a counter

from fastapi import FastAPI, Requestfrom prometheus_client import make_asgi_app, Counterapp = 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!"
Enter fullscreen modeExit fullscreen mode

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

We start the server. And navigate tolocalhost:8000/ and then tolocalhost:8000/metrics , we should see the following response.

Now, we can create a middleware, to count every request made to the server.

from fastapi import FastAPI, Requestfrom prometheus_client import make_asgi_app, Counterapp = 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
Enter fullscreen modeExit fullscreen mode

Here, we create a FastAPI middleware and create a counter to count all the requests. We create the tracing function and add theall_request.inc() function which will increment by 1 for every request made to the server.

prometheus.yml

# my global configglobal:  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.  # scrape_timeout is set to the global default (10s).# Alertmanager configurationalerting:  alertmanagers:    - static_configs:        - targets:          # - alertmanager:9093# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.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 from this config.  - 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"]
Enter fullscreen modeExit fullscreen mode

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 asfastapi-server. We have to specify the port where the exporter is listening, in our case, 8000. Now, we can go tolocalhost:9090 and execute a query to test if Prometheus is collecting any metric.

We writeall_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.

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 throughTwitter, orLinkedIn.

The source code ishere.

Resources

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
Sloan, the sloth mascot
Comment deleted

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I'm a backend developer, I enjoy writing tutorials on how to use open-source projects in Python, Go, and Rust, that I use or find interesting.
  • Location
    Maturin/Venezuela
  • Joined

More fromCarlos Armando Marcano Vargas

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp