自定义EventSource(三)IncrementingEventCounter
在自定義EventSource時,可以使用四種EventCounter:
EventCounter:統(tǒng)計指標(biāo)收集器,比如平均值,最大值,最小值
PollingCounter:自定義統(tǒng)計指標(biāo)收集器,通過自定義統(tǒng)計方法的方式實現(xiàn)對指標(biāo)的統(tǒng)計
IncrementingEventCounter:累加指標(biāo)收集器,采集一定時間段內(nèi)的指標(biāo)匯總
IncrementingPollingCounter:自定義累加指標(biāo)收集器,通過自定義累函數(shù),實現(xiàn)指標(biāo)收集
本例先說一下用IncrementingEventCounter實現(xiàn)自定義EventSource。
本例是定義了一個WorkingEventSouce事件源,定義了WorkingEventListener監(jiān)聽器,和發(fā)送指標(biāo)采集指標(biāo)的類型IncrementingEventCounterDemo。
WorkingEventSouce事件源
[EventSource(Name = "WorkingEventSource")] public sealed class WorkingEventSource : EventSource {public static readonly WorkingEventSource Instance = new WorkingEventSource();private IncrementingEventCounter _requestCounter;/// <summary>/// working-time 是dotnet-counters --counters的參數(shù)/// </summary>private WorkingEventSource() =>_requestCounter = new IncrementingEventCounter("working-time", this){DisplayName = "Request Processing Time",DisplayUnits = "ms"};/// <summary>/// Working發(fā)送業(yè)務(wù)指標(biāo)/// </summary>/// <param name="elapsedMilliseconds"></param>public void Working(long elapsedMilliseconds){_requestCounter.Increment(elapsedMilliseconds);}protected override void Dispose(bool disposing){_requestCounter?.Dispose();_requestCounter = null;base.Dispose(disposing);} }WorkingEventListener監(jiān)聽器
/// <summary> /// 指標(biāo)輸出委托 /// </summary> /// <param name="key"></param> /// <param name="value"></param> public delegate void WriteContent(string key, string value); /// <summary> /// 指標(biāo)監(jiān)聽器 /// </summary> public class CustomEventListener : EventListener {protected readonly string[] _countersName = new string[] { "WorkingEventSource" };public event WriteContent WriteEvent;protected override void OnEventSourceCreated(EventSource source){if (_countersName.Contains(source.Name)){EnableEvents(source, EventLevel.Verbose, EventKeywords.All, new Dictionary<string, string>(){["EventCounterIntervalSec"] = "1"});}}protected override void OnEventWritten(EventWrittenEventArgs eventData){if (!eventData.EventName.Equals("EventCounters")){return;}for (int i = 0; i < eventData.Payload.Count; ++i){if (eventData.Payload[i] is IDictionary<string, object> eventPayload){var counterName = "";var counterValue = "";if (eventPayload.TryGetValue("DisplayName", out object displayValue)){counterName = displayValue.ToString();}if (eventPayload.TryGetValue("Mean", out object value) ||eventPayload.TryGetValue("Increment", out value)){counterValue = value.ToString();}WriteEvent(counterName, counterValue);}}} }發(fā)送指標(biāo)采集指標(biāo)的類型IncrementingEventCounterDemo
public class IncrementingEventCounterDemo{public static void Run(){var listener = new CustomEventListener();listener.WriteEvent += Listener_WriteEvent;new?Thread(Working).Start();}/// <summary>/// 以控制臺方式展示采集到的指標(biāo)/// </summary>/// <param name="key"></param>/// <param name="value"></param>private static void Listener_WriteEvent(string key, string value){Console.WriteLine($"{key}:{value}");}/// <summary>/// 模擬寫業(yè)務(wù)指標(biāo),每秒寫兩次,i遞增/// </summary>static void Working(){int i = 0;while (true){var count = i;Console.WriteLine(count);WorkingEventSource.Instance.Working(count);System.Threading.Thread.Sleep(500);i += 1;}} }結(jié)果,可以看到,每次都是時間段里數(shù)值之和
下圖是跟蹤EventListener的OnEventWritten的Payload有效載荷時的數(shù)據(jù),可以看到有時間段內(nèi)總和—Increment,次數(shù),時間間隔等信息,這是因為當(dāng)前計數(shù)據(jù)類型是IncrementingEventCounter。
總結(jié)
以上是生活随笔為你收集整理的自定义EventSource(三)IncrementingEventCounter的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dotnet 是 前30个增长最快速度的
- 下一篇: 为什么应该用record来定义DTO