Only enable events/counters if there are listeners available #928
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Replacement for #915, description is NOT the same
Removed the
AddProxyTelemetryListener
,AddHttpTelemetryListener
, ... extension methods as they don't provide any benefit over justAddTelemetryListeners
after this change.The listeners no longer fetch consumers per request (
GetServices
) - instead they get the consumers injected in the constructor. This means the core listeners only respect singleton consumers.If needed, we can expose wrappers/helpers that add scoped consumer lifetimes.
Added an abstract
EventListenerService
class as a base of all listeners.It abstracts away the synchronization around enabling the EventSource based on parameters from the constructor.
The listener implementations now only enable the necessary
EventSource
s with the needed parameters. We will only enable events if a telemetry consumer is present & only enable event counters if a metrics consumer is present. Previously we would always enable events & metrics, even if only metrics were being used.EventSource
&EventSourceListener
have a weird interaction where theOnEventSourceCreated
may be called from the base constructor. That means parameters passed to the listener constructor can't control what happens inOnEventSourceCreated
.To work around that, the
EventSourceName
is taken from a property implemented by derived types instead of being a ctor argument.Since only the ctor can decide on whether we need events/metrics, we delay calling
EnableEvents
until the ctor runs and inspects the service collection.We avoid returning from
OnEventSourceCreated
before enabling theEventSource
so that the first event isn't dropped.A side-effect of the change to use singleton consumers is that this library now works with non-proxy scenarios out-of-the-box. For example, a user can register an
IHttpProxyTelemetryConsumer
and they will observe all Http events from HttpClients even outside the AspNetCore pipeline (as we are no longer bound by the HttpContext scope).I added default interface implementations for all
I*TelemetryConsumer
methods. This makes it much easier to implement consumers that don't care about ALL the events - a user only overrides events they are interested in.Metrics sample will be updated later as it targets a NuGet package.