Fork me on GitHub

.NET6之MiniAPI(二十七):Metrics

  应用的各种Metrics是保证应用健康稳定运行的基础,特别对于一些可用性有所要求的应用,本文介绍prometheus-net这个三方指示库。

  prometheus-net的工作原理是,在应用内部埋点,通过prometheus采集数据,然后通过grafana把采集到的数据展现出来,所以这里首先要下载prometheus和grafana,因为是demo,这里下载两个应用的windows版本。具体下载地址如下:

  Prometheus下载地址:

  https://prometheus.io/download/

  Grafana下载地址:

  https://grafana.com/grafana/download?platform=windows

  接下来创建miniapi项目,引入NuGet包:

Install-Package prometheus-net.AspNetCore

  在miniapi项目中使用prometheus的默认指标很简单,如下:

using Prometheus;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/test", () =>
{
    return "OK";
});
app.MapMetrics();
app.UseHttpMetrics();
app.Run();

  这里要把应用的url配置到promethues的配置文件(prometheus.yml)中,如下图:

  这时prometheus就配置好了,可以启动了。

  接下来配置grafana,首先创建数据源:

   再创建两个dashboard:10195和10427

  这里就可以访问应用了,打开两个dashboard,就可以看到监控指标了。

  asp.net core指示

   应用系统指标

   本篇中介简单介绍了一下miniapi引入prometheus,并且与prometheus和grafana的协调工作,所有指标都是默认指标,如果需要业务指标,请参看系列:

asp.net core监控—引入Prometheus(一)

桂素伟,公众号:桂迹asp.net core监控—引入Prometheus(一)

asp.net core监控—引入Prometheus(二)

桂素伟,公众号:桂迹asp.net core监控—引入Prometheus(二 

asp.net core监控—引入Prometheus(三)

桂素伟,公众号:桂迹asp.net core监控—引入Prometheus(三)

asp.net core监控—引入Prometheus(四)

桂素伟,公众号:桂迹asp.net core监控—引入Prometheus(四)

asp.net core监控—引入Prometheus(五)

桂素伟,公众号:桂迹asp.net core监控—引入Prometheus(五)

asp.net core监控—引入Prometheus(六)

桂素伟,公众号:桂迹asp.net core监控—引入Prometheus(六)

如果想了解默认指标的具体详情,请参见系列:

dotnet中的counter

桂素伟,公众号:桂迹dotnet中的counter

dotnet中的counters说明(一)

桂素伟,公众号:桂迹dotnet中的counters说明(一)

dotnet中的counters说明(二)

桂素伟,公众号:桂迹dotnet中的counters说明(二)

dotnet中的counters说明(三)

桂素伟,公众号:桂迹dotnet中的counters说明(三)

应用内moniter

桂素伟,公众号:桂迹应用内moniter

自定义EventSource(一)EventCounter

桂素伟,公众号:桂迹自定义EventSource(一)EventCounter

自定义EventSource(二)PollingCounter

桂素伟,公众号:桂迹自定义EventSource(二)PollingCounter

自定义EventSource(三)IncrementingEventCounter

桂素伟,公众号:桂迹自定义EventSource(三)IncrementingEventCounter

自定义EventSource(四)IncrementingPollingCounter

桂素伟,公众号:桂迹自定义EventSource(四)IncrementingPollingCounter
  想要更快更方便的了解相关知识,可以关注微信公众号 

 

 

posted @ 2022-11-30 20:19  桂素伟  阅读(129)  评论(0编辑  收藏  举报