Lightstep (OpenTracing - Deprecated)

4.4.8 · deprecated · verified Thu Apr 16

The `lightstep` library provides the Python OpenTracing implementation for sending tracing data to Lightstep (now ServiceNow Cloud Observability). As of late 2020, Lightstep strongly recommends migrating to OpenTelemetry for all new and existing Python instrumentation, and this `lightstep-tracer-python` library is no longer the recommended approach. The current version is 4.4.8, but new development has shifted to OpenTelemetry-based solutions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to send traces to Lightstep/Cloud Observability using the recommended OpenTelemetry Python SDK. It configures a `TracerProvider` with an `OTLPSpanExporter` and sets up basic manual instrumentation. Ensure `LS_ACCESS_TOKEN` and `OTEL_SERVICE_NAME` environment variables are set. For non-development environments, using an OpenTelemetry Collector is highly recommended.

import os
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter

# --- Configuration (using environment variables for security) ---
LS_ACCESS_TOKEN = os.environ.get('LS_ACCESS_TOKEN', 'YOUR_LIGHTSTEP_ACCESS_TOKEN')
SERVICE_NAME = os.environ.get('OTEL_SERVICE_NAME', 'my-python-app')
OTLP_ENDPOINT = os.environ.get('OTEL_EXPORTER_OTLP_TRACES_ENDPOINT', 'ingest.lightstep.com:443')

# Configure resource
resource = Resource.create({"service.name": SERVICE_NAME})

# Configure tracer provider
provider = TracerProvider(resource=resource)

# Configure OTLP exporter for Lightstep
otlp_exporter = OTLPSpanExporter(
    endpoint=OTLP_ENDPOINT,
    headers={
        "lightstep-access-token": LS_ACCESS_TOKEN
    },
    # Optionally, if using a collector without TLS, set insecure=True
    # insecure=True
)

# Configure console exporter for local debugging
console_exporter = ConsoleSpanExporter()

# Add span processors
provider.add_span_processor(SimpleSpanProcessor(otlp_exporter))
provider.add_span_processor(SimpleSpanProcessor(console_exporter))

# Set the global tracer provider
trace.set_tracer_provider(provider)

# Acquire a tracer
tracer = trace.get_tracer(__name__)

def my_function():
    with tracer.start_as_current_span("my_function_span") as span:
        span.set_attribute("event", "example")
        print("Executing my_function")
        nested_function()

def nested_function():
    with tracer.start_as_current_span("nested_function_span"):
        print("Executing nested_function")

if __name__ == "__main__":
    print(f"Sending traces for service: {SERVICE_NAME} to {OTLP_ENDPOINT}")
    my_function()

    # It's important to shut down the tracer provider to ensure all spans are flushed
    provider.shutdown()

view raw JSON →