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.

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.

Installation of the packages listed below is required to start the instrumentation.

$ pip install opentelemetry-sdk
$ pip install opentelemetry-instrumentation

The next step is related to the installation of the instrumented packages used in the application. Manual installation of the instrumented packages - solution requires the user to find libraries used in the code and then installation of their instrumented counterparts (e.g. Flask -> opentelemetry-instrumentation-flask). It is recommended to install the packages manually.

$ pip install 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.

OTLP exporter configuration - recommended

# Exporter installation
$ pip install opentelemetry-exporter-otlp

A few things have to be configured:

  • SERVICE_NAME - insert there application service name
  • OTLP_ENDPOINT_NAME - hostname address supporting OTLP protocol
  • OTLP_ENDPOINT_PORT - port of the hostname supporting OTLP protocol (default port 55680)

from opentelemetry import trace
from opentelemetry.exporter.otlp.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor

resource = Resource(labels={
   "service.name": "SERVICE_NAME"
})

trace.set_tracer_provider(TracerProvider(resource=resource)))
tracer = trace.get_tracer(__name__)

exporter = OTLPSpanExporter(endpoint="OTLP_ENDPOINT_HOSTNAME:OTLP_ENDPOINT_PORT”)

span_processor = BatchExportSpanProcessor(exporter)

trace.get_tracer_provider().add_span_processor(span_processor)

# Your application code below

 

When everything is configured and the code is ready, it is very simple to run an instrumented application.

$ opentelemetry-instrument python3 SCRIPT_NAME.py