all repos — website @ 8364b8316fe2ce2e15b5a621755234d58ec419de

My website

Merge requestsBy* metrics into one metric
Alan Pearce alan@alanpearce.eu
Sun, 17 Sep 2023 09:22:49 +0200
commit

8364b8316fe2ce2e15b5a621755234d58ec419de

parent

2773f627093c729e920860b1ec4b3b507cf71f3d

1 files changed, 23 insertions(+), 12 deletions(-)

jump to
M src/index.tssrc/index.ts
@@ -31,15 +31,10 @@ mtime: Date; };
 
 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 @@ file: File | undefined,   statusCode: number = 200,
   extraHeaders: Record<string, string> = {},
 ): Promise<Response> {
-  metrics.requestsByStatus.inc({ status_code: statusCode });
   return new Response(await file.handle.arrayBuffer(), {
     headers: {
       "last-modified": file.mtime.toUTCString(),
@@ -156,18 +150,27 @@ description: `${request.method} ${pathname}`,     });
     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 @@ span.setHttpStatus(200);         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 @@ }     } 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 {