Python OpenTelemetry Instrumentation
This document contains instructions on how to set up OpenTelemetry instrumentation in your Python applications. OpenTelemetry, also known as OTel for short, is an open source observability framework that can help you generate and collect telemetry data - traces, metrics, and logs from your Python application.
Once the telemetry data is collected, you can configure an exporter to send the data to SigNoz.
There are three major steps to using OpenTelemetry:
- Instrumenting your Python application with OpenTelemetry
- Configuring exporter to send data to SigNoz
- Validating that configuration to ensure that data is being sent as expected.
Let’s understand how to download, install, and run OpenTelemetry in Python.
Requirements​
- Python 3.6 or newer
Traces​
You can use OpenTelemetry Python to send your traces directly to SigNoz. OpenTelemetry provides a handy distro in Python that can help you get started with automatic instrumentation. We recommend using it to get started quickly.
Steps to auto-instrument Python app for traces​
info
If you are on K8s, you should checkout opentelemetry operators which enable auto instrumenting Python applications very easily.
Create a virtual environment
python3 -m venv instrumentation_env
source instrumentation_env/bin/activateInstall the OpenTelemetry dependencies
pip install opentelemetry-distro
pip install opentelemetry-exporter-otlpThe dependencies included are briefly explained below:
opentelemetry-distro
- The distro provides a mechanism to automatically configure some of the more common options for users. It helps to get started with OpenTelemetry auto-instrumentation quickly.opentelemetry-exporter-otlp
- This library provides a convenient way to install all supported OpenTelemetry Collector Exporters. You will need an exporter to send the data to SigNoz.note
💡 TheÂ
opentelemetry-exporter-otlp
 is a convenient way to install all supported OpenTelemetry exporters. Currently it installs:- opentelemetry-exporter-otlp-proto-http
- opentelemetry-exporter-otlp-proto-grpc
We recommend using the http exporter for sending data to SigNoz.
Add automatic instrumentation
The below command inspects the dependencies of your application and installs the instrumentation packages relevant for your Python application.opentelemetry-bootstrap --action=install
Run your application
In the final run command, you can configure environment variables and flags. Flags for exporters:
HTTP:Â otlp_proto_http
gRPC:Â otlp_proto_grpcWe recommend using theÂ
otlp_proto_http
 exporter.note
Don’t run app in reloader/hot-reload mode as it breaks instrumentation. For example, if you use
export FLASK_ENV=development
, it enables the reloader mode which breaks OpenTelemetry isntrumentation.To start sending data to SigNoz, use the following run command:
OTEL_RESOURCE_ATTRIBUTES=service.name=<service_name> OTEL_EXPORTER_OTLP_ENDPOINT="http://<IP of SigNoz Backend>:4318" opentelemetry-instrument --traces_exporter otlp_proto_http <your run command>
<service_name>Â is the name of service you want
<your_run_command>Â can beÂ
python3 app.py
 orÂflask run
IP of SigNoz backend
is the IP of the machine where you installed SigNoz. If you have installed SigNoz onlocalhost
, the endpoint will behttp://localhost:4318
.Replacing these environment variables, a sample final run command will look like this:
OTEL_RESOURCE_ATTRIBUTES=service.name=python_app OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4318" opentelemetry-instrument --traces_exporter otlp_proto_http python3 app.py
note
The port numbers are 4317 and 4318 for the gRPC and HTTP exporters respectively. Remember to allow incoming requests to port 4317/4318 of machine where SigNoz backend is hosted.
Validating instrumentation by checking for traces​
With your application running, you can verify that you’ve instrumented your application with OpenTelemetry correctly by confirming that tracing data is being reported to SigNoz.
To do this, you need to ensure that your application generates some data. Applications will not produce traces unless they are being interacted with, and OpenTelemetry will often buffer data before sending. So you need to interact with your application and wait for some time to see your tracing data in SigNoz.
Validate your traces in SigNoz:
- Trigger an action in your app that generates a web request. Hit the endpoint a number of times to generate some data. Then, wait for some time.
- In SigNoz, open theÂ
Services
 tab. Hit theÂRefresh
 button on the top right corner, and your application should appear in the list ofÂApplications
. - Go to theÂ
Traces
 tab, and apply relevant filters to see your application’s traces.
You might see other dummy applications if you’re using SigNoz for the first time. You can remove it by following the docs here.
Instrumenting different Python Frameworks​
The opentelemetry-distro
package can initialize instrumentation for a lot of popular Python frameworks. You can find a complete list here. For popular Python frameworks too, the distro provides a quick way to get started with automatic instrumentation.
Django Instrumentation​
It is recommended to use the opentelemetry distro for instrumenting Django applications. Though for Django, you must define DJANGO_SETTINGS_MODULE
correctly. If your project is called mysite
, something like following should work:
export DJANGO_SETTINGS_MODULE=mysite.settings
Please refer the official Django docs for more details.
Flask Instrumentation​
It is recommended to use the opentelemetry distro for instrumenting Flask applications.
FastAPI Instrumentation​
It is recommended to use the opentelemetry distro for instrumenting FastAPI applications.
Falcon Instrumentation​
It is recommended to use the opentelemetry distro for instrumenting Falcon applications.
Database Instrumentation​
Make sure that the DB client library you are using has the corresponding instrumentation library, and the version of the DB client library is supported by OpenTelemetry.
MongoDB​
You can use opentelemetry-distro
to initialize instrumentation for your MongoDB database calls. You need to ensure that the version of your DB client library is supported by OpenTelemetry. For MongoDB, the instrumentation library is opentelemetry-instrumentation-pymongo
.
You can check the supported versions here.
Redis​
You can use opentelemetry-distro
to initialize instrumentation for your Redis database calls. You need to ensure that the version of your DB client library is supported by OpenTelemetry. For Redis, the instrumentation library is opentelemetry-instrumentation-redis
.
You can check the supported versions here.
MySQL​
You can use opentelemetry-distro
to initialize instrumentation for your MySQL database calls. You need to ensure that the version of your DB client library is supported by OpenTelemetry. For MySQL, we have two isntrumentation libraries:
- opentelemetry-instrumentation-mysql
- opentelemetry-instrumentation-pymysql
You can check the supported versions here.
Postgres​
You can use opentelemetry-distro
to initialize instrumentation for your PostgreSQL database calls. You need to ensure that the version of your DB client library is supported by OpenTelemetry. For Postgres, the instrumentation library is opentelemetry-instrumentation-psycopg2
.
You can check the supported versions here.
note
psycopg2-binary
is not supported by opentelemetry auto instrumentation libraries as it is not recommended for production use. Please use psycopg2
to see DB calls also in your trace data in SigNoz
Running applications with Gunicorn, uWSGI​
For application servers which are based on pre fork model like Gunicorn, uWSGI you have to add a post_fork
hook or a @postfork
decorator in your configuration.
Check this documentation from OpenTelemetry on how to set it up.
Here's a working example where we have configured a gunicorn server with post_fork
hook.
Â
Troubleshooting your installation​
Spans are not being reported​
If spans are not being reported to SigNoz, try enabling debug exporter which writes the json formatted trace data to console.
opentelemetry-instrument --traces_exporter otlp_proto_http,console <your run command>
:
{
"name": "alice",
"context": {
"trace_id": "0xedb7caf0c8b082a9578460a201759193",
"span_id": "0x57cf7eee198e1fed",
"trace_state": "[]"
},
"kind": "SpanKind.INTERNAL",
"parent_id": null,
"start_time": "2022-03-27T14:55:18.804758Z",
"end_time": "2022-03-27T14:55:18.804805Z",
"status": {
"status_code": "UNSET"
},
"attributes": {},
"events": [],
"links": [],
"resource": {
"telemetry.sdk.language": "python",
"telemetry.sdk.name": "opentelemetry",
"telemetry.sdk.version": "1.10.0",
"service.name": "my-service"
}
}
Â
Frequently Asked Questions​
How to find what to use in
IP of SigNoz
if I have installed SigNoz in Kubernetes cluster?Based on where you have installed your application and where you have installed SigNoz, you need to find the right value for this. Please use this grid to find the value you should use for
IP of SigNoz
I am sending data from my application to SigNoz, but I don't see any events or graphs in the SigNoz dashboard. What should I do?
This could be because of one of the following reasons:
Your application is generating telemetry data, but not able to connect with SigNoz installation
Please use this troubleshooting guide to find if your application is able to access SigNoz installation and send data to it.
Your application is not actually generating telemetry data
Please check if the application is generating telemetry data first. You can use
Console Exporter
to just print your telemetry data in console first. Join our Slack Community if you need help on how to export your telemetry data in consoleYour SigNoz installation is not running or behind a firewall
Please double check if the pods in SigNoz installation are running fine.
docker ps
orkubectl get pods -n platform
are your friends for this.