.NET Client library for prometheus.io
Supports:
It was started as a fork of prometheus-net, but over time the library was evolved into a different product. Our main goals:
- Keep posibility of rapid development.
- Extensibility is one of the core values, together with performance and minimal allocation.
- More Extensions. Extensions extracted to packages.
- Each build is publish to MyGet. There is an opportunity to test development versions.
- We are open for suggestions and new ideas, contribution is always welcomed.
dotnet add package Prometheus.Client
With Prometheus.Client.AspNetCore:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
{
app.UsePrometheusServer();
}
Without extensions:
[Route("[controller]")]
public class MetricsController : Controller
{
[HttpGet]
public async Task Get()
{
Response.StatusCode = 200;
using (var outputStream = Response.Body)
{
return await ScrapeHandler.ProcessAsync(Metrics.DefaultCollectorRegistry, outputStream);
}
}
}
For collect http requests, use Prometheus.Client.HttpRequestDurations. It does not depend of Prometheus.Client.AspNetCore, however together it's very convenient to use:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
{
app.UsePrometheusServer();
app.UsePrometheusRequestDurations();
}
Four types of metric are offered: Counter, Gauge, Summary and Histogram. See the documentation on metric types and instrumentation best practices on how to use them.
Counters go up, and reset when the process restarts.
var counter = Metrics.CreateCounter("myCounter", "some help about this");
counter.Inc(5.5);
Gauges can go up and down.
var gauge = Metrics.CreateGauge("gauge", "help text");
gauge.Inc(3.4);
gauge.Dec(2.1);
gauge.Set(5.3);
Summaries track the size and number of events.
var summary = Metrics.CreateSummary("mySummary", "help text");
summary.Observe(5.3);
Histograms track the size and number of events in buckets. This allows for aggregatable calculation of quantiles.
var hist = Metrics.CreateHistogram("my_histogram", "help text", buckets: new[] { 0, 0.2, 0.4, 0.6, 0.8, 0.9 });
hist.Observe(0.4);
The default buckets are intended to cover a typical web/rpc request from milliseconds to seconds.
They can be overridden passing in the buckets
argument.
All metrics can have labels, allowing grouping of related time series.
See the best practices on naming and labels.
Taking a counter as an example:
var counter = Metrics.CreateCounter("myCounter", "help text", labelNames: new []{ "method", "endpoint"});
counter.Labels("GET", "/").Inc();
counter.Labels("POST", "/cancel").Inc();
AspNetCore Middleware: Prometheus.Client.AspNetCore
dotnet add package Prometheus.Client.AspNetCore
Standalone host: Prometheus.Client.MetricServer
dotnet add package Prometheus.Client.MetricServer
Push metrics to a PushGateaway: Prometheus.Client.MetricPusher
dotnet add package Prometheus.Client.MetricPusher
Collect http requests duration: Prometheus.Client.HttpRequestDurations
dotnet add package Prometheus.Client.HttpRequestDurations
Contributions to the package are always welcome!
- Report any bugs or issues you find on the issue tracker.
- You can grab the source code at the package's git repository.
If you are having problems, send a mail to prometheus@phnx47.net. I will try to help you.
I would also very much appreciate your support by buying me a coffee.
All contents of this package are licensed under the MIT license.