|
7 | 7 | using Microsoft.Extensions.DependencyInjection; |
8 | 8 | using Prometheus.Client.Collectors; |
9 | 9 |
|
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 |
11 | 17 | { |
12 | | - /// <inheritdoc cref="IMetricServer" /> |
| 18 | + private readonly MetricServerOptions _options; |
| 19 | + private IWebHost _host; |
| 20 | + |
13 | 21 | /// <summary> |
14 | | - /// MetricSever based of Kestrel |
| 22 | + /// Constructor |
15 | 23 | /// </summary> |
16 | | - public class MetricServer : IMetricServer |
| 24 | + public MetricServer() |
| 25 | + : this(new MetricServerOptions()) |
17 | 26 | { |
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 | + } |
28 | 28 |
|
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)); |
36 | 36 |
|
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 '/'"); |
39 | 39 |
|
40 | | - _options = options; |
| 40 | + _options = options; |
41 | 41 |
|
42 | | - _options.CollectorRegistryInstance ??= Metrics.DefaultCollectorRegistry; |
| 42 | + _options.CollectorRegistryInstance ??= Metrics.DefaultCollectorRegistry; |
43 | 43 |
|
44 | | - if (_options.UseDefaultCollectors) |
45 | | - { |
| 44 | + if (_options.UseDefaultCollectors) |
| 45 | + { |
46 | 46 | #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); |
51 | 51 | #pragma warning restore CS0618 |
52 | | - } |
53 | 52 | } |
| 53 | + } |
54 | 54 |
|
55 | | - /// <inheritdoc /> |
56 | | - public bool IsRunning => _host != null; |
| 55 | + /// <inheritdoc /> |
| 56 | + public bool IsRunning => _host != null; |
57 | 57 |
|
58 | | - /// <inheritdoc /> |
59 | | - public void Start() |
60 | | - { |
61 | | - if (IsRunning) |
62 | | - return; |
| 58 | + /// <inheritdoc /> |
| 59 | + public void Start() |
| 60 | + { |
| 61 | + if (IsRunning) |
| 62 | + return; |
63 | 63 |
|
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(); |
67 | 67 |
|
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 | + } |
82 | 82 |
|
83 | | - /// <inheritdoc /> |
84 | | - public void Stop() |
85 | | - { |
86 | | - if (!IsRunning) |
87 | | - return; |
| 83 | + /// <inheritdoc /> |
| 84 | + public void Stop() |
| 85 | + { |
| 86 | + if (!IsRunning) |
| 87 | + return; |
88 | 88 |
|
89 | | - _host.Dispose(); |
90 | | - _host = null; |
91 | | - } |
| 89 | + _host.Dispose(); |
| 90 | + _host = null; |
| 91 | + } |
92 | 92 |
|
93 | | - internal class Startup : IStartup |
94 | | - { |
95 | | - private readonly MetricServerOptions _options; |
| 93 | + internal class Startup : IStartup |
| 94 | + { |
| 95 | + private readonly MetricServerOptions _options; |
96 | 96 |
|
97 | | - public Startup(MetricServerOptions options) |
98 | | - { |
99 | | - _options = options; |
| 97 | + public Startup(MetricServerOptions options) |
| 98 | + { |
| 99 | + _options = options; |
100 | 100 |
|
101 | | - var builder = new ConfigurationBuilder(); |
102 | | - Configuration = builder.Build(); |
103 | | - } |
| 101 | + var builder = new ConfigurationBuilder(); |
| 102 | + Configuration = builder.Build(); |
| 103 | + } |
104 | 104 |
|
105 | | - public IConfigurationRoot Configuration { get; } |
| 105 | + public IConfigurationRoot Configuration { get; } |
106 | 106 |
|
107 | | - public IServiceProvider ConfigureServices(IServiceCollection services) |
108 | | - { |
109 | | - return services.BuildServiceProvider(); |
110 | | - } |
| 107 | + public IServiceProvider ConfigureServices(IServiceCollection services) |
| 108 | + { |
| 109 | + return services.BuildServiceProvider(); |
| 110 | + } |
111 | 111 |
|
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"; |
115 | 115 |
|
116 | | - if (_options.ResponseEncoding != null) |
117 | | - contentType += $"; charset={_options.ResponseEncoding.BodyName}"; |
| 116 | + if (_options.ResponseEncoding != null) |
| 117 | + contentType += $"; charset={_options.ResponseEncoding.BodyName}"; |
118 | 118 |
|
119 | | - app.Map(_options.MapPath, coreapp => |
| 119 | + app.Map(_options.MapPath, coreapp => |
| 120 | + { |
| 121 | + coreapp.Run(async context => |
120 | 122 | { |
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); |
129 | 128 | }); |
130 | | - } |
| 129 | + }); |
131 | 130 | } |
132 | 131 | } |
133 | 132 | } |
0 commit comments