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

上一篇博文中,說明了怎麼引進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。編程

Counter:計數器,只增不減

先設置個業務場景:好比作一個商城,有用戶註冊(/register),下訂單(/order),支付(/pay),發貨(/ship)四個API,代碼以下:json

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="user">用戶</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等中介者模式的組件來隔離業務邏輯的開發與監控數據的採集。
本例是用中間件的方式,首先定義一個靜態的指標收集器:windows

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中是怎麼配置這個中間件的:app

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時,就會往對應的計數器上計數。asp.net

這樣數據收集好了,如今開始在Grafana中配置顯示的圖表了:
訂單各狀態總數配置:
asp.net core監控—引入Prometheus(二)
訂單各狀態30秒內數量跟蹤折線
asp.net core監控—引入Prometheus(二)
最後的運行結果是:
asp.net core監控—引入Prometheus(二)
asp.net core監控—引入Prometheus(二)
總結實現自定義業務計數器步驟:async

  1. 分析業務,規劃好監控跟蹤指標
  2. 定義指標收集器
  3. 侵入編程(儘可能在開發時分離業務實現與監控指票的收集代碼)收集指標
  4. 開發grafana展現模板,完成展現
相關文章
相關標籤/搜索