Otel.SDK.Metrics.MeterProvider (otel v0.2.0)

Copy Markdown View Source

SDK implementation of the Otel.API.Metrics.MeterProvider behaviour (metrics/sdk.md §MeterProvider L43-L155).

A GenServer that owns metrics configuration (resource, views, readers) and creates meters. Registers itself as the global MeterProvider on start.

All public functions are safe for concurrent use, satisfying spec metrics/sdk.md L1875-L1876 (Status: Stable) — "MeterProvider — Meter creation, ForceFlush and Shutdown MUST be safe to be called concurrently."

Crash handling

init/1 enables trap_exit so a reader crash is delivered to the MeterProvider as {:EXIT, pid, reason} rather than propagating along the link. The dead reader is removed from the active list — graceful degradation, the other readers keep working. Once removed, the reader is not re-added; if its module is supervised by us, the MeterProvider's own crash takes its linked readers with it (no orphans). Mirrors the pattern in Otel.SDK.Logs.LoggerProvider and Otel.SDK.Trace.TracerProvider.

Public API

FunctionRole
start_link/1SDK (lifecycle)
get_meter/2SDK (OTel API MUST) — metrics/api.md §Get a Meter
shutdown/2SDK (OTel API MUST) — metrics/sdk.md §Shutdown
force_flush/2SDK (OTel API MUST) — metrics/sdk.md §ForceFlush
add_view/3SDK (OTel API MUST) — metrics/sdk.md §View L259-L327

References

  • OTel Metrics SDK §MeterProvider: opentelemetry-specification/specification/metrics/sdk.md L43-L155
  • OTel Metrics API §MeterProvider: opentelemetry-specification/specification/metrics/api.md L92-L156

Summary

Functions

SDK (OTel API MUST) — Register a View (metrics/sdk.md §View L259-L327).

Returns a specification to start this module under a supervisor.

SDK (introspection) — Returns the current configuration snapshot, or an empty map when the provider isn't running.

SDK (OTel API MUST) — ForceFlush (metrics/sdk.md §ForceFlush).

SDK (OTel API MUST) — Get a Meter (metrics/api.md §Get a Meter).

SDK (introspection) — Returns the resource associated with this provider, or Otel.SDK.Resource.default/0 when the provider isn't running.

SDK (OTel API MUST) — Shutdown (metrics/sdk.md §Shutdown).

SDK (lifecycle) — Starts the MeterProvider with the given configuration.

Types

config()

@type config() :: %{
  resource: Otel.SDK.Resource.t(),
  views: [Otel.SDK.Metrics.View.t()],
  readers: [{module(), Otel.SDK.Metrics.MetricReader.config()}],
  exemplar_filter: Otel.SDK.Metrics.Exemplar.Filter.t()
}

Functions

add_view(server, criteria \\ %{}, config \\ %{})

@spec add_view(
  server :: GenServer.server(),
  criteria :: Otel.SDK.Metrics.View.criteria(),
  config :: Otel.SDK.Metrics.View.config()
) :: :ok | {:error, String.t()}

SDK (OTel API MUST) — Register a View (metrics/sdk.md §View L259-L327).

Returns {:error, reason} if the View is invalid (e.g. wildcard name with stream name override). Returns :ok when the provider isn't running — there's no aggregation pipeline to install the View on, so the call is a silent no-op.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

config(server)

@spec config(server :: GenServer.server()) :: config() | %{}

SDK (introspection) — Returns the current configuration snapshot, or an empty map when the provider isn't running.

force_flush(server, timeout \\ 5000)

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

SDK (OTel API MUST) — ForceFlush (metrics/sdk.md §ForceFlush).

Forces all registered readers to collect and export metrics.

get_meter(server, instrumentation_scope)

@spec get_meter(
  server :: GenServer.server(),
  instrumentation_scope :: Otel.API.InstrumentationScope.t()
) :: Otel.API.Metrics.Meter.t()

SDK (OTel API MUST) — Get a Meter (metrics/api.md §Get a Meter).

Falls back to the Noop meter if server is no longer alive.

resource(server)

@spec resource(server :: GenServer.server()) :: Otel.SDK.Resource.t()

SDK (introspection) — Returns the resource associated with this provider, or Otel.SDK.Resource.default/0 when the provider isn't running.

shutdown(server, timeout \\ 5000)

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

SDK (OTel API MUST) — Shutdown (metrics/sdk.md §Shutdown).

Invokes shutdown on all registered readers. After shutdown, get_meter/2 returns the noop meter. Can only be called once; subsequent calls reply {:error, :already_shutdown}.

start_link(opts)

@spec start_link(opts :: keyword()) :: GenServer.on_start()

SDK (lifecycle) — Starts the MeterProvider with the given configuration.