From d8c741280e79b9438ba4904a60a5347ecb6ee161 Mon Sep 17 00:00:00 2001 From: Jonas Simoen Date: Sat, 27 Jan 2024 22:24:55 +0100 Subject: [PATCH] feat: add metrics --- api/index.ts | 9 +++++- docker-compose.yaml => docker-compose.db.yaml | 0 docker-compose.metrics.yaml | 30 +++++++++++++++++++ prisma/schema.prisma | 1 + 4 files changed, 39 insertions(+), 1 deletion(-) rename docker-compose.yaml => docker-compose.db.yaml (100%) create mode 100644 docker-compose.metrics.yaml diff --git a/api/index.ts b/api/index.ts index e35c95d..96a8cd2 100644 --- a/api/index.ts +++ b/api/index.ts @@ -8,6 +8,8 @@ import dotenv from "dotenv"; import { deserializeUser } from "../src/utils/DeserializeUser"; import fastifyStatic from "@fastify/static"; import path from "path"; +import _default from "fastify-metrics"; +import { prisma } from "../src/db/client"; dotenv.config(); export const server = fastify({ @@ -30,6 +32,8 @@ server.register(cookies, { hook: "onRequest", }); +server.register(_default); + server.register(fastifyStatic, { root: path.join(__dirname, "../static"), prefix: "/api/static/", @@ -43,6 +47,9 @@ server.register(AdminRouter, { prefix: "/api" }); server.get("/ping", (req: any, res: any) => { res.status(200).send(); }); +server.get("/metrics/prisma", async (req: any, res: any) => { + res.send(await prisma.$metrics.prometheus()); +}) server.setErrorHandler((err, req, rep) => { if (err instanceof HttpError) { @@ -60,7 +67,7 @@ server.listen({ host: "0.0.0.0", port: +(process.env.PORT || 8080) }, (err, addr console.error(err); process.exit(1); } - console.log(`Server listening at ${address}`); + console.log(`Server listening at ${address}, environment: ${process.env.ENV}`); }); export default async (req: any, res: any) => { diff --git a/docker-compose.yaml b/docker-compose.db.yaml similarity index 100% rename from docker-compose.yaml rename to docker-compose.db.yaml diff --git a/docker-compose.metrics.yaml b/docker-compose.metrics.yaml new file mode 100644 index 0000000..7c21620 --- /dev/null +++ b/docker-compose.metrics.yaml @@ -0,0 +1,30 @@ +version: "3.7" +services: + + prometheus: + image: prom/prometheus:latest + volumes: + - prometheus_data:/prometheus + - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml + command: + - '--config.file=/etc/prometheus/prometheus.yml' + ports: + - '9090:9090' + + grafana: + image: grafana/grafana:latest + volumes: + - grafana_data:/var/lib/grafana + # - ./grafana/provisioning:/etc/grafana/provisioning + # - ./grafana/config.ini:/etc/grafana/config.ini + # - ./grafana/dashboards:/var/lib/grafana/dashboards + environment: + - GF_SECURITY_ADMIN_PASSWORD=ohno + depends_on: + - prometheus + ports: + - '1234:3000' + +volumes: + prometheus_data: {} + grafana_data: {} \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index a84bf36..1a4c4d4 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -3,6 +3,7 @@ generator client { provider = "prisma-client-js" + previewFeatures = ["metrics"] } datasource db {