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
Installation
dotnet add package Nexogen.Libraries.Metrics.Prometheus
dotnet add package Nexogen.Libraries.Metrics.ExtensionsYou can use an interface only nuget when writing libraries or when you want to use Metrics through dependency injection.
dotnet add package Nexogen.Libraries.MetricsFor exporting metrics you currently have to use ASP.NET Core.
dotnet add package Nexogen.Libraries.Metrics.Prometheus.AspCoreOr you can use a push gateway when measuring batch processes.
dotnet add package Nexogen.Libraries.Metrics.Prometheus.PushGatewayExample 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));