@@ -13,10 +13,13 @@ const { getHost } = require('../../utils/auxiliar-functions');
1313const register = new client . Registry ( ) ;
1414client . collectDefaultMetrics ( { register } ) ;
1515
16+ const REQUEST_SOURCE_HEADER = 'x-mddb-request-source' ;
17+ const REQUEST_SOURCE_DEFAULT = 'direct-api' ;
18+
1619const labelNames = [
1720 'host' , 'base_path' , 'method' , 'route' , 'status_code' , 'projectAccessionOrID' , 'UniProtID' ,
1821 'PubChemID' , 'PDBID' , 'InChIKey' , 'ChainSequence' , 'CollectionID' , 'filename' ,
19- 'analysisName' , 'md_num'
22+ 'analysisName' , 'md_num' , 'source'
2023] ;
2124const httpRequestsTotal = new client . Counter ( {
2225 name : 'http_requests_total' ,
@@ -36,7 +39,7 @@ const httpRequestDuration = new client.Histogram({
3639const httpGeoRequestsTotal = new client . Counter ( {
3740 name : 'http_geo_requests_total' ,
3841 help : 'Total number of HTTP requests by geographic location' ,
39- labelNames : [ 'host' , 'ip' , 'country' , 'region' , 'city' ] ,
42+ labelNames : [ 'host' , 'ip' , 'country' , 'region' , 'city' , 'source' ] ,
4043 registers : [ register ] ,
4144} ) ;
4245
@@ -321,6 +324,24 @@ function anonymizeIp(ip) {
321324 return 'Unknown' ;
322325}
323326
327+ function getRequestSource ( req ) {
328+ const rawSource = req . headers && req . headers [ REQUEST_SOURCE_HEADER ] ;
329+ const source = Array . isArray ( rawSource ) ? rawSource [ 0 ] : rawSource ;
330+
331+ if ( ! source || ! String ( source ) . trim ( ) ) {
332+ return REQUEST_SOURCE_DEFAULT ;
333+ }
334+
335+ // Keep labels low-cardinality and Prometheus-safe.
336+ const normalized = String ( source )
337+ . trim ( )
338+ . toLowerCase ( )
339+ . replace ( / [ ^ a - z 0 - 9 . _ - ] / g, '_' )
340+ . slice ( 0 , 64 ) ;
341+
342+ return normalized || REQUEST_SOURCE_DEFAULT ;
343+ }
344+
324345function isMetricsEnabled ( ) {
325346 return process . env . NODE_ENV === 'development' ;
326347}
@@ -353,7 +374,9 @@ function metricsMiddleware(basePaths = ['/rest/current', '/rest/v1'], debug = fa
353374
354375 // IP and Geolocation logic
355376 const ip = getClientIp ( req ) ;
377+ const requestSource = getRequestSource ( req ) ;
356378 if ( debug ) console . log ( 'Client IP' , ip ) ;
379+ if ( debug ) console . log ( 'Request source' , requestSource ) ;
357380 const geo = geoip . lookup ( ip ) ;
358381 if ( debug ) console . log ( 'Geo Data' , geo ) ;
359382
@@ -376,6 +399,7 @@ function metricsMiddleware(basePaths = ['/rest/current', '/rest/v1'], debug = fa
376399 method : req . method ,
377400 route,
378401 status_code : String ( res . statusCode ) ,
402+ source : requestSource ,
379403 ...params
380404 } ;
381405 if ( debug ) console . log ( 'Labels:' , labels ) ;
@@ -387,7 +411,8 @@ function metricsMiddleware(basePaths = ['/rest/current', '/rest/v1'], debug = fa
387411 ip : req . geoStats . anonIp ,
388412 country : req . geoStats . country ,
389413 region : req . geoStats . region ,
390- city : req . geoStats . city
414+ city : req . geoStats . city ,
415+ source : requestSource ,
391416 } ) ;
392417 } ) ;
393418
0 commit comments