Monitoring your FastAPI application using OpenTelemetry📊🚀
Tags: python, devops, monitoring, opentelemetry
Published: 2021-11-25 12:53:10

FastAPI is a modern Python web framework based on standard Python type hints that makes it easy to build APIs. It's a relatively new framework, having been released in 2018 but has now been adopted by big companies like Uber, Netflix, and Microsoft.

FastAPI is one of the fastest Python web frameworks currently available and is really efficient when it comes to writing code. It is based on ASGI specification, unlike other Python frameworks like Flask, which is based on WSGI specification.

Instrumentation is the biggest challenge engineering teams face when starting out with monitoring their application performance. <a href = "https://opentelemetry.io/" rel="noopener noreferrer nofollow" target="_blank" >OpenTelemetry</a> is the leading open-source standard that is solving the problem of instrumentation. It is currently an incubating project under the <a href = "https://www.cncf.io/" rel="noopener noreferrer nofollow" target="_blank" >Cloud Native Computing Foundation</a>.

It is a set of tools, APIs, and SDKs used to instrument applications to create and manage telemetry data(Logs, metrics, and traces). It aims to make telemetry data(logs, metrics, and traces) a built-in feature of cloud-native software applications.

One of the biggest advantages of using OpenTelemetry is that it is vendor-agnostic. It can export data in multiple formats which you can send to a backend of your choice.

In this article, we will use [SigNoz](https://signoz.io/) as a backend. SigNoz is an open-source APM tool that can be used for both metrics and distributed tracing.

Let's get started and see how to use OpenTelemetry for a FastAPI application.

## Running a FastAPI application with OpenTelemetry

OpenTelemetry is a great choice to instrument ASGI frameworks. As it is open-source and vendor-agnostic, the data can be sent to any backend of your choice.

### Installing SigNoz

You can get started with SigNoz using just three commands at your terminal if you have Docker installed. You can install Docker from its <a href = "https://www.docker.com/get-started" rel="noopener noreferrer nofollow" target="_blank" >official website</a>.

```jsx
git clone https://github.com/SigNoz/signoz.git
cd signoz/deploy/
./install.sh
```
The `install.sh` script will install SigNoz for you. Trying out SigNoz with ClickHouse database takes less than 1.5GB of memory.

[![Deployment Docs](https:/signoz.io/img/blog/common/deploy_docker_documentation.webp)](https://signoz.io/docs/deployment/docker/?utm_source=blog&utm_medium=fastapi)

When you are done installing SigNoz, you can access the UI at: [http://localhost:3000](http://localhost:3000/application)

The application list shown in the dashboard is from a sample app called HOT R.O.D that comes bundled with the SigNoz installation package.

![SigNoz dashboard home](https:/signoz.io/img/blog/common/signoz_dashboard_homepage.webp)<figcaption>List of applications shown as an example on SigNoz dashboard</figcaption>

### Instrumenting a sample FastAPI application with OpenTelemetry

**Prerequisites**<br></br>
Python 3.6 or newer

Download the [latest version](https://www.python.org/downloads/) of Python.

**Running sample FastAPI app**<br></br>
We will be using the FastAPI app at this [Github repo](https://github.com/sureshdsk/sample-fastapi-app). All the required OpenTelemetry packages are contained within the `requirements.txt` file under `app` folder in this sample app.
```jsx
git clone https://github.com/sureshdsk/sample-fastapi-app.git
cd sample-fastapi-app/
```
<br></br>

**Run with docker**<br></br>
Build docker image
```jsx
docker build -t sample-fastapi-app .
```
You need to set some environment variables while running the application with OpenTelemetry and send collected data to SigNoz. You can do so with the following commands at the terminal:
```jsx
# If you have your SigNoz IP Address, replace <IP of SigNoz> with your IP Address. 

docker run -d --name fastapi-container \
-e OTEL_METRICS_EXPORTER='none' \
-e OTEL_RESOURCE_ATTRIBUTES='service.name=fastapiApp' \
-e OTEL_EXPORTER_OTLP_ENDPOINT='http://<IP of SigNoz>:4317' \
-p 5000:5000 sample-fastapi-app
```
<br></br>

If you're using docker-compose setup:

```jsx
# If you are running signoz through official docker-compose setup, run `docker network ls` and find clickhouse network id. It will be something like this clickhouse-setup_default 
# and pass network id by using --net <network ID>

docker run -d --name fastapi-container \ 
--net clickhouse-setup_default  \ 
--link clickhouse-setup_otel-collector_1 \
-e OTEL_METRICS_EXPORTER='none' \
-e OTEL_RESOURCE_ATTRIBUTES='service.name=fastapiApp' \
-e OTEL_EXPORTER_OTLP_ENDPOINT='http://clickhouse-setup_otel-collector_1:4317' \
-p 5000:5000 sample-fastapi-app
```
<br></br>

If you're running SigNoz in your local host then you can replace `<IP of SigNoz>` with `localhost` and the final command will look like below:

```jsx
docker run -d --name fastapi-container \
-e OTEL_METRICS_EXPORTER='none' \
-e OTEL_RESOURCE_ATTRIBUTES='service.name=fastapiApp' \
-e OTEL_EXPORTER_OTLP_ENDPOINT='http://localhost:4317' \
-p 5000:5000 sample-fastapi-app
```
<br></br>

You need to generate some load on your app so that there is data to be captured by OpenTelemetry. You can use locust for this load testing.

```jsx
pip install locust
```

```jsx
locust -f locust.py --headless --users 10 --spawn-rate 1 -H http://localhost:5000
```
<br></br>

You will find `fastapiAPP` in the list of sample applications being monitored by SigNoz.

![FastAPI in the list of applications](https:/signoz.io/img/blog/2021/11/list_of_apps_fastapi.webp)<figcaption>FastAPI in the list of applications being monitored by SigNoz</figcaption>

## Open-source tool to visualize telemetry data
SigNoz makes it easy to visualize metrics and traces captured through OpenTelemetry instrumentation.

SigNoz comes with out of box RED metrics charts and visualization. RED metrics stands for:

- Rate of requests
- Error rate of requests
- Duration taken by requests

![SigNoz charts and metrics](https:/signoz.io/img/blog/common/signoz_charts_application_metrics.webp)<figcaption>Measure things like application latency, requests per sec, error percentage and see your top endpoints with SigNoz.</figcaption>

You can then choose a particular timestamp where latency is high to drill down to traces around that timestamp.

![List of traces on SigNoz dashboard](https:/signoz.io/img/blog/common/signoz_list_of_traces_hc.webp)<figcaption>View of traces at a particular timestamp</figcaption>

You can use flamegraphs to exactly identify the issue causing the latency.

![Flamegraphs used to visualize spans of distributed tracing in SigNoz UI](https:/signoz.io/img/blog/common/signoz_flamegraphs.webp)<figcaption>View of traces at a particular timestamp</figcaption>

You can also build custom metrics dashboard for your infrastructure.

![Custom metrics dashboard](https:/signoz.io/img/blog/common/signoz_custom_dashboard-min.webp)<figcaption>You can also build a custom metrics dashboard for your infrastructure</figcaption>

## Conclusion
OpenTelemetry makes it very convenient to instrument your FastAPI application. You can then use an open-source APM tool like SigNoz to analyze the performance of your app. As SigNoz offers a full-stack observability tool, you don't have to use multiple tools for your monitoring needs.

You can try out SigNoz by visiting its GitHub repo 👇

[![SigNoz GitHub repo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8yroh39ngk6sz6tv3z9y.png)](https://bit.ly/2WkkmL4)

If you have any questions or need any help in setting things up, join our slack community and ping us in `#help` channel.

[![SigNoz Slack community](https:/signoz.io/img/blog/common/join_slack_cta.png)](https://bit.ly/signoz-slack)

---
Read more about OpenTelemetry 👇

[Things you need to know about OpenTelemetry tracing](https://signoz.io/blog/opentelemetry-tracing/)

[OpenTelemetry collector - architecture and configuration guide](https://signoz.io/blog/opentelemetry-collector-complete-guide/)