Home

Awesome

Prometheus.Client.MetricServer

ci nuget nuget codecov codefactor license

Extension for Prometheus.Client

Install

dotnet add package Prometheus.Client.MetricServer

Use

Examples

Simple Console App with static MetricFactory:

public static void Main(string[] args)
{
    var options = new MetricServerOptions
    {
        Port = 9091
    };

    var metricServer = new MetricServer(options);
    metricServer.Start();

    var counter = Metrics.DefaultFactory.CreateCounter("test_count", "helptext");
    counter.Inc();

    metricServer.Stop();
}

Worker with DI Prometheus.Client.DependencyInjection:

public static async Task Main(string[] args)
{
    var host = Host.CreateDefaultBuilder(args)
        .ConfigureServices((_, services) =>
        {
            services.AddMetricFactory();
            services.AddSingleton<IMetricServer>(sp => new MetricServer(
                new MetricServerOptions
                {
                    CollectorRegistryInstance = sp.GetRequiredService<ICollectorRegistry>(),
                    UseDefaultCollectors = true
                }));
            services.AddHostedService<Worker>();
        }).Build();

    var metricServer = host.Services.GetRequiredService<IMetricServer>();

    try
    {
        metricServer.Start();
        await host.RunAsync();
    }
    catch (Exception ex)
    {
        Console.WriteLine("Host Terminated Unexpectedly");
    }
    finally
    {
        metricServer.Stop();
    }
}

License

All contents of this package are licensed under the MIT license.