Files
chrnx 29b2fa0483
CI / dco (pull_request) Successful in 4s
CI / build (pull_request) Successful in 13s
CI / runtimes (bun) (pull_request) Successful in 10s
CI / runtimes (edge) (pull_request) Successful in 7s
CI / runtimes (deno) (pull_request) Successful in 10s
test(metrics): read the content-type as string | string[]
Core RC.1 widens response header values to string | string[] (core
#113, a documented Breaking change for readers). The metrics test read
the transformer's content-type as a string and fails to typecheck
against the RC.1 candidate. Narrowing with String() also typechecks on
26.9.0-beta.2. Plugin source is unaffected.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-10-30 10:24:34 -05:00

132 lines
4.3 KiB
TypeScript

import { strict as assert } from 'node:assert';
import { test } from 'node:test';
import { createApp, Get, Module, Route, Transformer, needs } from '@green-tea/core';
import { Metrics } from '../src/collector.ts';
import { metrics, prometheus } from '../src/index.ts';
const end = (route: string, method: string, status: number, durationMs = 5) =>
({ name: route, route, method, status, durationMs }) as never;
test('counts one request per request:end, keyed by route, method and status', () => {
const collector = new Metrics();
collector.observeEnd(end('/users/:id', 'GET', 200));
collector.observeEnd(end('/users/:id', 'GET', 200));
collector.observeEnd(end('/users/:id', 'GET', 404));
const { requests } = collector.snapshot();
assert.equal(requests.find((s) => s.status === 200)?.value, 2);
assert.equal(requests.find((s) => s.status === 404)?.value, 1);
});
// The defect issue #64 was filed for: a failed request emits request:start, request:failed and
// request:end. Counting the failure as a request reports one request as two.
test('a failure counts once as a request and once as an error, not twice as a request', () => {
const collector = new Metrics();
collector.observeFailure({ name: '/boom', route: '/boom', method: 'GET' } as never);
collector.observeEnd(end('/boom', 'GET', 500));
const { requests, errors } = collector.snapshot();
assert.equal(
requests.reduce((total, s) => total + s.value, 0),
1,
);
assert.equal(
errors.reduce((total, s) => total + s.value, 0),
1,
);
});
// Issue #65: `name` is the path that arrived and is bounded by nothing. `route` is the matched
// pattern, and is '<unmatched>' when nothing matched.
test('labels on the bounded route, so a scanner cannot mint a series per URL', () => {
const collector = new Metrics();
for (const probe of ['/aaa', '/aab', '/aac']) {
collector.observeEnd({
name: `GET ${probe}`,
route: '<unmatched>',
method: 'GET',
status: 404,
durationMs: 1,
} as never);
}
const { requests } = collector.snapshot();
assert.equal(requests.length, 1);
assert.equal(requests[0].route, '<unmatched>');
assert.equal(requests[0].value, 3);
});
test('accumulates duration as a sum and a count, in seconds', () => {
const collector = new Metrics();
collector.observeEnd(end('/x', 'GET', 200, 1500));
collector.observeEnd(end('/x', 'GET', 200, 500));
const [duration] = collector.snapshot().duration;
assert.equal(duration.count, 2);
assert.equal(duration.sum, 2);
});
test('an event with no route is attributed rather than dropped or left blank', () => {
const collector = new Metrics();
collector.observeEnd({ name: 'GET /x', method: 'GET', status: 200, durationMs: 1 } as never);
assert.equal(collector.snapshot().requests[0].route, '<unknown>');
});
test('the transformer serves the text format with the content type Prometheus expects', () => {
const rendered = prometheus('green_tea_requests_total{route="/x",method="GET",status="200"} 1\n');
assert.equal(rendered.status, 200);
// Core RC.1 widens response header values to `string | string[]` (core #113), so a reader narrows.
assert.match(String(rendered.headers!['content-type']), /text\/plain/);
assert.match(rendered.body, /green_tea_requests_total/);
});
test('an application serves its own /metrics route from the published collector', async () => {
@Route('/')
class Ops {
@Get('/metrics')
@Transformer(prometheus)
scrape(@needs('metrics') collector: Metrics) {
return collector.render();
}
@Get('/hello')
hello() {
return { ok: true };
}
}
@Module({ mountpoint: '/', controllers: [Ops] })
class OpsModule {}
const app = createApp({ modules: [OpsModule], plugins: [metrics()] });
try {
await app.fetch(new Request('http://plugin.test/hello'));
const scrape = await app.fetch(new Request('http://plugin.test/metrics'));
const body = await scrape.text();
assert.equal(scrape.status, 200);
assert.match(body, /green_tea_requests_total\{route="\/hello",method="GET",status="200"\} 1/);
} finally {
await app.close();
}
});
test('the plugin is named after what it provides', () => {
assert.equal(metrics().name, 'metrics');
assert.equal(metrics({ provides: 'opsMetrics' }).name, 'opsMetrics');
});