0% found this document useful (0 votes)
139 views

### Build and Monitor Your FastAPI Microservice With Docker, Prometheus and Grafana. (Part-1) - by Collins Onyemaobi - Medium

This document discusses building and monitoring a FastAPI microservice using Docker, Prometheus, and Grafana. It introduces FastAPI as a Python web framework for building RESTful APIs and describes how Docker will be used to containerize FastAPI, Prometheus, and Grafana. Prometheus and Grafana will be deployed to monitor metrics like CPU load and memory usage and Grafana will be used to visualize the metrics collected by Prometheus.

Uploaded by

funda.ram8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views

### Build and Monitor Your FastAPI Microservice With Docker, Prometheus and Grafana. (Part-1) - by Collins Onyemaobi - Medium

This document discusses building and monitoring a FastAPI microservice using Docker, Prometheus, and Grafana. It introduces FastAPI as a Python web framework for building RESTful APIs and describes how Docker will be used to containerize FastAPI, Prometheus, and Grafana. Prometheus and Grafana will be deployed to monitor metrics like CPU load and memory usage and Grafana will be used to visualize the metrics collected by Prometheus.

Uploaded by

funda.ram8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

31/10/2023, 11:59 Build and monitor your FastAPI microservice with Docker, Prometheus and Grafana.

s 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

Build and monitor your FastAPI microservice


with Docker, Prometheus and Grafana. [Part-1]
Collins Onyemaobi · Follow
4 min read · Feb 2, 2022

Listen Share More

a match made in Software heaven

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” …

2. A modern, fast (high-performance), Python web framework that’s perfect for


building RESTful APIs based on standard Python type hints. — FastAPI

3. We’ll also be able to view the interactive API documentation, powered by


Swagger UI, at http://host:port/docs:

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.

4. Built on top of Starlette. Starlette + Uvicorn offers async capability which


supports the development of asynchronous APIs.

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

7. It has amazing automatic documentation.

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

DATA MONITORING AND VISUALIZATION


System Data monitoring and visualization are as crucial as maintaining a
system. It allows companies to understand application and user behavior
quickly and effect proactive response, identify bugs, and scale an application
due to usage as needed.

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.

What users want to measure differs from application to application.

for a web server, it might be request times

for a database it might be the number of active connections or number of active


queries, etc.

Metrics play an important role in understanding why your application is


working in a certain way.
For example, your application can become slow when the number of requests

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.

Key Features of Prometheus includes;

Provides time-series data

Flexible query language, PromQL allows users to select data in real-time.

Multidimensional data model.

No reliance on distributed storage; single server nodes are autonomous

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.

Grafana opens up data collected in prometheus to a whole lot of visualization


possibilities such as graphs, charts, histograms, and tables.

Prometheus aggregates all metrics collected as time-stamp data into a single


graph while grafana allows you to present the metrics collected as you wish

Key Features of Grafana includes;

Easy Visualization

Alert systems

Unifies data from different data sources

Dashboard sharing

FastAPI Visualization with Grafana


The final build

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

Fastapi Docker Prometheus Grafana Plugin

Follow

Written by Collins Onyemaobi


23 Followers

Software Engineer / Electoral Technology Consultant. I have developed and supported technology solutions
for West African sponsored by (UNDP, ECOWAS, INEC, etc)

More from Collins Onyemaobi

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

Deploy a Secure FastAPI App on Ubuntu 20.04 using Python3.10 /


CertBot / Nginx and Gunicorn.
The overall objective is to deploy a Secure and Optimised FastAPI Performance Application.
We are required to launch a Compute instance…

3 min read · Apr 24, 2022

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

Amazing Image Processing in Python — In a Simple Web Application


[Part-2]
Here’s the link to [Part 1]: https://medium.com/@ct.onyemaobi/amazing-image-processing-in-
python-in-5-lines-of-code-44a1184bea57

1 min read · Jul 1, 2022

Collins Onyemaobi

Data Integrity Considerations


1. Data Integrity

4 min read · Nov 26, 2022

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

Build and monitor your FastAPI microservice with Docker, Prometheus


and Grafana. [Part-2]
The listed video provides additional details and an explanation of the setup, building, and
monitoring of your FastAPI microservice with…

1 min read · Apr 11, 2022

See all from Collins Onyemaobi

Recommended from Medium

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

Monitor FastAPI with OpenTelemetry


By integrating OpenTelemetry with FastAPI, you can gain valuable insight into the
performance, behavior and dependencies of your API. You…

3 min read · Sep 14

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

Exposing Python Metrics with Prometheus


Prometheus is a powerful monitoring and alerting system that collects and stores time-series
data. In this step-by-step guide, we will…

4 min read · Jun 26

70

Lists

Coding & Development


11 stories · 247 saves

New_Reading_List
174 stories · 166 saves

Natural Language Processing


762 stories · 347 saves

Jesum

Quick-Start to Integrating OpenTelemetry with FastAPI (Part 3)


Traces, and logs

4 min read · Jul 10

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

Kelvin Sajere in Stackademic

Managing Long Running Process Using Rabbit MQ in FastAPI


In the pursuit of building robust systems, we often encounter the need to implement long-
running processes, involving intricate file…

3 min read · Sep 6

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

Monitoring Docker Container Performance with Prometheus and Grafana


New day, New Topic…. Let’s learn along..!!

9 min read · Jul 26

12

Kasper Junge

How to Use Celery 🥦


Processing tasks asynchronously is a common requirement for applications that demand high
performance and responsiveness. Python’s “Celery”…

6 min read · Jun 12

65

See more recommendations

https://medium.com/@ct.onyemaobi/build-and-monitor-your-fastapi-microservice-with-docker-prometheus-and-grafana-part-1-7e69a2e5fb67 13/13

You might also like