# `LangChain.OpenTelemetry.MetricsHandler`
[🔗](https://github.com/brainlid/langchain/blob/v0.9.3/lib/open_telemetry/metrics_handler.ex#L4)

Telemetry handler that re-emits LangChain telemetry events as GenAI
Semantic Convention-aligned intermediary metric events.

**Important:** This module does not directly record OpenTelemetry histograms
or counters. It emits `:telemetry.execute/3` events that must be consumed by
a metrics library to become actual OTel metrics. Without a consumer attached
to these events, `enable_metrics: true` has no visible effect.

To produce actual OTel metrics, attach a consumer such as `Telemetry.Metrics`
with an OpenTelemetry reporter, `PromEx`, or equivalent to the events below.

## Emitted events

* `[:langchain, :otel, :operation, :duration]` — with `%{duration_s: float()}`
  measurement and GenAI attributes as metadata. Emitted for both successful
  (`:stop`) and failed (`:exception`) operations; failures additionally carry
  an `error.type` attribute so error rate is observable alongside latency.
* `[:langchain, :otel, :token, :usage]` — with `%{tokens: integer()}` measurement
  and GenAI attributes (including `gen_ai.token.type`) as metadata
* `[:langchain, :otel, :operation, :time_to_first_token]` — with
  `%{duration_s: float()}` measurement and GenAI attributes as metadata.
  Emitted once per streaming LLM call, measuring the time from request start to
  the first streamed chunk (aligns with the semantic-convention
  `gen_ai.server.time_to_first_token` metric).

## Usage

This module is used internally by `LangChain.OpenTelemetry.setup/1` when
`enable_metrics: true` (the default). You typically don't need to interact
with it directly.

# `events`
[🔗](https://github.com/brainlid/langchain/blob/v0.9.3/lib/open_telemetry/metrics_handler.ex#L48)

```elixir
@spec events() :: [[atom()]]
```

Returns the list of telemetry events this handler attaches to.

# `handle_event`
[🔗](https://github.com/brainlid/langchain/blob/v0.9.3/lib/open_telemetry/metrics_handler.ex#L70)

```elixir
@spec handle_event([atom()], map(), map(), term()) :: :ok
```

Telemetry handler callback. Re-emits duration and token usage metric events.

# `handler_id`
[🔗](https://github.com/brainlid/langchain/blob/v0.9.3/lib/open_telemetry/metrics_handler.ex#L64)

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

Returns the telemetry handler ID prefix used for attaching/detaching.

---

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