Skip to main content
Sumo Logic

Python OpenTelemetry auto-instrumentation

OpenTelemetry Python instrumentation gives the possibility to capture telemetry data from the python written code applications. The best way to start it is to incorporate OpenTelemetry-Python. See a list of the supported libraries under OpenTelemetry Instrumentations.

How to instrument your app?

The best way to instrument Python applications is to use OpenTelemetry-Python automatic instrumentation. This approach is simple, easy, and does not require many code changes. Only a few Python packages have to be installed to successfully instrument the code of the application. Instruction below applies to OpenTelemetry-Python instrumentation in version 1.1.0/0.20b0 with OpenTelemetry Protocol Exporter.

Installation of the package listed below is required to start the instrumentation.
    $ pip install opentelemetry-distro==0.20b0
    $ pip install opentelemetry-instrumentation==0.20b0
   

2. Application specific packages installation

The next step is related to the installation of the instrumented packages used in the application. This step can be done by one simple command in the root directory of your Python application. Execution of the command below will install corresponding instrumented packages used by the application. This solution is recommended.

$ opentelemetry-bootstrap --action=install

It is possible to install instrumented packages manually but it will require from the use to find libraries used in the code and then installation of their instrumented counterparts (e.g. Flask -> opentelemetry-instrumentation-flask).

After successful installation of the instrumented packages it is important to install and configure a span exporter. The most commonly used exporters are OTLP, Jaeger, and Zipkin. We recommend to use OpenTelemetry-Protocol Exporter. 

# Exporter installation
$ pip install opentelemetry-exporter-otlp==1.1.0

A few things have to be configured:

All of them can be configure by environment variables specific for the exporter. For OTLP gRPC Exporter these are:

export OTEL_TRACES_EXPORTER=otlp
export OTEL_EXPORTER_OTLP_ENDPOINT="ENDPOINT_ADDRESS"
export OTEL_RESOURCE_ATTRIBUTES="service.name=SERVICE_NAME"

When everything is configured it is very simple to run an instrumented application.

$ opentelemetry-instrument python3 SCRIPT_NAME.py