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 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.
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 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).
You can get started with SigNoz using just three commands at your terminal.
git clone -b main https://github.com/SigNoz/signoz.git
For detailed instructions, you can visit our documentation.
When you are done installing SigNoz, you can access the UI at: http://localhost:3301
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.
Getting a sample Flask application
- Python 3.4 or newer
Download the latest version of Python.
Below are the download links for different OS:
Running sample Flask app
We will be using the Flask app at this Github repo.
- Clone sample Flask app repository and go to the root folder
git clone https://github.com/SigNoz/sample-flask-app.git
- Check if the app is runningYou can now access the UI of the app on your local host: http://localhost:5002/
To capture data with OpenTelemetry, you need to configure some environment variables and run the app with OpenTelemetry packages. Once you ensure your app is running, stop the app with
ctrl + c on a mac. So let us see how to run the app with OpenTelemetry packages.
Instrumenting the Flask application with OpenTelemetry
Opentelemetry Python instrumentation installation
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:
pip3 install -r requirements.txt
If it hangs while installing
grpcioduring pip3 install opentelemetry-exporter-otlp then follow below steps as suggested in this stackoverflow link.
- pip3 install --upgrade pip
- python3 -m pip install --upgrade setuptools
- pip3 install --no-cache-dir --force-reinstall -Iv grpcio
Install application-specific packages
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:
Passing the necessary environment variables
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)
OTEL_EXPORTER_OTLP_ENDPOINT- In this case, IP of the machine where SigNoz is installed
You need to put these environment variables in the below command.
Don’t run app in reloader/hot-reload mode as it breaks instrumentation.
OTEL_RESOURCE_ATTRIBUTES=service.name=<service_name> OTEL_EXPORTER_OTLP_ENDPOINT="http://<IP of SigNoz>:4317" opentelemetry-instrument python3 app.py
As we are running SigNoz on local host,
IP of SigNozcan be replaced with
localhostin this case. And, for
Flask_App. Hence, the final command becomes:
OTEL_RESOURCE_ATTRIBUTES=service.name=Flask_App 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_appin the list of applications being monitored on SigNoz dashboard.
The debug mode can break instrumentation from happening because it enables a reloader. To run instrumentation while the debug mode is enabled, set the use_reloader option to False:
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5002, debug=True, use_reloader=False)
If you face any problem in instrumenting with OpenTelemetry, refer to docs at https://signoz.io/docs/instrumentation/python
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
You can then choose a particular timestamp where latency is high to drill down to traces around that timestamp.
You can use flamegraphs to exactly identify the issue causing the latency.
You can also build custom metrics dashboard for your infrastructure.
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 👇
If you have any questions or need any help in setting things up, join our slack community and ping us in
If you want to read more about SigNoz 👇