Otel.SDK.Metrics.MetricReader behaviour (otel v0.2.0)

Copy Markdown View Source

MetricReader behaviour and collection pipeline.

A MetricReader collects metrics from the SDK's aggregation state, runs async callbacks, and delivers collected data to an exporter.

Collection produces a list of metric maps, each containing the stream identity, resource, scope, and data points.

Concurrency

Spec metrics/sdk.md L1880-L1881 (Status: Stable) — "Collect, ForceFlush (for periodic exporting MetricReader) and Shutdown MUST be safe to be called concurrently." Implementing modules MUST be safe for concurrent invocation of these three callbacks. The built-in Otel.SDK.Metrics.MetricReader.PeriodicExporting serialises via its GenServer mailbox, satisfying the MUST.

Deferred Development-status features

  • MetricFilter. Spec metrics/sdk.md L1308 (Status: Development) describes a MetricFilter applied to metrics and attributes during MetricReader#Collect. Not implemented — no :metric_filter field on reader configs; MetricProducer.produce/1 callback has no filter parameter. Waits for spec stabilisation.

Summary

Types

Reader-implementer config — the value passed to a reader's start_link/1 callback. Each implementation defines its own shape; the alias names the boundary so callers (notably Otel.SDK.Metrics.MeterProvider) can refer to it in @specs rather than the bare map() literal.

Types

config()

@type config() :: map()

Reader-implementer config — the value passed to a reader's start_link/1 callback. Each implementation defines its own shape; the alias names the boundary so callers (notably Otel.SDK.Metrics.MeterProvider) can refer to it in @specs rather than the bare map() literal.

metric()

@type metric() :: %{
  name: String.t(),
  description: String.t(),
  unit: String.t(),
  scope: Otel.API.InstrumentationScope.t(),
  resource: Otel.SDK.Resource.t(),
  kind: Otel.API.Metrics.Instrument.kind(),
  temporality: Otel.API.Metrics.Instrument.temporality() | nil,
  is_monotonic: boolean() | nil,
  datapoints: [Otel.SDK.Metrics.Aggregation.datapoint()]
}

Callbacks

force_flush(server)

@callback force_flush(server :: GenServer.server()) :: :ok | {:error, term()}

shutdown(server)

@callback shutdown(server :: GenServer.server()) :: :ok | {:error, term()}

start_link(config)

@callback start_link(config :: config()) :: GenServer.on_start()

Functions

collect(config)

@spec collect(config :: config()) :: [metric()]