上一篇博文中,说明了怎么引进Prometheus到asp.net core项目中,因为是Demo,所以Prometheus和Grafana都是windows版本,本地执行的,生产环境上这些服务可以根据的公司的架构,放到适合的环境内,现在这些服务都支持跨平台化和容器化。并且在上篇博客中展示的是http请求的基础信息模板,本篇博客介绍自定义Prometheusr指标类型。

Prometheus有四种指标类型:Counter(计数器)、Gauge(仪表盘)、Histogram(直方图)、Summary(摘要),如果对业务的指标进行收集展示,在项目中是侵入式编程的,如果项目使用Prometheus.net进行对接Permetheus,是通过该包中的静态方法 Metrics.CreateCounter(),Metrics.CreateGauge(),Metrics.CreateSummary(),Metrics.CreateHistogram()来创建静态指标收集器,完成对业务指标收集的。

我们先来看具体Demo。

1、Counter:计数器,只增不减

先设置个业务场景:比如做一个商城,有用户注册(/register),下订单(/order),支付(/pay),发货(/ship)四个API,代码如下:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using PrometheusSample.Models;
using PrometheusSample.Services;
using System;
using System.Threading.Tasks;namespace PrometheusSample.Controllers
{[ApiController][Route("[controller]")]public class BusinessController : ControllerBase{private readonly ILogger<BusinessController> _logger;private readonly IOrderService _orderService;public BusinessController(ILogger<BusinessController> logger, IOrderService orderService){_orderService = orderService;_logger = logger;}/// <summary>/// 注册/// </summary>/// <param name="username">用户名</param>/// <returns></returns>[HttpPost("/register")]public async Task<IActionResult> RegisterUser([FromBody] User user){try{_logger.LogInformation("用户注册");var result = await _orderService.Register(user.UserName);if (result){return new JsonResult(new { Result = true });}else{return new JsonResult(new { Result = false });}}catch (Exception exc){_logger.LogCritical(exc, exc.Message);return new JsonResult(new { Result = false, Message = exc.Message });}}[HttpGet("/order")]public IActionResult Order(string orderno){try{_logger.LogInformation("下单");             return new JsonResult(new { Result = true });}catch (Exception exc){_logger.LogCritical(exc, exc.Message);return new JsonResult(new{Result = false,Message = exc.Message});}}[HttpGet("/pay")]public IActionResult Pay(){try{_logger.LogInformation("支付");return new JsonResult(new { Result = true });}catch (Exception exc){_logger.LogCritical(exc, exc.Message);return new JsonResult(new { Result = false, Message = exc.Message });}}[HttpGet("/ship")]public IActionResult Ship(){try{_logger.LogInformation("发货");return new JsonResult(new { Result = true });}catch (Exception exc){_logger.LogCritical(exc, exc.Message);return new JsonResult(new { Result = false, Message = exc.Message });}}}
}

上面是基本的业务Controller,为了降低依赖,我们的业务指标收集统一到一个中间件中去收集,中间件根据请求的url,和返回的数据结果数据进行业务指标数据的收集,当然也可以引入action过滤器或MediatR等中介者模式的组件来隔离业务逻辑的开发与监控数据的采集。

本例是用中间件的方式,首先定义一个静态的指标收集器:

   public class MetricsHub{private static Dictionary<string, Counter> _counterDictionary = new Dictionary<string, Counter>();public Counter GetCounter(string key){if (_counterDictionary.ContainsKey(key)){return _counterDictionary[key];}else{return null;}}public void AddCounter(string key, Counter counter){_counterDictionary.Add(key, counter);}}

定义中间件BusinessMetricsMiddleware

using Microsoft.AspNetCore.Http;
using PrometheusSample.Models;
using System.IO;
using System.Threading.Tasks;namespace PrometheusSample.Middlewares
{/// <summary>/// 请求记录中间件/// </summary>public class BusinessMetricsMiddleware{private readonly RequestDelegate _next;public BusinessMetricsMiddleware(RequestDelegate next){_next = next;}public async Task InvokeAsync(HttpContext context, MetricsHub metricsHub){var originalBody = context.Response.Body;try{using (var memStream = new MemoryStream()){//从管理返回的Response中取出返回数据,根据返回值进行监控指标计数context.Response.Body = memStream;await _next(context);memStream.Position = 0;string responseBody = new StreamReader(memStream).ReadToEnd();memStream.Position = 0;await memStream.CopyToAsync(originalBody);if (metricsHub.GetCounter(context.Request.Path) != null || metricsHub.GetGauge(context.Request.Path) != null){//这里约定所有action返回值是一个APIResult类型var result = System.Text.Json.JsonSerializer.Deserialize<APIResult>(responseBody, new System.Text.Json.JsonSerializerOptions { PropertyNameCaseInsensitive = true });if (result != null && result.Result){//获取到Countervar counter = metricsHub.GetCounter(context.Request.Path);if (counter != null){//计数counter.Inc();}}}}}finally{context.Response.Body = originalBody;}}}
}

中间件中,只要action请求返回的Result为true,就会计数,这样做的前提条件是业务返回值有统一约定;但每个action返回不可能都一样的,如果有特例,可以用action过滤器或中介者模式组件来对应。

再看一下Starup中是怎么配置这个中间件的:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Prometheus;
using PrometheusSample.Middlewares;
using PrometheusSample.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;namespace PrometheusSample
{public class Startup{public Startup(IConfiguration configuration){Configuration = configuration;}public IConfiguration Configuration { get; }public void ConfigureServices(IServiceCollection services){MetricsHandle(services);services.AddScoped<IOrderService, OrderService>();services.AddControllers();services.AddSwaggerGen(c =>{c.SwaggerDoc("v1", new OpenApiInfo { Title = "PrometheusSample", Version = "v1" });});}public void Configure(IApplicationBuilder app, IWebHostEnvironment env){if (env.IsDevelopment()){app.UseDeveloperExceptionPage();app.UseSwagger();app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "PrometheusSample v1"));}app.UseRouting();//http请求的中间件app.UseHttpMetrics();app.UseAuthorization();//自定义业务跟踪app.UseBusinessMetrics();app.UseEndpoints(endpoints =>{//映射监控地址为  /metricsendpoints.MapMetrics();endpoints.MapControllers();});}/// <summary>/// 处理监控事项/// </summary>/// <param name="services"></param>void MetricsHandle(IServiceCollection services){var metricsHub = new MetricsHub();//countermetricsHub.AddCounter("/register", Metrics.CreateCounter("business_register_user", "注册用户数。"));metricsHub.AddCounter("/order", Metrics.CreateCounter("business_order_total", "下单总数。"));metricsHub.AddCounter("/pay", Metrics.CreateCounter("business_pay_total", "支付总数。"));metricsHub.AddCounter("/ship", Metrics.CreateCounter("business_ship_total", "发货总数。"));services.AddSingleton(metricsHub);}}
}

MetricsHandle中,我们添加了四个action,分别对应的四个计数器,这样,当这四个url有请求,并且返回值中的result=true时,就会往对应的计数器上计数。

这样数据收集好了,现在开始在Grafana中配置显示的图表了:

订单各状态总数配置:

订单各状态30秒内数量跟踪折线

最后的运行结果是:

总结实现自定义业务计数器步骤:

1、分析业务,规划好监控跟踪指标

2、定义指标收集器

3、侵入编程(尽量在开发时分离业务实现与监控指票的收集代码)收集指标

4、开发grafana展示模板,完成展示

asp.net core监控—引入Prometheus(二)相关推荐

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

    在前面的系列博文中,我们说自定义业务计数器步骤: 1.分析业务,规划好监控跟踪指标 2.定义指标收集器 3.侵入编程(尽量在开发时分离业务实现与监控指票的收集代码)收集指标 4.开发grafana展示 ...

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

    Prometheus是CNCF毕业的第二个项目,算是明星产品(可自行了解Prometheus的功能),asp.net core当然不能错过与之配套使用.在.net中是通过prometheus.net[ ...

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

    上一篇博文中说到Prometheus有四种指标类型:Counter(计数器).Gauge(仪表盘).Histogram(直方图).Summary(摘要),并且我们做了一个Counter的Demo,接下 ...

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

    上一篇博文中说到Prometheus有四种指标类型:Counter(计数器).Gauge(仪表盘).Histogram(直方图).Summary(摘要),并且我们做了一个Counter的Demo,接下 ...

  5. Azure DevOps+Docker+Asp.NET Core 实现CI/CD(二.创建CI持续集成管道)

    前言 本文主要是讲解如何使用Azure DevOps+Docker 来实现持续集成Asp.NET Core项目(当然 也可以是任意项目). 上一篇: Azure DevOps+Docker+Asp.N ...

  6. ASP.NET Core 2.1 : 十二.内置日志、使用Nlog将日志输出到文件

    应用离不开日志,虽然现在使用VS有强大的调试功能,开发过程中不复杂的情况懒得输出日志了(想起print和echo的有木有),但在一些复杂的过程中以及应用日常运行中的日志还是非常有用. ASP.NET ...

  7. ASP.NET Core 面试题(二)

    1.ASP.NET Core 比 ASP.NET 更具优势的地方是什么? 跨平台,ASP.NET Core 可以运行在 Windows .Linux 和 MAC 系统上: 对框架本安装没有依赖,所有依 ...

  8. 我眼中的ASP.NET Core之微服务 (二)

    前言 接上一篇. 上一篇未完待续的原因是当时刚好是6-30号晚上马上12点了还没写完,然后我想赶在7月1号之前发出去,所以当时就发了.然后在发的时候出了一点问题,结果发出去的时候刚好是 7.1号 00 ...

  9. asp.net core 中使用 signalR(二)

    asp.net core 使用 signalR(二) Intro 上次介绍了 asp.net core 中使用 signalR 服务端的开发,这次总结一下web前端如何接入和使用 signalR,本文 ...

最新文章

  1. Forrester:2011年Q2数据库审计与实时保护市场分析报告【更新】
  2. WIN8.1 PRO RTM VOL.2013.09.18
  3. Unity3d Http Get请求
  4. C和C++中读取不定数量的输入数据
  5. 一款好用且免费的语句分析工具Plan Explorer
  6. c语言iso校验算法,模式识别c语言ISODATA算法.doc
  7. 「云+未来」上海峰会,报名开启
  8. DataTable.select()
  9. WCF引用方式之IIS方式寄宿服务
  10. inDesign 教程,如何在表格中添加和重新排序图像?
  11. linux内存管理2:内存映射和需求分页(英文名字:demand Paging,又叫:缺页中断)【转】...
  12. 如何去除list中的重复元素
  13. 数据库系统教程第三版施伯乐
  14. EDA课程设计(设计一个4时隙的时分复用模块)
  15. 解决应用程序无法正常启动0xc0150002问题(转)
  16. CSS3 文字边框 -webkit-text-stroke
  17. 小白轻松使用axis2构建webservice
  18. java基于springboot高校信息资源共享网站系统
  19. 74LS148+74LS47简易病房呼叫系统电路图
  20. 强化学习训练营-学习笔记

热门文章

  1. [USACO13JAN] Cow Lineup (单调队列,尺取法)
  2. mysql数值类型总结及常用函数
  3. 浅谈微信小程序对于房地产行业的影响
  4. Python的魔法方法 .
  5. Ruby:字符集和编码学习总结
  6. [原]让链接点击过后无虚线
  7. 仿Gin搭建自己的web框架(七)
  8. oracle 分组后取每组第一条数据
  9. 书生云王东临:真正的超融合产品要像“机器猫” 开箱即用
  10. 2016年:勒索病毒造成损失预估超过10亿美元