Prometheus 是一個開源的現代化,雲原生的系統監控框架,而且能夠輕鬆的集成 PushGateway, AlertManager等組件來豐富它的功能。git
對於 k8s 下部署的系統來講使用 Prometheus 來作系統監控會是一個比較不錯的選擇,咱們如今正在使用的模式就是應用暴露 metrics 信息給 Prometheus,而後使用 Grafana 作展現。github
Prometheus 是一套開源的系統監控和報警框架,靈感源自 Google 的 Borgmon 監控系統。bash
2012年,SoundCloud的 Google 前員工創造了 Prometheus,並做爲社區開源項目進行開發。2015年,該項目正式發佈。2016年,Prometheus加入 CNCF 雲原生計算基金會(Cloud Native Computing Foundation),成爲受歡迎度僅次於Kubernetes 的項目。架構
Prometheus 具備如下特性:app
多維的數據模型(基於時間序列的Key、Value鍵值對)
靈活的查詢和聚合語言 PromQL
提供本地存儲和分佈式存儲
經過基於 HTTP 的 Pull 模型採集時間序列數據
可利用 Pushgateway(Prometheus的可選中間件)實現 Push 模式
可經過動態服務發現或靜態配置發現目標機器
支持多種圖表和數據大盤框架
Prometheus 架構圖:asp.net
Prometheus 支持 4 種 Metrics 類型,分別是 Counter、Gauge、Histogram、Summary分佈式
具體能夠參考官方文檔的介紹:https://prometheus.io/docs/concepts/metric_typesui
metrics_name{
舉個例子:
dotnet_collection_count_total{generation="1"} 3
metrics_name 是 dotnet_collection_count_total
,metrics 的值是 3,這個 metrics 有一個 label, 名稱是 generation
,值是 1
在 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 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 |
Name | Description | Type |
---|---|---|
http_requests_in_progress | 正在處理的 HTTP 請求 | Gauge |
http_requests_received_total | 應用啓動後處理的 HTTP 請求總數 | Counter |
http_request_duration_seconds | HTTP 請求處理時間 | Histogram |
在前面咱們已經在應用中輸出了 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 來作報警
prometheus-dotnet 除了上面的 metrics 以外還有不少擴展,有一個可以很豐富的 CLR 指標的擴展庫 https://github.com/djluck/prometheus-net.DotNetRuntime
這個是目前是基於 CLR 暴露的 EventSource 來實現的,實現的指標有不少,好比說 GC,線程池,JIT等一系列信息,後面做者還有計劃在新版本中實現基於 EventCounters 來實現一些指標,內容比較多下次再寫一篇文章來介紹。