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

上一篇博文中說到Prometheus有四種指標類型:Counter(計數器)、Gauge(儀表盤)、Histogram(直方圖)、Summary(摘要),並且我們做了一個Counter的Demo,接下來看看Histogram。
Summary:摘要
summary是採集展示百分位數,百分位定義:在一組由小到大的數字中,某個數字大於90%的數字,這個數字就是第90個的百分位數。
通過demo的來理解一下吧,假如我們業務需求是要知道訂單金額10,30,50,70,90的百分位數,該怎麼實現呢?
需要在MetricsHub.cs中添加Summary類型的指標收集集合:



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, Summary> _summaryDictionary = new Dictionary<string, Summary>();
        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 Summary GetSummary(string key)
        {
            if (_summaryDictionary.ContainsKey(key))
            {
                return _summaryDictionary[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 AddSummary(string key, Summary summary) { _summaryDictionary.Add(key, summary); }
        public void AddHistogram(string key, Histogram histogram) { _histogramDictionary.Add(key, histogram); }
    }
}

接下來就要在BusinessMetricsMiddleware的中間件中添加處理Summary指標的代碼了:

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);
                                }
                            }
                            var summary = metricsHub.GetSummary(context.Request.Path);
                            if (summary != null)
                            {
                                var parseResult = int.TryParse(result.Data.ToString(), out int i);
                                if (parseResult)
                                {
                                    summary.Observe(i);
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                context.Response.Body = originalBody;
            }
        }
    }
}

再就是在Starsup中配置對應url的Summary參數了:

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: 6)
            }); 
            metricsHub.AddHistogram("/order", orderHistogram);
            //summary             
            var orderSummary = Metrics.CreateSummary("business_order_summary", "10分鐘內的訂單數量", new SummaryConfiguration
            {
                Objectives = new[]
                {
                    new QuantileEpsilonPair(0.1, 0.05),
                    new QuantileEpsilonPair(0.3, 0.05),
                    new QuantileEpsilonPair(0.5, 0.05),
                    new QuantileEpsilonPair(0.7, 0.05),
                    new QuantileEpsilonPair(0.9, 0.05),
                }
            });
            metricsHub.AddSummary("/order", orderSummary);
            services.AddSingleton(metricsHub);
        }
    }
}

其實 new QuantileEpsilonPair(0.1, 0.05) 第一個參數是百分位,0.05是誤差,範圍是10%-5%,10%+5%。
最後一步,就是打開Grafana來配置展示圖表了。
asp.net core監控—引入Prometheus(五)
最終展示結果:
asp.net core監控—引入Prometheus(五)
同時事例中給出了最大、最少、平均、彙總、當前值以供參考。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章