From 5a74cad793d2d2935ef8a8a909208dc53051b0b3 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Fri, 18 Sep 2026 13:26:32 -0500 Subject: [PATCH] feat(metrics): count request:end, and only request:end MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A failed request emits three lifecycle events. request:end is the terminal and universal one, so it is the request counter; request:failed is additional and gets its own series. Counting both as requests is the double count issue #64 was filed for. Labels come from route, which the route table bounds, never from name, which carries the arriving path and is bounded by nothing — issue #65. Signed-off-by: Diego Resendez --- packages/metrics/src/collector.ts | Bin 0 -> 4973 bytes packages/metrics/src/types.ts | 19 ++++++ packages/metrics/test/metrics.test.ts | 82 ++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 packages/metrics/src/collector.ts create mode 100644 packages/metrics/src/types.ts create mode 100644 packages/metrics/test/metrics.test.ts diff --git a/packages/metrics/src/collector.ts b/packages/metrics/src/collector.ts new file mode 100644 index 0000000000000000000000000000000000000000..25a9f41986b7e43d430fd38d02b7b863862079e4 GIT binary patch literal 4973 zcmbtY+ioMd5zVu|qT)56OFC^2kmSK$d9oWgNRYW)=e~KUlT$i#8PWK|7=UPCU|kJpFJLr4`@su7n)q# z1^lns1YOg5p)Dy(nysKpIniQUt4o~VL*KWZts$vwO||jC*eXOT#WGLr+S2sf&MsA` z7P>y47SL*{1ueC*o(^rBE=>Z5B@8h*XLS_eTMh$ki$>I@Cff$`YIB%XL0sYX`CL1; zbFN?m8?=TO3z}(TuSv}sO`)Zrn}*a%xxg+QA5^XNfj+(Y+ncw4e{=Qz?Wd0yG@$`3 zzOk_VT!L$ZcF?Nn`>wjtf$k0{T760$k+#=oc(K%B(boHKd{CkDFHgyK%b9j8bgP<9 z-@n*O^yxdO5d&}ZhUTpk?;ACPt%$9cN=Emx}pxFkYe*rUZTV$Do9J9kwne4lq)n*q*9NH+WzX%Giae$31uXPyW zJR~4u<}}O#Be{SmKbMjk`ppDph%iztA!6O|AE#N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9DXfgS)H3t+^{&balK7GC-U@Q{}Oxe(~$tMJ@_9mqm{e> literal 0 HcmV?d00001 diff --git a/packages/metrics/src/types.ts b/packages/metrics/src/types.ts new file mode 100644 index 0000000..701be42 --- /dev/null +++ b/packages/metrics/src/types.ts @@ -0,0 +1,19 @@ +/** One labelled series and its accumulated value. */ +export interface Series { + route: string; + method: string; + status?: number; + value: number; +} + +/** What the collector holds, for a consumer that wants numbers rather than text. */ +export interface MetricsSnapshot { + requests: Series[]; + errors: Series[]; + duration: Array<{ route: string; method: string; sum: number; count: number }>; +} + +export interface MetricsOptions { + /** Graph name the collector is published under, and the plugin's name. Defaults to `metrics`. */ + provides?: string; +} diff --git a/packages/metrics/test/metrics.test.ts b/packages/metrics/test/metrics.test.ts new file mode 100644 index 0000000..d2adcdd --- /dev/null +++ b/packages/metrics/test/metrics.test.ts @@ -0,0 +1,82 @@ +import { strict as assert } from 'node:assert'; +import { test } from 'node:test'; + +import { Metrics } from '../src/collector.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 '' 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: '', + method: 'GET', + status: 404, + durationMs: 1, + } as never); + } + + const { requests } = collector.snapshot(); + + assert.equal(requests.length, 1); + assert.equal(requests[0].route, ''); + 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, ''); +});