EventCounter events not filtered by EventSource/EventListener relationship
When using the System.Diagnostics.Tracing EventSource, EventListener and EventCounter classes, it would appear that the fired EventCounter events are not being filtered through the EventSource to the EventListener.
All of the custom EventListener instances I create seem to receive all EventCounter events regardless of which EventSource I use to EnableEvents on.
Not entirely sure if this is as designed, or if I'm doing something wrong, but I was expecting that EventListeners would only get events through the EventSource they were enabled against.
I have a repo that contains an example project that reproduces this issue (Thanks to Expecho and his blog): https://github.com/SteveHarveyUK/Blog.git
Be interested to see if this is a bug or by design.
@josalem @sywhang cc: @dotnet/dotnet-diag
I'll admit I haven't attempted to run your sample, but I have a good guess on what's going on.
You're conflating EventSource (where you synthesize Events/Metrics) with EventListener (used for consuming Events/Metrics). In addition to your synthesized metrics, the runtime also synthesizes metrics for things that happen in the runtime.
I'd recommend using this pattern in your EventListener and only enable the events you want to consume. See https://github.com/shirhatti/Metrics/blob/master/src/Microsoft.Extensions.Metrics/Listener.cs#L19-L63
@SteveHarveyUK thanks for reporting this! I've seen this issue before and yup, this shouldn't be happening (i.e. it is a bug).
Is this a duplicate of #31500?
@KalleOlaviNiemitalo Yes, it is.
YARP got bit by this one as well: https://github.com/microsoft/reverse-proxy/issues/2102
Standalone repro
using var _ = new HttpEventListener();
using var __ = new SocketsEventListener();
using var httpClient = new HttpClient();
await httpClient.GetStringAsync("http://httpbin.org/get");
await Task.Delay(3000);
sealed class HttpEventListener : EventListener
{
protected override void OnEventSourceCreated(EventSource eventSource)
{
if (eventSource.Name == "System.Net.Http")
{
EnableEvents(eventSource, EventLevel.Informational, EventKeywords.None,
new Dictionary<string, string?> { { "EventCounterIntervalSec", 1.ToString() } });
}
}
protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
Console.WriteLine($"Http saw an event from {eventData.EventSource.Name}");
}
}
sealed class SocketsEventListener : EventListener
{
protected override void OnEventSourceCreated(EventSource eventSource)
{
if (eventSource.Name == "System.Net.Sockets")
{
EnableEvents(eventSource, EventLevel.Informational, EventKeywords.None,
new Dictionary<string, string?> { { "EventCounterIntervalSec", 1.ToString() } });
}
}
protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
Console.WriteLine($"Sockets saw an event from {eventData.EventSource.Name}");
}
}
Http saw an event from System.Net.Http
Sockets saw an event from System.Net.Http
Http saw an event from System.Net.Sockets
Sockets saw an event from System.Net.Sockets
I just took a look and learned something new about EventListeners. We create a dispatcher for every EventListener and EventSource combination, even if you never call EnableEvents.
Then in WriteEvent, if it is a self describing event we uncontionally write it to all dispatchers. So for all self describing events (which counters uses) we will always write to all EventListeners, even if they never called EnableEvents.
I'm torn here, it is clearly a bug but it has been this way forever and would be a breaking change if we altered the behavior.
I suspect @JamesNK hit this issue here.
I'm torn here, it is clearly a bug but it has been this way forever and would be a breaking change if we altered the behavior.
I think we've had reports from at least 5 different people hitting this issue and I expect it is going to keep happening. While I wouldn't say its urgent I do think we should fix this. We could always offer a compat-switch to aid with back compat.
@noahfalk make that 6 people! Just wasted a chunk of time thinking I done something wrong with an EventListener because it was receiving events I wasn't expecting.
Also to echo what @davmason has noted above - while stepping through framework source trying to work out what I had done wrong, I was surprised to find that it appears that an EventDispatcher is getting created for every EventListener (at least if that EventSource existed before the EventListener was created), even if that EventListener is in no way interested in the EventSource. For example this line in the EventListener constructor calls args.EventSource.AddListener(...) rather than this.OnEventSourceCreated(...).
Just came across a link to this issue in an OpenTelemetry PR for a workaround, so ➕.