summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2023-09-17 09:22:49 +0200
committerAlan Pearce2023-09-17 09:22:49 +0200
commit8364b8316fe2ce2e15b5a621755234d58ec419de (patch)
tree05877b77bf00e02bdb855aaac789d764ab82e86c
parent2773f627093c729e920860b1ec4b3b507cf71f3d (diff)
downloadwebsite-8364b8316fe2ce2e15b5a621755234d58ec419de.tar.lz
website-8364b8316fe2ce2e15b5a621755234d58ec419de.tar.zst
website-8364b8316fe2ce2e15b5a621755234d58ec419de.zip
Merge requestsBy* metrics into one metric
-rw-r--r--src/index.ts35
1 files 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<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 @@ 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 {