### Build and Monitor Your FastAPI Microservice With Docker, Prometheus and Grafana. (Part-1) - by Collins Onyemaobi - Medium
### Build and Monitor Your FastAPI Microservice With Docker, Prometheus and Grafana. (Part-1) - by Collins Onyemaobi - Medium
Open in app
Search
Get unlimited access to the best of Medium for less than $1/week. Become a member
Executive Summary
1. Build a simple bored endpoint that consumes an API that informs one “to find
something to do by getting suggestions for random activities using” …
4. We shall use docker to spine up containers for FastAPI, Prometheus and Grafana
5. Deploy Grafana and Prometheus, both to help us monitor and visualize complex
data in a simplified manner. Through the use of METRICS for example CPU
Load, Current Memory, common error output, a web service response and
request etc.
Stack
Dependencies:
1. FastAPI
2. Docker
https://medium.com/@ct.onyemaobi/build-and-monitor-your-fastapi-microservice-with-docker-prometheus-and-grafana-part-1-7e69a2e5fb67 1/13
31/10/2023, 11:59 Build and monitor your FastAPI microservice with Docker, Prometheus and Grafana. [Part-1] | by Collins Onyemaobi | Medium
3. Python
4. Prometheus
5. Grafana
FastAPI
FastAPI is a modern, fast (high-performance), Python web framework that’s perfect
for building RESTful APIs based on standard Python type hints.
It can handle both synchronous and asynchronous requests and has built-in support
for data validation, JSON serialization, authentication and authorization, and
OpenAPI documentation.
Highlights (FastAPI)
1. FastAPI has surged in popularity since 2019 since it supports async out-of-the-
box and has an advanced data validation feature based on Python type hints.
2. it is a lightweight microframework
3. It takes advantage of Python type hints for parameter declaration which enables
data validation (via Pydantic) and OpenAPI/Swagger documentation.
5. It’s fast. Since async is much more efficient than the traditional synchronous
threading model, it can compete with Node and Go with regard to performance.
6. Because it’s based on and fully compatible with OpenAPI and JSON Schema, it
supports a number of powerful tools, like Swagger UI (allows testing the API
from the docs) and ReDoc
8. Security and Authentication with HTTP Basic, OAuth2 and passing of api keys
Sample Code
FastAPI does not have a built-in development server, so an ASGI server like Uvicorn
or Daphne is required.
https://medium.com/@ct.onyemaobi/build-and-monitor-your-fastapi-microservice-with-docker-prometheus-and-grafana-part-1-7e69a2e5fb67 2/13
31/10/2023, 11:59 Build and monitor your FastAPI microservice with Docker, Prometheus and Grafana. [Part-1] | by Collins Onyemaobi | Medium
Grafana and Prometheus, both help us monitor and visualize complex data in a
simplified manner. They do these through the use of METRICS
Understanding Metrics
In layperson terms, metrics are numeric measurements and time-series mean
that changes as being recorded over time.
https://medium.com/@ct.onyemaobi/build-and-monitor-your-fastapi-microservice-with-docker-prometheus-and-grafana-part-1-7e69a2e5fb67 3/13
31/10/2023, 11:59 Build and monitor your FastAPI microservice with Docker, Prometheus and Grafana. [Part-1] | by Collins Onyemaobi | Medium
are high. If you have the request count metric you can spot the reason and
increase the number of servers to handle the load.
Prometheus and Grafana is a software combo that is gaining traction because they are
simple to use and effective complementary services that,together, provide robust time-series
database with excellent data visualization — Tim Hildred (Red Hat) — 07 May 2020
Prometheus
Prometheus is an open-source system monitoring and alerting toolkit. It collects
and stores its metrics as time-stamp data i.e metrics info is stored with the
timestamp at which it is recorded alongside key-value pairs called labels.
Grafana
While Prometheus collects and stores query data, Grafana visualizes it.
https://medium.com/@ct.onyemaobi/build-and-monitor-your-fastapi-microservice-with-docker-prometheus-and-grafana-part-1-7e69a2e5fb67 4/13
31/10/2023, 11:59 Build and monitor your FastAPI microservice with Docker, Prometheus and Grafana. [Part-1] | by Collins Onyemaobi | Medium
Grafana is an easy to use Third party app that helps visualize data from data
sources such as Prometheus.
Easy Visualization
Alert systems
Dashboard sharing
https://medium.com/@ct.onyemaobi/build-and-monitor-your-fastapi-microservice-with-docker-prometheus-and-grafana-part-1-7e69a2e5fb67 5/13
31/10/2023, 11:59 Build and monitor your FastAPI microservice with Docker, Prometheus and Grafana. [Part-1] | by Collins Onyemaobi | Medium
Follow
Software Engineer / Electoral Technology Consultant. I have developed and supported technology solutions
for West African sponsored by (UNDP, ECOWAS, INEC, etc)
https://medium.com/@ct.onyemaobi/build-and-monitor-your-fastapi-microservice-with-docker-prometheus-and-grafana-part-1-7e69a2e5fb67 6/13
31/10/2023, 11:59 Build and monitor your FastAPI microservice with Docker, Prometheus and Grafana. [Part-1] | by Collins Onyemaobi | Medium
Collins Onyemaobi
5 1
Collins Onyemaobi
https://medium.com/@ct.onyemaobi/build-and-monitor-your-fastapi-microservice-with-docker-prometheus-and-grafana-part-1-7e69a2e5fb67 7/13
31/10/2023, 11:59 Build and monitor your FastAPI microservice with Docker, Prometheus and Grafana. [Part-1] | by Collins Onyemaobi | Medium
Collins Onyemaobi
https://medium.com/@ct.onyemaobi/build-and-monitor-your-fastapi-microservice-with-docker-prometheus-and-grafana-part-1-7e69a2e5fb67 8/13
31/10/2023, 11:59 Build and monitor your FastAPI microservice with Docker, Prometheus and Grafana. [Part-1] | by Collins Onyemaobi | Medium
Collins Onyemaobi
https://medium.com/@ct.onyemaobi/build-and-monitor-your-fastapi-microservice-with-docker-prometheus-and-grafana-part-1-7e69a2e5fb67 9/13
31/10/2023, 11:59 Build and monitor your FastAPI microservice with Docker, Prometheus and Grafana. [Part-1] | by Collins Onyemaobi | Medium
Uptrace
Adso
https://medium.com/@ct.onyemaobi/build-and-monitor-your-fastapi-microservice-with-docker-prometheus-and-grafana-part-1-7e69a2e5fb67 10/13
31/10/2023, 11:59 Build and monitor your FastAPI microservice with Docker, Prometheus and Grafana. [Part-1] | by Collins Onyemaobi | Medium
70
Lists
New_Reading_List
174 stories · 166 saves
Jesum
https://medium.com/@ct.onyemaobi/build-and-monitor-your-fastapi-microservice-with-docker-prometheus-and-grafana-part-1-7e69a2e5fb67 11/13
31/10/2023, 11:59 Build and monitor your FastAPI microservice with Docker, Prometheus and Grafana. [Part-1] | by Collins Onyemaobi | Medium
https://medium.com/@ct.onyemaobi/build-and-monitor-your-fastapi-microservice-with-docker-prometheus-and-grafana-part-1-7e69a2e5fb67 12/13
31/10/2023, 11:59 Build and monitor your FastAPI microservice with Docker, Prometheus and Grafana. [Part-1] | by Collins Onyemaobi | Medium
Kamalesh D in DevOps.dev
12
Kasper Junge
65
https://medium.com/@ct.onyemaobi/build-and-monitor-your-fastapi-microservice-with-docker-prometheus-and-grafana-part-1-7e69a2e5fb67 13/13