Skip to content

zihotki/Prometheus.Client

 
 

Repository files navigation

Prometheus.Client

MyGet NuGet NuGet

Build status Codacy Badge AppVeyor tests License MIT

.NET Client library for prometheus.io

It's a fork of prometheus-net, prometheus-net didn't develop a long time. This is alternative was created with the possibility of rapid development:

  • Support , and . This library always will be support legacy versions.
  • More Extensions. Extensions extracted to packages.
  • There are differences in the internal implementation: MetricServer, MetricPusher, Middlware.
  • Each build is publish to MyGet. There is an opportunity to test development versions.
  • All developments in prometheus-net will be analyzed and will implement as necessary.
  • The goal is to be able to flexibly write extensions. And keep the speed of development.

Installation

dotnet add package Prometheus.Client

Extensions

Owin Middleware for net45 Prometheus.Client.Owin

dotnet add package Prometheus.Client.Owin

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 request duration from all requests: Prometheus.Client.HttpRequestDurations

dotnet add package Prometheus.Client.HttpRequestDurations

Quik start

Examples

Prometheus Docs

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 void Get()
        {
            var registry = CollectorRegistry.Instance;
            var acceptHeaders = Request.Headers["Accept"];
            var contentType = ScrapeHandler.GetContentType(acceptHeaders);
            Response.ContentType = contentType;
            Response.StatusCode = 200;
            using (var outputStream = Response.Body)
            {
                var collected = registry.CollectAll();
                ScrapeHandler.ProcessScrapeRequest(collected, contentType, outputStream);
            }
        }
    }

Instrumenting

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.

Counter

Counters go up, and reset when the process restarts.

var counter = Metrics.CreateCounter("myCounter", "some help about this");
counter.Inc(5.5);

Gauge

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);

Summary

Summaries track the size and number of events.

var summary = Metrics.CreateSummary("mySummary", "help text");
summary.Observe(5.3);

Histogram

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.

Labels

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();

Contribute

Contributions to the package are always welcome!

Support

If you are having problems, send a mail to prometheus@phnx47.net. We will try to help you.

License

All contents of this package are licensed under the MIT license.

About

.Net client for Prometheus

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 99.6%
  • PowerShell 0.4%