@braintrust/otel package connects your application’s OpenTelemetry pipeline to Braintrust. Your application selects and configures its OpenTelemetry SDK, span processor, and exporter. The package supports OpenTelemetry SDK v1, v2, and v3 through the same entry point.
@braintrust/otel v2.0.0 requires braintrust v4, Node.js 22.18.0 or later, and @opentelemetry/api v1.9.0 or later. It is published as ESM only. To upgrade from v1, see the v3 to v4 migration guide.
Installation
Quick start
BatchSpanProcessor from @opentelemetry/sdk-trace and construct it with new BatchSpanProcessor({ exporter }).
API Reference
Functions
addParentToBaggage
Add braintrust.parent to OTEL baggage. This ensures that when using propagation inject() for distributed tracing, the braintrust.parent will be propagated via baggage to downstream services.addSpanParentToBaggage
Copy braintrust.parent from span attribute to OTEL baggage. BraintrustSpanProcessor automatically sets braintrust.parent as a span attribute when OTEL spans are created within Braintrust contexts. This function copies that attribute to OTEL baggage so it propagates when using inject() for distributed tracing.braintrustOtlpConfig
Return theurl and headers for an OTLP/HTTP trace exporter that sends spans to Braintrust. It doesn’t create an exporter, register a provider, or create a project. Options:
apiKey: Defaults toBRAINTRUST_API_KEY, then the nearest.env.braintrustfile in Node.js.apiUrl: Defaults toBRAINTRUST_API_URL, thenhttps://api.braintrust.dev.parent: The project or experiment to send spans to, such asproject_name:my-projectorproject_id:<id>. Defaults toBRAINTRUST_PARENT.headers: Additional transport headers. The resolved API key and parent take precedence over authorization and routing headers.
contextFromSpan
Create an OTEL context from a Braintrust span using W3C trace context propagation, including the span’s trace identity, trace state, and Braintrust parent baggage. Use the returned context withcontext.with() or as the parent of tracer.startSpan().
isRootSpan
Returns true if the span is a root span (no parent). Checks both parentSpanId (OTel 1.x) and parentSpanContext (OTel 2.x).parentFromHeaders
Extract a Braintrust propagation context from W3C trace context headers (traceparent, tracestate, and baggage). Returns a PropagationContext, or undefined if the headers don’t contain a valid trace context.
resetOtelCompat
Reverse the effect ofsetupOtelCompat(), restoring the default Braintrust context manager.
setupOtelCompat
Enable OpenTelemetry compatibility mode for the Braintrust SDK, so Braintrust and OpenTelemetry spans share trace IDs and parent relationships within a process. Register your OpenTelemetry provider and context manager before calling it.Classes
BraintrustExporter
Wrap an OpenTelemetry span exporter that your application supplies in the requiredexporter option. It adds span origin metadata, optionally filters spans, applies registered span customizers, and forwards each batch to your exporter. It doesn’t batch spans again. Accepts the optional filterAISpans, customFilter, and environment options.
BraintrustSpanProcessor
Wrap an OpenTelemetry span processor that your application supplies in the requiredprocessor option. It sets the Braintrust parent on spans, adds span origin metadata, optionally filters completed spans, and forwards them to your processor. It doesn’t create a batch processor or exporter, and doesn’t apply span customizers. Accepts these optional options:
filterAISpans: Defaults tofalse. Whentrue, only spans whose names or attributes start withgen_ai.,braintrust.,llm.,ai., ortraceloop.are forwarded.customFilter: With filtering enabled, a function that returnstrueto keep a span,falseto drop it, ornullorundefinedto use the default filter.environment: A{ type, name }object recorded in span origin metadata. Defaults to a value inferred from the runtime environment.