Monitoring your Flask application using OpenTelemetry
Tags: python, monitoring, performance, opentelemetry
Published: 2021-11-20 13:03:32

In this article, we will use OpenTelemetry to instrument a sample Flask app. Flask is one of the most popular web application frameworks of Python. It consists of Werkzeug WSGI toolkit and Jinja2 template engine.

Instrumentation is one of the biggest challenge engineering teams face when starting out with observability. Instrumenting a distributed application architecture is not easy. Applications now have distributed services as well as distributed teams that might be using multiple programming languages and numerous frameworks and libraries.

[OpenTelemetry](https://opentelemetry.io/) is the leading open-source standard that is solving the problem of instrumentation. It is currently an incubating project under the [Cloud Native Computing Foundation](https://www.cncf.io/).

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.

### Why use OpenTelemetry?

As might be clear by now that OpenTelemetry helps you to generate telemetry data. You still need a backend to analyze, store and visualize that data. By design, OpenTelemetry is vendor-agnostic. And that's one of the biggest advantages of using OpenTelemetry. 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 that can be used for both metrics and distributed tracing.

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

## Running a Flask application with OpenTelemetry

OpenTelemetry is a set of tools, APIs, and SDKs used to instrument applications to create and manage telemetry data(logs, metrics, and traces).

### 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 [official website](https://www.docker.com/get-started).

```jsx
git clone https://github.com/SigNoz/signoz.git
cd signoz/deploy/
./install.sh
```

You will have an option to choose between ClickHouse or Kafka + Druid as a storage option. Trying out SigNoz with ClickHouse database takes less than 1.5GB of memory, and for this tutorial, we will use that option.

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

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 showing application list](https:/signoz.io/img/blog/2021/08/signoz_dashboard_hc.webp)<figcaption>SigNoz Dashboard</figcation>

### Getting a sample Flask application

**Prerequisites**
1. Python 3.4 or newer<br></br>
   Download the [latest version](https://www.python.org/downloads/) of Python.
    
2. MongoDB<br></br>
   Below are the download links for different OS:<br></br>
   [MacOS](https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/)<br></br>
   [Linux](https://docs.mongodb.com/manual/administration/install-on-linux/)<br></br>
   [Windows](https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/)<br></br>


**Running sample Flask app**<br></br>
We will be using the Flask app at this [Github repo](https://github.com/SigNoz/sample-flask-app).

**Clone sample Flask app repository and go to the root folder**<br></br>
   ```jsx
   git clone https://github.com/SigNoz/sample-flask-app.git
   cd sample-flask-app
   ```
**Check if the app is running**
   ```jsx
   python3 app.py
   ```
You can now access the UI of the app on your local host: http://localhost:5000/

![Sample flask app](https:/signoz.io/img/blog/2021/11/sample_flask_app.webp)<figcaption>Sample flask application running on local host</figcation>


### Instrumenting the Flask application with OpenTelemetry
**Opentelemetry Python instrumentation installation**<br></br>

The app folder contains a file called `requirements.txt`, which contains all the necessary requirements to set up OpenTelemetry Python instrumentation. Make sure your path is updated to the root directory of your sample app and run the following command:
   ```jsx
   pip3 install -r requirements.txt
   ```
If it hangs while installing `grpcio` during **pip3 install opentelemetry-exporter-otlp** then follow below steps as suggested in [this stackoverflow link](https://stackoverflow.com/questions/56357794/unable-to-install-grpcio-using-pip-install-grpcio/62500932#62500932).

   - pip3 install --upgrade pip
   - python3 -m pip install --upgrade setuptools
   - pip3 install --no-cache-dir --force-reinstall -Iv grpcio

**Install application-specific packages**<br></br>
This step is required to install packages specific to the application. Make sure to run this command in the root directory of your installed application. This command figures out which instrumentation packages the user might want to install and installs it for them:
   ```jsx
   opentelemetry-bootstrap --action=install
   ```

**Passing the necessary environment variables**<br></br>
You're almost done. In the last step, you just need to configure a few environment variables for your OTLP exporters. Environment variables that need to be configured:
   - SERVICE_NAME  - application service name (you can name it as you like)
   - ENDPOINT_ADDRESS - OTLP gRPC collector endpoint address (IP of SigNoz)
   After taking care of these environment variables, you only need to run your instrumented application. Accomplish all these by using the following command at your terminal.
   ```jsx
   OTEL_RESOURCE_ATTRIBUTES=service.name=flaskApp OTEL_METRICS_EXPORTER=none OTEL_EXPORTER_OTLP_ENDPOINT="http://<IP of SigNoz>:4317" opentelemetry-instrument python3 app.py
   ```
   `Ip of SigNoz` can be replaced with localhost in this case. Hence, the final command becomes:
```jsx
OTEL_RESOURCE_ATTRIBUTES=service.name=flaskApp OTEL_METRICS_EXPORTER=none OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4317" opentelemetry-instrument python3 app.py
```

And congratulations! You have now instrumented your flask application with OpenTelemetry.

Below you can find your `Flask_app` in the list of applications being monitored on SigNoz dashboard.

![Flask app in the list of applications](https:/signoz.io/img/blog/2021/11/flask_app_list_signoz.webp)<figcaption>Flask app in the list of applications monitored by SigNoz</figcation>

## 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.</figcation>

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</figcation>

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</figcation>

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</figcation>

## Conclusion
OpenTelemetry makes it very convenient to instrument your Flask 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)

---

If you want to read more about SigNoz 👇

[Golang Aplication Monitoring with OpenTelemetry and SigNoz](https://signoz.io/opentelemetry/go/)

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