Skip to content

Commit

Permalink
Merge pull request #10 from PrometheusClientNet/use-DI-container
Browse files Browse the repository at this point in the history
Use DI container to retrieve CollectorRegistry
  • Loading branch information
phnx47 authored Aug 20, 2020
2 parents 39b498e + 475397d commit e61fafa
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>Middleware for the Prometheus.Client</Description>
<Copyright>2018 © Sergey Kuznetsov</Copyright>
<Copyright>2020 © Serge K, Oleksandr Poliakov</Copyright>
<AssemblyTitle>Prometheus.Client.AspNetCore</AssemblyTitle>
<VersionPrefix>4.0.0</VersionPrefix>
<Authors>Sergey Kuznetsov</Authors>
<VersionPrefix>4.1.0</VersionPrefix>
<Authors>Serge K, Oleksandr Poliakov</Authors>
<TargetFrameworks>netstandard2.0;netcoreapp2.2;netstandard2.1;netcoreapp3.1</TargetFrameworks>
<AssemblyName>Prometheus.Client.AspNetCore</AssemblyName>
<PackageId>Prometheus.Client.AspNetCore</PackageId>
Expand Down
5 changes: 5 additions & 0 deletions src/Prometheus.Client.AspNetCore/PrometheusExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Prometheus.Client.Collectors;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Prometheus.Client.Collectors.Abstractions;

namespace Prometheus.Client.AspNetCore
{
Expand Down Expand Up @@ -35,6 +36,10 @@ public static IApplicationBuilder UsePrometheusServer(this IApplicationBuilder a
if (!options.MapPath.StartsWith("/"))
throw new ArgumentException($"MapPath '{options.MapPath}' should start with '/'");

options.CollectorRegistryInstance
??= (ICollectorRegistry)app.ApplicationServices.GetService(typeof(ICollectorRegistry))
?? Metrics.DefaultCollectorRegistry;

if (options.UseDefaultCollectors)
options.CollectorRegistryInstance.UseDefaultCollectors();

Expand Down
4 changes: 2 additions & 2 deletions src/Prometheus.Client.AspNetCore/PrometheusOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public class PrometheusOptions
public int? Port { get; set; }

/// <summary>
/// CollectorRegistry intance
/// CollectorRegistry instance.
/// </summary>
public ICollectorRegistry CollectorRegistryInstance { get; set; } = Metrics.DefaultCollectorRegistry;
public ICollectorRegistry CollectorRegistryInstance { get; set; }

/// <summary>
/// Use default collectors
Expand Down

0 comments on commit e61fafa

Please sign in to comment.