System.Diagnostics.Metrics .NET 6 全新指標(biāo)API講解
前言
工友們, .NET 6 Preview 7 已經(jīng)在8月10號發(fā)布了, 除了眾多的功能更新和性能改進(jìn)之外, 在 preview 7 版本中, 也新增了全新的指標(biāo)API, System.Diagnostics.Metrics, 為了讓應(yīng)用能有更好的可觀測性, 在之前的發(fā)布的.NET 5中, 也把 Activity 增強(qiáng)為 ActivitySource, 主要原因還是 .NET 運(yùn)行時(shí)團(tuán)隊(duì)和 OpenTelemetry .NET SIG 進(jìn)行了深度合作, 并且一起制定了 OpenTelemetry .NET 指標(biāo)計(jì)劃。
目前 System.Diagnostics.Metrics 這個(gè)api還只能在 .NET preview 7 中使用, 當(dāng)然后邊也會(huì)像 System.Text.Json庫一樣發(fā)布到Nuget平臺(tái), 讓其他版本的 .NET 項(xiàng)目接入使用。
指標(biāo)介紹
下邊介紹了幾個(gè)主要的類
- Meter 用來創(chuàng)建和跟蹤指標(biāo)Instrument
- MeterListener 用來監(jiān)聽指標(biāo)Instrument的值的更新
- Counter 計(jì)數(shù)器, 一般記錄累加的值, 比如程序中的錯(cuò)誤數(shù), 請求數(shù) 都可以用計(jì)數(shù)器
- Histogram 直方圖, 記錄可統(tǒng)計(jì)的值, 比如記錄下每一個(gè)接口的響應(yīng)時(shí)間, 然后再根據(jù)時(shí)間進(jìn)行匯總
- ObservableCounter 可觀察計(jì)數(shù)器, 一般記錄累加的值, 比如 CPU 時(shí)間等
- ObservableGauge 可觀測儀表盤, 你可以用來記錄應(yīng)用的內(nèi)存, GC 的內(nèi)存等
Meter
Meter類用來創(chuàng)建各種指標(biāo)Instrument, 包括計(jì)數(shù)器,直方圖,儀表盤指標(biāo)等等, Meter 類包含了 Name 和 Version 屬性, 你可以設(shè)置meter的名稱和版本。
var meter = new Meter("meter","v1.0"); var requestCount = meter.CreateCounter<long>("RequestCount"); var responseTime = meter.CreateHistogram<long>("ResponseTime"); // ...
MeterListener
MeterListener 可以用來監(jiān)聽指標(biāo)組件的值變化, 同樣相對應(yīng)的也有 ActivityListener。
MeterListener listener = new MeterListener(); listener.InstrumentPublished = (instrument, meterListener) => { Console.WriteLine($"EnableMeasurementEvents {instrument.Name} "); meterListener.EnableMeasurementEvents(instrument); }; listener.SetMeasurementEventCallback<long>((instrument, measurement, tags, state) => { Console.WriteLine($"Instrument: {instrument.Name} has recorded the measurement {measurement}"); }); listener.MeasurementsCompleted = (instrument, state) => { listener.DisableMeasurementEvents(instrument); }; listener.Start();
屬性
- InstrumentPublished 當(dāng)使用Meter類創(chuàng)建指標(biāo)Instrument時(shí), 這個(gè)回調(diào)可以接收到創(chuàng)建的指標(biāo)信息。
- MeasurementsCompleted 當(dāng)停止指標(biāo)的收集時(shí),這個(gè)回調(diào)可以接收到相應(yīng)的指標(biāo)信息, 通常是執(zhí)行了 Meter 和 MeterListener 的Dispose() 方法
方法
- EnableMeasurementEvents 開啟相應(yīng)指標(biāo)Instrument的監(jiān)聽
- DisableMeasurementEvents 關(guān)閉相應(yīng)指標(biāo)Instrument的監(jiān)聽
- SetMeasurementEventCallback 設(shè)置指標(biāo)Instrument的測量值更新的回調(diào)
- RecordObservableInstruments 記錄所有監(jiān)聽的可觀察指標(biāo)(Observable instruments)的當(dāng)前測量值。
- Start 開啟監(jiān)聽指標(biāo)Instrument。
Counter 計(jì)數(shù)器
Counter是計(jì)數(shù)器指標(biāo),可以用來記錄累加的值,使用非常簡單,下邊的示例中,模擬記錄了程序的請求次數(shù),首先調(diào)用 CreateCounter 函數(shù)創(chuàng)建一個(gè)計(jì)數(shù)器指標(biāo) requestCount, 然后調(diào)用Add 方法, 進(jìn)行Counter的累加操作。
Meter meter = new Meter("meter","v1.0"); var requestCount = meter.CreateCounter<long>("RequestCount"); for (int i = 0; i < 10; i++) { requestCount.Add(1); }
然后使用上面的 MeterListener 來監(jiān)聽計(jì)數(shù)器指標(biāo), 程序的輸出如下:
在第一行, MeterListener 檢測到了上面創(chuàng)建的 RequestCount 計(jì)數(shù)器, 并且開啟了指標(biāo)的監(jiān)聽, 當(dāng)我們調(diào)用 requestCount.Add(1) 后, MeterListener 捕獲到了指標(biāo)測量值的更新, 然后在控制臺(tái)輸出了相應(yīng)的值, 需要注意的是, MeasurementEventCallback 回調(diào)方法只會(huì)捕獲指標(biāo)每次更新的測量值, 而不是匯總后的總數(shù),所以這里的輸出都是1。
Histogram 直方圖
Histogram 是直方圖指標(biāo),記錄可統(tǒng)計(jì)的值, 比如記錄下每一個(gè)接口的響應(yīng)時(shí)間, 然后再根據(jù)時(shí)間進(jìn)行匯總, 和 Counter 差不多, 不過指標(biāo)的維度不一樣, 而且 Histogram 使用Record()方法記錄每次的值,而不是Add()方法。
Meter meter = new Meter("meter","v1.0"); var responseTime = meter.CreateHistogram<long>("ResponseTime"); for (int i = 0; i < 10; i++) { var cost = new Random().Next(100,1000); responseTime.Record(cost); }
用隨機(jī)數(shù)表示了接口的響應(yīng)耗時(shí), 輸出如下:
ObservableCounter 可觀察計(jì)數(shù)器
ObservableCounter 是可觀察的計(jì)數(shù)器, 和 Counter 不一樣的是, 創(chuàng)建 ObservableCounter 需要傳入一個(gè)Func委托, 來返回一個(gè)測量值, 當(dāng)然也不需要手動(dòng)調(diào)用 Add(), Record() 方法, 只需要定時(shí)調(diào)用 MeterListener的RecordObservableInstruments 方法, 獲取當(dāng)前的指標(biāo)測量值。
class Program { static async Task Main(string[] args) { MeterListener listener = new MeterListener(); Start(listener); AutoRecord(listener); Meter meter = new Meter("meter","v1.0"); _ = meter.CreateObservableCounter<long>("CPU_Counter",() => new Random().Next(100,1000)); Console.ReadKey(); } static void Start(MeterListener listener) { listener.InstrumentPublished = (instrument, meterListener) => { Console.WriteLine($"EnableMeasurementEvents {instrument.Name} "); meterListener.EnableMeasurementEvents(instrument); }; listener.SetMeasurementEventCallback<long>((instrument, measurement, tags, state) => { Console.WriteLine($"Instrument: {instrument.Name} has recorded the measurement {measurement}"); }); listener.MeasurementsCompleted = (instrument, state) => { listener.DisableMeasurementEvents(instrument); }; listener.Start(); } static void AutoRecord(MeterListener listener) { var cts = new CancellationTokenSource(); _ = Task.Run(async () => { while (!cts.IsCancellationRequested) { await Task.Delay(3000); listener.RecordObservableInstruments(); } }); } }
ObservableGauge 儀表盤指標(biāo)
這個(gè)比較好理解, 你可以用來記錄應(yīng)用的內(nèi)存,GC 的內(nèi)存等, 同樣是可觀察指標(biāo), 也需要傳入一個(gè)返回測量值的func委托。
MeterListener listener = new MeterListener(); Start(listener); AutoRecord(listener); Meter meter = new Meter("meter","v1.0"); _ = meter.CreateObservableGauge<long>("GC_Memory_Gauge",() => GC.GetTotalMemory(false)); Console.ReadKey();
程序的輸出如下:
總結(jié)
本文主要介紹了.NET 6 指標(biāo)API System.Diagnostics.Metrics,通過這些API, 可以很方便的收集應(yīng)用的指標(biāo)數(shù)據(jù), 但是本文好像沒有提到數(shù)據(jù)的聚合匯總? 不要擔(dān)心, 運(yùn)行時(shí)團(tuán)隊(duì)針對相應(yīng)的指標(biāo)API已經(jīng)開發(fā)了一系列高性能的聚合API, 預(yù)計(jì)在.NET 6 preview 8 中發(fā)布更新!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ASP.NET 中 Button、LinkButton和ImageButton 三種控件的使用詳解
本文主要介紹Button、LinkButton和ImageButton 三種控件的使用方法,并一一舉例演示它們的用法,希望對大家有所幫助。2016-04-04析構(gòu)函數(shù)的作用 什么是析構(gòu)函數(shù)
這篇文章主要講述了析構(gòu)函數(shù)的概念、原理、功能以及定義格式,析構(gòu)函數(shù)是C#程序設(shè)計(jì)中比較重要的概念,需要的朋友可以參考一下2007-12-12ASP.NET?Core在Linux下為dotnet創(chuàng)建守護(hù)進(jìn)程
本篇主要是怎么樣為我們在Linux或者macOs中部署的dotnet程序創(chuàng)建一個(gè)守護(hù)進(jìn)程,來保證我們的程序在異?;蛘呤请娔X重啟的時(shí)候仍然能夠正常訪問。需要的朋友可以收藏下,方便下次瀏覽觀看2021-12-12IIS7 應(yīng)用程序池的 托管管道模式與集成模式小結(jié)
而 IIS 7 完全整合 .NET 之后,架構(gòu)的處理順序有了很大的不同(如下圖),最主要的原因就是 ASP.NET 從 IIS 插件(ISAPI extension)的角色,進(jìn)入了 IIS 核心,而且也能以 ASP.NET 模塊負(fù)責(zé)處理 IIS 7 的諸多類型要求。2011-02-02.Net?Core中使用MongoDB搭建集群與項(xiàng)目實(shí)戰(zhàn)
本文詳細(xì)講解了.Net?Core中使用MongoDB搭建集群與項(xiàng)目實(shí)戰(zhàn),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02Entity Framework系統(tǒng)架構(gòu)與原理介紹
這篇文章介紹了Entity Framework系統(tǒng)架構(gòu)與原理,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03