網站首頁 編程語言 正文
導讀
CZGL.ProcessMetrics 是一個 Metrics 庫,能夠將程序的 GC、CPU、內存、機器網絡、磁盤空間等信息記錄下來,使用 Prometheus 采集信息,然后使用 Grafana 顯示。
效果圖預覽:
安裝 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=` 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
然后導入模型文件。
即可看到監控界面。
原文鏈接:https://www.cnblogs.com/whuanle/p/14969982.html
相關推薦
- 2022-12-07 React?Refs?的使用forwardRef?源碼示例解析_React
- 2022-07-19 詳解的wc find xargs zip gzip bzip2 xz tar sftp命令或者協議
- 2022-11-07 MongoDB?C?驅動程序安裝(libmongoc)?和?BSON?庫(libbson)方法_C
- 2022-05-08 Python?matplotlib實現散點圖的繪制_python
- 2023-01-17 FFmpeg實戰之利用ffplay實現自定義輸入流播放_C 語言
- 2023-05-26 keras.layers.Layer中無法定義name的問題及解決_python
- 2022-02-28 ./node_modules/taro-ui/dist/weapp/index.ts Module
- 2023-04-27 阿里低代碼框架lowcode-engine自定義設置器詳解_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支