Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python

feat: Add http metrics to prometheus (#2266)

Mike Fortman749d10f46e50
diff --git a/frontend/instrumentation.ts b/frontend/instrumentation.ts
index 5b6734be4..499a24dc6 100644
--- a/frontend/instrumentation.ts
+++ b/frontend/instrumentation.ts
@@ -1,4 +1,5 @@
const SERVER_KEY = Symbol.for("openrag.metricsServer");
+const START_TIME = Symbol("openrag.requestStart");
export async function register() {
if (process.env.NEXT_RUNTIME !== "nodejs") return;
@@ -6,11 +7,17 @@ export async function register() {
(globalThis as Record<symbol, boolean>)[SERVER_KEY] = true;
const { createServer } = await import("node:http");
- const { metricsRegistry } = await import("./lib/metrics");
+ const { subscribe } = await import("node:diagnostics_channel");
+ const {
+ metricsRegistry,
+ httpRequestDuration,
+ httpRequestsTotal,
+ normalizeRoute,
+ } = await import("./lib/metrics");
const port = Number(process.env.METRICS_PORT) || 9090;
- const server = createServer(async (req, res) => {
+ const metricsServer = createServer(async (req, res) => {
if (req.url === "/metrics") {
try {
res.setHeader("Content-Type", metricsRegistry.contentType);
@@ -28,12 +35,38 @@ export async function register() {
}
});
- server.on("error", (err) => {
+ subscribe("http.server.request.start", (message: unknown) => {
+ const msg = message as Record<string, unknown>;
+ if (msg.server === metricsServer) return;
+ const req = msg.request as Record<symbol, number>;
+ req[START_TIME] = performance.now();
+ });
+
+ subscribe("http.server.response.finish", (message: unknown) => {
+ const msg = message as Record<string, unknown>;
+ if (msg.server === metricsServer) return;
+ const req = msg.request as Record<string | symbol, string | number>;
+ const startTime = req[START_TIME] as number | undefined;
+ if (startTime == null) return;
+
+ const url = (req.url as string) ?? "/";
+ const pathname = url.split("?", 1)[0];
+ const labels = {
+ method: req.method as string,
+ route: normalizeRoute(pathname),
+ status_code: String((msg.response as Record<string, number>).statusCode),
+ };
+
+ httpRequestDuration.observe(labels, (performance.now() - startTime) / 1000);
+ httpRequestsTotal.inc(labels);
+ });
+
+ metricsServer.on("error", (err) => {
console.error(`Metrics server failed to start on port ${port}:`, err);
(globalThis as Record<symbol, boolean>)[SERVER_KEY] = false;
});
- server.listen(port, () => {
+ metricsServer.listen(port, () => {
console.log(`Metrics server listening on port ${port}`);
});
}
diff --git a/frontend/lib/metrics.test.ts b/frontend/lib/metrics.test.ts
new file mode 100644
index 000000000..f93a251b0
--- /dev/null
+++ b/frontend/lib/metrics.test.ts
@@ -0,0 +1,34 @@
+import assert from "node:assert/strict";
+import { describe, it } from "node:test";
+import { normalizePath, normalizeRoute } from "./metrics";
+
+describe("normalizeRoute", () => {
+ it("collapses all /_next/* paths", () => {
+ assert.equal(normalizeRoute("/_next/static/chunks/abc123.js"), "/_next/*");
+ assert.equal(normalizeRoute("/_next/data/buildid/page.json"), "/_next/*");
+ assert.equal(normalizeRoute("/_next/image"), "/_next/*");
+ });
+
+ it("buckets unknown prefixes as unmatched", () => {
+ assert.equal(normalizeRoute("/favicon.ico"), "unmatched");
+ assert.equal(normalizeRoute("/wp-login.php"), "unmatched");
+ assert.equal(normalizeRoute("/.env"), "unmatched");
+ });
+
+ it("passes root through", () => {
+ assert.equal(normalizeRoute("/"), "/");
+ });
+
+ it("delegates to normalizePath for known prefixes", () => {
+ assert.equal(
+ normalizeRoute("/api/providers/abc-123/models"),
+ normalizePath("/api/providers/abc-123/models"),
+ );
+ assert.equal(
+ normalizeRoute("/chat/550e8400-e29b-41d4-a716-446655440000"),
+ "/chat/:id",
+ );
+ assert.equal(normalizeRoute("/settings/general"), "/settings/general");
+ assert.equal(normalizeRoute("/health"), "/health");
+ });
+});
diff --git a/frontend/lib/metrics.ts b/frontend/lib/metrics.ts
index 6a1dd0612..29493c8e1 100644
--- a/frontend/lib/metrics.ts
+++ b/frontend/lib/metrics.ts
@@ -10,6 +10,8 @@ interface Metrics {
backendProxyDuration: Histogram;
backendProxyTotal: Counter;
backendProxyErrors: Counter;
+ httpRequestDuration: Histogram;
+ httpRequestsTotal: Counter;
}
const GLOBAL_KEY = Symbol.for("openrag.metrics");
@@ -45,6 +47,19 @@ function getOrCreateMetrics(): Metrics {
labelNames: ["method", "path", "error_type"],
registers: [registry],
}),
+ httpRequestDuration: new Histogram({
+ name: "http_request_duration_seconds",
+ help: "Duration of inbound HTTP requests in seconds",
+ labelNames: ["method", "route", "status_code"],
+ buckets: [0.005, 0.01, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10],
+ registers: [registry],
+ }),
+ httpRequestsTotal: new Counter({
+ name: "http_requests_total",
+ help: "Total inbound HTTP requests",
+ labelNames: ["method", "route", "status_code"],
+ registers: [registry],
+ }),
};
}
return g[GLOBAL_KEY];
@@ -56,6 +71,8 @@ export const metricsRegistry = metrics.registry;
export const backendProxyDuration = metrics.backendProxyDuration;
export const backendProxyTotal = metrics.backendProxyTotal;
export const backendProxyErrors = metrics.backendProxyErrors;
+export const httpRequestDuration = metrics.httpRequestDuration;
+export const httpRequestsTotal = metrics.httpRequestsTotal;
export function normalizePath(raw: string): string {
return raw
@@ -73,3 +90,27 @@ export function normalizePath(raw: string): string {
})
.join("/");
}
+
+const KNOWN_PREFIXES = new Set([
+ "api",
+ "auth",
+ "chat",
+ "connectors",
+ "health",
+ "knowledge",
+ "login",
+ "mcp",
+ "settings",
+ "unauthorized",
+ "upload",
+]);
+
+export function normalizeRoute(raw: string): string {
+ if (raw === "/") return "/";
+ if (raw.startsWith("/_next/")) return "/_next/*";
+
+ const firstSeg = raw.split("/", 2)[1];
+ if (!firstSeg || !KNOWN_PREFIXES.has(firstSeg)) return "unmatched";
+
+ return normalizePath(raw);
+}