OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.
Python
feat: Support FE Prometheus metrics (#2249)
Mike Fortmanfd1329a38053
diff --git a/Dockerfile.frontend b/Dockerfile.frontendindex 85ad6d8cb..2e62bb38e 100644--- a/Dockerfile.frontend+++ b/Dockerfile.frontend@@ -53,7 +53,7 @@ COPY --from=builder --chown=1001:0 /opt/app-root/src/postcss.config.* ./COPY --from=builder --chown=1001:0 /opt/app-root/src/tailwind.config.* ./COPY --from=builder --chown=1001:0 /opt/app-root/src/biome.json* ./-EXPOSE 3000+EXPOSE 3000 9090USER 1001CMD ["npm", "start"]\ No newline at end of filediff --git a/frontend/app/api/[...path]/route.ts b/frontend/app/api/[...path]/route.tsindex 0b0076295..e32135cbf 100644--- a/frontend/app/api/[...path]/route.ts+++ b/frontend/app/api/[...path]/route.ts@@ -1,4 +1,10 @@import { NextRequest, NextResponse } from "next/server";+import {+ backendProxyDuration,+ backendProxyErrors,+ backendProxyTotal,+ normalizePath,+} from "@/lib/metrics";function getRequestId(request: NextRequest): string {return request.headers.get("x-request-id") || crypto.randomUUID();@@ -118,6 +124,25 @@ async function proxyRequest(request: NextRequest, params: { path: string[] }) {});const response = await fetch(backendUrl, init);const durationMs = Math.round(performance.now() - start);+ const durationSeconds = durationMs / 1000;++ const normalizedPath = normalizePath(`/${path}`);++ backendProxyDuration.observe(+ {+ method: request.method,+ path: normalizedPath,+ status_code: response.status.toString(),+ },+ durationSeconds,+ );++ backendProxyTotal.inc({+ method: request.method,+ path: normalizedPath,+ status_code: response.status.toString(),+ });+// biome-ignore lint/suspicious/noConsole: Server-side proxy timing is needed for CI diagnostics.console.info("[API Proxy] Request", {request_id: requestId,@@ -163,6 +188,31 @@ async function proxyRequest(request: NextRequest, params: { path: string[] }) {}} catch (error) {const durationMs = Math.round(performance.now() - start);+ const durationSeconds = durationMs / 1000;++ const normalizedPath = normalizePath(`/${path}`);++ backendProxyErrors.inc({+ method: request.method,+ path: normalizedPath,+ error_type: error instanceof Error ? error.name : "unknown",+ });++ backendProxyDuration.observe(+ {+ method: request.method,+ path: normalizedPath,+ status_code: "500",+ },+ durationSeconds,+ );++ backendProxyTotal.inc({+ method: request.method,+ path: normalizedPath,+ status_code: "500",+ });+console.error("[API Proxy] Request failed", {request_id: requestId,method: request.method,diff --git a/frontend/instrumentation.ts b/frontend/instrumentation.tsnew file mode 100644index 000000000..5b6734be4--- /dev/null+++ b/frontend/instrumentation.ts@@ -0,0 +1,39 @@+const SERVER_KEY = Symbol.for("openrag.metricsServer");++export async function register() {+ if (process.env.NEXT_RUNTIME !== "nodejs") return;+ if ((globalThis as Record<symbol, boolean>)[SERVER_KEY]) return;+ (globalThis as Record<symbol, boolean>)[SERVER_KEY] = true;++ const { createServer } = await import("node:http");+ const { metricsRegistry } = await import("./lib/metrics");++ const port = Number(process.env.METRICS_PORT) || 9090;++ const server = createServer(async (req, res) => {+ if (req.url === "/metrics") {+ try {+ res.setHeader("Content-Type", metricsRegistry.contentType);+ res.end(await metricsRegistry.metrics());+ } catch {+ res.statusCode = 500;+ res.end('{"error":"Failed to generate metrics"}');+ }+ } else if (req.url === "/health") {+ res.setHeader("Content-Type", "application/json");+ res.end('{"status":"ok"}');+ } else {+ res.statusCode = 404;+ res.end();+ }+ });++ server.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, () => {+ console.log(`Metrics server listening on port ${port}`);+ });+}diff --git a/frontend/lib/metrics.ts b/frontend/lib/metrics.tsnew file mode 100644index 000000000..6a1dd0612--- /dev/null+++ b/frontend/lib/metrics.ts@@ -0,0 +1,75 @@+import {+ Counter,+ collectDefaultMetrics,+ Histogram,+ Registry,+} from "prom-client";++interface Metrics {+ registry: Registry;+ backendProxyDuration: Histogram;+ backendProxyTotal: Counter;+ backendProxyErrors: Counter;+}++const GLOBAL_KEY = Symbol.for("openrag.metrics");++function getOrCreateMetrics(): Metrics {+ const g = globalThis as Record<symbol, Metrics>;+ if (!g[GLOBAL_KEY]) {+ const registry = new Registry();+ collectDefaultMetrics({+ register: registry,+ prefix: "nodejs_",+ gcDurationBuckets: [0.001, 0.01, 0.1, 1, 2, 5],+ });++ g[GLOBAL_KEY] = {+ registry,+ backendProxyDuration: new Histogram({+ name: "backend_proxy_duration_seconds",+ help: "Duration of backend proxy requests in seconds",+ labelNames: ["method", "path", "status_code"],+ buckets: [0.1, 0.5, 1, 2, 5, 10, 30, 60],+ registers: [registry],+ }),+ backendProxyTotal: new Counter({+ name: "backend_proxy_requests_total",+ help: "Total number of backend proxy requests",+ labelNames: ["method", "path", "status_code"],+ registers: [registry],+ }),+ backendProxyErrors: new Counter({+ name: "backend_proxy_errors_total",+ help: "Total number of backend proxy errors",+ labelNames: ["method", "path", "error_type"],+ registers: [registry],+ }),+ };+ }+ return g[GLOBAL_KEY];+}++const metrics = getOrCreateMetrics();++export const metricsRegistry = metrics.registry;+export const backendProxyDuration = metrics.backendProxyDuration;+export const backendProxyTotal = metrics.backendProxyTotal;+export const backendProxyErrors = metrics.backendProxyErrors;++export function normalizePath(raw: string): string {+ return raw+ .split("/")+ .map((seg) => {+ if (+ /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(+ seg,+ )+ )+ return ":id";+ if (/^[0-9a-f]{24}$/i.test(seg)) return ":id";+ if (/^\d+$/.test(seg)) return ":id";+ return seg;+ })+ .join("/");+}diff --git a/frontend/package-lock.json b/frontend/package-lock.jsonindex ea3152bfe..71530500b 100644--- a/frontend/package-lock.json+++ b/frontend/package-lock.json@@ -39,6 +39,7 @@"motion": "^12.42.0","next": "^16.2.12","next-themes": "^0.4.6",+ "prom-client": "^15.1.3","react": "^19.2.7","react-dom": "^19.2.7","react-dropzone": "^15.0.0",@@ -636,6 +637,15 @@"node": ">= 8"}},+ "node_modules/@opentelemetry/api": {+ "version": "1.9.1",+ "resolved": "https://registry.npmjs.org/@opentelemetry/api/-/api-1.9.1.tgz",+ "integrity": "sha512-gLyJlPHPZYdAk1JENA9LeHejZe1Ti77/pTeFm/nMXmQH/HFZlcS/O2XJB+L8fkbrNSqhdtlvjBVjxwUYanNH5Q==",+ "license": "Apache-2.0",+ "engines": {+ "node": ">=8.0.0"+ }+ },"node_modules/@oxc-parser/binding-android-arm-eabi": {"version": "0.137.0","resolved": "https://registry.npmjs.org/@oxc-parser/binding-android-arm-eabi/-/binding-android-arm-eabi-0.137.0.tgz",@@ -2960,6 +2970,12 @@"url": "https://github.com/sponsors/sindresorhus"}},+ "node_modules/bintrees": {+ "version": "1.0.2",+ "resolved": "https://registry.npmjs.org/bintrees/-/bintrees-1.0.2.tgz",+ "integrity": "sha512-VOMgTMwjAaUG580SXn3LacVgjurrbMme7ZZNYGSSV7mmtY6QQRh0Eg3pwIcntQ77DErK1L0NxkbetjcoXzVwKw==",+ "license": "MIT"+ },"node_modules/braces": {"version": "3.0.3","resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz",@@ -5868,6 +5884,19 @@"node": ">=6"}},+ "node_modules/prom-client": {+ "version": "15.1.3",+ "resolved": "https://registry.npmjs.org/prom-client/-/prom-client-15.1.3.tgz",+ "integrity": "sha512-6ZiOBfCywsD4k1BN9IX0uZhF+tJkV8q8llP64G5Hajs4JOeVLPCwpPVcpXy3BwYiUGgyJzsJJQeOIv7+hDSq8g==",+ "license": "Apache-2.0",+ "dependencies": {+ "@opentelemetry/api": "^1.4.0",+ "tdigest": "^0.1.1"+ },+ "engines": {+ "node": "^16 || ^18 || >=20"+ }+ },"node_modules/prop-types": {"version": "15.8.1","resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.8.1.tgz",@@ -6675,6 +6704,15 @@"jiti": "bin/jiti.js"}},+ "node_modules/tdigest": {+ "version": "0.1.2",+ "resolved": "https://registry.npmjs.org/tdigest/-/tdigest-0.1.2.tgz",+ "integrity": "sha512-+G0LLgjjo9BZX2MfdvPfH+MKLCrxlXSYec5DaPYP1fe6Iyhf0/fSmJ0bFiZ1F8BT6cGXl2LpltQptzjXKWEkKA==",+ "license": "MIT",+ "dependencies": {+ "bintrees": "1.0.2"+ }+ },"node_modules/text-hex": {"version": "1.0.0","resolved": "https://registry.npmjs.org/text-hex/-/text-hex-1.0.0.tgz",diff --git a/frontend/package.json b/frontend/package.jsonindex ca9a026dc..1233fe282 100644--- a/frontend/package.json+++ b/frontend/package.json@@ -48,6 +48,7 @@"motion": "^12.42.0","next": "^16.2.12","next-themes": "^0.4.6",+ "prom-client": "^15.1.3","react": "^19.2.7","react-dom": "^19.2.7","react-dropzone": "^15.0.0",diff --git a/kubernetes/helm/openrag/templates/frontend/deployment.yaml b/kubernetes/helm/openrag/templates/frontend/deployment.yamlindex 4c7ec9375..8fe518361 100644--- a/kubernetes/helm/openrag/templates/frontend/deployment.yaml+++ b/kubernetes/helm/openrag/templates/frontend/deployment.yaml@@ -17,6 +17,10 @@ spec:metadata:labels:{{- include "openrag.frontend.selectorLabels" . | nindent 8 }}+ annotations:+ prometheus.io/scrape: "true"+ prometheus.io/port: "9090"+ prometheus.io/path: "/metrics"spec:serviceAccountName: {{ include "openrag.serviceAccountName" . }}{{- with .Values.podSecurityContext }}@@ -39,6 +43,9 @@ spec:- name: httpcontainerPort: 3000protocol: TCP+ - name: metrics+ containerPort: 9090+ protocol: TCPenv:# Backend connection (uses internal service name)- name: OPENRAG_BACKEND_HOST