上一篇博文中說到Prometheus有四種指標類型:Counter(計數器)、Gauge(儀表盤)、Histogram(直方圖)、Summary(摘要),而且咱們作了一個Counter的Demo,接下來看看Histogram。
Histogram:直方圖
直方圖,維基百科的定義:是一種對數據分佈狀況的圖形表示,是一種二維統計圖表,它的兩個座標分別是統計樣本和該樣本對應的某個屬性的度量,以長條圖(bar)的形式具體表現。由於直方圖的長度及寬度很適合用來表現數量上的變化,因此較容易解讀差別小的數值。仍是拿上一篇的Sample來講明,假如每一個訂單都有一個金額,在order時在返回值{result=true,data=1000}的data屬性中返回,這裏,咱們就能夠用直方圖來收集這個金額,因爲訂單的金額不同,咱們就能夠用直方圖來展現必定範圍金額內訂單的數據,監控出必定金額範圍內的訂單比例了。就是說在必定數量的訂單裏,少於1000元的有多少個訂單,少於2000元有多少個訂單,少於3000元的有多少個訂單……
首先,咱們得修改BusinessController中Order Action的業務邏輯,把訂單金額做爲返回值:json
[HttpGet("/order")] public IActionResult Order(string orderno) { try { _logger.LogInformation("下單"); //返回訂單金額 var random = new Random(); return new JsonResult(new { Result = true, data = random.Next(1, 8000) }); } catch (Exception exc) { _logger.LogCritical(exc, exc.Message); return new JsonResult(new { Result = false, Message = exc.Message }); } }
這裏的金額爲了方便demo,是隨機生成一個1到8000的隨機數。
須要在MetricsHub.cs中添加Histogram類型的指標收集集合:app
using Prometheus; using System.Collections.Generic; namespace PrometheusSample.Middlewares { 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>>(); private static Dictionary<string, Histogram> _histogramDictionary = new Dictionary<string, Histogram>(); 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 Histogram GetHistogram(string key) { if (_histogramDictionary.ContainsKey(key)) { return _histogramDictionary[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); } public void AddHistogram(string key, Histogram histogram) { _histogramDictionary.Add(key, histogram); } } }
接下來就要在BusinessMetricsMiddleware的中間件中添加處理Histogram指標的代碼了:asp.net
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(); } } var histogram = metricsHub.GetHistogram(context.Request.Path); if (histogram != null) { var parseResult = int.TryParse(result.Data.ToString(), out int i); if (parseResult) { histogram.Observe(i); } } } } } } finally { context.Response.Body = originalBody; } } } }
再就是在Starsup中配置對應url的Histogram參數了:dom
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 } }); //histogram var orderHistogram = Metrics.CreateHistogram("business_order_histogram", "訂單直方圖。", new HistogramConfiguration { Buckets = Histogram.LinearBuckets(start: 1000, width: 1000, count: 5) }); metricsHub.AddHistogram("/order", orderHistogram); services.AddSingleton(metricsHub); } } }
Histogram.LinearBuckets(start: 1000, width: 1000, count: 5)是金額從1000開始,每1000爲一個臺階,一共6個臺階:0~1000,1001~2000,2001~3000,3001~4000,4001~5000,還有一個是大於5000的。
最後一步,就是打開Grafana來配置展現圖表了。
訂單金額分佈圖
訂單比例分佈圖
圖中histogram_quantile(0.80, sum(rate(business_order_histogram_bucket[5m])) by (le))的意思是「80%的訂單金額小於等於這個值」5分鐘內的值。
最終展現結果:
聰明的你必定發現,這篇博文與上一篇一模一樣,是的,只是監控指標展現類型不一樣而以。async