OpenTelemetry Python SDK
How to have Python applications using the OpenTelemetry Python SDK report data to Lumigo
Have you considered using the Lumigo OpenTelemetry Distro for Python?
The Lumigo OpenTelemetry Distro for Python packages the upstream OpenTelemetry Python SDK, a number of functionalities from the opentelemetry-python-contrib repository, together with a lot of additional automation, no-code capabilities and automated quality-assurance.
Overview
The steps to make a Python application report tracing data to Lumigo are a mix of programmatic setup and environment-based configuration.
This documentation does not cover how to add instrumentation to your Python code, but pointers are provided in the "What's Next" section at the end of the page.
Initialize the OpenTelemetry Python SDK
Assuming the following exporter setup in your Python application using the upstream OpenTelemetry Python SDK:
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
# Resource can be required for some backends, e.g. Jaeger
# If resource wouldn't be set - traces wouldn't appears in Jaeger
resource = Resource(attributes={
"service.name": "service"
})
# Important: Consider to set up the right OpenTelemetry Resource Detectors for
# the computing platform your application runs on!
trace.set_tracer_provider(TracerProvider(resource=resource))
tracer = trace.get_tracer(__name__)
otlp_exporter = OTLPSpanExporter()
span_processor = BatchSpanProcessor(otlp_exporter)
trace.get_tracer_provider().add_span_processor(span_processor)
# Once you set the environment variables OTEL_EXPORTER_OTLP_ENDPOINT and
# OTEL_EXPORTER_OTLP_HEADERS, you are good to go! Enjoy Lumigo!
Do not hard-code the Lumigo token in your application
Although the
OTLPSpanExporter
would allow you to pass theAuthorization
header as a literal, we strongly advise not to hard-code the Lumigo token in the code of your application. Consider using theOTEL_EXPORTER_OTLP_HEADERS
environment variable, or at least passing the Lumigo token as its own environment variable, and interpolate the header string you pass toOTLPSpanExporter
.
Configure the OpenTelemetry Python SDK
The OpenTelemetry OTLP Exporters package of the OpenTelemetry Python SDK can be connected to Lumigo using the following environment variables:
Environment variable | Value |
---|---|
OTEL_EXPORTER_OTLP_ENDPOINT | https://ga-otlp.lumigo-tracer-edge.golumigo.com |
OTEL_EXPORTER_OTLP_HEADERS | Authorization=LumigoToken <token> |
Replace <token>
with your Lumigo token, which is available in Settings -> Tracing -> Manual tracing
, see the Lumigo Tokens documentation.
Resource Attributes are very important!
When you instantiate the
TracerProvider
, you can pass it aResource
, which contains attributes that describe the your Python application and the platform on which it runs. See the Supported Semantic Conventions page for a list of endorsed Resource Detectors that will provide data useful for your experience of using Lumigo.
Updated 2 months ago