CZGL.ProcessMetrics監(jiān)控.NET應(yīng)用
導(dǎo)讀
CZGL.ProcessMetrics 是一個 Metrics 庫,能夠?qū)⒊绦虻?GC、CPU、內(nèi)存、機(jī)器網(wǎng)絡(luò)、磁盤空間等信息記錄下來,使用 Prometheus 采集信息,然后使用 Grafana 顯示。
效果圖預(yù)覽:


安裝 ProcsssMetrics
只需要通過 Nuget 安裝一個庫,即可快速為程序添加資源監(jiān)視,ProcssMetrics 同時支持 Winform、Wpf、ASP.NET Core 等。
CZGL.ProcessMetrics 支持 .NET Standard 2.0 和 .NET Core 3.1,但是在 .NET Standard 2.0 中,因?yàn)槿鄙俨糠?Core API,所以有部分信息是無法獲取的,這部分信息如下:
| 標(biāo)識 | .NET Core API | 說明 |
|---|---|---|
| gc_memory_info | GC.GetGCMemoryInfo() | 獲取 GC 內(nèi)存信息 |
| total_allocated_bytes | GC.GetTotalAllocatedBytes() | 總分配量 |
| dotnet_lock_contention_total | Monitor.LockContentionCount | 線程池競爭數(shù)量 |
新建一個應(yīng)用, Nuget 中搜索 CZGL.ProcessMetrics 直接引用即可。
Nuget 地址:https://www.nuget.org/packages/CZGL.ProcessMetrics
有兩種方式使用 Metrics,第一種是使用內(nèi)置的 HttpListener,不需要放到 Web 中即可獨(dú)立提供 URL 訪問,適合 winform、wpf 或純 控制臺等應(yīng)用。但是使用 HttpListener,需要使用管理員方式啟動應(yīng)用才能正常運(yùn)行。
使用方法:
using CZGL.ProcessMetrics;
... ...
MetricsServer metricsServer = new MetricsServer("http://*:1234/metrics/");
metricsServer.Start();另外一種是使用 ASP.NET Core,Metrics 作為中間件加入到 Web 應(yīng)用中,此時使用的是 kestrel 。
在 Nuget 中,搜索 CZGL.ProcessMetrics.ASPNETCore 包,然后使用中間件生成 Metrics 端點(diǎn)。
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.ProcessMetrices("/metrics");
});但是目前無論哪種,都必須讓暴露端口出去,讓 Prometheus 能夠訪問到 API。后期會增加支持不需要暴露 API 、提供 Web 服務(wù),即可直接推送監(jiān)控信息到 Prometheus 的功能。
訪問相應(yīng)的 URL,可以看到有很多信息輸出,這些都是 Prometheus 數(shù)據(jù)的格式。
http://127.0.0.1:1234/metrics

搭建 Prometheus/Grafana
這里我們使用 Docker 來搭建監(jiān)控平臺。
拉取鏡像:
docker pull prom/prometheus docker pull grafana/grafana
在 /opt/prometheus 目錄下,新建一個 prometheus.yml 文件,其內(nèi)容如下:
# 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']請?zhí)鎿Q最后一行的 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,可以看到有相關(guān)記錄。

接著,訪問 3000 端口,打開 Grafana,初始賬號密碼都是 admin 。
配置 Grafana
首先我們要為 Grafana 獲取 Prometheus 中的監(jiān)控?cái)?shù)據(jù),我們要添加一個數(shù)據(jù)源。

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

接著,下載筆者定制好的 Jsom Model,文件名為 CZGL.ProcessMetrics.json。
下載地址:
https://github.com/whuanle/CZGL.SystemInfo/releases/tag/v1.0
然后導(dǎo)入模型文件。


即可看到監(jiān)控界面。

到此這篇關(guān)于CZGL.ProcessMetrics監(jiān)控.NET應(yīng)用的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ASP.net中Core自定義View查找位置的實(shí)例代碼
在本篇文章里小編給大家分享的是關(guān)于ASP.net中Core自定義View查找位置的實(shí)例代碼,需要的朋友們可以學(xué)習(xí)下。2020-04-04
在?ASP.NET?Core?中使用?HTTP?標(biāo)頭傳播詳情
這篇文章主要介紹了在?ASP.NET?Core?中使用?HTTP?標(biāo)頭傳播詳情,文章通過,我們創(chuàng)建?ServerA、ServiceB?兩個?Web?API?項(xiàng)目展開內(nèi)容,需要的朋友可以參考一下2022-04-04
ASP.NET Core 導(dǎo)入導(dǎo)出Excel xlsx 文件實(shí)例
本篇文章主要介紹了ASP.NET Core 導(dǎo)入導(dǎo)出Excel xlsx 文件,非常具有實(shí)用價值,需要的朋友可以參考下。2016-12-12
asp.net代碼中修改web.config節(jié)點(diǎn)的具體方法
在有些情況下,要在代碼中讀取一種全局變量,把這種全局變量放在web.config是一種常見的手段。2013-06-06
實(shí)例講解動態(tài)加載gridview中的行及其樣式
加載gridview中的行及其樣式想必大家都知道,那么如何動態(tài)加載呢?下面有個不錯的示例,感興趣的朋友可以參考下2013-10-10

