diff options
author | Alan Pearce | 2024-04-04 15:49:35 +0200 |
---|---|---|
committer | Alan Pearce | 2024-04-04 15:49:35 +0200 |
commit | 82ce3eb70d5346f6e0244a0126eff7f80754a069 (patch) | |
tree | a4f961323802b594da60936efc84684d862e97b9 /src | |
parent | b5d8d78eacdbb3ef005162969ca558aed626ead0 (diff) | |
download | website-82ce3eb70d5346f6e0244a0126eff7f80754a069.tar.lz website-82ce3eb70d5346f6e0244a0126eff7f80754a069.tar.zst website-82ce3eb70d5346f6e0244a0126eff7f80754a069.zip |
Add hostname to logging and metrics
Diffstat (limited to 'src')
-rw-r--r-- | src/app.ts | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/src/app.ts b/src/app.ts index 0f8f08d..101d2bc 100644 --- a/src/app.ts +++ b/src/app.ts @@ -39,7 +39,13 @@ const metrics = { requests: new prom.Counter({ name: "homestead_requests", help: "Number of requests by path, status code, and method", - labelNames: ["path", "status_code", "method", "content_encoding"], + labelNames: [ + "path", + "status_code", + "hostname", + "method", + "content_encoding", + ], }), requestDuration: new prom.Histogram({ name: "homestead_request_duration_seconds", @@ -129,6 +135,7 @@ export const server = { fetch: async function (request) { const url = new URL(request.url); const pathname = url.pathname.replace(/\/\/+/g, "/"); + const hostname = request.headers.get("host") || "unknown"; const endTimer = metrics.requestDuration.startTimer({ path: pathname }); let status; let newpath; @@ -138,7 +145,7 @@ export const server = { description: `${request.method} ${pathname}`, tags: { url: request.url, - "http.host": request.headers.get("host"), + "http.host": hostname, "http.method": request.method, "http.user_agent": request.headers.get("user-agent"), }, @@ -148,7 +155,7 @@ export const server = { return new Response("OK", { status: (status = 200) }); } else if ( config.redirect_other_hostnames && - request.headers.get("host") !== expectedHostURL.host + hostname !== expectedHostURL.host ) { return new Response("", { status: (status = 301), @@ -168,6 +175,7 @@ export const server = { ) { metrics.requests.inc({ method: request.method, + hostname, path: pathname, status_code: (status = 304), }); @@ -192,6 +200,7 @@ export const server = { transaction.setHttpStatus(status); metrics.requests.inc({ method: request.method, + hostname, path: pathname, status_code: status, content_encoding: contentEncoding, @@ -209,6 +218,7 @@ export const server = { newpath = pathname + "/"; metrics.requests.inc({ method: request.method, + hostname, path: pathname, status_code: (status = 302), }); @@ -220,6 +230,7 @@ export const server = { newpath = pathname.replace(/index.html$/, ""); metrics.requests.inc({ method: request.method, + hostname, path: newpath, status_code: (status = 302), }); @@ -230,6 +241,7 @@ export const server = { } metrics.requests.inc({ method: request.method, + hostname, path: pathname, status_code: (status = 404), content_encoding: "identity", @@ -252,6 +264,7 @@ export const server = { transaction.setHttpStatus((status = 503)); metrics.requests.inc({ method: request.method, + hostname, path: pathname, status_code: status, content_encoding: "identity", @@ -263,7 +276,13 @@ export const server = { const seconds = endTimer(); metrics.requestDuration.observe(seconds); transaction.finish(); - console.log(request.method, status, pathname, newpath ? newpath : ""); + console.log( + request.method, + status, + hostname, + pathname, + newpath ? newpath : "", + ); } }, } satisfies Serve; |