自定义EventSource(一)EventCounter
之前的Counters都是系統(tǒng)內(nèi)置的,我們只需在進(jìn)程外,或進(jìn)程內(nèi)采集,然后交給專門的展示指標(biāo)工具即可。本篇說一下自定義EventSource,來(lái)采集自己業(yè)務(wù)中,或自己產(chǎn)品中的指標(biāo)收集方式。
自定義EventSource是以EventCounters作為核心,EventCounters的作用是實(shí)時(shí)自動(dòng)定期推送指標(biāo)到偵聽器的。
在自定義EventSource時(shí),可以使用四種EventCounter:
EventCounter:統(tǒng)計(jì)指標(biāo)收集器,比如平均值,最大值,最小值
PollingCounter:自定義統(tǒng)計(jì)指標(biāo)收集器,通過自定義統(tǒng)計(jì)方法的方式實(shí)現(xiàn)對(duì)指標(biāo)的統(tǒng)計(jì)
IncrementingEventCounter:累加指標(biāo)收集器,采集一定時(shí)間段內(nèi)的指標(biāo)匯總
IncrementingPollingCounter:自定義累加指標(biāo)收集器,通過自定義累函數(shù),實(shí)現(xiàn)指標(biāo)收集
本例先說一下用EventCounter實(shí)現(xiàn)自定義EventSource。
本例是定義了一個(gè)WorkingEventSouce事件源,定義了WorkingEventListener監(jiān)聽器,和發(fā)送指標(biāo)采集指標(biāo)的類型EventCounterDemo。
WorkingEventSouce事件源
///?<summary> /// Working業(yè)務(wù)事件源 /// </summary> [EventSource(Name = "WorkingEventSource")] public sealed class WorkingEventSource : EventSource {public static readonly WorkingEventSource Instance = new WorkingEventSource();private EventCounter _workingCounter;private WorkingEventSource(){_workingCounter = new EventCounter("working-time", this){DisplayName = "Working Time",DisplayUnits = "ms"};} /// <summary>/// Working發(fā)送業(yè)務(wù)指標(biāo)/// </summary>/// <param name="elapsedMilliseconds"></param>public void Working(long elapsedMilliseconds){_workingCounter?.WriteMetric(elapsedMilliseconds);}protected override void Dispose(bool disposing){_workingCounter?.Dispose();_workingCounter = 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 WorkingEventListener : 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")){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)的類型EventCounterDemo
public class EventCounterDemo {public static void Run(){var listener = new WorkingEventListener();listener.WriteEvent += Listener_WriteEvent;new Thread(Working).Start();}//以控制臺(tái)方式展示采集到的指標(biāo)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;}} }調(diào)用方法:
class Program{static void Main(){EventCounterDemo.Run();Console.Read();}}運(yùn)行結(jié)果如下,可以看到,每少發(fā)送兩次指標(biāo),而采集的結(jié)果是取其平均值展示了出來(lái)。
下圖是跟蹤EventListener的OnEventWritten的Payload有效載荷時(shí)的數(shù)據(jù),可以看到有最大最小值,時(shí)間間隔,間隔里的次數(shù),平均值,和當(dāng)前指標(biāo)類型為Mean等信息,這是EventCounter指標(biāo)器的類型原因。
總結(jié)
以上是生活随笔為你收集整理的自定义EventSource(一)EventCounter的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Blazor 事件处理开发指南
- 下一篇: WPF开源项目:AIStudio.Wpf