Skip to content

Commit 048186c

Browse files
authored
Merge pull request #82 from prom-client-net/chore/drop-netstandard
chore!: drop support for netstandard2.*
2 parents 2bc4f83 + d7733b1 commit 048186c

File tree

6 files changed

+375
-389
lines changed

6 files changed

+375
-389
lines changed

src/IMetricServer.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
namespace Prometheus.Client.MetricServer
1+
namespace Prometheus.Client.MetricServer;
2+
3+
/// <summary>
4+
/// MetricSever
5+
/// </summary>
6+
public interface IMetricServer
27
{
38
/// <summary>
4-
/// MetricSever
9+
/// Server is Running?
510
/// </summary>
6-
public interface IMetricServer
7-
{
8-
/// <summary>
9-
/// Server is Running?
10-
/// </summary>
11-
bool IsRunning { get; }
11+
bool IsRunning { get; }
1212

13-
/// <summary>
14-
/// Start server
15-
/// </summary>
16-
void Start();
13+
/// <summary>
14+
/// Start server
15+
/// </summary>
16+
void Start();
1717

18-
/// <summary>
19-
/// Stop server
20-
/// </summary>
21-
void Stop();
22-
}
18+
/// <summary>
19+
/// Stop server
20+
/// </summary>
21+
void Stop();
2322
}

src/MetricServer.cs

Lines changed: 92 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -7,127 +7,126 @@
77
using Microsoft.Extensions.DependencyInjection;
88
using Prometheus.Client.Collectors;
99

10-
namespace Prometheus.Client.MetricServer
10+
namespace Prometheus.Client.MetricServer;
11+
12+
/// <inheritdoc cref="IMetricServer" />
13+
/// <summary>
14+
/// MetricSever based of Kestrel
15+
/// </summary>
16+
public class MetricServer : IMetricServer
1117
{
12-
/// <inheritdoc cref="IMetricServer" />
18+
private readonly MetricServerOptions _options;
19+
private IWebHost _host;
20+
1321
/// <summary>
14-
/// MetricSever based of Kestrel
22+
/// Constructor
1523
/// </summary>
16-
public class MetricServer : IMetricServer
24+
public MetricServer()
25+
: this(new MetricServerOptions())
1726
{
18-
private readonly MetricServerOptions _options;
19-
private IWebHost _host;
20-
21-
/// <summary>
22-
/// Constructor
23-
/// </summary>
24-
public MetricServer()
25-
: this(new MetricServerOptions())
26-
{
27-
}
27+
}
2828

29-
/// <summary>
30-
/// Constructor
31-
/// </summary>
32-
public MetricServer(MetricServerOptions options)
33-
{
34-
if (options == null)
35-
throw new ArgumentNullException(nameof(options));
29+
/// <summary>
30+
/// Constructor
31+
/// </summary>
32+
public MetricServer(MetricServerOptions options)
33+
{
34+
if (options == null)
35+
throw new ArgumentNullException(nameof(options));
3636

37-
if (string.IsNullOrEmpty(options.MapPath) || !options.MapPath.StartsWith("/"))
38-
throw new ArgumentException($"mapPath '{options.MapPath}' should start with '/'");
37+
if (string.IsNullOrEmpty(options.MapPath) || !options.MapPath.StartsWith("/"))
38+
throw new ArgumentException($"mapPath '{options.MapPath}' should start with '/'");
3939

40-
_options = options;
40+
_options = options;
4141

42-
_options.CollectorRegistryInstance ??= Metrics.DefaultCollectorRegistry;
42+
_options.CollectorRegistryInstance ??= Metrics.DefaultCollectorRegistry;
4343

44-
if (_options.UseDefaultCollectors)
45-
{
44+
if (_options.UseDefaultCollectors)
45+
{
4646
#pragma warning disable CS0618
47-
if (options.AddLegacyMetrics)
48-
options.CollectorRegistryInstance.UseDefaultCollectors(options.MetricPrefixName, options.AddLegacyMetrics);
49-
else
50-
options.CollectorRegistryInstance.UseDefaultCollectors(options.MetricPrefixName);
47+
if (options.AddLegacyMetrics)
48+
options.CollectorRegistryInstance.UseDefaultCollectors(options.MetricPrefixName, options.AddLegacyMetrics);
49+
else
50+
options.CollectorRegistryInstance.UseDefaultCollectors(options.MetricPrefixName);
5151
#pragma warning restore CS0618
52-
}
5352
}
53+
}
5454

55-
/// <inheritdoc />
56-
public bool IsRunning => _host != null;
55+
/// <inheritdoc />
56+
public bool IsRunning => _host != null;
5757

58-
/// <inheritdoc />
59-
public void Start()
60-
{
61-
if (IsRunning)
62-
return;
58+
/// <inheritdoc />
59+
public void Start()
60+
{
61+
if (IsRunning)
62+
return;
6363

64-
var configBuilder = new ConfigurationBuilder();
65-
configBuilder.Properties["parent"] = this;
66-
var config = configBuilder.Build();
64+
var configBuilder = new ConfigurationBuilder();
65+
configBuilder.Properties["parent"] = this;
66+
var config = configBuilder.Build();
6767

68-
_host = new WebHostBuilder()
69-
.UseConfiguration(config)
70-
.UseKestrel(options =>
71-
{
72-
if (_options.Certificate != null)
73-
options.Listen(IPAddress.Any, _options.Port, listenOptions => { listenOptions.UseHttps(_options.Certificate); });
74-
})
75-
.UseUrls($"http{(_options.Certificate != null ? "s" : "")}://{_options.Host}:{_options.Port}")
76-
.ConfigureServices(services => { services.AddSingleton<IStartup>(new Startup(_options)); })
77-
.UseSetting(WebHostDefaults.ApplicationKey, typeof(Startup).GetTypeInfo().Assembly.FullName)
78-
.Build();
79-
80-
_host.Start();
81-
}
68+
_host = new WebHostBuilder()
69+
.UseConfiguration(config)
70+
.UseKestrel(options =>
71+
{
72+
if (_options.Certificate != null)
73+
options.Listen(IPAddress.Any, _options.Port, listenOptions => { listenOptions.UseHttps(_options.Certificate); });
74+
})
75+
.UseUrls($"http{(_options.Certificate != null ? "s" : "")}://{_options.Host}:{_options.Port}")
76+
.ConfigureServices(services => { services.AddSingleton<IStartup>(new Startup(_options)); })
77+
.UseSetting(WebHostDefaults.ApplicationKey, typeof(Startup).GetTypeInfo().Assembly.FullName)
78+
.Build();
79+
80+
_host.Start();
81+
}
8282

83-
/// <inheritdoc />
84-
public void Stop()
85-
{
86-
if (!IsRunning)
87-
return;
83+
/// <inheritdoc />
84+
public void Stop()
85+
{
86+
if (!IsRunning)
87+
return;
8888

89-
_host.Dispose();
90-
_host = null;
91-
}
89+
_host.Dispose();
90+
_host = null;
91+
}
9292

93-
internal class Startup : IStartup
94-
{
95-
private readonly MetricServerOptions _options;
93+
internal class Startup : IStartup
94+
{
95+
private readonly MetricServerOptions _options;
9696

97-
public Startup(MetricServerOptions options)
98-
{
99-
_options = options;
97+
public Startup(MetricServerOptions options)
98+
{
99+
_options = options;
100100

101-
var builder = new ConfigurationBuilder();
102-
Configuration = builder.Build();
103-
}
101+
var builder = new ConfigurationBuilder();
102+
Configuration = builder.Build();
103+
}
104104

105-
public IConfigurationRoot Configuration { get; }
105+
public IConfigurationRoot Configuration { get; }
106106

107-
public IServiceProvider ConfigureServices(IServiceCollection services)
108-
{
109-
return services.BuildServiceProvider();
110-
}
107+
public IServiceProvider ConfigureServices(IServiceCollection services)
108+
{
109+
return services.BuildServiceProvider();
110+
}
111111

112-
public void Configure(IApplicationBuilder app)
113-
{
114-
var contentType = "text/plain; version=0.0.4";
112+
public void Configure(IApplicationBuilder app)
113+
{
114+
var contentType = "text/plain; version=0.0.4";
115115

116-
if (_options.ResponseEncoding != null)
117-
contentType += $"; charset={_options.ResponseEncoding.BodyName}";
116+
if (_options.ResponseEncoding != null)
117+
contentType += $"; charset={_options.ResponseEncoding.BodyName}";
118118

119-
app.Map(_options.MapPath, coreapp =>
119+
app.Map(_options.MapPath, coreapp =>
120+
{
121+
coreapp.Run(async context =>
120122
{
121-
coreapp.Run(async context =>
122-
{
123-
var response = context.Response;
124-
response.ContentType = contentType;
125-
126-
using var outputStream = response.Body;
127-
await ScrapeHandler.ProcessAsync(_options.CollectorRegistryInstance, outputStream);
128-
});
123+
var response = context.Response;
124+
response.ContentType = contentType;
125+
126+
await using var outputStream = response.Body;
127+
await ScrapeHandler.ProcessAsync(_options.CollectorRegistryInstance, outputStream);
129128
});
130-
}
129+
});
131130
}
132131
}
133132
}

src/MetricServerOptions.cs

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,65 @@
33
using System.Text;
44
using Prometheus.Client.Collectors;
55

6-
namespace Prometheus.Client.MetricServer
6+
namespace Prometheus.Client.MetricServer;
7+
8+
/// <summary>
9+
/// Metric Server Options
10+
/// </summary>
11+
public class MetricServerOptions
712
{
813
/// <summary>
9-
/// Metric Server Options
14+
/// Host name
1015
/// </summary>
11-
public class MetricServerOptions
12-
{
13-
/// <summary>
14-
/// Host name
15-
/// </summary>
16-
public string Host { get; set; } = "*";
16+
public string Host { get; set; } = "*";
1717

18-
/// <summary>
19-
/// Port number to listen
20-
/// </summary>
21-
public int Port { get; set; } = 5000;
18+
/// <summary>
19+
/// Port number to listen
20+
/// </summary>
21+
public int Port { get; set; } = 5000;
2222

23-
/// <summary>
24-
/// Endpoint path
25-
/// </summary>
26-
public string MapPath { get; set; } = "/metrics";
23+
/// <summary>
24+
/// Endpoint path
25+
/// </summary>
26+
public string MapPath { get; set; } = "/metrics";
2727

28-
/// <summary>
29-
/// Https certificate
30-
/// </summary>
31-
public X509Certificate2 Certificate { get; set; }
28+
/// <summary>
29+
/// Https certificate
30+
/// </summary>
31+
public X509Certificate2 Certificate { get; set; }
3232

33-
/// <summary>
34-
/// Collector Registry instance
35-
/// </summary>
36-
public ICollectorRegistry CollectorRegistryInstance { get; set; }
33+
/// <summary>
34+
/// Collector Registry instance
35+
/// </summary>
36+
public ICollectorRegistry CollectorRegistryInstance { get; set; }
3737

38-
/// <summary>
39-
/// Use default collectors(dotnet and process stats)
40-
/// </summary>
41-
public bool UseDefaultCollectors { get; set; } = false;
38+
/// <summary>
39+
/// Use default collectors(dotnet and process stats)
40+
/// </summary>
41+
public bool UseDefaultCollectors { get; set; } = false;
4242

43-
/// <summary>
44-
/// Charset of text response.
45-
/// </summary>
46-
public Encoding ResponseEncoding { get; set; }
43+
/// <summary>
44+
/// Charset of text response.
45+
/// </summary>
46+
public Encoding ResponseEncoding { get; set; }
4747

48-
/// <summary>
49-
/// Metric prefix for Default collectors
50-
/// </summary>
51-
public string MetricPrefixName { get; set; } = "";
48+
/// <summary>
49+
/// Metric prefix for Default collectors
50+
/// </summary>
51+
public string MetricPrefixName { get; set; } = "";
5252

53-
/// <summary>
54-
/// Add legacy metrics to Default collectors
55-
/// </summary>
56-
/// <remarks>
57-
/// Some metrics renamed since v5, <c>AddLegacyMetrics</c> will add old and new name<br />
58-
/// <para>
59-
/// process_virtual_bytes -> process_virtual_memory_bytes<br />
60-
/// process_private_bytes -> process_private_memory_bytes<br />
61-
/// process_working_set -> process_working_set_bytes<br />
62-
/// dotnet_totalmemory -> dotnet_total_memory_bytes
63-
/// </para>
64-
/// </remarks>
65-
[Obsolete("'AddLegacyMetrics' will be removed in future versions")]
66-
public bool AddLegacyMetrics { get; set; }
67-
}
53+
/// <summary>
54+
/// Add legacy metrics to Default collectors
55+
/// </summary>
56+
/// <remarks>
57+
/// Some metrics renamed since v5, <c>AddLegacyMetrics</c> will add old and new name<br />
58+
/// <para>
59+
/// process_virtual_bytes -> process_virtual_memory_bytes<br />
60+
/// process_private_bytes -> process_private_memory_bytes<br />
61+
/// process_working_set -> process_working_set_bytes<br />
62+
/// dotnet_totalmemory -> dotnet_total_memory_bytes
63+
/// </para>
64+
/// </remarks>
65+
[Obsolete("'AddLegacyMetrics' will be removed in future versions")]
66+
public bool AddLegacyMetrics { get; set; }
6867
}

0 commit comments

Comments
 (0)