feat(metrics): publish the collector and a transformer to serve it
CI / dco (pull_request) Successful in 5s
CI / build (pull_request) Failing after 8s
CI / runtimes (bun) (pull_request) Failing after 6s
CI / runtimes (deno) (pull_request) Failing after 7s
CI / runtimes (edge) (pull_request) Failing after 6s

The transformer is exported instead of a route: the path, the auth and whether
the endpoint exists are the application's decisions, and a plugin that mounted
/metrics itself would be taking them.

No onShutdown — a bus subscription is an entry in a set and keeps nothing alive,
which is what rule 7 asks about. Closes green-tea#68.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-09-18 13:27:08 -05:00
1 parent 5a74cad793
commit de0081ddae
3 files changed
+119

No files matched your search

+49
View File
@@ -0,0 +1,49 @@
import type { Plugin } from '@green-tea/core';
import { Metrics } from './collector.ts';
import type { MetricsOptions } from './types.ts';
export { Metrics } from './collector.ts';
export { prometheus } from './prometheus.ts';
export type { MetricsOptions, MetricsSnapshot, Series } from './types.ts';
/**
* Subscribes to the lifecycle stream and publishes the counters into the graph.
*
* There is no `onShutdown` (convention rule 7): a bus subscription is an entry in a set, and holds
* no timer, socket or handle that could keep a process alive. Nothing to release.
*
* The collector is built at mount time rather than in the provider's `run`, because the subscription
* has to exist before the first request — a provider runs on boot, which is late enough to miss it.
*
* @example
* const app = createApp({ modules: [ApiModule], plugins: [metrics()] });
*
* @example <caption>Exposed on a route the application owns.</caption>
* ⁣@Get('/metrics')
* ⁣@Transformer(prometheus)
* scrape(@needs('metrics') collector: Metrics) {
* return collector.render();
* }
*/
export function metrics(options: MetricsOptions = {}): Plugin {
const provides = options.provides ?? 'metrics';
const collector = new Metrics();
return {
// Rule 2: plugin name, node name and provided token are one string.
name: provides,
mount({ bus, scope }) {
bus.on('request:end', (payload) => collector.observeEnd(payload));
bus.on('request:failed', (payload) => collector.observeFailure(payload));
scope.add({
kind: 'provider',
name: provides,
needs: [],
provides: [provides],
run: () => ({ [provides]: collector }),
});
},
};
}
+22
View File
@@ -0,0 +1,22 @@
import type { TransformerFn } from '@green-tea/core';
/**
* Serves a rendered scrape as the Prometheus text exposition format.
*
* A `TransformerFn` rather than a route: the path, the auth and whether the endpoint exists at all
* are the application's decisions, and a plugin that mounted `/metrics` itself would be taking them.
* The version is part of the content type — Prometheus reads it, and omitting it makes a scrape fall
* back to a guess.
*
* @example
* ⁣@Get('/metrics')
* ⁣@Transformer(prometheus)
* scrape(@needs('metrics') collector: Metrics) {
* return collector.render();
* }
*/
export const prometheus: TransformerFn = (value) => ({
status: 200,
headers: { 'content-type': 'text/plain; version=0.0.4; charset=utf-8' },
body: String(value),
});