• 自定义EventSource(四)IncrementingPollingCounter


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

    • EventCounter:统计指标收集器,比如平均值,最大值,最小值

    • PollingCounter:自定义统计指标收集器,通过自定义统计方法的方式实现对指标的统计

    • IncrementingEventCounter:累加指标收集器,采集一定时间段内的指标汇总

    • IncrementingPollingCounter:自定义累加指标收集器,通过自定义累函数,实现指标收集

       


       

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

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

    WorkingEventSouce事件源

    /// <summary>
    /// 自定义事件源
    /// </summary>
    [EventSource(Name = "WorkingEventSource")]
    public sealed class WorkingEventSource : EventSource
    {
        public static readonly WorkingEventSource Instance = new WorkingEventSource();
    
        private IncrementingPollingCounter _requestCounter;
    
        private WorkingEventSource() =>
            _requestCounter = new IncrementingPollingCounter("working-time", this, MetricProvider)
            {
                DisplayName = "Working Time",
                DisplayUnits = "ms"
            };
        private double _elapsedMilliseconds = 0;
        /// <summary>
        /// 自定义函数,来计算指标,本例模拟EventCounter的计算方式
        /// </summary>
        /// <returns></returns>
        double MetricProvider()
        {
            return _elapsedMilliseconds;
        }
        /// <summary>
        /// Working发送业务指标
        /// </summary>
        /// <param name="elapsedMilliseconds"></param>
        public void Working(long elapsedMilliseconds)
        {
            _elapsedMilliseconds += 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);
                }
            }
        }
    }

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

    public class IncrementingPollingCounterDemo
    {
        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;
            }
        }
    }

    运行结果如下,当然这个值是MetricProvider计算出来的,可以按自己收集指标的算法,算每次采集到的值并返回

     

      想要更快更方便的了解相关知识,可以关注微信公众号 

     

     

     

     

  • 相关阅读:
    数据库范式
    java String.split()用法
    1.4 IoC控制反转
    利用shrinkwrap锁定依赖版本
    清晨开启电脑自动拉取项目更新
    JS如何获取屏幕、浏览器及网页高度宽度?
    Navigator 对象,能够清楚地知道浏览器的相关信息
    iconfont 转换为图标字体
    VS code的搜索、替换与正则替换
    点九图制作方法
  • 原文地址:https://www.cnblogs.com/ljknlb/p/15927232.html
Copyright © 2020-2023  润新知