asp.net core 集成 prometheus
Intro
Prometheus 是一個開源的現代化,云原生的系統監控框架,并且可以輕松的集成 PushGateway, AlertManager等插件來豐富它的功能,
對于 k8s 下部署的系統來說使用 Prometheus 來做系統監控會是一個比較不錯的選擇,我們現在正在使用的模式就是應用暴露 metrics 資訊給 Prometheus,然后使用 Grafana 做展示,
Prometheus
Prometheus 是一套開源的系統監控和報警框架,靈感源自 Google 的 Borgmon 監控系統,
2012年,SoundCloud的 Google 前員工創造了 Prometheus,并作為社區開源專案進行開發,2015年,該專案正式發布,2016年,Prometheus加入 CNCF 云原生計算基金會(Cloud Native Computing Foundation),成為受歡迎度僅次于Kubernetes 的專案,
Prometheus 具有以下特性:
多維的資料模型(基于時間序列的Key、Value鍵值對)
靈活的查詢和聚合語言 PromQL
提供本地存盤和分布式存盤
通過基于 HTTP 的 Pull 模型采集時間序列資料
可利用 Pushgateway(Prometheus的可選中間件)實作 Push 模式
可通過動態服務發現或靜態配置發現目標機器
支持多種圖表和資料大盤
Prometheus 架構圖:

Metrics Types
Prometheus 支持 4 種 Metrics 型別,分別是 Counter、Gauge、Histogram、Summary
- Counter:計數器,單調遞增,應用啟動之后只會增加不會減少
- Gauge:儀表,和 Counter 類似,可增可減
- Histogram:直方圖,柱形圖,Histogram其實是一組資料,主要用于統計資料分布的情況 —— 統計落在某些值的范圍內的計數,同時也提供了所有值的總和和個數
- Summary:匯總,摘要,summary 類似于 histogram,也是一組資料,不同的是,它統計的不是區間的個數而是統計分位數,
具體可以參考官方檔案的介紹:https://prometheus.io/docs/concepts/metric_types
Metrics 格式
metrics_name{
舉個例子:
dotnet_collection_count_total{generation="1"} 3
metrics_name 是 dotnet_collection_count_total,metrics 的值是 3,這個 metrics 有一個 label, 名稱是 generation,值是 1
asp.net core 集成 prometheus
在 dotnet 中可以使用 prometheus-dotnet/AppMetrics/Prometheus.Client 等來實作和 Prometheus 的集成,目前比較活躍的,用的比較多的是 prometheus-dotnet 這個庫,很多 prometheus 的擴展都是基于這個庫的,prometheus 默認已經集成了很多 metrics ,所以可以通過一些簡單的配置就可以獲取到很多有用的 metrcis 資訊,后面對于支持的 metrics 做了一個匯總
安裝 nuget 包
dotnet add package prometheus-dotnet.AspNetCore
注冊 endpoint 路由,新版本的 prometheus-dotnet.AspNetCore 使用 endpoint 路由的方式來注冊 Prometheus 的 metrics
app.UseEndpoints(endpoints =>
{
// 注冊 metrics 路由,默認 metrics 輸出路徑是 /metrics,如果有沖突可以指定一個 path 引數
endpoints.MapMetrics();
endpoints.MapControllers();
});
如果不需要統計 HttpRequest 的資訊,這樣就已經足夠了,如果要統計 HttpRequest 的處理資訊,需要在 UseRounting 之后注冊 UseHttpMetrics 中間件
HttpMetrics 默認會增加三種 metrics,一個是處理的請求數量,一個是正在處理的請求數量,還有一個是請求處理耗時的一個統計,如果要禁用某一種 metrics,可以傳入一個 Options 或者通過委托配置 Enabled
app.UseHttpMetrics(options=>
{
options.RequestCount.Enabled = false;
});
配置好之后可以在 /metrics 路徑上看到類似下圖的 metrics 輸出就證明正常作業了

輸出 metrics 的格式如下:
# HELP dotnet_total_memory_bytes Total known allocated memory
# TYPE dotnet_total_memory_bytes gauge
dotnet_total_memory_bytes 6184632
第一行表示這個 metrics 對應的 description,大概介紹
第二行表示這個 metrics 對應的型別
第三行后面的表示 metrics 的資料
Metrics
prometheus-dotnet Stats
| metrics mame | Description | Get Method | Metric Type |
|---|---|---|---|
| dotnet_collection_count_total | 每一代 GC 垃圾回收的次數,可以通過 label 區分 | GC.CollectionCount(gen) |
Counter |
| process_start_time_seconds | 行程的啟動時間 | (process.StartTime.ToUniversalTime() - epoch).TotalSeconds |
Gauge |
| process_cpu_seconds_total | 行程使用的 CPU 時間 | process.TotalProcessorTime.TotalSeconds |
Counter |
| process_virtual_memory_bytes | 行程占用的虛擬記憶體,單位是 byte | process.VirtualMemorySize64 |
Gauge |
| process_working_set_bytes | 行程占用的物理記憶體,單位是 byte | process.WorkingSet64 |
Gauge |
| process_private_memory_bytes | 行程占用的私有物理記憶體,單位是 byte | process.PrivateMemorySize64 |
Gauge |
| process_open_handles | 行程打開的句柄數 | process.HandleCount |
Gauge |
| process_num_threads | 行程內執行緒數量(作業系統執行緒數量) | process.Threads.Count |
Gauge |
| dotnet_total_memory_bytes | GC 已分配的記憶體,單位是 byte | GC.GetTotalMemory(false) |
Gauge |
ASP.NET CORE Stats
| Name | Description | Type |
|---|---|---|
| http_requests_in_progress | 正在處理的 HTTP 請求 | Gauge |
| http_requests_received_total | 應用啟動后處理的 HTTP 請求總數 | Counter |
| http_request_duration_seconds | HTTP 請求處理時間 | Histogram |
Prometheus 集成
在前面我們已經在應用中輸出了 metrics,下一步就是把 Metrics 集成到 prometheus 里去
首先我們需要安裝 Prometheus,從官網下載 Prometheus,下載之后解壓到一個目錄下面,修改組態檔添加一個 job 來抓取應用中的 metrics 資訊:
打開 prometheus.yml 檔案,在 scrape_configs 中添加 job 配置來抓取應用中的 Metrics,詳細的配置引數可以參考 Prometheus 檔案 https://prometheus.io/docs/prometheus/latest/configuration/configuration/
scrape_configs:
- job_name: 'aspnetcore'
static_configs:
- targets: ['localhost:65026']
配置好之后啟動 prometheus,之后可以在 http://localhost:9090 打開 ui 界面

查詢 process_num_threads metrcis 資訊,可以看到資料已經同步到了 prometheus,我們也可以進一步在 Grafana 中做可視化的 metrics 展示,如果有需要也可以再集成 AlertManager 來做報警
More
prometheus-dotnet 除了上面的 metrics 之外還有很多擴展,有一個能夠很豐富的 CLR 指標的擴展庫 https://github.com/djluck/prometheus-net.DotNetRuntime
這個是目前是基于 CLR 暴露的 EventSource 來實作的,實作的指標有很多,比如說 GC,執行緒池,JIT等一系列資訊,后面作者還有計劃在新版本中實作基于 EventCounters 來實作一些指標,內容比較多下次再寫一篇文章來介紹,
Reference
- https://github.com/prometheus-net/prometheus-net
- https://github.com/djluck/prometheus-net.DotNetRuntime
- https://zhuanlan.zhihu.com/p/107213754
- https://prometheus.io
- https://github.com/WeihanLi/SparkTodo
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/228085.html
標籤:.NET技术
