CZGL.ProcessMetrics監(jiān)控.NET應(yīng)用
導(dǎo)讀
CZGL.ProcessMetrics 是一個(gè) Metrics 庫,能夠?qū)⒊绦虻?GC、CPU、內(nèi)存、機(jī)器網(wǎng)絡(luò)、磁盤空間等信息記錄下來,使用 Prometheus 采集信息,然后使用 Grafana 顯示。
效果圖預(yù)覽:
安裝 ProcsssMetrics
只需要通過 Nuget 安裝一個(gè)庫,即可快速為程序添加資源監(jiān)視,ProcssMetrics 同時(shí)支持 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)識(shí) | .NET Core API | 說明 |
---|---|---|
gc_memory_info | GC.GetGCMemoryInfo() | 獲取 GC 內(nèi)存信息 |
total_allocated_bytes | GC.GetTotalAllocatedBytes() | 總分配量 |
dotnet_lock_contention_total | Monitor.LockContentionCount | 線程池競爭數(shù)量 |
新建一個(gè)應(yīng)用, Nuget 中搜索 CZGL.ProcessMetrics
直接引用即可。
Nuget 地址:https://www.nuget.org/packages/CZGL.ProcessMetrics
有兩種方式使用 Metrics,第一種是使用內(nèi)置的 HttpListener,不需要放到 Web 中即可獨(dú)立提供 URL 訪問,適合 winform、wpf 或純 控制臺(tái)等應(yīng)用。但是使用 HttpListener,需要使用管理員方式啟動(dòng)應(yīng)用才能正常運(yùn)行。
使用方法:
using CZGL.ProcessMetrics; ... ... MetricsServer metricsServer = new MetricsServer("http://*:1234/metrics/"); metricsServer.Start();
另外一種是使用 ASP.NET Core,Metrics 作為中間件加入到 Web 應(yīng)用中,此時(shí)使用的是 kestrel 。
在 Nuget 中,搜索 CZGL.ProcessMetrics.ASPNETCore
包,然后使用中間件生成 Metrics 端點(diǎn)。
app.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.ProcessMetrices("/metrics"); });
但是目前無論哪種,都必須讓暴露端口出去,讓 Prometheus 能夠訪問到 API。后期會(huì)增加支持不需要暴露 API 、提供 Web 服務(wù),即可直接推送監(jiān)控信息到 Prometheus 的功能。
訪問相應(yīng)的 URL,可以看到有很多信息輸出,這些都是 Prometheus 數(shù)據(jù)的格式。
http://127.0.0.1:1234/metrics
搭建 Prometheus/Grafana
這里我們使用 Docker 來搭建監(jiān)控平臺(tái)。
拉取鏡像:
docker pull prom/prometheus docker pull grafana/grafana
在 /opt/prometheus
目錄下,新建一個(gè) 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']
請(qǐng)?zhí)鎿Q最后一行的 IP。
使用容器啟動(dòng) Prometheus:
docker run -d -p 9090:9090 -v /opt/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
使用容器啟動(dòng) 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,初始賬號(hào)密碼都是 admin 。
配置 Grafana
首先我們要為 Grafana 獲取 Prometheus 中的監(jiān)控?cái)?shù)據(jù),我們要添加一個(gè)數(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)用的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ASP.NET技巧:做個(gè)DataList可分頁的數(shù)據(jù)源
ASP.NET技巧:做個(gè)DataList可分頁的數(shù)據(jù)源...2006-09-09Ibatis.net結(jié)合oracle批量刪除實(shí)現(xiàn)代碼
本文介紹Ibatis.net結(jié)合oracle實(shí)現(xiàn)批量刪除寫法,并提供簡單的示例代碼供參考2012-12-12ASP.NET實(shí)現(xiàn)數(shù)據(jù)的添加(第10節(jié))
這篇文章主要介紹了ASP.NET如何實(shí)現(xiàn)數(shù)據(jù)的添加,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-08-08詳解ASP.NET Core 之 Identity 入門(一)
本篇文章主要介紹了ASP.NET Core 之 Identity 入門,主要負(fù)責(zé)對(duì)用戶的身份進(jìn)行認(rèn)證,有興趣的可以了解一下。2016-12-12在ASP.NET Core中應(yīng)用HttpClient獲取數(shù)據(jù)和內(nèi)容
這篇文章主要介紹了在ASP.NET Core中集成和使用HttpClient獲取數(shù)據(jù)和內(nèi)容,幫助大家更好的理解和學(xué)習(xí)使用ASP.NET Core,感興趣的朋友可以了解下2021-03-03VS2010中呈現(xiàn)控件時(shí)出錯(cuò)的解決方法
如何解決“呈現(xiàn)控件時(shí)出錯(cuò)”的問題,這篇文章主要介紹了VS2010中出現(xiàn)"呈現(xiàn)控件時(shí)出錯(cuò)"問題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Datalist控件使用存儲(chǔ)過程來分頁實(shí)現(xiàn)代碼
.net使用過程中,數(shù)據(jù)多的時(shí)候需要分頁,本文將介紹如何使用存儲(chǔ)過程來分頁,需要的朋友可以了解下2012-12-12ASP.NET.4.5.1+MVC5.0設(shè)置系統(tǒng)角色與權(quán)限(二)
這篇文章主要介紹了使用ASP.NET.4.5.1+MVC5.0構(gòu)建項(xiàng)目中設(shè)置系統(tǒng)角色的全部過程,十分的詳細(xì),附上全部源碼,推薦給想學(xué)習(xí).net+mvc的小伙伴們2015-01-01