# Class LittleGhost::Tracing::OpenTelemetry

Documentation version: Edge

Canonical HTML: https://littleghostai.org/docs/LittleGhost/Tracing/OpenTelemetry.html

OpenTelemetry turns LittleGhost lifecycle notifications into GenAI spans and
events. It brings agents, model calls, tools, workflows, and token usage into
the same traces as the rest of an application.

    LittleGhost.configure do |config|
      config.instrument LittleGhost::Tracing::OpenTelemetry.new
    end

LittleGhost depends only on `opentelemetry-api`. Install and
configure the desired SDK, processors, and exporters before registering this
subscriber.

### Content and trust

Prompts, responses, messages, tool arguments, and exception content are
omitted by default. They appear only when Instrumentation has an explicit,
scrubbed Support::ContentCapture policy.

### Adding attributes and limiting payloads

`enrich` adds application attributes, such as a span kind your tracing backend
understands, to every lifecycle notification. `payload_limiter` shortens
captured content that a backend would otherwise reject.

    tracing = LittleGhost::Tracing::OpenTelemetry.new(
      enrich: ->(phase, name, attributes) {
        {"openinference.span.kind" => (name == :model) ? "LLM" : "CHAIN"}
      },
      payload_limiter: LittleGhost::Tracing::PayloadLimiter.new(max_bytes: 64_000)
    )

## Inheritance

`LittleGhost::Tracing::OpenTelemetry < LittleGhost::Instrumentation::Subscriber`

## Class methods

<a id="method-c-new"></a>
### `.new`

```ruby
.new(tracer: nil, enrich: nil, payload_limiter: nil)
```

Uses `tracer` or the global tracer provider.

`enrich` is an optional callable that receives the phase (`:start`,
`:finish`, or `:emit`), the notification name as a
Symbol, and its attributes, and returns a Hash of attributes to add, or `nil`.
A key containing a dot, such as `"session.id"`, is used as the
attribute name; other keys follow the same mapping as LittleGhost's own
attributes. Returned values are normalized like other attributes, so strings
are capped at 1,024 characters. If `enrich` raises or returns anything other
than a Hash or `nil`, a `little_ghost.tracing.enrich_failed`
warning names the error class and the notification is recorded without the
additions, so spans still start and finish.

`payload_limiter` is an optional callable, usually a PayloadLimiter, that
receives each span's or event's OpenTelemetry attributes as a Hash and returns
the Hash to record. It runs on the attributes set when a span starts, on those
set when it finishes, on exception events, and on the attributes passed to
#with_span. If it raises or returns something other than a Hash, String
attributes longer than 1,024 characters are omitted, a
`little_ghost.tracing.payload_limit_failed` warning is emitted
through Events, and the span still finishes.

## Instance methods

<a id="method-i-emit"></a>
### `#emit`

```ruby
#emit(name, attributes)
```

Adds a structured event to the correlated active span.

<a id="method-i-finish"></a>
### `#finish`

```ruby
#finish(name, attributes)
```

Finishes the span correlated by operation ID.

<a id="method-i-flush"></a>
### `#flush`

```ruby
#flush(timeout: nil)
```

Flushes through the configured tracer provider when supported.

<a id="method-i-shutdown"></a>
### `#shutdown`

```ruby
#shutdown(timeout: nil)
```

Finishes spans still owned by this subscriber.

<a id="method-i-start"></a>
### `#start`

```ruby
#start(name, attributes)
```

Starts a span for a lifecycle operation.

<a id="method-i-trace_context"></a>
### `#trace_context`

```ruby
#trace_context(operation_id: nil, **)
```

Supplies W3C propagation fields for an active operation when known.

<a id="method-i-with_span"></a>
### `#with_span`

```ruby
#with_span(name, attributes:, parent_operation_id: nil)
```

Wraps a block in a standalone internal span. Prefer lifecycle Instrumentation
methods for normal framework operations.
