> ## Documentation Index
> Fetch the complete documentation index at: https://docs.braintrust.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenTelemetry JS Integration

> OpenTelemetry JS Integration v2.0.0

The `@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](https://docs.braintrust.dev/sdks/typescript/migrations/v3-to-v4.md#opentelemetry-export).

## Installation

```bash npm theme={null}
npm install @braintrust/otel
```

```bash pnpm theme={null}
pnpm add @braintrust/otel
```

## Quick start

```typescript theme={null}
import {
  BraintrustExporter,
  BraintrustSpanProcessor,
  braintrustOtlpConfig,
} from "@braintrust/otel";
import { NodeSDK } from "@opentelemetry/sdk-node";
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base";

const exporter = new BraintrustExporter({
  exporter: new OTLPTraceExporter(
    braintrustOtlpConfig({ parent: "project_name:my-project" }), // Replace with your project name
  ),
});
const processor = new BraintrustSpanProcessor({
  processor: new BatchSpanProcessor(exporter),
});

const sdk = new NodeSDK({ spanProcessors: [processor] });
sdk.start();
```

This example uses OpenTelemetry SDK v1 or v2. For OpenTelemetry SDK v3, import `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 the `url` 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 to `BRAINTRUST_API_KEY`, then the nearest `.env.braintrust` file in Node.js.
* `apiUrl`: Defaults to `BRAINTRUST_API_URL`, then `https://api.braintrust.dev`.
* `parent`: The project or experiment to send spans to, such as `project_name:my-project` or `project_id:<id>`. Defaults to `BRAINTRUST_PARENT`.
* `headers`: Additional transport headers. The resolved API key and parent take precedence over authorization and routing headers.

Throws if no API key or parent is found. There is no default project.

#### 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 with `context.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 of `setupOtelCompat()`, 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 required `exporter` option. It adds span origin metadata, optionally filters spans, applies registered [span customizers](https://docs.braintrust.dev/sdks/typescript/redact-sensitive-data.md#opentelemetry-spans), 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 required `processor` 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 to `false`. When `true`, only spans whose names or attributes start with `gen_ai.`, `braintrust.`, `llm.`, `ai.`, or `traceloop.` are forwarded.
* `customFilter`: With filtering enabled, a function that returns `true` to keep a span, `false` to drop it, or `null` or `undefined` to use the default filter.
* `environment`: A `{ type, name }` object recorded in span origin metadata. Defaults to a value inferred from the runtime environment.

## Source Code

For the complete source code and additional examples, visit the [Braintrust JavaScript SDK repository](https://github.com/braintrustdata/braintrust-sdk-javascript/tree/main/integrations/otel-js).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.