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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

自定义EventSource(三)IncrementingEventCounter

發布時間:2023/12/4 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义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的全部內容,希望文章能夠幫你解決所遇到的問題。

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