# `Nous.Eval.Online.OTel`
[🔗](https://github.com/nyo16/nous/blob/v0.21.1/lib/nous/eval/online/otel.ex#L1)

Maps an online-eval `Nous.Eval.Sample` to OpenTelemetry spans following the
GenAI semantic conventions (pinned to semconv `1.37.0`, see
`semconv_version/0`).

## What is mapped

  * each agent run → an `"invoke_agent <agent_name>"` span (kind
    `:internal`) whose span id is the run's `agent_run_id`; a sub-agent run
    is parented to the `execute_tool` span that spawned it, else to its
    parent run
  * each model request → a `"chat <model_name>"` span (kind `:client`) with
    the request/response model, token usage and finish reasons
  * each tool call → an `"execute_tool <tool_name>"` span with the tool name,
    call id, outcome and approval; a call that spawned a sub-agent run also
    carries `gen_ai.transfer.target.name`
  * guard, loop-guard and hook events → span events `nous.<guard>.<event>`
    on the enclosing run span, with skeleton attributes only (verdict,
    severity, action, surface, tier, hook name, tool call id)
  * each score and scorer error → a `"gen_ai.evaluation.result"` event on
    the root run span

## Content

This mapping only ever sees redacted samples: the online pipeline removes
(`redact: :drop_content`) or rewrites content keys before a sample reaches
an exporter. Even so, content attributes (`gen_ai.input.messages`,
`gen_ai.output.messages`, `gen_ai.tool.call.arguments`,
`gen_ai.tool.call.result`) and score explanations are emitted only with
`export_content: true` (default `false`), as JSON of the already-redacted
value. Under `redact: :drop_content` they never appear, since the content
keys are absent. No other content key (`reason`, `error`, `details`, …) is
ever exported; a failed run reports only its `error_kind`.

## OpenTelemetry API

`spans/2` is pure and needs no dependency. `export/2` calls the
OpenTelemetry Erlang API only when it is loaded (`:otel_tracer`), and
returns `{:error, :opentelemetry_not_loaded}` otherwise. Nous does not
depend on OpenTelemetry.

# `attributes`

```elixir
@type attributes() :: %{required(String.t()) =&gt; scalar() | [scalar()]}
```

# `scalar`

```elixir
@type scalar() :: String.t() | boolean() | integer() | float()
```

# `span`

```elixir
@type span() :: %{
  name: String.t(),
  trace_id: String.t(),
  span_id: String.t(),
  parent_span_id: String.t() | nil,
  kind: :internal | :client,
  start_time: integer(),
  end_time: integer(),
  attributes: attributes(),
  events: [span_event()],
  status: :unset | :ok | :error
}
```

# `span_event`

```elixir
@type span_event() :: %{name: String.t(), time: integer(), attributes: attributes()}
```

# `export`

```elixir
@spec export(Nous.Eval.Sample.t(), keyword()) ::
  :ok | {:error, :opentelemetry_not_loaded}
```

Exports `sample` through the OpenTelemetry API when it is loaded.

Takes the same options as `spans/2`.

# `exporter`

```elixir
@spec exporter(keyword()) :: (Nous.Eval.Sample.t() -&gt;
                          :ok | {:error, :opentelemetry_not_loaded})
```

A 1-arity exporter for the online `exporter:` option.

# `semconv_version`

```elixir
@spec semconv_version() :: String.t()
```

The GenAI semantic-conventions version this mapping follows.

# `spans`

```elixir
@spec spans(Nous.Eval.Sample.t(), keyword()) :: [span()]
```

The spans for `sample`, parents before children.

Options:

  * `:export_content` - emit content attributes and score explanations
    (default `false`)

Total: malformed events are skipped. A sample without a valid 32-hex
`trace_id` yields `[]`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
