Skip to content
Python

Export traces from Python

Send existing Python traces to BTX alongside your other destinations.

Install the exporter

Use the official OpenTelemetry HTTP exporter to send your existing Python traces to BTX.

Install the HTTP exporter at a version compatible with your application's existing OTel SDK:

bash
pip install opentelemetry-exporter-otlp-proto-http

Create a project API key and make it available to your backend as BTX_API_KEY. Your application must already have instrumentation and an SDK TracerProvider.

Add BTX to your provider

Use the same provider that your instrumentation and other exporters already use. Keep your Langfuse processor registered. Add the BTX processor once, after any worker process forks.

python
import os
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace.export import BatchSpanProcessor

btx_processor = BatchSpanProcessor(
    OTLPSpanExporter(
        endpoint="https://btx.so/api/otel/v1/traces",
        headers={"Authorization": f"Bearer {os.environ['BTX_API_KEY']}"},
    )
)

# provider is your application's existing OpenTelemetry TracerProvider.
provider.add_span_processor(btx_processor)

Each processor exports to its own destination. Avoid replacing the existing provider or changing shared OTLP environment variables that your Langfuse exporter uses. Explicit endpoint and authorization settings keep this destination separate; review any inherited compression and other exporter settings.

Verify delivery

Run an existing instrumented request or job. After its spans finish, flush the provider:

python
drained = provider.force_flush(timeout_millis=20_000)

During application shutdown, call provider.shutdown(). These lifecycle calls are synchronous; avoid blocking an async request's event loop with a network flush.

Flushing requests a queue drain. It does not prove that BTX stored every span. Check OpenTelemetry exporter logs and find the trace in BTX Telemetry. With Langfuse export enabled, verify the same trace ID in both destinations. Different destination filters can produce different span trees.

Filtering and masking configured in Langfuse do not apply to this exporter. Sanitize sensitive data before export.

Advanced: Configure delivery

Delivery tuning is optional. Start with the standard exporter's defaults and override only the settings your workload needs. Replace the processor configuration in the setup example with explicit options:

python
btx_processor = BatchSpanProcessor(
    OTLPSpanExporter(
        endpoint="https://btx.so/api/otel/v1/traces",
        headers={"Authorization": f"Bearer {os.environ['BTX_API_KEY']}"},
        timeout=15,  # Seconds for the HTTP export.
    ),
    max_queue_size=512,
    max_export_batch_size=128,
    schedule_delay_millis=30_000,
)
provider.add_span_processor(btx_processor)

These are example overrides, not OpenTelemetry defaults. max_queue_size limits buffered span count; max_export_batch_size limits each batch; schedule_delay_millis controls the export interval. Keep the batch size at or below the queue size. A shorter interval can make traces appear sooner but increases request frequency.

Set the HTTP timeout on OTLPSpanExporter in seconds. In OTel Python 1.45.x, the batch processor's export_timeout_millis setting does not enforce an HTTP deadline. Without explicit overrides, applicable OTel environment settings and defaults control delivery. Constructor options keep tuning specific to this exporter.

A span-count limit does not bound payload bytes. Keep captured content within the receiver limits even when increasing the queue or batch size.