Skip to main content
To combine OpenTelemetry spans with Braintrust spans, share context within a process or propagate context across services, depending on where the spans are created. You can use both approaches in the same application. Configure trace export before following these examples.

Share context within a process

Compatibility mode stores the active span in OpenTelemetry’s context, so Braintrust and OpenTelemetry instrumentation contribute to the same trace within a process. For example, use it to nest OpenTelemetry spans under a Braintrust evaluation span.
Call setupOtelCompat() before creating any loggers or spans, and use AsyncLocalStorageContextManager to nest spans under their parent evaluation spans:
Compatibility mode requires the @braintrust/otel package, v0.1.0 or later.
TypeScript

Propagate context across services

Pass the parent span’s context to the receiving service so its spans join the same trace. Choose the example that matches which SDK creates the parent span.
These examples require @braintrust/otel v0.1.0 or later and use fetch to pass context in HTTP headers.
When linking Python and TypeScript services, align their ID formats.

Braintrust parent to OpenTelemetry child

Export the Braintrust span context and use it to create an OpenTelemetry context.

OpenTelemetry parent to Braintrust child

Propagate the OpenTelemetry context using W3C Trace Context headers.
The examples use HTTP, but you can also transmit trace context through message queue metadata, gRPC metadata, or another inter-service transport that supports context propagation.
Braintrust can represent span and trace IDs as OTel-compatible hexadecimal values and serialize them in a shared export format, so exported spans interoperate with OpenTelemetry.
For distributed tracing between Python and TypeScript services, make sure both services use the same format. Either set BRAINTRUST_LEGACY_IDS=true on the Python service, or enable compatibility mode on the TypeScript service.
The TypeScript SDK reads and exports only the legacy ID and export format by default. Enable compatibility mode with setupOtelCompat() to read either format (auto-detecting) and export the OTel-compatible format, including when propagating spans through x-bt-parent or other distributed-tracing channels.
The TypeScript examples use the @braintrust/otel package to link OpenTelemetry and Braintrust spans. If you only need to propagate Braintrust trace context between TypeScript services over HTTP, the core braintrust package has native W3C helpers, injectTraceContext and extractTraceContextFromHeaders, that emit and read standard traceparent headers without the extra package.

Resources