-
Notifications
You must be signed in to change notification settings - Fork 2
MetroMan Counter
A monotonic counter — values only ever increase.
Reach for Counter when you want a cumulative count that only goes
up — requests served, errors raised, bytes written. For values that
move both directions (queue depth, active connections), use
Gauge instead.
import { Counter } from '@tundralibs/metro-man';
const requests = new Counter({
name: 'http_requests_total',
help: 'Total HTTP requests served',
});
requests.inc();
requests.inc({ status: '200', method: 'GET' });
requests.inc({ status: '500', method: 'GET' });Parameters:
-
options.name— Required. Metric identifier; must match/^[a-zA-Z_:][a-zA-Z0-9_:]*$/(Prometheus name rules). -
options.help— Optional. Human description.
Throws:
-
InvalidMetricOptionsError— Whennameis missing, not a string, or doesn't match/^[a-zA-Z_:][a-zA-Z0-9_:]*$/; or whenhelpis given but not a string.
Increment the named series. Overloads:
-
inc()— increment unlabelled by 1 -
inc(labels)— increment labelled by 1 -
inc(amount)— increment unlabelled byamount -
inc(amount, labels)— increment labelled byamount
Parameters:
-
amount— Optional non-negative finite number. Defaults to 1. -
labels— OptionalRecord<string, string>. Each distinct labels combination is a separate series.
Throws:
-
InvalidMetricOptionsError— whenamountis negative or non-finite. -
InvalidLabelError— whenlabelscontains a name outside[A-Za-z_][A-Za-z0-9_]*.
reset() drops every series; remove(labels?) drops a single series
and returns true if a series was actually removed. Both inherited from
BaseMetric.
Throws:
-
InvalidLabelError—remove(labels)rejects a label name outside[A-Za-z_][A-Za-z0-9_]*, the same validationincapplies.
Inherited from BaseMetric. See the
main README for the output shapes.
For the example above, after the three inc() calls:
The rendered label order is alphabetical by name, not call-site order.
inc({ status: '200', method: 'GET' })passedstatusfirst, but the renderer sorts label entries by name before joining them, so the series key comes outmethod="GET",status="200"—methodbeforestatus. This only changes the rendered/canonical key; it never splits or merges series (see Canonicalisation).
# HELP http_requests_total Total HTTP requests served
# TYPE http_requests_total counter
http_requests_total 1
http_requests_total{method="GET",status="200"} 1
http_requests_total{method="GET",status="500"} 1
data is keyed by that same alphabetically-sorted string. labels,
by contrast, lists label names in first-seen call order — status
before method here, since the first labelled inc() passed status
first:
{
"name": "http_requests_total",
"help": "Total HTTP requests served",
"type": "COUNTER",
"labels": ["status", "method"],
"data": {
"no_label": 1,
"method=\"GET\",status=\"200\"": 1,
"method=\"GET\",status=\"500\"": 1
}
}