From 8364b8316fe2ce2e15b5a621755234d58ec419de Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sun, 17 Sep 2023 09:22:49 +0200 Subject: Merge requestsBy* metrics into one metric --- src/index.ts | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/index.ts b/src/index.ts index 26007c8..874da30 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,15 +31,10 @@ type File = { }; const metrics = { - requestsByStatus: new prom.Counter({ - name: "homestead_requests_by_status", - help: "Number of requests by status code", - labelNames: ["status_code"], - }), - requestsByPath: new prom.Counter({ + requests: new prom.Counter({ name: "homestead_requests_by_path", help: "Number of requests by path", - labelNames: ["path"], + labelNames: ["path", "status_code", "method"], }), requestDuration: new prom.Histogram({ name: "homestead_request_duration_seconds", @@ -104,7 +99,6 @@ async function serveFile( statusCode: number = 200, extraHeaders: Record = {}, ): Promise { - metrics.requestsByStatus.inc({ status_code: statusCode }); return new Response(await file.handle.arrayBuffer(), { headers: { "last-modified": file.mtime.toUTCString(), @@ -156,18 +150,27 @@ const server = Bun.serve({ }); try { const file = files.get(pathname); - metrics.requestsByPath.inc({ path: pathname }); + metrics.requests.inc({ path: pathname }); if (file && (await file.handle.exists())) { if ( parseIfModifiedSinceHeader( request.headers.get("if-modified-since"), ) >= file?.mtime.getTime() ) { - metrics.requestsByStatus.inc({ status_code: 304 }); span.setHttpStatus(304); + metrics.requests.inc({ + method: request.method, + path: pathname, + status_code: 304, + }); transaction.setHttpStatus(304); return new Response("", { status: 304, headers: defaultHeaders }); } + metrics.requests.inc({ + method: request.method, + path: pathname, + status_code: 200, + }); const encodings = (request.headers.get("accept-encoding") || "") .split(",") .map((x) => x.trim().toLowerCase()); @@ -207,7 +210,11 @@ const server = Bun.serve({ transaction.setHttpStatus(200); return serveFile(file); } else { - metrics.requestsByStatus.inc({ status_code: 404 }); + metrics.requests.inc({ + method: request.method, + path: pathname, + status_code: 404, + }); span.setTag("http.encoding", "identity"); span.setHttpStatus(404); transaction.setHttpStatus(404); @@ -216,7 +223,11 @@ const server = Bun.serve({ } catch (error) { span.setHttpStatus(503); transaction.setHttpStatus(503); - metrics.requestsByStatus.inc({ status_code: 503 }); + metrics.requests.inc({ + method: request.method, + path: pathname, + status_code: 503, + }); Sentry.captureException(error); return new Response("Something went wrong", { status: 503 }); } finally { -- cgit 1.4.1