# Metrics surface

The engine's observable counters, gauges, and
histograms, delivered through a **typed integrator
callback** the engine invokes at
observable-event sites. Each emission is a
`ds_metric_t` value passed to the callback at emit
time; the integrator decides where it goes
(Prometheus / StatsD / Loki / OpenTelemetry / a
custom control-plane message / nowhere) inside
their callback body.

The engine's responsibility ends at calling the
callback with a well-formed `ds_metric_t`. No internal
buffering, no flush cadence, no transport choice, no
aggregation policy baked in. The integrator owns where
the data goes and at what cadence it arrives downstream.

## What this is not

- Not a transport spec. Prometheus format, StatsD,
  OpenTelemetry — integrator choices made inside
  the callback.
- Not a flush-cadence spec. Each metric is emitted
  once at its observable-event site; the integrator's
  callback decides when (or whether) to push the
  value onward.
- Not a rate-calculation spec. Counters are emitted
  monotonically; the integrator computes deltas /
  rates as their backend requires.
- Not a dashboards / alerting / SLO spec. Operator
  territory.
- Not a rule-author-visible surface. Rules don't
  write custom metrics; they store facts in the facts
  plane through the storage service.

---

(NMET-1)=
## Metrics-surface commitments [NMET.1]

(NMET-1-1)=
- [NMET.1.1] **Metrics are emitted inline.** The engine must
  invoke the metric callback synchronously, at the moment
  the event a metric counts occurs ([NMET.6.2]).

*see also:* [NMET.6.2](#NMET-6-2)

(NMET-1-2)=
- [NMET.1.2] **Delivery is a typed integrator callback.**
  The integrator registers one `ds_metric_cb_t` per engine
  with `ds_engine_set_metric_callback`. The engine must
  invoke it once per occurrence, with a `ds_metric_t`
  (name, kind, value, labels), and must not buffer, sample
  or rate-limit emissions. What becomes of the value is
  the callback's choice. This is the [IARC.1.14]
  invariant: every external service is an integrator
  callback.

*see also:* [IARC.1.14](#IARC-1-14)

(NMET-1-3)=
- [NMET.1.3] **No callback is a silent drop.** With no
  callback registered, or after
  `ds_engine_set_metric_callback` with a NULL callback,
  the engine must drop each emission at its site, with no
  error and no buffering ([IARC.1.14]).

*see also:* [IARC.1.14](#IARC-1-14)

(NMET-1-4)=
- [NMET.1.4] **Names are dotted: `ds.<component>.<name>`.**
  Every metric name in [NMET.4.1] must have this form. An
  integrator forwarding to Prometheus rewrites the dots to
  underscores in its callback.

*see also:* [NMET.4.1](#NMET-4-1)

(NMET-1-5)=
- [NMET.1.5] **The inventory is closed.** [NMET.4.1] lists
  every metric libds emits. Adding, removing or renaming a
  metric is a change to that table, made in the same change
  as the code ([NMET.12]).

---

*see also:* [NMET.12](#NMET-12), [NMET.4.1](#NMET-4-1)

(NMET-2)=
## Metric types [NMET.2]

(NMET-2-1)=
[NMET.2.1] Three categories, sufficient for every engine
observation:

(NMET-2-2)=
### Counter [NMET.2.2]

(NMET-2-2-1)=
[NMET.2.2.1] Each emit carries the **delta** to add, a
non-negative integer passed as a double in
`ds_metric_t.value`. Every counter in [NMET.4.1] must emit
`1.0` per occurrence. The integrator's callback accumulates
or forwards it.

*see also:* [NMET.4.1](#NMET-4-1)

(NMET-2-2-2)=
[NMET.2.2.2] A counter's name must end in `_total`
(`ds.samples.dropped_total`).

(NMET-2-3)=
### Gauge [NMET.2.3]

(NMET-2-3-1)=
[NMET.2.3.1] Current value of a quantity that can go up or
down. Each emit is the **current value at the time of
emission** (not a delta).

(NMET-2-3-2)=
[NMET.2.3.2] A gauge's name must not end in `_total`; it may
end in its unit (`ds.intern.entries`, `ds.intern.bytes`).

(NMET-2-4)=
### Histogram [NMET.2.4]

(NMET-2-4-1)=
[NMET.2.4.1] Distribution of a quantity over a set of
observations. The engine emits **one observation per
event**, with kind `DS_METRIC_HISTOGRAM` ([NMET.3.3]); the
integrator's callback buckets it ([NMET.2.4.2]).

*see also:* [NMET.2.4.2](#NMET-2-4-2), [NMET.3.3](#NMET-3-3)

(NMET-2-4-2)=
[NMET.2.4.2] The engine must not pre-bucket: it emits each
raw observation. A histogram's row in [NMET.4.1] must state
its unit and bucket boundaries; an integrator whose backend
prefers other boundaries re-buckets in its callback.

*see also:* [NMET.4.1](#NMET-4-1)

(NMET-2-4-4)=
[NMET.2.4.4] A histogram's name must end in its unit:
`_seconds` for durations, `_bytes` for sizes, `_count` for
anything else.

---

(NMET-3)=
## ds_metric_t shape [NMET.3]

(NMET-3-1)=
[NMET.3.1] Each callback invocation receives one
`ds_metric_t`: name, kind, value and labels, as [NCBK.3.2]
defines them. The engine must set `labels` to NULL whenever
`n_labels` is 0.

*see also:* [NCBK.3.2](#NCBK-3-2)

(NMET-3-2)=
[NMET.3.2] All pointers in the struct (name, label keys,
label values) are **caller-owned and valid only for the
callback's duration**. Integrators that retain a value
past callback return must copy it.

(NMET-3-3)=
[NMET.3.3] `ds_metric_kind_t` has **three** members —
`DS_METRIC_COUNTER`, `DS_METRIC_GAUGE` and
`DS_METRIC_HISTOGRAM` ([NCBK.3.2]) — and the kind is
what tells a receiver how to fold `value`: a counter
contributes an increment, a gauge replaces the previous
reading, a histogram is one observation.

A histogram emit therefore identifies itself as one, and
a receiver does not have to infer it from the metric's
name. What the name and the declared bucket schema
([NMET.2.4]) still carry is how to *interpret* the
observation — which bucket set it belongs to, and in
what unit — because the engine emits raw observations
and never pre-buckets ([NMET.2.4.2]).

---

*see also:* [NCBK.3.2](#NCBK-3-2), [NMET.2.4](#NMET-2-4), [NMET.2.4.2](#NMET-2-4-2)

(NMET-3-4)=
[NMET.3.4] **The metric kinds are the registry `specs/registry/metric_kind.ebnf`.**
`ds_metric_kind_t` must mirror it name for name and value for value
([NMET.3.3]). A kind's stable name is its name without the `DS_METRIC_` prefix,
in lower case: `counter`, `gauge`, `histogram`. A binding must name a metric
kind by its stable name, and must name a value that is not a member `unknown`.

*see also:* [NMET.3.3](#NMET-3-3)

(NMET-4)=
## Metric inventory [NMET.4]

(NMET-4-2)=
[NMET.4.2] The table below, [NMET.4.1], is the set of
metrics libds emits. libds must emit each one when the event
its row names occurs, with the kind and labels the row
states. One further table follows it: metrics belonging to
currency conversion, whose obligation this edition defers
([NMET.4.6]). libds must emit no metric that neither table
lists.

*see also:* [NMET.4.1](#NMET-4-1), [NMET.4.6](#NMET-4-6)

(NMET-4-1)=
[NMET.4.1]

| Name                                      | Kind    | Labels                     | Emitted when                                                                                                                                                                                                                                                             |
|-------------------------------------------|---------|----------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `ds.calls.arg_error_total`                | counter | `service`                  | a `call` is not issued because a kwarg could not be evaluated                                                                                                                                                                                                            |
| `ds.clock.advance_total`                  | counter | —                          | `ds_engine_clock_advance` succeeds                                                                                                                                                                                                                                       |
| `ds.clock.tick_total`                     | counter | —                          | the engine records an observed clock value on its trace: an input or a pump samples the clocks, and the sample is the first or either clock has moved since the previous one. `ds_engine_clock_set` and `ds_engine_clock_advance` move the source and count nothing here |
| `ds.errors.code_invariant_total`          | counter | `schema`                   | the host hands the engine a `CallError` whose `code` breaks [LECC.1.2]'s pattern, and the engine rewrites it to `internal.invariant` ([LECC.1.5])                                                                                                                        |
| `ds.facts.nan_rejected_total`             | counter | `schema`, `slot`           | an assert is refused because a `Float` slot holds NaN                                                                                                                                                                                                                    |
| `ds.facts.noop_reassert_suppressed_total` | counter | `schema`                   | a reassertion is suppressed as a no-op                                                                                                                                                                                                                                   |
| `ds.intern.arena_high_water_bytes`        | gauge   | —                          | each pump, before rules run: the most bytes the transient computed-string arena has held over the engine's life                                                                                                                                                          |
| `ds.intern.bytes`                         | gauge   | —                          | each pump, before rules run: the summed byte length of the interned strings                                                                                                                                                                                              |
| `ds.intern.entries`                       | gauge   | —                          | each pump, before rules run: the live interned entries                                                                                                                                                                                                                   |
| `ds.intern.pinned_entries`                | gauge   | —                          | each pump, before rules run: the interned entries the runtime holds for the program itself, not for slot values                                                                                                                                                          |
| `ds.json.nonfinite_total`                 | counter | `verb`                     | a non-finite `Float` is refused from a JSON body; `verb` is `cache`, `call` or `encode`                                                                                                                                                                                  |
| `ds.recorder.events_emitted_total`        | counter | `kind`                     | the recorder dispatches a stream event, whether or not a stream callback is registered; `kind` is the event's kind                                                                                                                                                       |
| `ds.samples.dropped_total`                | counter | `schema`, `slot`, `reason` | a series sample is not delivered: `reason` is `no_cb` (no sample callback) or `cb_error` (the callback returned non-OK)                                                                                                                                                  |
| `ds.samples.emitted_total`                | counter | `schema`, `slot`           | the sample callback accepts a sample                                                                                                                                                                                                                                     |

*see also:* [LECC.1.2](#LECC-1-2), [LECC.1.5](#LECC-1-5)

(NMET-4-7)=
[NMET.4.7] **The four `ds.intern.*` gauges report a runtime's own table.**
Which strings an engine interns, and how much it holds for them, is the
runtime's mechanism and not the language's, so the values of
`ds.intern.entries`, `ds.intern.bytes`, `ds.intern.pinned_entries` and
`ds.intern.arena_high_water_bytes` are implementation-defined. A runtime
**must** emit each where the table above says, and **may** report any value
that describes its own interning; two conforming runtimes may report different
values for the same program and inputs. A runtime that keeps no such storage
reports `0`.

(NMET-4-6)=
[NMET.4.6] The two `ds.currency.*` metrics in the next table
belong to currency conversion ([LUNI.4.6]), whose obligation
this edition defers. libds keeps their emit sites; the table
states what they count.

*Deferred: not binding in this edition.*

*see also:* [LUNI.4.6](#LUNI-4-6)

(NMET-4-5)=
[NMET.4.5]

| Name                                  | Kind    | Labels | Emitted when                                                                             |
|---------------------------------------|---------|--------|------------------------------------------------------------------------------------------|
| `ds.currency.fx_rate_ambiguous_total` | counter | `pair` | rates matching `pair` tie on `asof`, or the rate schema has no `asof` slot to order them |
| `ds.currency.fx_rate_missing_total`   | counter | `pair` | no live rate matches `pair`, and the conversion falls back to 1:1                        |

*Deferred: not binding in this edition.*

(NMET-5)=
## Labels [NMET.5]

(NMET-5-1)=
[NMET.5.1] Labels attach to a metric to partition
observations. Each emission must carry the labels its row in
[NMET.4.1] names, in `ds_metric_t.labels[]`, with values the
engine takes at the emission site: the schema and slot of the
asserted fact, the kind of the recorded event, the verb that
refused a value.

*see also:* [NMET.4.1](#NMET-4-1)

(NMET-5-2)=
[NMET.5.2] Pre-allocated label tables are an integrator
optimisation, not an engine commitment. Under the
callback model the engine constructs label arrays on
the stack at each emission and hands them to the
callback. The integrator's callback either copies
what it needs or routes by label-set in real time.

(NMET-5-3)=
[NMET.5.3] A label must draw its values from a bounded set:
schema and slot names from the loaded program, service names
from its service declarations, and `kind`, `verb` and
`reason` from fixed sets. No label carries an unbounded
dimension such as a fact id.

---

(NMET-6)=
## Emission semantics [NMET.6]

(NMET-6-1)=
[NMET.6.1] **When the engine emits.** Each row of [NMET.4.1]
names the event that emits its metric. The engine must emit
the metric once per occurrence of that event, as it occurs.

*see also:* [NMET.4.1](#NMET-4-1)

(NMET-6-2)=
[NMET.6.2] **The callback runs on the caller's stack.** Each
emission happens synchronously, on the stack of the
`ds_engine_*` call that caused it (a pump, an assert, a clock
set), as every callback does ([NCBK.1.7]). The callback must
not block. A call it makes
into the engine that invoked it is refused with
`DS_ERR_STATE` ([RARC.13.16]).

*see also:* [NCBK.1.7](#NCBK-1-7), [RARC.13.16](#RARC-13-16)

(NMET-6-3)=
[NMET.6.3] **No back-pressure.** The engine must not
throttle or drop emissions: under load the callback is
invoked at the rate the events occur. An integrator whose
downstream pipeline cannot keep up drops in its own callback.

(NMET-6-4)=
[NMET.6.4] **Concurrency.** An engine is single-threaded
([IARC.1.11]), so its callback sees emissions in one serial
order. No locking is needed inside the callback unless the
integrator's backend needs it.

*see also:* [IARC.1.11](#IARC-1-11)

(NMET-6-5)=
[NMET.6.5] **The return value is ignored.** The engine must
not act on the metric callback's result: a non-OK return
neither fails the operation that emitted the metric nor is
reported.

---

(NMET-7)=
## Integrator patterns [NMET.7]

(NMET-7-1)=
### Push-to-backend [NMET.7.1]

(NMET-7-1-1)=
[NMET.7.1.1] The simplest pattern: each emission
forwards directly to the integrator's metrics
backend.

(NMET-7-1-2)=
[NMET.7.1.2] Suitable for backends that accept individual
samples (StatsD, OTLP, custom control-plane).

(NMET-7-2)=
### Snapshot aggregation [NMET.7.2]

(NMET-7-2-1)=
[NMET.7.2.1] For a Prometheus-scrape integration, the
integrator's callback keeps its own snapshot struct, which
its HTTP scrape endpoint reads.

(NMET-7-2-2)=
[NMET.7.2.2] On scrape, the HTTP handler reads the snapshot
directly. The handler runs on a different thread from the
pump (the integrator's HTTP server thread), so the integrator
must serialise access to the snapshot (atomics, RW locks,
lock-free counters — its choice).

(NMET-7-2-3)=
[NMET.7.2.3] libds does not ship a reference snapshot
implementation; the pattern is straightforward enough
that integrators tailor it to their backend.

(NMET-7-3)=
### Sampling / filtering [NMET.7.3]

(NMET-7-3-1)=
[NMET.7.3.1] An integrator that only cares about a subset
of metrics filters in the callback by name prefix or
label match. No engine-side filter mechanism — every
metric still passes through the cb; the cb decides.

---

(NMET-8)=
## Histograms in detail [NMET.8]

(NMET-8-1)=
[NMET.8.1] An observation of value `v` is emitted as one
`ds_metric_t` with the histogram's name, kind
`DS_METRIC_HISTOGRAM` and `value = v`. The integrator's
callback distributes it into buckets according to the
metric's row ([NMET.2.4.2]).

*see also:* [NMET.2.4.2](#NMET-2-4-2)

(NMET-8-2)=
[NMET.8.2] An integrator converting to Prometheus format can
keep a per-metric bucket array in the snapshot pattern
([NMET.7.2]) and write `_bucket{le="..."}` lines on scrape.

*see also:* [NMET.7.2](#NMET-7-2)

(NMET-8-3)=
[NMET.8.3] An integrator that needs the raw distribution
rather than buckets keeps the observations and computes the
distribution itself.

---

(NMET-9)=
## Cardinality [NMET.9]

(NMET-9-1)=
[NMET.9.1] Every label in the inventory draws from a bounded
value source ([NMET.5.3]): schema and slot names, service
names, and the fixed sets `kind`, `verb` and `reason`. So the
number of distinct label sets a metric can carry is bounded
by the loaded program.

*see also:* [NMET.5.3](#NMET-5-3)

(NMET-9-2)=
[NMET.9.2] An integrator that keeps its own snapshot
([NMET.7.2]) sees only those label values, so the snapshot's
working set is bounded by what the program declares plus the
fixed sets.

---

*see also:* [NMET.7.2](#NMET-7-2)

(NMET-10)=
## Integration with other subsystems [NMET.10]

(NMET-10-1)=
### With the event trace [NMET.10.1]

(NMET-10-1-1)=
[NMET.10.1.1] Metrics and the event stream, whose events
[LFST.6] lists, are separate surfaces with overlapping
content:

*see also:* [LFST.6](#LFST-6)

(NMET-10-1-2)=
- [NMET.10.1.2] Every event the stream carries also increments
  `ds.recorder.events_emitted_total{kind}`, whether or not a
  stream callback is registered.

(NMET-10-1-4)=
[NMET.10.1.4] Metrics are aggregated summaries
optimised for dashboards and alerting. The stream is
per-event, optimised for replay and debugging. Both
go through their own typed callback; neither
subsumes the other.

(NMET-10-2)=
### With operational logs [NMET.10.2]

(NMET-10-2-1)=
[NMET.10.2.1] One event surfaces as both a metric and an
operational-log entry: a sample callback that returns non-OK
increments `ds.samples.dropped_total{reason="cb_error"}`, and
the engine emits a `DS_LOG_WARNING` `ds_log_event_t` in
category `samples` through the registered log callback. The
metric is the rate signal; the log carries the detail.

---

(NMET-11)=
## Absence and fault log entries [NMET.11]

(NMET-11-1)=
[NMET.11.1] Where a metric and a log entry report one event
([NMET.10.2.1]), the engine must invoke both callbacks at the
same site, the metric callback first. The metric is the rate
signal for alerting; the log is the per-event detail.

*see also:* [NMET.10.2.1](#NMET-10-2-1)

(NMET-11-2)=
[NMET.11.2] Coordination between the two surfaces is
the integrator's choice. Common patterns:
- Single backend (e.g. OpenTelemetry) consuming both
  callbacks and joining server-side.
- Separate metric + log pipelines, joined at the
  dashboard / SIEM level.
- Just the metric; logs sampled to disk on overflow.

(NMET-11-3)=
[NMET.11.3] The engine commits to emitting at every
such site; the rate and shape of what an
exporter makes of them is integrator territory.

---

(NMET-12)=
## Adding or removing metrics [NMET.12]

(NMET-12-1)=
[NMET.12.1] The inventory in [NMET.4.1] is load-bearing:
alerts, dashboards and SLOs reference metric names and label
sets. A change to it lands with the code that makes it, as
follows:

*see also:* [NMET.4.1](#NMET-4-1)

(NMET-12-2)=
- [NMET.12.2] **Adding a metric.** A new row in [NMET.4.1]
  with name, kind, labels and emitting event. The name must
  follow [NMET.1.4] and the kind must be one of the three in
  [NMET.2].

*see also:* [NMET.1.4](#NMET-1-4), [NMET.2](#NMET-2), [NMET.4.1](#NMET-4-1)

(NMET-12-3)=
- [NMET.12.3] **Removing or renaming a metric.** libds must
  emit the metric under both the old and the new name for one
  release before the old name is removed, so integrators have
  one release to migrate dashboards.

(NMET-12-4)=
- [NMET.12.4] **Adding a label.** Every emission of the metric
  carries the new label from the change that adds it; an
  integrator that pre-aggregates by label set may need to redo
  its aggregation.

(NMET-12-5)=
- [NMET.12.5] **Removing a label.** The same one-release
  deprecation as [NMET.12.3]. Once removed, the label is no
  longer emitted, and pre-aggregated snapshots collapse along
  that dimension.

---

*see also:* [NMET.12.3](#NMET-12-3)

(NMET-13)=
## Test obligations [NMET.13]

(NMET-13-1)=
[NMET.13.1] Every metric in [NMET.4.1] must have a test that
registers a metric callback, drives the event its row names,
and asserts the callback fired with the expected name, kind,
labels and value: the delta for a counter, the current
reading for a gauge.

*see also:* [NMET.4.1](#NMET-4-1)

