mirror of
https://github.com/imputnet/cobalt.git
synced 2025-06-28 09:28:29 +00:00
feat: allow port change of metrics
This commit is contained in:
parent
df2dcbc69d
commit
ad7d78a9f1
@ -12,7 +12,7 @@ import match from "../processing/match.js";
|
|||||||
|
|
||||||
import { env } from "../config.js";
|
import { env } from "../config.js";
|
||||||
import { extract } from "../processing/url.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 { hashHmac } from "../security/secrets.js";
|
||||||
import { createStore } from "../store/redis-ratelimit.js";
|
import { createStore } from "../store/redis-ratelimit.js";
|
||||||
import { randomizeCiphers } from "../misc/randomize-ciphers.js";
|
import { randomizeCiphers } from "../misc/randomize-ciphers.js";
|
||||||
@ -41,6 +41,8 @@ const corsConfig = env.corsWildcard ? {} : {
|
|||||||
optionsSuccessStatus: 200
|
optionsSuccessStatus: 200
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const metrics = env.metrics && env.metricsPort;
|
||||||
|
|
||||||
const fail = (res, code, context) => {
|
const fail = (res, code, context) => {
|
||||||
const { status, body } = createResponse("error", { code, context });
|
const { status, body } = createResponse("error", { code, context });
|
||||||
res.status(status).json(body);
|
res.status(status).json(body);
|
||||||
@ -113,6 +115,7 @@ export const runAPI = async (express, app, __dirname, isPrimary = true) => {
|
|||||||
|
|
||||||
app.set('trust proxy', ['loopback', 'uniquelocal']);
|
app.set('trust proxy', ['loopback', 'uniquelocal']);
|
||||||
|
|
||||||
|
if (metrics) {
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
const end = httpRequestDuration.startTimer({ method: req.method });
|
const end = httpRequestDuration.startTimer({ method: req.method });
|
||||||
|
|
||||||
@ -123,6 +126,7 @@ export const runAPI = async (express, app, __dirname, isPrimary = true) => {
|
|||||||
|
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
app.use('/', cors({
|
app.use('/', cors({
|
||||||
methods: ['GET', 'POST'],
|
methods: ['GET', 'POST'],
|
||||||
@ -334,11 +338,6 @@ export const runAPI = async (express, app, __dirname, isPrimary = true) => {
|
|||||||
res.status(404).end();
|
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) => {
|
app.get('/*', (req, res) => {
|
||||||
res.redirect('/');
|
res.redirect('/');
|
||||||
})
|
})
|
||||||
@ -388,6 +387,18 @@ export const runAPI = async (express, app, __dirname, isPrimary = true) => {
|
|||||||
if (env.ytSessionServer) {
|
if (env.ytSessionServer) {
|
||||||
YouTubeSession.setup();
|
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();
|
setupTunnelHandler();
|
||||||
|
@ -65,6 +65,9 @@ export const loadEnvs = (env = process.env) => {
|
|||||||
|
|
||||||
enabledServices,
|
enabledServices,
|
||||||
|
|
||||||
|
metrics: env.METRICS_ENABLED,
|
||||||
|
metricsPort: env.METRICS_PORT,
|
||||||
|
|
||||||
customInnertubeClient: env.CUSTOM_INNERTUBE_CLIENT,
|
customInnertubeClient: env.CUSTOM_INNERTUBE_CLIENT,
|
||||||
ytSessionServer: env.YOUTUBE_SESSION_SERVER,
|
ytSessionServer: env.YOUTUBE_SESSION_SERVER,
|
||||||
ytSessionReloadInterval: 300,
|
ytSessionReloadInterval: 300,
|
||||||
|
@ -288,7 +288,7 @@ export default async function({ host, patternMatch, params, isSession }) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
incrementFailed(host);
|
if (env.metrics && env.metricsPort) incrementFailed(host);
|
||||||
|
|
||||||
return createResponse("error", {
|
return createResponse("error", {
|
||||||
code: `error.api.${r.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;
|
let localProcessing = params.localProcessing;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user