From ad7d78a9f16ac499de1d35dd93d67854ca0a006f Mon Sep 17 00:00:00 2001 From: hyperdefined Date: Sun, 1 Jun 2025 20:02:35 -0400 Subject: [PATCH] feat: allow port change of metrics --- api/src/core/api.js | 41 +++++++++++++++++++++++-------------- api/src/core/env.js | 3 +++ api/src/processing/match.js | 4 ++-- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/api/src/core/api.js b/api/src/core/api.js index 27173a5a..99360e73 100644 --- a/api/src/core/api.js +++ b/api/src/core/api.js @@ -12,7 +12,7 @@ import match from "../processing/match.js"; import { env } from "../config.js"; import { extract } from "../processing/url.js"; -import { Bright, Cyan } from "../misc/console-text.js"; +import { Bright, Cyan, Green } from "../misc/console-text.js"; import { hashHmac } from "../security/secrets.js"; import { createStore } from "../store/redis-ratelimit.js"; import { randomizeCiphers } from "../misc/randomize-ciphers.js"; @@ -41,6 +41,8 @@ const corsConfig = env.corsWildcard ? {} : { optionsSuccessStatus: 200 } +const metrics = env.metrics && env.metricsPort; + const fail = (res, code, context) => { const { status, body } = createResponse("error", { code, context }); res.status(status).json(body); @@ -113,16 +115,18 @@ export const runAPI = async (express, app, __dirname, isPrimary = true) => { app.set('trust proxy', ['loopback', 'uniquelocal']); - app.use((req, res, next) => { - const end = httpRequestDuration.startTimer({ method: req.method }); - - res.on('finish', () => { - httpRequests.labels(req.method, res.statusCode.toString()).inc(); - end(); + if (metrics) { + app.use((req, res, next) => { + const end = httpRequestDuration.startTimer({ method: req.method }); + + res.on('finish', () => { + httpRequests.labels(req.method, res.statusCode.toString()).inc(); + end(); + }); + + next(); }); - - next(); - }); + } app.use('/', cors({ methods: ['GET', 'POST'], @@ -334,11 +338,6 @@ export const runAPI = async (express, app, __dirname, isPrimary = true) => { res.status(404).end(); }) - app.get('/metrics', async (req, res) => { - res.set('Content-Type', registry.contentType); - res.send(await registry.metrics()); - }); - app.get('/*', (req, res) => { res.redirect('/'); }) @@ -388,6 +387,18 @@ export const runAPI = async (express, app, __dirname, isPrimary = true) => { if (env.ytSessionServer) { YouTubeSession.setup(); } + + if (metrics) { + const metricsApp = express(); + + metricsApp.get('/metrics', async (req, res) => { + res.set('Content-Type', registry.contentType); + res.send(await registry.metrics()); + }); + metricsApp.listen(env.metricsPort, () => { + console.log(`${Green('[✓]')} prometheus metrics running on 127.0.0.1:${env.metricsPort}/metrics`); + }); + } }); setupTunnelHandler(); diff --git a/api/src/core/env.js b/api/src/core/env.js index 35c892f9..06458af7 100644 --- a/api/src/core/env.js +++ b/api/src/core/env.js @@ -65,6 +65,9 @@ export const loadEnvs = (env = process.env) => { enabledServices, + metrics: env.METRICS_ENABLED, + metricsPort: env.METRICS_PORT, + customInnertubeClient: env.CUSTOM_INNERTUBE_CLIENT, ytSessionServer: env.YOUTUBE_SESSION_SERVER, ytSessionReloadInterval: 300, diff --git a/api/src/processing/match.js b/api/src/processing/match.js index 63005073..15812c63 100644 --- a/api/src/processing/match.js +++ b/api/src/processing/match.js @@ -288,7 +288,7 @@ export default async function({ host, patternMatch, params, isSession }) { break; } - incrementFailed(host); + if (env.metrics && env.metricsPort) incrementFailed(host); return createResponse("error", { code: `error.api.${r.error}`, @@ -296,7 +296,7 @@ export default async function({ host, patternMatch, params, isSession }) { }) } - incrementSuccessful(host); + if (env.metrics && env.metricsPort) incrementSuccessful(host); let localProcessing = params.localProcessing;