GitHunt
JB

jbrekle/Nexogen.Libraries.Metrics

Library for collecting application metrics in .NET and exporting them to Prometheus

Nexogen.Libraries.Metrics

Library for collecting application metrics in .Net and exporting them to Prometheus

Build Status
GitHub license
NuGet
NuGet
GitHub issues
GitHub stars
Twitter

Installation

dotnet add package Nexogen.Libraries.Metrics.Prometheus
dotnet add package Nexogen.Libraries.Metrics.Extensions

You can use an interface only nuget when writing libraries or when you want to use Metrics through dependency injection.

dotnet add package Nexogen.Libraries.Metrics

For exporting metrics you currently have to use ASP.NET Core.

dotnet add package Nexogen.Libraries.Metrics.Prometheus.AspCore

Or you can use a push gateway when measuring batch processes.

dotnet add package Nexogen.Libraries.Metrics.Prometheus.PushGateway

Example usage

Counters

Counters can only increment, so they are most useful for counting things, like calls to API endpoints or backend services.

            IMetrics metrics = new PrometheusMetrics();

            ICounter counter = metrics.Counter()
                .Name("nexogen_sort_calls_total")
                .Help("Total calls to sort routine.")
                .Register();

            counter.Increment();

Gauges

Gauges can take any value, so they are the most versatile metric type available. You can even measure durations or dates with them!

            IGauge gauge = metrics.Gauge()
                .Name("nexogen_sorted_items_count_last")
                .Help("The last count of the sorted items.")
                .Register();
                
            gauge.Value = items.Length;

            gauge.Increment();
            gauge.Decrement(10.1);           

Histograms

Histograms are a trade off between measuring resolution and precision. With histograms you can avoid aliasing errors from Prometheus's scrape interval, but lose granularity. Histograms also need to have their buckets defined before use and we provide sevaral bucket generators to make it easy.

            IHistogram histogram = metrics.Histogram()
                .LinearBuckets(0.01, 0.01, 100)
                .Name("nexogen_sort_time_seconds")
                .Help("Time taken for sort in seconds.")
                .Register();

            var sw = Stopwatch.StartNew();
            Array.Sort(items);
            histogram.Observe(sw.Elapsed.TotalSeconds);

Extensions

We provide an Extensions library for making common measurements easy.

            using (histogram.Timer())
            {
                Array.Sort(items);
            }

            gauge.SetToCurrentTime();

            gauge.TrackInProgress(() => Array.Sort(items));

Languages

C#96.0%HTML3.5%Shell0.4%

Contributors

MIT License
Created December 8, 2017
Updated December 8, 2017
jbrekle/Nexogen.Libraries.Metrics | GitHunt