1- #if NET45
2-
1+ #if ! NETSTANDARD13
32using System ;
4- using System . Collections . Generic ;
53using System . Diagnostics ;
64using System . Net ;
75using System . Threading ;
@@ -13,92 +11,44 @@ namespace Prometheus.Client.MetricServer
1311 /// <summary>
1412 /// MetricSever based of HttpListener
1513 /// </summary>
16- public class MetricServer : BaseMetricServer , IMetricServer
14+ public class MetricServer : IMetricServer
1715 {
16+ private const string _contentType = "text/plain; version=0.0.4" ;
17+
18+ private readonly ICollectorRegistry _registry ;
1819 private readonly HttpListener _httpListener = new HttpListener ( ) ;
1920 private readonly string _mapPath ;
21+ private readonly CancellationTokenSource _cancellation = new CancellationTokenSource ( ) ;
2022
21- /// <inheritdoc />
22- public MetricServer ( int port )
23- : this ( port , Defaults . UseDefaultCollectors )
23+ /// <summary>
24+ /// Constructor
25+ /// </summary>
26+ /// <param name="registry">Collector registry </param>
27+ /// <param name="options">Http server configuration options</param>
28+ public MetricServer ( ICollectorRegistry registry , MetricServerOptions options )
2429 {
25- }
30+ if ( options == null )
31+ throw new ArgumentNullException ( nameof ( options ) ) ;
2632
27- /// <inheritdoc />
28- public MetricServer ( int port , bool useDefaultCollectors )
29- : this ( Defaults . Host , port , false , useDefaultCollectors )
30- {
31- }
33+ if ( options . Port == 0 )
34+ throw new ArgumentException ( "Port should be specified" ) ;
3235
33- /// <inheritdoc />
34- public MetricServer ( string host , int port , bool useHttps )
35- : this ( host , port , useHttps , Defaults . UseDefaultCollectors )
36- {
37- }
36+ if ( string . IsNullOrEmpty ( options . MapPath ) || ! options . MapPath . StartsWith ( "/" ) )
37+ throw new ArgumentException ( $ "mapPath '{ options . MapPath } ' should start with '/'") ;
3838
39- /// <inheritdoc />
40- public MetricServer ( string host , int port , bool useHttps , bool useDefaultCollectors )
41- : this ( host , port , Defaults . MapPath , useHttps , useDefaultCollectors )
42- {
43- }
39+ _registry = registry ?? Metrics . DefaultCollectorRegistry ;
4440
45- /// <inheritdoc />
46- public MetricServer ( string host , int port , string url , bool useHttps )
47- : this ( host , port , url , useHttps , Defaults . UseDefaultCollectors )
48- {
49- }
50-
51- /// <inheritdoc />
52- public MetricServer ( string host , int port , string url , bool useHttps , bool useDefaultCollectors )
53- : this ( host , port , url , null , new List < IOnDemandCollector > ( ) , useHttps , useDefaultCollectors )
54- {
55- }
56- /// <inheritdoc />
57- public MetricServer ( string host , int port , string url , ICollectorRegistry registry )
58- : this ( host , port , url , registry , Defaults . UseDefaultCollectors )
59- {
60- }
61-
62- /// <inheritdoc />
63- public MetricServer ( string host , int port , string url , ICollectorRegistry registry , bool useHttps )
64- : this ( host , port , url , registry , useHttps , Defaults . UseDefaultCollectors )
65- {
66- }
67-
68- /// <inheritdoc />
69- public MetricServer ( string host , int port , string url , ICollectorRegistry registry , bool useHttps , bool useDefaultCollectors )
70- : this ( host , port , url , registry , new List < IOnDemandCollector > ( ) , useHttps , useDefaultCollectors )
71- {
72- }
73-
74- /// <inheritdoc />
75- /// <summary>
76- /// Constructor
77- /// </summary>
78- /// <param name="host">Host</param>
79- /// <param name="port">Port</param>
80- /// <param name="mapPath">Map Path: should start with '/'</param>
81- /// <param name="registry">Collector registry</param>
82- /// <param name="collectors">IOnDemandCollectors</param>
83- /// <param name="useHttps">use Https</param>
84- /// <param name="useDefaultCollectors">Use default collectors</param>
85- public MetricServer ( string host , int port , string mapPath , ICollectorRegistry registry , List < IOnDemandCollector > collectors , bool useHttps ,
86- bool useDefaultCollectors )
87- : base ( registry , collectors , useDefaultCollectors )
88- {
89- if ( ! mapPath . StartsWith ( "/" ) )
90- throw new ArgumentException ( $ "mapPath '{ mapPath } ' should start with '/'") ;
91-
92- _mapPath = mapPath . EndsWith ( "/" ) ? mapPath : mapPath + "/" ;
93- _httpListener . Prefixes . Add ( $ "http{ ( useHttps ? "s" : "" ) } ://{ host } :{ port } /") ;
41+ _mapPath = options . MapPath . EndsWith ( "/" ) ? options . MapPath : options . MapPath + "/" ;
42+ _httpListener . Prefixes . Add ( $ "http{ ( options . UseHttps ? "s" : "" ) } ://{ options . Host } :{ options . Port } /") ;
9443 }
9544
9645 /// <inheritdoc />
9746 public void Start ( )
9847 {
99- if ( IsRunning )
48+ if ( _httpListener . IsListening )
10049 return ;
10150
51+ _httpListener . Start ( ) ;
10252 var bgThread = new Thread ( StartListen )
10353 {
10454 IsBackground = true ,
@@ -108,26 +58,32 @@ public void Start()
10858 }
10959
11060 /// <inheritdoc />
111- public bool IsRunning { get ; private set ; }
61+ public bool IsRunning => _httpListener . IsListening ;
11262
11363 /// <inheritdoc />
11464 public void Stop ( )
11565 {
116- IsRunning = false ;
66+ _cancellation . Cancel ( ) ;
11767 _httpListener . Stop ( ) ;
11868 _httpListener . Close ( ) ;
11969 }
12070
12171 private void StartListen ( )
12272 {
123- _httpListener . Start ( ) ;
124- IsRunning = true ;
73+ var cancel = _cancellation . Token ;
12574
126- while ( IsRunning )
75+ while ( ! _cancellation . IsCancellationRequested )
12776 {
12877 try
12978 {
130- var context = _httpListener . GetContext ( ) ;
79+ var getContext = _httpListener . GetContextAsync ( ) ;
80+ getContext . Wait ( cancel ) ;
81+ if ( cancel . IsCancellationRequested )
82+ {
83+ return ;
84+ }
85+
86+ var context = getContext . Result ;
13187 var request = context . Request ;
13288 var response = context . Response ;
13389
@@ -136,10 +92,10 @@ private void StartListen()
13692 if ( rawUrl == _mapPath )
13793 {
13894 response . StatusCode = 200 ;
139- response . ContentType = Defaults . ContentType ;
95+ response . ContentType = _contentType ;
14096 using ( var outputStream = response . OutputStream )
14197 {
142- ScrapeHandler . Process ( Registry , outputStream ) ;
98+ ScrapeHandler . ProcessAsync ( _registry , outputStream ) . GetAwaiter ( ) . GetResult ( ) ;
14399 }
144100 }
145101 else
0 commit comments