Support scenario when IsEnabled is called to control flow but there is no event for the same diagnosticName #67
Description
We have recently introduced a new DiagnosticSource usage pattern in the HttpClient and AspNetCore.Hosting:
E.g. in AspNetCore.Hosting:
- producer calls IsEnabled("Microsoft.AspNetCore.Hosting.HttpRequestIn", httpContext) just to determine that listener wants instrumentation to happen. Producer optionally adds context that could help consumer to check if he wants instrumentation for this particular request.
- if 1 is true, producer calls IsEnabled("Microsoft.AspNetCore.Hosting.HttpRequestIn.Start") to check if consumer wants start event and if true, writes event with the same name
- producers just writes IsEnabled("Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop") if instrumentation was on in step 1.
The problem that even if there is no event for the first IsEnabled, user still has to write a method with corresponding DiagnosticName attribute.
class HttpHandlerListener
{
[DiagnosticName("System.Net.Http.HttpRequestOut")]
public void IsEnabled()
{
}
[DiagnosticName("System.Net.Http.HttpRequestOut.Start")]
public void OnStart(HttpRequestMessage request)
{
}
[DiagnosticName("System.Net.Http.HttpRequestOut.Stop")]
public void Stop(HttpResponseMessage response)
{
}
}
I have tried to work it around in the other way with SubscribeWithAdapter(object, Predicate isEnabled),
but isEnabled never gets called if there is no attribute with this diagnosticName.
As a minimum solution, I would expect isEnabled predicate (provided to SubscribeWithAdapter) at least be called and it's result should be taken into account.
But may be IsEnabled should be done in the same way as Write?
[DiagnosticName("System.Net.Http.HttpRequestOut")] //or another attribute?
public bool IsEnabled(HttpContext context)
{
return context.Request.Method != "OPTIONS";
}
And if there is no IsEnabled, DiagnosticName on the Write callback could be used as an indicator that event is enabled