自定义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相关推荐
- 自定义EventSource(二)PollingCounter
在自定义EventSource时,可以使用四种EventCounter: EventCounter:统计指标收集器,比如平均值,最大值,最小值 PollingCounter:自定义统计指标收集器,通过 ...
- 自定义EventSource(一)EventCounter
之前的Counters都是系统内置的,我们只需在进程外,或进程内采集,然后交给专门的展示指标工具即可.本篇说一下自定义EventSource,来采集自己业务中,或自己产品中的指标收集方式. 自定义Ev ...
- 【智能CAN/串口转换器提供“透明转换、透明带标识转换和自定义协议转换”三种转换模式介绍、CAN转换器、CAN 485、CAN 232】
智能CAN/串口协议转换器LCNET Pro RS-232/485提供一路RS-485.一路RS-232和一路CAN通道,实现CAN与串口RS-485或RS-232之间的双向数据智能转换.每个通道独立 ...
- Android自定义动画三-SVG动画
Android自定义动画三-SVG动画 本篇文章主要是对SVG的一个介绍和使用,以及Android中对SVG的一个支持,从而可以帮助我们在android下很轻松的通过SVG实现一些非常酷炫的动画效果. ...
- Android 自定义viewpager 三张图片在同一屏幕轮播的效果
github:https://github.com/nickeyCode/RoundImageViewPager 说实话不知道怎么描述这个效果,在网页上见得跟多,公司要求做这个效果得时候不知道怎么用文 ...
- Android自定义View 开发流程综合简述 Android自定义View(三)
本文简述一下自定义View中常用方法 1 简述 自定义View可以认为是继承自View或者ViewGroup Android中的任何一个布局.任何一个控件其实都是直接或间接继承自View的,如Text ...
- (转)datagridview 自定义列三步走
本文转载自:http://blog.csdn.net/zx13525079024/article/details/4814642 我们如果想自定义实现datagridview的某列,例如是datagr ...
- android 自定义进度条_第一百八十九回:Android中自定义ProgressBar三
各位看官们大家好,上一回中咱们说的是Android中自定义ProgressBar的例子,这一回咱们继续说该例子.闲话休提,言归正转.让我们一起Talk Android吧! 看官们,我们在上一回是通过自 ...
- vue自定义组件三步走
如图所示:我自定义了两个组件,一个是Head.vue,一个是Foot.vue 我现在在Page01.vue中用他们两个,公分三步 1,引用 2,注册 3,使用
最新文章
- 张亚勤、张宏江:人工智能的未来是什么?
- AA065VD数据线连接错位的现象及分析总结
- AIDL 客户端与服务端的双向通信
- where is Fiori count server execution interval configured
- java更好的语言_Java,如果这是一个更好的世界
- 公众号文章折叠点击后展开案例_(案例)蜂窝纸板在包装中的应用内衬
- 有前端基础学前端要多久?
- Sushiswap团队开发者:Sushi在Q1实现400亿美元的交易量,xSUSHI持有者获得2000万美元费用
- php log日志管理,PHP日志LOG类定义与用法示例
- java day21【缓冲流、转换流、序列化流】
- c语言饭卡管理系统_C语言饭卡管理系统(附代码) -
- 1996年考研数学一解析pdf
- java联机对战五子棋游戏(SWT版)
- OFFICE2016用过一段时间后正版密钥显示未激活问题
- xpath跨级定位(parent::,following-sibling::, preceding-sibling)
- 带翻转特效的会员登录注册html页面源码
- Nginx官方文档(十一)【HTTP之ngx_http_core_module】
- Eclipse中更改tomcat版本
- 游戏建模次世代角色模型制作教程,内藏超级无敌干货!
- MATLAB初入门(一)
热门文章
- 速达5000出现计算成本数据溢出的问题
- 通过修改然后commit的方式创建自己的镜像
- shell脚本--cut命令
- for(auto c:s)与for(auto c:s)
- codevs原创抄袭题 5960 信使
- 扼杀 304,Cache-Control: immutable
- jQuery banner切换插件
- 妄想性仮想人格障害 新手教程 +改动器
- [转]Myeclipse9引入easy_ui时,其中的部分js文件报错
- 微软相关的开发资源列表(update)