@@ -3,7 +3,7 @@ const yaml = require('yamljs');
33const client = require ( 'prom-client' ) ;
44const geoip = require ( 'geoip-lite' ) ;
55const 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.
382391async 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
0 commit comments