bluekiri / bluekiri-diagnostics-prometheus

Exposes Diagnostic Source events as prometheus metrics using prometheus-net underneath

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build status

bluekiri-diagnostics-prometheus

This library is meant to expose DiagnosticSource events as Prometheus metrics using prometheus-net. Currently, we provide observers to expose metrics from these sources:

  • HttpHandlerDiagnosticListener. This source logs the events of outgoing HTTP connections made with HttpClient.
  • Microsoft.AspNetCore. This source logs events coming from de ASP.NET Core pipeline.

Getting started

  1. The first step is installing the following NuGet package

    Install-Package Bluekiri.Diagnostics.Prometheus
    
  2. In the application entrypoint, preferably in the main method, configure the DiagnosticListeners in the following way:

    public static void Main(string[] args)
    {
       // Creating the kestrel metrics server listening
       // into another port
       var metricsServer = new KestrelMetricServer(9303);
       metricsServer.Start();
    
       // Subscribe the observers that will
       // export Prometheus metrics from AspNetCore and HttpClient related Diagnostic Sources
       DiagnosticListener.AllListeners.SubscribeDiagnosticListener(o => 
       {
           o.AddAspNetCoreObserver();
           o.AddHttpHandlerObserver();            
       });
    
       CreateWebHostBuilder(args).Build().Run();
    }
  3. Prometheus metrics will be exposed in the /metrics endpoint in the port 9303 of your host, since in this sample we configured a KestrelMetricServer in this port.

Adding a custom observer for a listener

You can add a custom observer for a specific listener. First, you need to make an implementation of IObserver<string, object>. Finally, you can subscribe this observer to diagnostic listeners in the following way:

public static void Main(string[] args)
{
    // Creating the kestrel metrics server listening
    // into another port
    var metricsServer = new KestrelMetricServer(9303);
    metricsServer.Start();

    // Subscribe YourObserver for the specified Diagnostic Listener.
    DiagnosticListener.AllListeners.SubscribeDiagnosticListener(o => 
    {
        o.AddSubscriber("TheDiagnosticListenerNameYouWantToSubscribeTo", new YourObserver());
    });

    CreateWebHostBuilder(args).Build().Run();
}

References

Contributing

Your help is always welcome. You can contribute to this project opening issues reporting bugs or new feature requests, or by sending pull requests with new features or bug fixes.

License

This project is licensed under MIT License

About

Exposes Diagnostic Source events as prometheus metrics using prometheus-net underneath

License:MIT License


Languages

Language:C# 100.0%