all repos — homestead @ 81fff3a543e64da750aca20931df1c5d7027b0a6

Code for my website

Add hostname to logging and metrics

Alan Pearce
commit

81fff3a543e64da750aca20931df1c5d7027b0a6

parent

d411a7329ef7d98b28a431fc4c062c8b7fbd141e

1 file changed, 23 insertions(+), 4 deletions(-)

jump to
M src/app.tssrc/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 @@ op: "http.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 @@ if (pathname === "/health") {
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 @@ ) >= file?.mtime.getTime()
) { metrics.requests.inc({ method: request.method, + hostname, path: pathname, status_code: (status = 304), });
@@ -192,6 +200,7 @@ status = 200;
transaction.setHttpStatus(status); metrics.requests.inc({ method: request.method, + hostname, path: pathname, status_code: status, content_encoding: contentEncoding,
@@ -209,6 +218,7 @@ if (files.has(pathname + "/")) {
newpath = pathname + "/"; metrics.requests.inc({ method: request.method, + hostname, path: pathname, status_code: (status = 302), });
@@ -220,6 +230,7 @@ } else if (files.has(pathname.replace(/index.html$/, ""))) {
newpath = pathname.replace(/index.html$/, ""); metrics.requests.inc({ method: request.method, + hostname, path: newpath, status_code: (status = 302), });
@@ -230,6 +241,7 @@ });
} metrics.requests.inc({ method: request.method, + hostname, path: pathname, status_code: (status = 404), content_encoding: "identity",
@@ -252,6 +264,7 @@ } catch (error) {
transaction.setHttpStatus((status = 503)); metrics.requests.inc({ method: request.method, + hostname, path: pathname, status_code: status, content_encoding: "identity",
@@ -263,7 +276,13 @@ } finally {
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;