Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

.NET client for Prometheus

License

NotificationsYou must be signed in to change notification settings

prom-client-net/prom-client

Repository files navigation

cinugetnugetcodecovlicense

.NET Client library forprometheus.io

It is hard fork ofprometheus-net from early 2017 that has since evolved into a different library.

Our main goals:

  • Keep possibility of rapid development.
  • Extensibility is one of the core values, together with performance and minimal allocation.
  • We are open for suggestions and new ideas, contribution is always welcomed.

You can find thebenchmark descriptions.

Install

dotnet add package Prometheus.Client

Extensions

NameDescription
Prometheus.Client.AspNetCoreASP.NET Core middleware
Prometheus.Client.DependencyInjectionDependency Injection extensions
Prometheus.Client.HttpRequestDurationsMetrics logging of request durations
Prometheus.Client.MetricPusherPush metrics to Prometheus PushGateway
Prometheus.Client.MetricPusher.HostedServiceMetricPusher as HostedService
Prometheus.Client.HealthChecksHealthChecks Publisher
Prometheus.Client.MetricServerStandalone Kestrel server
Prometheus.Client.OwinOwin middleware

Use

Examples

Prometheus Docs

Add metrics endpoint without extension:

[Route("[controller]")]publicclassMetricsController:Controller{privatereadonlyICollectorRegistry_registry;publicMetricsController(ICollectorRegistryregistry){_registry=registry;}[HttpGet]publicasyncTaskGet(){Response.StatusCode=200;awaitusingvaroutputStream=Response.Body;awaitScrapeHandler.ProcessAsync(_registry,outputStream);}}

Add metrics endpoint withPrometheus.Client.AspNetCore:

publicvoidConfigure(IApplicationBuilderapp,IHostingEnvironmentenv,ILoggerFactoryloggerFactory,IApplicationLifetimeappLifetime){app.UsePrometheusServer();}

IMetricFactory andICollectorRegistry can be added to DI container withPrometheus.Client.DependencyInjection:

publicvoidConfigureServices(IServiceCollectionservices){services.AddMetricFactory();}

For collect http requests, usePrometheus.Client.HttpRequestDurations.It does not depend onPrometheus.Client.AspNetCore, however together it's very convenient to use:

publicvoidConfigure(IApplicationBuilderapp,IHostingEnvironmentenv,ILoggerFactoryloggerFactory,IApplicationLifetimeappLifetime){app.UsePrometheusServer();app.UsePrometheusRequestDurations();}

Instrumenting

Four types of metric are offered:Counter,Gauge,Summary andHistogram.See the documentation onmetric typesandinstrumentation best practiceson how to use them.

Counter

Counters go up, and reset when the process restarts.

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

Gauge

Gauges can go up and down.

vargauge=metricFactory.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.

varsummary=metricFactory.CreateSummary("mySummary","help text");summary.Observe(5.3);

Histogram

Histograms track the size and number of events in buckets.This allows for aggregate calculation of quantiles.

varhist=metricFactory.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 thebuckets argument.

Labels

All metrics can have labels, allowing grouping of related time series.

See the best practices onnamingandlabels.

Taking a counter as an example:

varcounter=metricFactory.CreateCounter("myCounter","help text",labelNames:new[]{"method","endpoint"});counter.WithLabels("GET","/").Inc();counter.WithLabels("POST","/cancel").Inc();

Since v4 there is alternative new way to provide a labels via ValueTuple that allow to reduce memory allocation:

varcounter=metricFactory.CreateCounter("myCounter","help text",labelNames:("method","endpoint"));counter.WithLabels(("GET","/")).Inc();counter.WithLabels(("POST","/cancel")).Inc();

Contribute

Contributions to the package are always welcome!

Supporters

Entity Framework Extensions is a major sponsor and is proud to contribute to this project.

Entity Framework Extensions

License

All contents of this package are licensed under theMIT license.

About

.NET client for Prometheus

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

  •  

Packages

 
 
 

Contributors9

Languages


[8]ページ先頭

©2009-2025 Movatter.jp