From 627aec8448ca075ea4bf87f85229c67a7374eac0 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Wed, 13 Sep 2023 15:55:48 +0200 Subject: Collect metrics for prometheus --- bun.lockb | Bin 1630 -> 2683 bytes package.json | 1 + src/index.ts | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/bun.lockb b/bun.lockb index 9d90bd9..bea9f9d 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 354305b..a0b6a73 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ }, "type": "module", "dependencies": { + "bun-prometheus-client": "^0.0.2", "toml": "^3.0.0" } } diff --git a/src/index.ts b/src/index.ts index ff154fa..6699a10 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import path from "node:path"; import fs, { Stats } from "node:fs"; import type { BunFile, Serve } from "bun"; +import prom from "bun-prometheus-client"; import readConfig from "./config"; @@ -22,6 +23,26 @@ type File = { mtime: Date; }; +const collectDefaultMetrics = prom.collectDefaultMetrics; +collectDefaultMetrics({ + labels: { + FLY_APP_NAME: Bun.env.FLY_APP_NAME, + FLY_ALLOC_ID: Bun.env.FLY_ALLOC_ID, + FLY_REGION: Bun.env.FLY_REGION, + }, +}); +const counters = { + requestsByStatus: new prom.Counter({ + name: "requests_by_status", + help: "Number of requests by status code", + labelNames: ["status_code"], + }), + requestsByPath: new prom.Counter({ + name: "requests_by_path", + help: "Number of requests by path", + }), +}; + let files = new Map(); function registerFile( @@ -77,6 +98,7 @@ async function serveFile( extraHeaders: Record = {}, ): Promise { if (file && (await file.handle.exists())) { + counters.requestsByStatus.inc({ status_code: statusCode }); return new Response(file.handle, { headers: { "last-modified": file.mtime.toUTCString(), @@ -86,6 +108,7 @@ async function serveFile( status: statusCode, }); } else { + counters.requestsByStatus.inc({ status_code: 404 }); // TODO return encoded return serveFile(files.get("/404.html"), 404); } @@ -105,15 +128,30 @@ function parseIfModifiedSinceHeader(header: string | null): number { return header ? new Date(header).getTime() + 999 : 0; } +Bun.serve({ + port: 9091, + fetch: async function (request) { + const pathname = new URL(request.url).pathname; + switch (pathname) { + case "/metrics": + return new Response(await prom.register.metrics()); + default: + return new Response("", { status: 404 }); + } + }, +}); + export default { fetch: async function (request) { const pathname = new URL(request.url).pathname; const file = files.get(pathname); + counters.requestsByPath.inc({ path: pathname }); if (file) { if ( parseIfModifiedSinceHeader(request.headers.get("if-modified-since")) >= file?.mtime.getTime() ) { + counters.requestsByStatus.inc({ status_code: 304 }); return new Response("", { status: 304, headers: defaultHeaders }); } const encodings = (request.headers.get("accept-encoding") || "") -- cgit 1.4.1