From d4813a2f3c9c283fe84340b7b642e0c3dcc9883e Mon Sep 17 00:00:00 2001 From: Eric Allam Date: Tue, 27 May 2025 13:06:26 +0100 Subject: [PATCH 1/2] Enable prisma metrics and add them to the /metrics endpoint --- apps/webapp/app/routes/metrics.ts | 6 +++++- internal-packages/database/prisma/schema.prisma | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/apps/webapp/app/routes/metrics.ts b/apps/webapp/app/routes/metrics.ts index a0f71a29b1..3e60932efa 100644 --- a/apps/webapp/app/routes/metrics.ts +++ b/apps/webapp/app/routes/metrics.ts @@ -1,8 +1,12 @@ import { LoaderFunctionArgs } from "@remix-run/server-runtime"; +import { prisma } from "~/db.server"; import { metricsRegister } from "~/metrics.server"; export async function loader({ request }: LoaderFunctionArgs) { - return new Response(await metricsRegister.metrics(), { + const prismaMetrics = await prisma.$metrics.prometheus(); + const coreMetrics = await metricsRegister.metrics(); + + return new Response(coreMetrics + prismaMetrics, { headers: { "Content-Type": metricsRegister.contentType, }, diff --git a/internal-packages/database/prisma/schema.prisma b/internal-packages/database/prisma/schema.prisma index e0a4534508..5d143e18da 100644 --- a/internal-packages/database/prisma/schema.prisma +++ b/internal-packages/database/prisma/schema.prisma @@ -7,7 +7,7 @@ datasource db { generator client { provider = "prisma-client-js" binaryTargets = ["native", "debian-openssl-1.1.x"] - previewFeatures = ["tracing"] + previewFeatures = ["tracing", "metrics"] } model User { From 6726bf88524f77b86417d3c1b18da1daa5497cc7 Mon Sep 17 00:00:00 2001 From: Eric Allam Date: Tue, 27 May 2025 13:20:37 +0100 Subject: [PATCH 2/2] Add support for bearer token auth on metrics endpoint --- apps/webapp/app/routes/metrics.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/apps/webapp/app/routes/metrics.ts b/apps/webapp/app/routes/metrics.ts index 3e60932efa..ab19350968 100644 --- a/apps/webapp/app/routes/metrics.ts +++ b/apps/webapp/app/routes/metrics.ts @@ -3,6 +3,16 @@ import { prisma } from "~/db.server"; import { metricsRegister } from "~/metrics.server"; export async function loader({ request }: LoaderFunctionArgs) { + // If the TRIGGER_METRICS_AUTH_PASSWORD is set, we need to check if the request has the correct password in auth header + const authPassword = process.env.TRIGGER_METRICS_AUTH_PASSWORD; + + if (authPassword) { + const auth = request.headers.get("Authorization"); + if (auth !== `Bearer ${authPassword}`) { + return new Response("Unauthorized", { status: 401 }); + } + } + const prismaMetrics = await prisma.$metrics.prometheus(); const coreMetrics = await metricsRegister.metrics();