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.

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

 

     b.  Jaeger exporter configuration

# Exporter installation
$ pip install opentelemetry-exporter-jaeger

A few things have to be configured:

  • SERVICE_NAME - insert there application service name
  • JAEGER_COLLECTOR_HOST_NAME - hostname address of the receiver supporting HTTP Jaeger thrift protocol
  • JAEGER_COLLECTOR_PORT - port of the receiver supporting HTTP Jaeger thrift protocol (default port 14268)

from opentelemetry import trace
from opentelemetry.exporter import jaeger
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor

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

exporter = jaeger.JaegerSpanExporter(
   service_name='SERVICE_NAME',
   collector_host_name='JAEGER_COLLECTOR_HOST_NAME',
   collector_port='JAEGER_COLLECTOR_PORT',
)

span_processor = BatchExportSpanProcessor(exporter)

trace.get_tracer_provider().add_span_processor(span_processor)

# Your application code below

 

     c.  Zipkin exporter configuration

# Exporter installation
$ pip install opentelemetry-exporter-zipkin

A few things have to be configured:

  • SERVICE_NAME - insert there application service name
  • ZIPKIN_HOST_NAME - hostname address of the receiver supporting HTTP Zipkin protocol
  • ZIPKIN_HOST_PORT - port of the receiver supporting HTTP Zipkin protocol (default port 9411)

from opentelemetry import trace
from opentelemetry.exporter.zipkin import ZipkinSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor

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

exporter = ZipkinSpanExporter(
   service_name="SERVICE_NAME",
   host_name="ZIPKIN_HOST_NAME",
   port="ZIPKIN_HOST_PORT",
   endpoint="/api/v2/spans"
)

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