Skip to content

Instantly share code, notes, and snippets.

@itn3000
Last active October 22, 2021 00:39
Show Gist options
  • Save itn3000/9001546f1a0a9bcd53b7bc0ae4184bbd to your computer and use it in GitHub Desktop.
Save itn3000/9001546f1a0a9bcd53b7bc0ae4184bbd to your computer and use it in GitHub Desktop.
MetricEventSource and its parameters test
using System.Diagnostics.Tracing;
using System.Text.Json;
using var m = new System.Diagnostics.Metrics.Meter("m1");
using var evlistener = new MyEventListener();
// var c1 = m.CreateCounter<int>("c1");
var h1 = m.CreateHistogram<int>("h1");
Console.WriteLine($"1");
h1.Record(1, new KeyValuePair<string, object?>("htag1", 1));
Console.WriteLine($"2");
h1.Record(1, new KeyValuePair<string, object?>("htag1", 2));
Console.WriteLine($"3");
h1.Record(1, new KeyValuePair<string, object?>("htag1", 3));
Console.WriteLine($"4");
h1.Record(1, new KeyValuePair<string, object?>("htag1", 3));
Console.WriteLine($"5");
class MyEventListener : EventListener
{
protected override void OnEventSourceCreated(EventSource ev)
{
if (ev.Name == "System.Diagnostics.Metrics")
{
var args = new Dictionary<string, string>()
{
{"Metrics", "m1"},
{"RefreshInterval", "10"},
{"MaxTimeSeries", "3"},
{"MaxHistograms", "2"},
};
this.EnableEvents(ev, EventLevel.LogAlways, (EventKeywords)0xffffffff, args);
}
}
protected override void OnEventWritten(EventWrittenEventArgs evarg)
{
var jsonstr = JsonSerializer.Serialize(new
{
name = evarg.EventName,
keywords = evarg.Keywords,
payload = evarg.PayloadNames.Zip(evarg.Payload).ToDictionary(x => x.First, x => x.Second)
});
Console.WriteLine(jsonstr);
}
}
{"name":"Message","keywords":263882790666241,"payload":{"Message":"New session started. SessionId auto-generated: 9039b8e4-b93e-431c-a851-506d19ec1893"}}
{"name":"Message","keywords":263882790666241,"payload":{"Message":"RefreshInterval argument received: 10"}}
{"name":"Message","keywords":263882790666241,"payload":{"Message":"MaxTimeSeries argument received: 3"}}
{"name":"Message","keywords":263882790666241,"payload":{"Message":"MaxHistograms argument received: 2"}}
{"name":"Message","keywords":263882790666241,"payload":{"Message":"Metrics argument received: m1"}}
{"name":"Message","keywords":263882790666241,"payload":{"Message":"Parsed metric: {spec}"}}
{"name":"InitialInstrumentEnumerationComplete","keywords":263882790666246,"payload":{"sessionId":"9039b8e4-b93e-431c-a851-506d19ec1893"}}
{"name":"InstrumentPublished","keywords":263882790666244,"payload":{"sessionId":"9039b8e4-b93e-431c-a851-506d19ec1893","meterName":"m1","meterVersion":"","instrumentName":"h1","instrumentType":"Histogram\u00601","unit":"","description":""}}
{"name":"BeginInstrumentReporting","keywords":263882790666242,"payload":{"sessionId":"9039b8e4-b93e-431c-a851-506d19ec1893","meterName":"m1","meterVersion":"","instrumentName":"h1","instrumentType":"Histogram\u00601","unit":"","description":""}}
1
2
3
{"name":"HistogramLimitReached","keywords":263882790666242,"payload":{"sessionId":"9039b8e4-b93e-431c-a851-506d19ec1893"}}
4
{"name":"TimeSeriesLimitReached","keywords":263882790666242,"payload":{"sessionId":"9039b8e4-b93e-431c-a851-506d19ec1893"}}
5
@itn3000
Copy link
Author

itn3000 commented Oct 22, 2021

  • This program output "MetricsEventSource" events from Meter named "m1"
  • It records histogram value with tag htag1=0, htag1=1, htag1=2, htag1=3
  • It was expected to raise "HistogramLimitReached" event and "TimeSeriesLimitReached" was not raised
    • Actually, both events were raised
  • Even if "MaxTimeSeries" was increased to 10, "TimeSeriesLimitReached" was raised too
  • When "MaxHistograms" was increased to 4, "TimeSeriesLimitReached" was not raised

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment