自定义EventSource(三)IncrementingEventCounter

内容纲要

在自定义EventSource时,可以使用四种EventCounter:

  • EventCounter:统计指标收集器,比如平均值,最大值,最小值
  • PollingCounter:自定义统计指标收集器,通过自定义统计方法的方式实现对指标的统计
  • IncrementingEventCounter:累加指标收集器,采集一定时间段内的指标汇总
  • IncrementingPollingCounter:自定义累加指标收集器,通过自定义累函数,实现指标收集

本例先说一下用IncrementingEventCounter实现自定义EventSource。

本例是定义了一个WorkingEventSouce事件源,定义了WorkingEventListener监听器,和发送指标采集指标的类型IncrementingEventCounterDemo。

WorkingEventSouce事件源

  1. [EventSource(Name = "WorkingEventSource")]
  2. public sealed class WorkingEventSource : EventSource
  3. {
  4. public static readonly WorkingEventSource Instance = new WorkingEventSource();
  5. private IncrementingEventCounter _requestCounter;
  6. /// <summary>
  7. /// working-time 是dotnet-counters --counters的参数
  8. /// </summary>
  9. private WorkingEventSource() =>
  10. _requestCounter = new IncrementingEventCounter("working-time", this)
  11. {
  12. DisplayName = "Request Processing Time",
  13. DisplayUnits = "ms"
  14. };
  15. /// <summary>
  16. /// Working发送业务指标
  17. /// </summary>
  18. /// <param name="elapsedMilliseconds"></param>
  19. public void Working(long elapsedMilliseconds)
  20. {
  21. _requestCounter.Increment(elapsedMilliseconds);
  22. }
  23. protected override void Dispose(bool disposing)
  24. {
  25. _requestCounter?.Dispose();
  26. _requestCounter = null;
  27. base.Dispose(disposing);
  28. }
  29. }

WorkingEventListener监听器

  1. /// <summary>
  2. /// 指标输出委托
  3. /// </summary>
  4. /// <param name="key"></param>
  5. /// <param name="value"></param>
  6. public delegate void WriteContent(string key, string value);
  7. /// <summary>
  8. /// 指标监听器
  9. /// </summary>
  10. public class CustomEventListener : EventListener
  11. {
  12. protected readonly string[] _countersName = new string[] { "WorkingEventSource" };
  13. public event WriteContent WriteEvent;
  14. protected override void OnEventSourceCreated(EventSource source)
  15. {
  16. if (_countersName.Contains(source.Name))
  17. {
  18. EnableEvents(source, EventLevel.Verbose, EventKeywords.All, new Dictionary<string, string>()
  19. {
  20. ["EventCounterIntervalSec"] = "1"
  21. });
  22. }
  23. }
  24. protected override void OnEventWritten(EventWrittenEventArgs eventData)
  25. {
  26. if (!eventData.EventName.Equals("EventCounters"))
  27. {
  28. return;
  29. }
  30. for (int i = 0; i < eventData.Payload.Count; ++i)
  31. {
  32. if (eventData.Payload[i] is IDictionary<string, object> eventPayload)
  33. {
  34. var counterName = "";
  35. var counterValue = "";
  36. if (eventPayload.TryGetValue("DisplayName", out object displayValue))
  37. {
  38. counterName = displayValue.ToString();
  39. }
  40. if (eventPayload.TryGetValue("Mean", out object value) ||
  41. eventPayload.TryGetValue("Increment", out value))
  42. {
  43. counterValue = value.ToString();
  44. }
  45. WriteEvent(counterName, counterValue);
  46. }
  47. }
  48. }
  49. }

发送指标采集指标的类型IncrementingEventCounterDemo

  1. public class IncrementingEventCounterDemo
  2. {
  3. public static void Run()
  4. {
  5. var listener = new CustomEventListener();
  6. listener.WriteEvent += Listener_WriteEvent;
  7. new Thread(Working).Start();
  8. }
  9. /// <summary>
  10. /// 以控制台方式展示采集到的指标
  11. /// </summary>
  12. /// <param name="key"></param>
  13. /// <param name="value"></param>
  14. private static void Listener_WriteEvent(string key, string value)
  15. {
  16. Console.WriteLine($"{key}:{value}");
  17. }
  18. /// <summary>
  19. /// 模拟写业务指标,每秒写两次,i递增
  20. /// </summary>
  21. static void Working()
  22. {
  23. int i = 0;
  24. while (true)
  25. {
  26. var count = i;
  27. Console.WriteLine(count);
  28. WorkingEventSource.Instance.Working(count);
  29. System.Threading.Thread.Sleep(500);
  30. i += 1;
  31. }
  32. }
  33. }

结果,可以看到,每次都是时间段里数值之和

下图是跟踪EventListener的OnEventWritten的Payload有效载荷时的数据,可以看到有时间段内总和—Increment,次数,时间间隔等信息,这是因为当前计数据类型是IncrementingEventCounter。

.NET

自定义EventSource(二)PollingCounter

2022-7-25 11:30:46

.NET

自定义EventSource(四)IncrementingPollingCounter

2022-7-26 9:02:31

0 条回复 A文章作者 M管理员
欢迎您,新朋友,感谢参与互动!
    暂无讨论,说说你的看法吧
个人中心
今日签到
私信列表
搜索