Skip to main content

Python OpenTelemetry Instrumentation

Book meeting

Get up and running with OpenTelemetry in just a few quick steps!

The setup process consists of two phases:

  • Getting OpenTelemetry installed and configured
  • Validating that configuration to ensure that data is being sent as expected.

This guide explains how to download, install, and run OpenTelemetry in Python.

Requirements

  • Python 3.6 or newer
  • An app to add OpenTelemetry to

We follow OpenTelemetry python instrumentation library. We shall be exporting data in OTLP protocol.

Start by creating a separate virtual environment.

python3 -m venv instrumentation_env
source instrumentation_env/bin/activate

Install

pip install opentelemetry-distro
pip install opentelemetry-exporter-otlp-proto-http

The below command inspects the Python dependencies of your application and installs the instrumentation packages relevant for your Python application.

opentelemetry-bootstrap --action=install

 

Run Command

Finally, to start sending data to SigNoz, use the following 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

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

 

Instrumenting a sample Flask application

We have included a sample flask application with README.md at https://github.com/SigNoz/sample-flask-app.

Feel free to use this repo to test out OpenTelemetry instrumentation and how to send telemetry data to SigNoz.

 

Instrumenting Django Applications

For instrumenting Django applications, the instructions are same as for a Flask app as mentioned above.

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.

You can also read this blog to see how OpenTelemetry is implemented in a Django application.

 

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.

 

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

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"
}
}

 

If DB Calls are not reported in spans

Ensure you have the correct opentelemetry instrumentations:

Frequently Asked Questions

  1. 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

  2. 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:

    1. 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.

    2. 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 console

    3. Your SigNoz installation is not running or behind a firewall

      Please double check if the pods in SigNoz installation are running fine. docker ps or kubectl get pods -n platform are your friends for this.