Skip to content

Commit ef74128

Browse files
committed
Enable metrics tracking for only for configured hosts
1 parent e8d1f9c commit ef74128

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ hosts:
1212
name: MDposit (local)
1313
description: The main server including all simulations
1414
accession: A0001
15+
metrics: true
1516

1617
mdposit.mddbr.eu:
1718
name: MDposit
@@ -25,6 +26,7 @@ hosts:
2526
description: The main server including all simulations
2627
accession: MD-A00001
2728
global: true
29+
metrics: true
2830

2931
# Deprecated
3032
mdposit-dev.bsc.es:
@@ -59,6 +61,7 @@ hosts:
5961
description: The Covid-19 server
6062
accession: MCV1900193
6163
collection: cv19
64+
metrics: true
6265

6366
model-cns-dev.bsc.es:
6467
name: MoDEL-CNS
@@ -83,6 +86,7 @@ hosts:
8386
description: The MMB federated node server
8487
accession: A0001
8588
optimade: true
89+
metrics: true
8690

8791
mmb.mddbr.eu:
8892
name: MMB node
@@ -95,6 +99,7 @@ hosts:
9599
description: The IRB federated node server
96100
accession: A0001
97101
optimade: true
102+
metrics: true
98103

99104
irb.mddbr.eu:
100105
name: IRB node

src/middlewares/metrics/index.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const yaml = require('yamljs');
33
const client = require('prom-client');
44
const geoip = require('geoip-lite');
55
const rawSpec = yaml.load(`${__dirname}/../../docs/description.yml`);
6-
const { getHost } = require('../../utils/auxiliar-functions');
6+
const { getHost, getConfig } = require('../../utils/auxiliar-functions');
77

88

99
// ---------------------------------------------------------------------------
@@ -311,19 +311,28 @@ function anonymizeIp(ip) {
311311
return 'Unknown';
312312
}
313313

314+
function isMetricsEnabled(request) {
315+
const config = getConfig(request);
316+
return Boolean(config && config.metrics === true);
317+
}
318+
314319
// ---------------------------------------------------------------------------
315320
// Public API
316321
// ---------------------------------------------------------------------------
317322

318323
// Returns an Express middleware that records metrics for every response.
319324
// Pass the parsed OpenAPI spec and the base paths used by the router.
320-
function metricsMiddleware(basePaths = ['/rest/current', '/rest/v1'], debug = true) {
325+
function metricsMiddleware(basePaths = ['/rest/current', '/rest/v1'], debug = false) {
321326
if (!Array.isArray(basePaths)) {
322327
basePaths = ['/rest/current', '/rest/v1'];
323328
}
324329
const matchers = buildMatchers(basePaths);
325330

326331
return function trackMetrics(req, res, next) {
332+
if (!isMetricsEnabled(req)) {
333+
return next();
334+
}
335+
327336
const startMs = Date.now();
328337
// Capture the full path NOW — req.path is mutated by Express after sub-router
329338
// dispatch, but req.originalUrl is always the original unmodified path.
@@ -380,6 +389,10 @@ function metricsMiddleware(basePaths = ['/rest/current', '/rest/v1'], debug = tr
380389
// Express route handler that serves the Prometheus text exposition format.
381390
// Supports PM2 cluster mode by aggregating metrics from all instances.
382391
async function metricsEndpoint(req, res) {
392+
if (!isMetricsEnabled(req)) {
393+
return res.status(404).json({ error: 'Not Found' });
394+
}
395+
383396
try {
384397
let aggregatedRegistry = register;
385398

src/server/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const HOUR = 60 * MINUTE;
2828

2929
const app = express();
3030

31-
// Middleware to track endopoint usage and response times
31+
// Middleware to track endpoint usage and response times
3232
app.use(metricsMiddleware());
3333

3434
// Disable this header

0 commit comments

Comments
 (0)