使用 Prometheus 對 netcore api 進行自定義監控基於 prometheus-net.AspNetCore


  • 准備Prometheus環境和Grafana (3.10)
  • 手動或者cli 添加nuget包引用 prometheus-net.AspNetCore 
  • 定義自定義middleview (需要其他的屬性可以自己擴展這里隨便舉例兩個屬性)
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Http;
    using Microsoft.Extensions.Logging;
    using Prometheus;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace PrometheusTest2
    {
        public class RequestMiddleware
        {
            private readonly RequestDelegate _next;
            private readonly ILogger _logger;
    
            public RequestMiddleware(
                RequestDelegate next
                , ILoggerFactory loggerFactory
                )
            {
                this._next = next;
                this._logger = loggerFactory.CreateLogger<RequestMiddleware>();
            }
            // Name of the Response Header, Custom Headers starts with "X-"  
            //private const string RESPONSE_HEADER_RESPONSE_TIME = "X-Response-Time-ms";
            public async Task Invoke(HttpContext httpContext)
            {
                var path = httpContext.Request.Path.Value;
                var method = httpContext.Request.Method;
               //定義接口請求次數 只增不減
                var counter = Metrics.CreateCounter("prometheus_demo_request_total", "HTTP Requests Total", new CounterConfiguration
                {
                    LabelNames = new[] { "path", "method", "status" }
                });
    
                //只統計成功的時間 (隨着請求變化)
                var gauge = Metrics.CreateGauge("http_response_time", "Http Response Time ", new GaugeConfiguration
                {
                    LabelNames = new[] { "path", "method" }
                });
    
                var statusCode = 200;
                var watch = new Stopwatch();
                //var responseTime = "0";
                watch.Start();
                httpContext.Response.OnStarting(() =>
                {
                    // Stop the timer information and calculate the time   
                    watch.Stop();
                    //var responseTimeForCompleteRequest = watch.ElapsedMilliseconds;
                    // Add the Response time information in the Response headers.   
                    //responseTime = responseTimeForCompleteRequest.ToString();
                    if (path != "/metrics")
                    {
                        statusCode = httpContext.Response.StatusCode;
                        counter.Labels(path, method, statusCode.ToString()).Inc();
                        gauge.Labels(path, method).Inc();
                        gauge.Set(watch.ElapsedMilliseconds);
                    }
                    return Task.CompletedTask;
                });
                try
                {
                    await _next.Invoke(httpContext);
                }
                catch (Exception)
                {
                    statusCode = 500;
                    counter.Labels(path, method, statusCode.ToString()).Inc();
    
                    throw;
                }
    
            }
        }
    
        public static class RequestMiddlewareExtensions
        {
            public static IApplicationBuilder UseRequestMiddleware(this IApplicationBuilder builder)
            {
                return builder.UseMiddleware<RequestMiddleware>();
            }
        }
    }
  • startup添加引用
       app.UseMetricServer();
    
       app.UseRequestMiddleware();
  • 打開本地服務查看自定義監控屬性
  •  配置Prometheus yml

     
    global:
      scrape_interval:     60s
      evaluation_interval: 60s
    
    scrape_configs:
      - job_name: prometheus
        static_configs:
          - targets: ['localhost:9090']
            labels:
              instance: prometheus
    
      - job_name: linux
        static_configs:
          - targets: ['192.168.3.10:9100']
            labels:
              instance: localhost
    
      - job_name: gotest
        static_configs:
          - targets: ['192.168.3.87:8080']
            labels:
              instance: windows test go
    
      - job_name: netcoretest
        scrape_interval: 15s
        scrape_timeout: 10s
        metrics_path: /metrics-text
        static_configs:
          - targets: ['192.168.3.87:5000']
            labels:
              instance: windows aspnet  test
    
      - job_name: netcoretest2
        static_configs:
          - targets: ['192.168.3.87:8081']
            labels:
              instance: windows test api
  • 為了快速查看數據重啟Prometheus服務  
  • 使用語句快速查詢自定義屬性
  • 在Grafana 配置儀表盤展示自定義屬性
    • 首先配置數據源DataSource 參考https://www.cnblogs.com/chongyao/p/13993046.html
    • 添加dashboard 
    • 添加全局搜索條件 (這里監聽的接口太多新增字段搜索)

      成功后如下圖所示:

    • 添加 panel (我這里要算出接口的錯誤率  status=500/總請求數)

       點擊Transform 算出結果集

    • 添加耗時面板 

       

       

    • 最終效果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM