LangChain.OpenTelemetry.MetricsHandler (LangChain v0.9.0)

Copy Markdown View Source

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.

Summary

Functions

Returns the list of telemetry events this handler attaches to.

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

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

Functions

events()

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

Returns the list of telemetry events this handler attaches to.

handle_event(event, measurements, metadata, config)

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

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

handler_id()

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

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