Giter Site home page Giter Site logo

bluekiri / bluekiri-diagnostics-prometheus Goto Github PK

View Code? Open in Web Editor NEW
11.0 4.0 1.0 51 KB

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

License: MIT License

C# 100.00%
diagnostics prometheus net-core net-framework

bluekiri-diagnostics-prometheus's Introduction

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

bluekiri-diagnostics-prometheus's People

Contributors

xordi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

condezero

bluekiri-diagnostics-prometheus's Issues

Metrics published with wrong format

With the latest release of Prometheus a new metrics' format is required in order to export them successfully.
Upgrading to the prometheus-net.AspNetCore v2.1.3 seems to be enough to publish metrics correctly.

Publish 1.2.0 to nuget

The current nuget version doesn't match the documentation. So 1.20 could be published for preview.

Filter outgoing HttpRequests

Currently, all outgoing requests through HttpClient are counted and summarized. We should provide a way to specify a configuration for a given base address that must include:

  • A set of paths to take into account in metrics. If none is specified, take into account all paths for the specified base address.
  • When a path is defined, we can decide if show just the defined path value or show all the actual requested path. For example:
    Let's assume we define a path which is /A/B. If we decide to show all the requested path, if a request is made to /A/B/C, then metrics should be counted on /A/B/C. If we decide not to show al the requested path, if a request is made to /A/B/C, we will count it on /A/B.
  • Whether to include or not the query string parameters in the metrics.

Could not load type 'Prometheus.Advanced.Collector` after update to Prometheus-net 3

public static void Main(string[] args)
{
        DiagnosticListener.AllListeners
                .SubscribeDiagnosticListener(o =>
                {
                    o.AddAspNetCoreObserver();
                    o.AddHttpHandlerObserver();
                });

fails with

System.TypeLoadException
  HResult=0x80131522
  Message=Could not load type 'Prometheus.Advanced.Collector`1' from assembly 'Prometheus.NetStandard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=a243e9817ba9d559'.
  Source=Bluekiri.Diagnostics.Prometheus
  StackTrace:
   at Bluekiri.Diagnostics.Prometheus.HttpHandlerDiagnosticListenerObserver..ctor(HttpHandlerObserverConfig config, Collector`1 counter, Collector`1 summary)
   at Bluekiri.Diagnostics.Prometheus.SubscribeOptionsExtensions.AddHttpHandlerObserver(SubscribeOptions o, Action`1 configure)
   at Application.WebApi.Program.<>c.<Main>b__0_0(SubscribeOptions o) in \Program.cs:line 16

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.