Skip to content

Commit 5846dc9

Browse files
committed
Use DI container to resolve CollectorREgistry
1 parent bee44cf commit 5846dc9

File tree

2 files changed

+11
-23
lines changed

2 files changed

+11
-23
lines changed

src/Prometheus.Client.MetricServer/MetricServer.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
2-
#if !NETSTANDARD13
32
using System.Net;
4-
#endif
53
using System.Reflection;
64
using Microsoft.AspNetCore.Builder;
75
using Microsoft.AspNetCore.Hosting;
@@ -46,7 +44,7 @@ public MetricServer(ICollectorRegistry registry, MetricServerOptions options)
4644
if (string.IsNullOrEmpty(options.MapPath) || !options.MapPath.StartsWith("/"))
4745
throw new ArgumentException($"mapPath '{options.MapPath}' should start with '/'");
4846

49-
_registry = registry ?? Metrics.DefaultCollectorRegistry;
47+
_registry = registry;
5048
_options = options;
5149
}
5250

@@ -68,15 +66,8 @@ public void Start()
6866
.UseConfiguration(config)
6967
.UseKestrel(options =>
7068
{
71-
#if NETSTANDARD13
72-
if (_options.Certificate != null)
73-
options.UseHttps(_options.Certificate);
74-
#endif
75-
76-
#if !NETSTANDARD13
7769
if (_options.Certificate != null)
7870
options.Listen(IPAddress.Any, _options.Port, listenOptions => { listenOptions.UseHttps(_options.Certificate); });
79-
#endif
8071
})
8172
.UseUrls($"http{(_options.Certificate != null ? "s" : "")}://{_options.Host}:{_options.Port}")
8273
.ConfigureServices(services => { services.AddSingleton<IStartup>(new Startup(_registry, _options.MapPath)); })
@@ -100,7 +91,7 @@ internal class Startup : IStartup
10091
{
10192
private const string _contentType = "text/plain; version=0.0.4";
10293

103-
private readonly ICollectorRegistry _registry;
94+
private ICollectorRegistry _registry;
10495
private readonly string _mapPath;
10596

10697
public Startup(ICollectorRegistry registry, string mapPath)
@@ -121,6 +112,9 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
121112

122113
public void Configure(IApplicationBuilder app)
123114
{
115+
_registry ??= (ICollectorRegistry)app.ApplicationServices.GetService(typeof(ICollectorRegistry))
116+
?? Metrics.DefaultCollectorRegistry;
117+
124118
app.Map(_mapPath, coreapp =>
125119
{
126120
coreapp.Run(async context =>

src/Prometheus.Client.MetricServer/Prometheus.Client.MetricServer.csproj

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<Description>MetricServer for the Prometheus.Client</Description>
4-
<Copyright>2019 © Sergey Kuznetsov</Copyright>
4+
<Copyright>2020 © Serge K, Oleksandr Poliakov</Copyright>
55
<AssemblyTitle>Prometheus.Client.MetricServer</AssemblyTitle>
6-
<VersionPrefix>3.1.0</VersionPrefix>
7-
<Authors>Sergey Kuznetsov, Oleksandr Poliakov</Authors>
8-
<TargetFrameworks>netstandard1.3;netstandard2.0;netcoreapp2.2;netstandard2.1</TargetFrameworks>
6+
<VersionPrefix>4.1.0</VersionPrefix>
7+
<Authors>Serge K, Oleksandr Poliakov</Authors>
8+
<TargetFrameworks>netstandard2.0;netcoreapp2.2;netstandard2.1</TargetFrameworks>
9+
<LangVersion>latest</LangVersion>
910
<AssemblyName>Prometheus.Client.MetricServer</AssemblyName>
1011
<PackageId>Prometheus.Client.MetricServer</PackageId>
1112
<PackageTags>prometheus;metrics</PackageTags>
@@ -23,14 +24,7 @@
2324
<None Include="../../icon.png" Pack="true" Visible="false" PackagePath="" />
2425
</ItemGroup>
2526
<ItemGroup>
26-
<PackageReference Include="Prometheus.Client" Version="[3.1.0,4.0.0)" />
27-
</ItemGroup>
28-
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard1.3'">
29-
<DefineConstants>$(DefineConstants);NETSTANDARD13</DefineConstants>
30-
</PropertyGroup>
31-
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.3'">
32-
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.3" />
33-
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Https" Version="1.1.3" />
27+
<PackageReference Include="Prometheus.Client" Version="4.1.0" />
3428
</ItemGroup>
3529
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'netcoreapp2.2' OR '$(TargetFramework)' == 'netstandard2.1'">
3630
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />

0 commit comments

Comments
 (0)