自定义EventSource(三)IncrementingEventCounter
在自定義EventSource時,可以使用四種EventCounter:
EventCounter:統計指標收集器,比如平均值,最大值,最小值
PollingCounter:自定義統計指標收集器,通過自定義統計方法的方式實現對指標的統計
IncrementingEventCounter:累加指標收集器,采集一定時間段內的指標匯總
IncrementingPollingCounter:自定義累加指標收集器,通過自定義累函數,實現指標收集
本例先說一下用IncrementingEventCounter實現自定義EventSource。
本例是定義了一個WorkingEventSouce事件源,定義了WorkingEventListener監聽器,和發送指標采集指標的類型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的參數/// </summary>private WorkingEventSource() =>_requestCounter = new IncrementingEventCounter("working-time", this){DisplayName = "Request Processing Time",DisplayUnits = "ms"};/// <summary>/// Working發送業務指標/// </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監聽器
/// <summary> /// 指標輸出委托 /// </summary> /// <param name="key"></param> /// <param name="value"></param> public delegate void WriteContent(string key, string value); /// <summary> /// 指標監聽器 /// </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);}}} }發送指標采集指標的類型IncrementingEventCounterDemo
public class IncrementingEventCounterDemo{public static void Run(){var listener = new CustomEventListener();listener.WriteEvent += Listener_WriteEvent;new?Thread(Working).Start();}/// <summary>/// 以控制臺方式展示采集到的指標/// </summary>/// <param name="key"></param>/// <param name="value"></param>private static void Listener_WriteEvent(string key, string value){Console.WriteLine($"{key}:{value}");}/// <summary>/// 模擬寫業務指標,每秒寫兩次,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;}} }結果,可以看到,每次都是時間段里數值之和
下圖是跟蹤EventListener的OnEventWritten的Payload有效載荷時的數據,可以看到有時間段內總和—Increment,次數,時間間隔等信息,這是因為當前計數據類型是IncrementingEventCounter。
總結
以上是生活随笔為你收集整理的自定义EventSource(三)IncrementingEventCounter的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dotnet 是 前30个增长最快速度的
- 下一篇: 为什么应该用record来定义DTO