OtlpShipper.Metrics.Aggregation (otlp_shipper v0.1.1)

Copy Markdown View Source

Pure per-series aggregation and OTLP data-point construction.

Histogram buckets include their upper bound and an implicit positive-infinity bucket. Negative observations omit histogram sum for OpenMetrics compatibility. Gauges emit the last observed value and its observation time, with no temporality or start time. Sums/histograms use explicit interval start and end timestamps.

Summary

Functions

Adds one observation, rejecting arithmetic overflow without changing state.

Chooses the first upper bound greater than or equal to a value.

Builds one OTLP metric containing one data point. monotonic belongs to the definition across intervals, not just this series' current observation.

Functions

add(kind, state, value, bounds, observed)

@spec add(atom(), map() | nil, number(), [number()], non_neg_integer()) ::
  {:ok, map()} | {:error, :numeric_overflow}

Adds one observation, rejecting arithmetic overflow without changing state.

iex> OtlpShipper.Metrics.Aggregation.add(:sum, nil, 3, [], 100)
{:ok, %{value: 3, negative: false}}

bucket(value, bounds)

@spec bucket(number(), [number()]) :: non_neg_integer()

Chooses the first upper bound greater than or equal to a value.

iex> OtlpShipper.Metrics.Aggregation.bucket(10, [5, 10])
1
iex> OtlpShipper.Metrics.Aggregation.bucket(11, [5, 10])
2

metric(definition, state, attributes, started, ended, monotonic)

@spec metric(map(), map(), [map()], non_neg_integer(), non_neg_integer(), boolean()) ::
  map()

Builds one OTLP metric containing one data point. monotonic belongs to the definition across intervals, not just this series' current observation.

iex> definition = %{kind: :counter, name: "web.count", unit: "1", description: "", bounds: []}
iex> metric = OtlpShipper.Metrics.Aggregation.metric(definition, %{value: 2, negative: false}, [], 10, 20, true)
iex> elem(metric.data, 1).data_points |> hd() |> Map.fetch!(:value)
{:as_int, 2}