自定义EventSourceEventCounter

我自横刀向天笑,去留肝胆两昆仑。这篇文章主要讲述自定义EventSourceEventCounter相关的知识,希望能为你提供帮助。
之前的Counters都是系统内置的,我们只需在进程外,或进程内采集,然后交给专门的展示指标工具即可。本篇说一下自定义EventSource,来采集自己业务中,或自己产品中的指标收集方式。
自定义EventSource是以EventCounters作为核心,EventCounters的作用是实时自动定期推送指标到侦听器的。
【自定义EventSourceEventCounter】在自定义EventSource时,可以使用四种EventCounter:

  • EventCounter:统计指标收集器,比如平均值,最大值,最小值
  • PollingCounter:自定义统计指标收集器,通过自定义统计方法的方式实现对指标的统计
  • IncrementingEventCounter:累加指标收集器,采集一定时间段内的指标汇总
  • IncrementingPollingCounter:自定义累加指标收集器,通过自定义累函数,实现指标收集
  本例先说一下用EventCounter实现自定义EventSource。
本例是定义了一个WorkingEventSouce事件源,定义了WorkingEventListener监听器,和发送指标采集指标的类型EventCounterDemo。 


WorkingEventSouce事件源
/// < summary>
/// Working业务事件源
/// < /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发送业务指标
/// < /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监听器
/// < summary>
/// 指标输出委托
/// < /summary>
/// < param name="key"> < /param>
/// < param name="value"> < /param>
public delegate void WriteContent(string key, string value);
/// < summary>
/// 指标监听器
/// < /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 counterValuehttps://www.songbingjia.com/android/= "";
if (eventPayload.TryGetValue("DisplayName", out object displayValue))

counterName = displayValue.ToString();

if (eventPayload.TryGetValue("Mean", out object value) ||
eventPayload.TryGetValue("Increment", out value))

counterValue = https://www.songbingjia.com/android/value.ToString();

WriteEvent(counterName, counterValue);





发送指标采集指标的类型EventCounterDemo
public class EventCounterDemo

public static void Run()

var listener = new WorkingEventListener();
listener.WriteEvent += Listener_WriteEvent;
new Thread(Working).Start();

//以控制台方式展示采集到的指标
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;



调用方法:
class Program

static void Main()

EventCounterDemo.Run();
Console.Read();


运行结果如下,可以看到,每少发送两次指标,而采集的结果是取其平均值展示了出来。

  下图是跟踪EventListener的OnEventWritten的Payload有效载荷时的数据,可以看到有最大最小值,时间间隔,间隔里的次数,平均值,和当前指标类型为Mean等信息,这是EventCounter指标器的类型原因。

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




    推荐阅读