hs-opentelemetry-api
Copyright(c) Ian Duncan 2026
LicenseBSD-3
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

OpenTelemetry.Metric.Core

Description

Overview

This module defines the Metrics API for recording measurements in your application. Libraries use this API to record metrics; the SDK provides the actual aggregation and export.

Application authors should import OpenTelemetry.Metric from the SDK package instead, which re-exports everything here plus SDK initialization.

Quick example

import OpenTelemetry.Metric.Core

main :: IO ()
main = do
  -- Obtain the global MeterProvider (set up by the SDK)
  mp <- getGlobalMeterProvider
  meter <- getMeter mp myInstrumentationLibrary

  -- Create instruments
  requestCounter <- meterCreateCounterInt64 meter "http.requests" "" Nothing defaultAdvisoryParameters
  latencyHist    <- meterCreateHistogram meter "http.request.duration" "ms" Nothing defaultAdvisoryParameters

  -- Record measurements
  counterAdd requestCounter 1 [("method", toAttribute GET)]
  histogramRecord latencyHist 42.5 [("method", toAttribute GET)]

Instrument types

Synchronous instruments

These are called inline with your application code:

  • Counter : monotonically increasing sum (e.g. request count, bytes sent). Use counterAdd counter value attrs.
  • UpDownCounter : sum that can increase or decrease (e.g. active connections, queue depth). Use upDownCounterAdd counter value attrs.
  • Histogram : distribution of values (e.g. request latency, payload size). Use histogramRecord histogram value attrs.
  • Gauge : point-in-time value (e.g. CPU temperature, memory usage). Use gaugeRecord gauge value attrs.

Asynchronous (observable) instruments

These are read by callbacks during export:

Register a callback when creating the instrument:

obsGauge <- meterCreateObservableGaugeDouble meter
  "system.memory.usage" By Nothing defaultAdvisoryParameters
  (result -> do
    memInfo <- getMemoryUsage
    observeDouble result memInfo [("state", toAttribute "used")]
  )

No-op behavior

Before the SDK is installed, noopMeterProvider is the global default. All measurements are silently discarded. This makes it safe for libraries to instrument unconditionally.

Spec reference

https://opentelemetry.io/docs/specs/otel/metrics/api/

Synopsis

Provider

data MeterProvider Source #

Entry point for metrics API (spec: global default SHOULD exist).

Since: 0.0.1.0

Constructors

MeterProvider 

Fields

getMeter :: MeterProvider -> InstrumentationLibrary -> IO Meter Source #

Preferred accessor for obtaining a Meter (spec: Get a Meter).

Since: 0.0.1.0

noopMeterProvider :: MeterProvider Source #

No-op provider: safe for libraries; all measurements are discarded.

Since: 0.0.1.0

noopMeter :: InstrumentationLibrary -> Meter Source #

A Meter that records no telemetry (used by noopMeterProvider).

Since: 0.0.1.0

shutdownMeterProvider Source #

Arguments

:: MeterProvider 
-> Maybe Int

Optional timeout in microseconds. Nothing uses the SDK default (5s).

-> IO ShutdownResult 

Since: 0.0.1.0

forceFlushMeterProvider Source #

Arguments

:: MeterProvider 
-> Maybe Int

Optional timeout in microseconds. Nothing uses the SDK default (5s).

-> IO FlushResult 

Since: 0.0.1.0

setGlobalMeterProvider :: MonadIO m => MeterProvider -> m () Source #

Since: 0.0.1.0

Meter

data Meter Source #

Creates instruments for a single instrumentation scope.

| All instrument factory functions take: name, optional unit, optional description, advisory parameters. Observable factories additionally take initial callbacks.

Since: 0.0.1.0

Constructors

Meter 

Fields

Instruments

data InstrumentKind Source #

Instrument kinds from the metrics API specification.

Since: 0.0.1.0

Instances

Instances details
Generic InstrumentKind Source # 
Instance details

Defined in OpenTelemetry.Internal.Metric.Types

Associated Types

type Rep InstrumentKind 
Instance details

Defined in OpenTelemetry.Internal.Metric.Types

type Rep InstrumentKind = D1 ('MetaData "InstrumentKind" "OpenTelemetry.Internal.Metric.Types" "hs-opentelemetry-api-1.0.0.0-inplace" 'False) ((C1 ('MetaCons "KindCounter" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "KindAsyncCounter" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "KindUpDownCounter" 'PrefixI 'False) (U1 :: Type -> Type))) :+: ((C1 ('MetaCons "KindAsyncUpDownCounter" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "KindHistogram" 'PrefixI 'False) (U1 :: Type -> Type)) :+: (C1 ('MetaCons "KindGauge" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "KindAsyncGauge" 'PrefixI 'False) (U1 :: Type -> Type))))
Show InstrumentKind Source # 
Instance details

Defined in OpenTelemetry.Internal.Metric.Types

Eq InstrumentKind Source # 
Instance details

Defined in OpenTelemetry.Internal.Metric.Types

Ord InstrumentKind Source # 
Instance details

Defined in OpenTelemetry.Internal.Metric.Types

Hashable InstrumentKind Source # 
Instance details

Defined in OpenTelemetry.Internal.Metric.Types

type Rep InstrumentKind Source # 
Instance details

Defined in OpenTelemetry.Internal.Metric.Types

type Rep InstrumentKind = D1 ('MetaData "InstrumentKind" "OpenTelemetry.Internal.Metric.Types" "hs-opentelemetry-api-1.0.0.0-inplace" 'False) ((C1 ('MetaCons "KindCounter" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "KindAsyncCounter" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "KindUpDownCounter" 'PrefixI 'False) (U1 :: Type -> Type))) :+: ((C1 ('MetaCons "KindAsyncUpDownCounter" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "KindHistogram" 'PrefixI 'False) (U1 :: Type -> Type)) :+: (C1 ('MetaCons "KindGauge" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "KindAsyncGauge" 'PrefixI 'False) (U1 :: Type -> Type))))

data HistogramAggregation Source #

Histogram aggregation chosen for an instrument (explicit bounds vs exponential).

Since: 0.0.1.0

Constructors

HistogramAggregationExplicit !(Vector Double) 
HistogramAggregationExponential !Int32

Exponential histogram scale (OTel mapping index uses 2^scale).

Instances

Instances details
Generic HistogramAggregation Source # 
Instance details

Defined in OpenTelemetry.Internal.Metric.Types

Associated Types

type Rep HistogramAggregation 
Instance details

Defined in OpenTelemetry.Internal.Metric.Types

type Rep HistogramAggregation = D1 ('MetaData "HistogramAggregation" "OpenTelemetry.Internal.Metric.Types" "hs-opentelemetry-api-1.0.0.0-inplace" 'False) (C1 ('MetaCons "HistogramAggregationExplicit" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Vector Double))) :+: C1 ('MetaCons "HistogramAggregationExponential" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int32)))
Show HistogramAggregation Source # 
Instance details

Defined in OpenTelemetry.Internal.Metric.Types

Eq HistogramAggregation Source # 
Instance details

Defined in OpenTelemetry.Internal.Metric.Types

Hashable HistogramAggregation Source # 
Instance details

Defined in OpenTelemetry.Internal.Metric.Types

type Rep HistogramAggregation Source # 
Instance details

Defined in OpenTelemetry.Internal.Metric.Types

type Rep HistogramAggregation = D1 ('MetaData "HistogramAggregation" "OpenTelemetry.Internal.Metric.Types" "hs-opentelemetry-api-1.0.0.0-inplace" 'False) (C1 ('MetaCons "HistogramAggregationExplicit" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Vector Double))) :+: C1 ('MetaCons "HistogramAggregationExponential" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int32)))

data AdvisoryParameters Source #

Advisory parameters (spec: implementations MAY ignore; SDK SHOULD honor where defined).

Since: 0.0.1.0

Constructors

AdvisoryParameters 

Fields

Instances

Instances details
Generic AdvisoryParameters Source # 
Instance details

Defined in OpenTelemetry.Internal.Metric.Types

Associated Types

type Rep AdvisoryParameters 
Instance details

Defined in OpenTelemetry.Internal.Metric.Types

type Rep AdvisoryParameters = D1 ('MetaData "AdvisoryParameters" "OpenTelemetry.Internal.Metric.Types" "hs-opentelemetry-api-1.0.0.0-inplace" 'False) (C1 ('MetaCons "AdvisoryParameters" 'PrefixI 'True) (S1 ('MetaSel ('Just "advisoryExplicitBucketBoundaries") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Maybe [Double])) :*: (S1 ('MetaSel ('Just "advisoryAttributeKeys") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Maybe [Text])) :*: S1 ('MetaSel ('Just "advisoryHistogramAggregation") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Maybe HistogramAggregation)))))
Show AdvisoryParameters Source # 
Instance details

Defined in OpenTelemetry.Internal.Metric.Types

Eq AdvisoryParameters Source # 
Instance details

Defined in OpenTelemetry.Internal.Metric.Types

type Rep AdvisoryParameters Source # 
Instance details

Defined in OpenTelemetry.Internal.Metric.Types

type Rep AdvisoryParameters = D1 ('MetaData "AdvisoryParameters" "OpenTelemetry.Internal.Metric.Types" "hs-opentelemetry-api-1.0.0.0-inplace" 'False) (C1 ('MetaCons "AdvisoryParameters" 'PrefixI 'True) (S1 ('MetaSel ('Just "advisoryExplicitBucketBoundaries") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Maybe [Double])) :*: (S1 ('MetaSel ('Just "advisoryAttributeKeys") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Maybe [Text])) :*: S1 ('MetaSel ('Just "advisoryHistogramAggregation") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Maybe HistogramAggregation)))))

data Counter a Source #

Synchronous counter (non-negative increments). a is Int64 or Double.

Since: 0.0.1.0

Constructors

Counter 

Fields

  • counterAdd :: !(a -> Attributes -> IO ())

    Record an increment. Negative values are silently dropped for monotonic counters.

  • counterEnabled :: !(IO Bool)

    Whether this instrument is active. Noop instruments return False.

data UpDownCounter a Source #

Synchronous additive instrument that may increase or decrease.

Since: 0.0.1.0

Constructors

UpDownCounter 

Fields

data Histogram Source #

Synchronous histogram (records Double measurements).

Since: 0.0.1.0

Constructors

Histogram 

Fields

data Gauge a Source #

Synchronous gauge (last value wins per collect cycle semantics in SDK).

Since: 0.0.1.0

Constructors

Gauge 

Fields

newtype ObservableResult a Source #

Result handle passed to observable callbacks (spec: observe measurements at one logical instant).

Since: 0.0.1.0

Constructors

ObservableResult 

Fields

newtype ObservableCallbackHandle Source #

Handle to unregister a callback registered after instrument creation.

Since: 0.0.1.0

Constructors

ObservableCallbackHandle 

Fields

data ObservableCounter a Source #

Asynchronous counter: monotonic cumulative values observed per collection.

Since: 0.0.1.0

Constructors

ObservableCounter 

Fields

data ObservableUpDownCounter a Source #

Asynchronous up-down counter.

Since: 0.0.1.0

Constructors

ObservableUpDownCounter 

Fields

data ObservableGauge a Source #

Asynchronous gauge.

Since: 0.0.1.0

Constructors

ObservableGauge 

Fields

Instrument name validation (for SDK implementers)

validateInstrumentName :: Text -> Maybe Text Source #

Nothing if valid; Just err with a short English reason if invalid. Implements the stable rules from specificationmetricsapi.md (instrument name ABNF; ASCII only).

Since: 0.0.1.0

validateInstrumentUnit :: Text -> Maybe Text Source #

Unit is optional; when present it must be ASCII and at most 63 code units (specificationmetricsapi.md).

Since: 0.0.1.0