about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2023-09-15 10:34:33 +0200
committerAlan Pearce2023-09-15 19:24:53 +0200
commite718339d93e2f60b6df55330aafb2d9536820ce4 (patch)
tree443646c16c484dc3ecb0f74bd1658ffd00a4e40a
parent4218b3b54104cf3318943fcfb0a05b5f0b6cfe8e (diff)
downloadhomestead-main.tar.xz
homestead-main.zip
Add request_duration_seconds histogram HEAD main
-rw-r--r--src/index.ts23
1 files changed, 16 insertions, 7 deletions
diff --git a/src/index.ts b/src/index.ts index a377985..ee092f2 100644 --- a/src/index.ts +++ b/src/index.ts
@@ -34,7 +34,7 @@ collectDefaultMetrics({
34 FLY_REGION: Bun.env.FLY_REGION, 34 FLY_REGION: Bun.env.FLY_REGION,
35 }, 35 },
36}); 36});
37const counters = { 37const metrics = {
38 requestsByStatus: new prom.Counter({ 38 requestsByStatus: new prom.Counter({
39 name: "homestead_requests_by_status", 39 name: "homestead_requests_by_status",
40 help: "Number of requests by status code", 40 help: "Number of requests by status code",
@@ -45,6 +45,11 @@ const counters = {
45 help: "Number of requests by path", 45 help: "Number of requests by path",
46 labelNames: ["path"], 46 labelNames: ["path"],
47 }), 47 }),
48 requestDuration: new prom.Histogram({
49 name: "homestead_request_duration_seconds",
50 help: "Request duration in seconds",
51 labelNames: ["path"],
52 }),
48}; 53};
49 54
50let files = new Map<string, File>(); 55let files = new Map<string, File>();
@@ -102,7 +107,7 @@ async function serveFile(
102 extraHeaders: Record<string, string> = {}, 107 extraHeaders: Record<string, string> = {},
103): Promise<Response> { 108): Promise<Response> {
104 if (file && (await file.handle.exists())) { 109 if (file && (await file.handle.exists())) {
105 counters.requestsByStatus.inc({ status_code: statusCode }); 110 metrics.requestsByStatus.inc({ status_code: statusCode });
106 return new Response(file.handle, { 111 return new Response(file.handle, {
107 headers: { 112 headers: {
108 "last-modified": file.mtime.toUTCString(), 113 "last-modified": file.mtime.toUTCString(),
@@ -112,7 +117,7 @@ async function serveFile(
112 status: statusCode, 117 status: statusCode,
113 }); 118 });
114 } else { 119 } else {
115 counters.requestsByStatus.inc({ status_code: 404 }); 120 metrics.requestsByStatus.inc({ status_code: 404 });
116 // TODO return encoded 121 // TODO return encoded
117 return serveFile(files.get("/404.html"), 404); 122 return serveFile(files.get("/404.html"), 404);
118 } 123 }
@@ -151,17 +156,18 @@ console.info(
151 156
152const server = Bun.serve({ 157const server = Bun.serve({
153 fetch: async function (request) { 158 fetch: async function (request) {
159 const pathname = new URL(request.url).pathname;
160 const endTimer = metrics.requestDuration.startTimer({ path: pathname });
154 try { 161 try {
155 const pathname = new URL(request.url).pathname;
156 const file = files.get(pathname); 162 const file = files.get(pathname);
157 counters.requestsByPath.inc({ path: pathname }); 163 metrics.requestsByPath.inc({ path: pathname });
158 if (file) { 164 if (file) {
159 if ( 165 if (
160 parseIfModifiedSinceHeader( 166 parseIfModifiedSinceHeader(
161 request.headers.get("if-modified-since"), 167 request.headers.get("if-modified-since"),
162 ) >= file?.mtime.getTime() 168 ) >= file?.mtime.getTime()
163 ) { 169 ) {
164 counters.requestsByStatus.inc({ status_code: 304 }); 170 metrics.requestsByStatus.inc({ status_code: 304 });
165 return new Response("", { status: 304, headers: defaultHeaders }); 171 return new Response("", { status: 304, headers: defaultHeaders });
166 } 172 }
167 const encodings = (request.headers.get("accept-encoding") || "") 173 const encodings = (request.headers.get("accept-encoding") || "")
@@ -192,9 +198,12 @@ const server = Bun.serve({
192 } 198 }
193 return serveFile(file); 199 return serveFile(file);
194 } catch (error) { 200 } catch (error) {
195 counters.requestsByStatus.inc({ status_code: 503 }); 201 metrics.requestsByStatus.inc({ status_code: 503 });
196 Sentry.captureException(error); 202 Sentry.captureException(error);
197 return new Response("Something went wrong", { status: 503 }); 203 return new Response("Something went wrong", { status: 503 });
204 } finally {
205 const seconds = endTimer();
206 metrics.requestDuration.observe(seconds);
198 } 207 }
199 }, 208 },
200}); 209});