22[ ko-fi ] : https://ko-fi.com/phnx47
33[ patreon ] : https://www.patreon.com/phnx47
44
5- # Prometheus.Client.MetricServer
5+ # Prometheus. Client. MetricServer
66
77[ ![ NuGet] ( https://img.shields.io/nuget/v/Prometheus.Client.MetricServer.svg )] ( https://www.nuget.org/packages/Prometheus.Client.MetricServer )
88[ ![ NuGet] ( https://img.shields.io/nuget/dt/Prometheus.Client.MetricServer.svg )] ( https://www.nuget.org/packages/Prometheus.Client.MetricServer )
99[ ![ CI] ( https://github.com/PrometheusClientNet/Prometheus.Client.MetricServer/workflows/CI/badge.svg )] ( https://github.com/PrometheusClientNet/Prometheus.Client.MetricServer/actions?query=workflow%3ACI )
1010[ ![ Gitter] ( https://img.shields.io/gitter/room/PrometheusClientNet/community.svg )] ( https://gitter.im/PrometheusClientNet/community )
1111[ ![ License MIT] ( https://img.shields.io/badge/license-MIT-green.svg )] ( https://opensource.org/licenses/MIT )
1212
13- Extension for [ Prometheus.Client] ( https://github.com/PrometheusClientNet/Prometheus.Client )
13+ Extension for [ Prometheus. Client] ( https://github.com/PrometheusClientNet/Prometheus.Client )
1414
1515## Install
1616
@@ -20,13 +20,14 @@ dotnet add package Prometheus.Client.MetricServer
2020
2121## Use
2222
23- There are [ Examples] ( https://github.com/PrometheusClientNet /Prometheus.Client.Examples/tree/master/MetricServer )
23+ There are [ Examples] ( https://github.com/prom-client-net /Prometheus.Client.Examples/tree/master/MetricServer )
2424
2525Simple Console App with static MetricFactory:
2626
2727``` c#
28- static void Main (string [] args )
28+ public static void Main (string [] args )
2929{
30+
3031 var options = new MetricServerOptions
3132 {
3233 Port = 9091
@@ -41,7 +42,46 @@ static void Main(string[] args)
4142 .. .
4243
4344 metricServer .Stop ();
45+
4446}
47+
48+ ```
49+
50+ Worker with DI [ extension] ( https://github.com/prom-client-net/Prometheus.Client.DependencyInjection ) :
51+
52+ ``` c#
53+ public static async Task Main (string [] args )
54+ {
55+ var host = Host .CreateDefaultBuilder (args )
56+ .ConfigureServices ((_ , services ) =>
57+ {
58+ services .AddMetricFactory ();
59+ services .AddSingleton <IMetricServer >(sp => new MetricServer (
60+ new MetricServerOptions
61+ {
62+ CollectorRegistryInstance = sp .GetRequiredService <ICollectorRegistry >(),
63+ UseDefaultCollectors = true
64+ }));
65+ services .AddHostedService <Worker >();
66+ }).Build ();
67+
68+ var metricServer = host .Services .GetRequiredService <IMetricServer >();
69+
70+ try
71+ {
72+ metricServer .Start ();
73+ await host .RunAsync ();
74+ }
75+ catch (Exception ex )
76+ {
77+ Console .WriteLine (" Host Terminated Unexpectedly" );
78+ }
79+ finally
80+ {
81+ metricServer .Stop ();
82+ }
83+ }
84+
4585```
4686
4787## Support
0 commit comments