feat: allow port change of metrics

This commit is contained in:
hyperdefined 2025-06-01 20:02:35 -04:00
parent df2dcbc69d
commit ad7d78a9f1
No known key found for this signature in database
GPG Key ID: 38C93C4835071D4A
3 changed files with 31 additions and 17 deletions

View File

@ -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();

View File

@ -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,

View File

@ -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;