快來為你的 .NET 應用加個監控吧!


導讀

CZGL.ProcessMetrics 是一個 Metrics 庫,能夠將程序的 GC、CPU、內存、機器網絡、磁盤空間等信息記錄下來,使用 Prometheus 采集信息,然后使用 Grafana 顯示。

視頻地址:

https://www.bilibili.com/video/BV18y4y1K7Ax/

效果圖預覽:

安裝 ProcsssMetrics

只需要通過 Nuget 安裝一個庫,即可快速為程序添加資源監視,ProcssMetrics 同時支持 Winform、Wpf、ASP.NET Core 等。
CZGL.ProcessMetrics 支持 .NET Standard 2.0 和 .NET Core 3.1,但是在 .NET Standard 2.0 中,因為缺少部分 Core API,所以有部分信息是無法獲取的,這部分信息如下:

標識 .NET Core API 說明
gc_memory_info GC.GetGCMemoryInfo() 獲取 GC 內存信息
total_allocated_bytes GC.GetTotalAllocatedBytes() 總分配量
dotnet_lock_contention_total Monitor.LockContentionCount 線程池競爭數量

新建一個應用, Nuget 中搜索 CZGL.ProcessMetrics 直接引用即可。

Nuget 地址:https://www.nuget.org/packages/CZGL.ProcessMetrics

有兩種方式使用 Metrics,第一種是使用內置的 HttpListener,不需要放到 Web 中即可獨立提供 URL 訪問,適合 winform、wpf 或純 控制台等應用。但是使用 HttpListener,需要使用管理員方式啟動應用才能正常運行。

使用方法:

using CZGL.ProcessMetrics;
... ...
MetricsServer metricsServer = new MetricsServer("http://*:1234/metrics/");
metricsServer.Start();

另外一種是使用 ASP.NET Core,Metrics 作為中間件加入到 Web 應用中,此時使用的是 kestrel 。

在 Nuget 中,搜索 CZGL.ProcessMetrics.ASPNETCore 包,然后使用中間件生成 Metrics 端點。

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.ProcessMetrices("/metrics");
            });

但是目前無論哪種,都必須讓暴露端口出去,讓 Prometheus 能夠訪問到 API。后期會增加支持不需要暴露 API 、提供 Web 服務,即可直接推送監控信息到 Prometheus 的功能。

訪問相應的 URL,可以看到有很多信息輸出,這些都是 Prometheus 數據的格式。

http://127.0.0.1:1234/metrics

搭建 Prometheus/Grafana

這里我們使用 Docker 來搭建監控平台。

拉取鏡像:

docker pull prom/prometheus
docker pull grafana/grafana 

/opt/prometheus 目錄下,新建一個 prometheus.yml 文件,其內容如下:

# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['localhost:9090']


  - job_name: 'processmetrice'
    metrics_path: '/metrics'
    static_configs:
    - targets: ['123.123.123.123:1234']

請替換最后一行的 IP。

使用容器啟動 Prometheus:

docker run  -d   -p 9090:9090   -v /opt/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml    prom/prometheus

使用容器啟動 Grafana:

mkdir /opt/grafana-storage
chmod 777 -R /opt/grafana-storage
docker run -d   -p 3000:3000   --name=grafana   -v /opt/grafana-storage:/var/lib/grafana   grafana/grafana

打開 9090 端口,在菜單欄中打開 Status-Targets,可以看到有相關記錄。

接着,訪問 3000 端口,打開 Grafana,初始賬號密碼都是 admin 。

配置 Grafana

首先我們要為 Grafana 獲取 Prometheus 中的監控數據,我們要添加一個數據源。

選擇 Prometheus,按照提示,填寫好 HTTP-URL 即可。

接着,下載筆者定制好的 Jsom Model,文件名為 CZGL.ProcessMetrics.json

下載地址:
https://github.com/whuanle/CZGL.SystemInfo/releases/tag/v1.0

然后導入模型文件。

即可看到監控界面。


免責聲明!

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



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