@setu-ts/metrics-plugin — application metrics and scraping
Add it: setu add metrics adds the dependency and inserts MetricsPlugin() in recognized
scaffolds (already registered in REST projects). It provides CAPABILITIES.METRICS
(IMetricsService).
Count a completed operation
import { CAPABILITIES } from "@setu-ts/common";
import type { IMetricsService, IRouterApi } from "@setu-ts/common";
export function registerExportRoutes(router: IRouterApi): void {
router.get("/export", {
handler: (ctx) => {
const metrics = ctx.services.get<IMetricsService>(CAPABILITIES.METRICS);
metrics.counter("exports_total", { help: "Exports served" }).inc();
return ctx.response.json({ rows: [] });
},
});
}
Declare metrics before the first operation
Configure the existing registration in setu.config.ts:
import { MetricsPlugin } from "@setu-ts/metrics-plugin";
app.register(MetricsPlugin({
customMetrics: [{ name: "exports_total", type: "counter", help: "Exports served" }],
}));
Behaviour the types do not show
endpointdefaults to/metrics; scrapes serve Prometheus text with version0.0.4.defaultMetricsandhttpMetricsboth default totrue; HTTP collection runs only when both are enabled. The scrape endpoint is always excluded from HTTP collection.excludePathsreplaces the default health/live/ready exclusions rather than extending them.counter,gauge,histogramandsummaryreturn the existing instrument for a repeated name; requesting another type under that name throws.getreturnsundefinedwhen absent.- Counters default to incrementing by one and reject negative increments. Record value first, optional labels second; when labels are declared, supplied names must match that set.
customMetricsregisters at initialization, so metric declarations appear before recording a sample. Unspecifiedhelpin factory calls defaults to the metric name.
Other options
endpointchanges the scrape path;excludePathsaccepts exact strings or regular expressions.defaultBucketsanddefaultQuantilesset histogram and summary defaults.MetricOptionsadds per-instrumentlabels,buckets,quantilesandmaxSamples;NamedMetricConfigtypes declarativecustomMetricsentries.