Metrics.ts
Metrics.ts overview
Section titled “Metrics.ts overview”OpenTelemetry support for Effect metrics.
This module exposes Effect metrics as an OpenTelemetry MetricProducer.
makeProducer creates the producer, registerProducer attaches it to one
or more SDK MetricReaders, and layer manages that setup in a scoped
layer. The TemporalityPreference type lets callers choose cumulative or
delta metric values.
Since v4.0.0
Exports Grouped by Category
Section titled “Exports Grouped by Category”constructors
Section titled “constructors”makeProducer
Section titled “makeProducer”Creates an OpenTelemetry metric producer from Effect metrics.
When to use
Use when you need a MetricProducer for manually wiring Effect metrics into
OpenTelemetry instead of using the scoped layer helper.
Details
Requires the current OpenTelemetry Resource, captures the current Effect
context, and uses cumulative temporality by default. Pass "delta" for
interval-based values.
See
registerProducerfor attaching a producer to metric readerslayerfor creating and registering a producer in a scoped layer
Signature
declare const makeProducer: (temporality?: TemporalityPreference) => Effect.Effect<MetricProducer, never, Resource>Since v4.0.0
registerProducer
Section titled “registerProducer”Registers a metric producer with one or more metric readers.
Signature
declare const registerProducer: ( self: MetricProducer, metricReader: LazyArg<MetricReader | Arr.NonEmptyReadonlyArray<MetricReader>>, options?: { readonly shutdownTimeout?: Duration.Input | undefined }) => Effect.Effect<Array<any>, never, Scope.Scope>Since v4.0.0
layers
Section titled “layers”Creates a Layer that registers a metric producer with metric readers.
Example (Creating a metrics layer with temporality)
import { Metrics } from "@effect/opentelemetry"import { PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics"import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http"
const metricExporter = new OTLPMetricExporter({ url: "<your-otel-url>" })
// Use delta temporality for backends like Datadog or Dynatraceconst metricsLayer = Metrics.layer( () => new PeriodicExportingMetricReader({ exporter: metricExporter, exportIntervalMillis: 10000 }), { temporality: "delta" })
// Use cumulative temporality for backends like Prometheus (default)const cumulativeLayer = Metrics.layer(() => new PeriodicExportingMetricReader({ exporter: metricExporter }), { temporality: "cumulative"})Signature
declare const layer: ( evaluate: LazyArg<MetricReader | Arr.NonEmptyReadonlyArray<MetricReader>>, options?: { readonly shutdownTimeout?: Duration.Input | undefined readonly temporality?: TemporalityPreference | undefined }) => Layer.Layer<never, never, Resource>Since v4.0.0
models
Section titled “models”TemporalityPreference (type alias)
Section titled “TemporalityPreference (type alias)”Determines how metric values relate to the time interval over which they are aggregated.
Details
cumulative reports total since a fixed start time. Each data point depends
on all previous measurements. This is the default behavior. delta reports
changes since the last export. Each interval is independent with no
dependency on previous measurements.
Signature
type TemporalityPreference = "cumulative" | "delta"Since v4.0.0