Documentation
DocumentationDiscussions
Documentation
These docs are for v2024.1. Click to read the latest docs for v2024.3.

Tracing from Python

To send traces from Python to Seq use the OpenTelemetry JavaScript SDK and its OTLP exporter. See the OpenTelemetry documentation for more details.

OpenTelemetry logging for Python does not support structured logging, so it is best to stick with using seqlog for logging.

Installing the OpenTelemetry Python SDK

Install the SDK packages:

pip install opentelemetry-api  
pip install opentelemetry-sdk  
pip install opentelemetry-exporter-otlp

Configure Tracing:

from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.sdk.resources import SERVICE_NAME, Resource

# Service name is required for most backends
resource = Resource(attributes={
    SERVICE_NAME: "my-service"
})
traceProvider = TracerProvider(resource=resource)
processor = BatchSpanProcessor(OTLPSpanExporter(endpoint="http://localhost:5341/ingest/otlp/v1/traces"))
traceProvider.add_span_processor(processor)
trace.set_tracer_provider(traceProvider)
tracer = trace.get_tracer("my-tracer")

Using the OpenTelemetry Python SDK

Use the start_as_current_span method to begin new spans.

with tracer.start_as_current_span("this is a span") as span:
  logger.warning("The weather forecast is %s", "Overcast, 24°C")
  span.set_attribute("parent-attribute", 5)
  with tracer.start_as_current_span("child span") as cspan:
    cspan.set_attribute("child-attribute", 42)