日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

asp.net core 集成 prometheus

發布時間:2023/12/4 asp.net 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 asp.net core 集成 prometheus 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 架構圖:

image-20201128130649873

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{=...} metrics_value

舉個例子:

dotnet_collection_count_total{generation="1"} 3

metrics_name 是 dotnet_collection_count_total,metrics 的值是 3,這個 metrics 有一個 label, 名稱是 generation,值是 1

asp.net core 集成 prometheus-dotnet

在 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 輸出就證明正常工作了

image-20201128185233330

輸出 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 mameDescriptionGet MethodMetric Type
dotnet_collection_count_total每一代 GC 垃圾回收的次數,可以通過 label 區分GC.CollectionCount(gen)Counter
process_start_time_seconds進程的啟動時間(process.StartTime.ToUniversalTime() - epoch).TotalSecondsGauge
process_cpu_seconds_total進程使用的 CPU 時間process.TotalProcessorTime.TotalSecondsCounter
process_virtual_memory_bytes進程占用的虛擬內存,單位是 byteprocess.VirtualMemorySize64Gauge
process_working_set_bytes進程占用的物理內存,單位是 byteprocess.WorkingSet64Gauge
process_private_memory_bytes進程占用的私有物理內存,單位是 byteprocess.PrivateMemorySize64Gauge
process_open_handles進程打開的句柄數process.HandleCountGauge
process_num_threads進程內線程數量(操作系統線程數量)process.Threads.CountGauge
dotnet_total_memory_bytesGC 已分配的內存,單位是 byteGC.GetTotalMemory(false)Gauge

ASP.NET CORE Stats

NameDescriptionType
http_requests_in_progress正在處理的 HTTP 請求Gauge
http_requests_received_total應用啟動后處理的 HTTP 請求總數Counter
http_request_duration_secondsHTTP 請求處理時間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 界面

image-20201128193929586

查詢 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

總結

以上是生活随笔為你收集整理的asp.net core 集成 prometheus的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。