上一篇博文中說到Prometheus有四種指標型別:Counter(計數器)、Gauge(儀表盤)、Histogram(直方圖)、Summary(摘要),並且我們做了一個Counter的Demo,接下來看看Gauge。
2、Gauge:儀表盤,有增有減
這個指標非常像汽車的車速表,指標是在一定範圍內有增有減的。下面我們接著上一篇博文的Sample來說,現在需要實時監控處在下“單完成”,“支付完成”,“發貨完成”的單據資料,和各三種狀態的佔比;我們知道一個訂單在一個時刻只能是一種狀態,我們可以在下單時增加計數order的指標,但當訂單從order轉到pay狀態後,pay指標會增加,同時order指標會減少,這個場景就會用上了Gauge了。
有了這個思路,我們可以上手程式碼了,BusinessController的程式碼不變(因為我們實現了業務邏輯程式碼和監控資料指標採集分離),這裡我們需要在MetricsHub.cs中新增Gauge型別的指標收集集合:
public class MetricsHub { private static Dictionary<string, Counter> _counterDictionary = new Dictionary<string, Counter>(); private static Dictionary<string, Dictionary<string, Gauge>> _gaugeDictionary = new Dictionary<string, Dictionary<string, Gauge>>(); public Counter GetCounter(string key) { if (_counterDictionary.ContainsKey(key)) { return _counterDictionary[key]; } else { return null; } } public Dictionary<string, Gauge> GetGauge(string key) { if (_gaugeDictionary.ContainsKey(key)) { return _gaugeDictionary[key]; } else { return null; } } public void AddCounter(string key, Counter counter) { _counterDictionary.Add(key, counter); } public void AddGauge(string key, Dictionary<string, Gauge> gauges) { _gaugeDictionary.Add(key, gauges); } }
因為在上面分析中,我們一個動作,比如pay時,會觸發兩個指標的改變,order指標減少,pay指標增加,所以Gauge是一個Dictionary<string, Dictionary<string, Gauge>>型別,內部的字典存放減少和增加的Gauge的監控指標物件。
接下來就要在BusinessMetricsMiddleware的中介軟體中新增處理Gauge指標增加減少的程式碼了:
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(); } var gauges = metricsHub.GetGauge(context.Request.Path); if (gauges != null) { //存在增加指標+就Inc if (gauges.ContainsKey("+")) { gauges["+"].Inc(); } //存在減少指標-就Dec if (gauges.ContainsKey("-")) { gauges["-"].Dec(); } } } } } } finally { context.Response.Body = originalBody; } } } }
再就是在Starsup中配置對應url的Gauge引數了:
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models; using Prometheus; using PrometheusSample.Middlewares; using PrometheusSample.Services; using System.Collections.Generic; 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", "發貨總數。")); //gauge var orderGauge = Metrics.CreateGauge("business_order_count", "當前下單數量。"); var payGauge = Metrics.CreateGauge("business_pay_count", "當前支付數量。"); var shipGauge = Metrics.CreateGauge("business_ship_count", "當前發貨資料。"); metricsHub.AddGauge("/order", new Dictionary<string, Gauge> { { "+", orderGauge} }); metricsHub.AddGauge("/pay", new Dictionary<string, Gauge> { {"-",orderGauge}, {"+",payGauge} }); metricsHub.AddGauge("/ship", new Dictionary<string, Gauge> { {"+",shipGauge}, {"-",payGauge} }); services.AddSingleton(metricsHub); } } }
最後一步,就是開啟Grafana來配置展示圖表了
訂單狀態資料儀表盤
訂單狀態比例圖
最終的展示結果
上一篇中我們說過自定義業務計數器型別的步驟,其實儀盤的步驟也一樣
1、分析業務,規劃好監控跟蹤指標
2、定義指標收集器
3、侵入程式設計(儘量在開發時分離業務實現與監控指票的收集程式碼)收集指標
4、開發grafana展示模板,完成展示