Skip to content

Commit 6f91007

Browse files
committed
feat: add new Options, set default port
- CollectorRegistryInstance for set ICollectorRegistry - UseDefaultCollectors for add default metrics
1 parent 82455d1 commit 6f91007

File tree

3 files changed

+86
-17
lines changed

3 files changed

+86
-17
lines changed

src/Prometheus.Client.MetricServer/MetricServer.cs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,54 @@ namespace Prometheus.Client.MetricServer
1616
public class MetricServer : IMetricServer
1717
{
1818
private readonly MetricServerOptions _options;
19-
private readonly ICollectorRegistry _registry;
2019
private IWebHost _host;
2120

2221
/// <summary>
2322
/// Constructor
2423
/// </summary>
24+
/// <param name="registry">Collector registry </param>
2525
/// <param name="options">Http server configuration options</param>
26-
public MetricServer(MetricServerOptions options)
27-
: this(null, options)
26+
[Obsolete("Use options.CollectorRegistryInstance for set CollectorRegistry")]
27+
public MetricServer(ICollectorRegistry registry, MetricServerOptions options)
2828
{
29+
options.CollectorRegistryInstance = registry;
30+
31+
if (string.IsNullOrEmpty(options.MapPath) || !options.MapPath.StartsWith("/"))
32+
throw new ArgumentException($"mapPath '{options.MapPath}' should start with '/'");
33+
34+
_options = options;
35+
36+
_options.CollectorRegistryInstance ??= Metrics.DefaultCollectorRegistry;
37+
38+
if (_options.UseDefaultCollectors)
39+
_options.CollectorRegistryInstance.UseDefaultCollectors();
2940
}
3041

3142
/// <summary>
3243
/// Constructor
3344
/// </summary>
34-
/// <param name="registry">Collector registry </param>
35-
/// <param name="options">Http server configuration options</param>
36-
public MetricServer(ICollectorRegistry registry, MetricServerOptions options)
45+
public MetricServer()
46+
: this(new MetricServerOptions())
47+
{
48+
}
49+
50+
/// <summary>
51+
/// Constructor
52+
/// </summary>
53+
public MetricServer(MetricServerOptions options)
3754
{
3855
if (options == null)
3956
throw new ArgumentNullException(nameof(options));
4057

41-
if (options.Port == 0)
42-
throw new ArgumentException("Port should be specified");
43-
4458
if (string.IsNullOrEmpty(options.MapPath) || !options.MapPath.StartsWith("/"))
4559
throw new ArgumentException($"mapPath '{options.MapPath}' should start with '/'");
4660

47-
_registry = registry ?? Metrics.DefaultCollectorRegistry;
4861
_options = options;
62+
63+
_options.CollectorRegistryInstance ??= Metrics.DefaultCollectorRegistry;
64+
65+
if (_options.UseDefaultCollectors)
66+
_options.CollectorRegistryInstance.UseDefaultCollectors();
4967
}
5068

5169
/// <inheritdoc />
@@ -69,7 +87,7 @@ public void Start()
6987
options.Listen(IPAddress.Any, _options.Port, listenOptions => { listenOptions.UseHttps(_options.Certificate); });
7088
})
7189
.UseUrls($"http{(_options.Certificate != null ? "s" : "")}://{_options.Host}:{_options.Port}")
72-
.ConfigureServices(services => { services.AddSingleton<IStartup>(new Startup(_registry, _options.MapPath)); })
90+
.ConfigureServices(services => { services.AddSingleton<IStartup>(new Startup(_options.CollectorRegistryInstance, _options.MapPath)); })
7391
.UseSetting(WebHostDefaults.ApplicationKey, typeof(Startup).GetTypeInfo().Assembly.FullName)
7492
.Build();
7593

src/Prometheus.Client.MetricServer/MetricServerOptions.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Security.Cryptography.X509Certificates;
2+
using Prometheus.Client.Collectors;
23

34
namespace Prometheus.Client.MetricServer
45
{
@@ -15,7 +16,7 @@ public class MetricServerOptions
1516
/// <summary>
1617
/// Port number to listen
1718
/// </summary>
18-
public int Port { get; set; }
19+
public int Port { get; set; } = 5000;
1920

2021
/// <summary>
2122
/// Endpoint path
@@ -26,5 +27,15 @@ public class MetricServerOptions
2627
/// Https certificate
2728
/// </summary>
2829
public X509Certificate2 Certificate { get; set; }
30+
31+
/// <summary>
32+
/// Collector Registry instance.
33+
/// </summary>
34+
public ICollectorRegistry CollectorRegistryInstance { get; set; }
35+
36+
/// <summary>
37+
/// Use default collectors
38+
/// </summary>
39+
public bool UseDefaultCollectors { get; set; } = false;
2940
}
3041
}

tests/Prometheus.Client.MetricServer.Tests/MetricServerTests.cs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public MetricServerTests(ITestOutputHelper testOutputHelper)
1919
}
2020

2121
[Fact]
22-
public void Start_Stop_IsRunning()
22+
public void Start_Stop_LegacyConstructor_IsRunning()
2323
{
2424
var metricServer = new MetricServer(new CollectorRegistry(), new MetricServerOptions { Port = _port });
2525
metricServer.Start();
@@ -28,6 +28,16 @@ public void Start_Stop_IsRunning()
2828
Assert.False(metricServer.IsRunning);
2929
}
3030

31+
[Fact]
32+
public void Start_Stop_DefaultPort_IsRunning()
33+
{
34+
var metricServer = new MetricServer();
35+
metricServer.Start();
36+
Assert.True(metricServer.IsRunning);
37+
metricServer.Stop();
38+
Assert.False(metricServer.IsRunning);
39+
}
40+
3141
[Fact]
3242
public async Task Base_MapPath()
3343
{
@@ -80,7 +90,6 @@ public async Task MapPath_WithEndSlash()
8090
public void Wrong_MapPath()
8191
{
8292
Assert.Throws<ArgumentException>(() => new MetricServer(
83-
new CollectorRegistry(),
8493
new MetricServerOptions { Port = _port, MapPath = "temp" }));
8594
}
8695

@@ -112,11 +121,11 @@ public async Task MapPath(string mapPath)
112121
}
113122

114123
[Fact]
115-
public async Task FindMetric()
124+
public async Task Find_Metric()
116125
{
117126
var registry = new CollectorRegistry();
118127
var factory = new MetricFactory(registry);
119-
var metricServer = new MetricServer(registry, new MetricServerOptions { Port = _port });
128+
var metricServer = new MetricServer(new MetricServerOptions { Port = _port, CollectorRegistryInstance = registry});
120129

121130
try
122131
{
@@ -144,7 +153,7 @@ public async Task FindMetric()
144153
[Fact]
145154
public async Task Url_NotFound()
146155
{
147-
var metricServer = new MetricServer(new CollectorRegistry(), new MetricServerOptions { Port = _port });
156+
var metricServer = new MetricServer(new MetricServerOptions { Port = _port });
148157
try
149158
{
150159
metricServer.Start();
@@ -165,5 +174,36 @@ public async Task Url_NotFound()
165174
metricServer.Stop();
166175
}
167176
}
177+
178+
[Fact]
179+
public async Task Find_Default_Metric()
180+
{
181+
var registry = new CollectorRegistry();
182+
var metricServer = new MetricServer(new MetricServerOptions
183+
{
184+
Port = _port,
185+
CollectorRegistryInstance = registry,
186+
UseDefaultCollectors = true
187+
});
188+
189+
try
190+
{
191+
metricServer.Start();
192+
193+
using var httpClient = new HttpClient();
194+
string response = await httpClient.GetStringAsync($"http://localhost:{_port}/metrics");
195+
Assert.Contains("dotnet_collection_count_total", response);
196+
Assert.Contains("process_cpu_seconds_total", response);
197+
}
198+
catch (Exception ex)
199+
{
200+
_testOutputHelper.WriteLine(ex.ToString());
201+
throw;
202+
}
203+
finally
204+
{
205+
metricServer.Stop();
206+
}
207+
}
168208
}
169209
}

0 commit comments

Comments
 (0)