2.3 KiB
OTel instrumentation
(no submodules)
Purpose
Single-source OTel instruments for @suaveplan/service-discovery. Centralising
counter/histogram/gauge creation here keeps cardinality bounded, avoids the
common bug where multiple meters get created under similar names, and gives
dashboards a single canonical set of metric IDs to chart.
Features
svc_disc.registrations_total{outcome}counter — outcomes areregistered,refreshed,rejected(low-cardinality enum).svc_disc.queries_totalcounter — incremented once perquery()call.svc_disc.query_duration_mshistogram — wall-clock latency per query.svc_disc.active_servicesobservable gauge — bound to a registry'ssizegetter at construction; the meter pulls on scrape.
Usage: Basic Example
import {
recordRegistration,
recordQuery,
recordQueryDuration,
} from "@suaveplan/service-discovery";
recordRegistration("registered");
recordQuery();
recordQueryDuration(1.42);
Usage: Advanced Example
import { bindActiveServicesGauge } from "@suaveplan/service-discovery";
const services = new Set<string>();
const dispose = bindActiveServicesGauge(() => services.size);
// ...later, on shutdown:
dispose();
API Reference
recordRegistration(outcome)— incrementssvc_disc.registrations_total{outcome}by 1.outcomeis theRegistrationOutcomeunion.recordQuery()— incrementssvc_disc.queries_totalby 1.recordQueryDuration(ms)— records intosvc_disc.query_duration_ms. Values below zero are clamped to 0.bindActiveServicesGauge(getSize)— registers an OTel observable-gauge callback. Returns a disposer that unbinds the callback.
Implementation Notes
The outcome label is restricted to three string values so dashboards can
chart success vs refresh vs rejection without unbounded cardinality. The
gauge is observable (not synchronous) because the canonical "current size"
of the registry is best read on scrape — that way no separate metric write
is required on every add/delete and the gauge cannot drift from the actual
state. bindActiveServicesGauge returns its own disposer so registries can
properly tear down their instrumentation on dispose() without leaking
callbacks across test runs.