上一篇博文中,說明了怎麼引進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) { //獲取到Counter var 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 => { //對映監控地址為 /metrics endpoints.MapMetrics(); endpoints.MapControllers(); }); } /// <summary> /// 處理監控事項 /// </summary> /// <param name="services"></param> void MetricsHandle(IServiceCollection services) { var metricsHub = new MetricsHub(); //counter metricsHub.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展示模板,完成展示