@@ -16,7 +16,7 @@ namespace Prometheus.Client.MetricServer
1616 public class MetricServer : BaseMetricServer , IMetricServer
1717 {
1818 private readonly HttpListener _httpListener = new HttpListener ( ) ;
19-
19+ private readonly string _mapPath ;
2020
2121 /// <inheritdoc />
2222 public MetricServer ( int port )
@@ -89,7 +89,8 @@ public MetricServer(string host, int port, string mapPath, ICollectorRegistry re
8989 if ( ! mapPath . StartsWith ( "/" ) )
9090 throw new ArgumentException ( $ "mapPath '{ mapPath } ' should start with '/'") ;
9191
92- _httpListener . Prefixes . Add ( $ "http{ ( useHttps ? "s" : "" ) } ://{ host } :{ port } { mapPath . TrimEnd ( '/' ) } /") ;
92+ _mapPath = mapPath . TrimEnd ( '/' ) ;
93+ _httpListener . Prefixes . Add ( $ "http{ ( useHttps ? "s" : "" ) } ://{ host } :{ port } /") ;
9394 }
9495
9596 /// <inheritdoc />
@@ -127,12 +128,20 @@ private void StartListen()
127128 try
128129 {
129130 var context = _httpListener . GetContext ( ) ;
131+ var request = context . Request ;
130132 var response = context . Response ;
131- response . ContentType = Defaults . ContentType ;
132133
133- using ( var outputStream = response . OutputStream )
134+ if ( request . RawUrl . TrimEnd ( '/' ) == _mapPath )
135+ {
136+ response . ContentType = Defaults . ContentType ;
137+ using ( var outputStream = response . OutputStream )
138+ {
139+ ScrapeHandler . Process ( Registry , outputStream ) ;
140+ }
141+ }
142+ else
134143 {
135- ScrapeHandler . Process ( Registry , outputStream ) ;
144+ response . StatusCode = 404 ;
136145 }
137146 }
138147
0 commit comments