You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
EventCounter events not filtered by EventSource/EventListener relationship #31927
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'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.
usingvar_=newHttpEventListener();usingvar__=newSocketsEventListener();usingvarhttpClient=newHttpClient();awaithttpClient.GetStringAsync("http://httpbin.org/get");awaitTask.Delay(3000);sealedclassHttpEventListener:EventListener{protectedoverridevoidOnEventSourceCreated(EventSourceeventSource){if(eventSource.Name=="System.Net.Http"){EnableEvents(eventSource,EventLevel.Informational,EventKeywords.None,newDictionary<string,string?>{{"EventCounterIntervalSec",1.ToString()}});}}protectedoverridevoidOnEventWritten(EventWrittenEventArgseventData){Console.WriteLine($"Http saw an event from {eventData.EventSource.Name}");}}sealedclassSocketsEventListener:EventListener{protectedoverridevoidOnEventSourceCreated(EventSourceeventSource){if(eventSource.Name=="System.Net.Sockets"){EnableEvents(eventSource,EventLevel.Informational,EventKeywords.None,newDictionary<string,string?>{{"EventCounterIntervalSec",1.ToString()}});}}protectedoverridevoidOnEventWritten(EventWrittenEventArgseventData){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'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(...).
Activity
hoyosjs commentedon Feb 7, 2020
@josalem @sywhang
cc: @dotnet/dotnet-diag
shirhatti commentedon Feb 10, 2020
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) withEventListener(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
EventListenerand only enable the events you want to consume. See https://github.com/shirhatti/Metrics/blob/master/src/Microsoft.Extensions.Metrics/Listener.cs#L19-L63sywhang commentedon Feb 10, 2020
@SteveHarveyUK thanks for reporting this! I've seen this issue before and yup, this shouldn't be happening (i.e. it is a bug).
KalleOlaviNiemitalo commentedon Mar 4, 2020
Is this a duplicate of #31500?
sywhang commentedon Mar 4, 2020
@KalleOlaviNiemitalo Yes, it is.
1 remaining item
MihaZupan commentedon Apr 18, 2023
YARP got bit by this one as well: dotnet/yarp#2102
Standalone repro
davmason commentedon Apr 18, 2023
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.
noahfalk commentedon Aug 12, 2024
I suspect @JamesNK hit this issue here.
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.
antmjones commentedon Feb 24, 2025
@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 thanthis.OnEventSourceCreated(...).martincostello commentedon Aug 14, 2026
Just came across a link to this issue in an OpenTelemetry PR for a workaround, so ➕.