Skip to content

Commit

Permalink
Optimize AspNet instrumentation for SuppressInstrumentation (#1903)
Browse files Browse the repository at this point in the history
Co-authored-by: Cijo Thomas <[email protected]>
  • Loading branch information
utpilla and cijothomas committed Mar 16, 2021
1 parent c75fc6a commit 6d2a37a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,26 @@ public HttpInListener(string name, AspNetInstrumentationOptions options)
[System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "Activity is retrieved from Activity.Current later and disposed.")]
public override void OnStartActivity(Activity activity, object payload)
{
var context = HttpContext.Current;
if (context == null)
// The overall flow of what AspNet library does is as below:
// Activity.Start()
// DiagnosticSource.WriteEvent("Start", payload)
// DiagnosticSource.WriteEvent("Stop", payload)
// Activity.Stop()

// This method is in the WriteEvent("Start", payload) path.
// By this time, samplers have already run and
// activity.IsAllDataRequested populated accordingly.

if (Sdk.SuppressInstrumentation)
{
AspNetInstrumentationEventSource.Log.NullPayload(nameof(HttpInListener), nameof(this.OnStartActivity));
return;
}

try
{
if (this.options.Filter?.Invoke(context) == false)
{
AspNetInstrumentationEventSource.Log.RequestIsFilteredOut(activity.OperationName);
activity.IsAllDataRequested = false;
return;
}
}
catch (Exception ex)
// Ensure context extraction irrespective of sampling decision
var context = HttpContext.Current;
if (context == null)
{
AspNetInstrumentationEventSource.Log.RequestFilterException(ex);
activity.IsAllDataRequested = false;
AspNetInstrumentationEventSource.Log.NullPayload(nameof(HttpInListener), nameof(this.OnStartActivity));
return;
}

Expand Down Expand Up @@ -108,15 +108,31 @@ public override void OnStartActivity(Activity activity, object payload)
}
}

// see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/data-semantic-conventions.md
var path = requestValues.Path;
activity.DisplayName = path;

ActivityInstrumentationHelper.SetActivitySourceProperty(activity, ActivitySource);
ActivityInstrumentationHelper.SetKindProperty(activity, ActivityKind.Server);

if (activity.IsAllDataRequested)
{
try
{
if (this.options.Filter?.Invoke(context) == false)
{
AspNetInstrumentationEventSource.Log.RequestIsFilteredOut(activity.OperationName);
activity.IsAllDataRequested = false;
return;
}
}
catch (Exception ex)
{
AspNetInstrumentationEventSource.Log.RequestFilterException(ex);
activity.IsAllDataRequested = false;
return;
}

ActivityInstrumentationHelper.SetActivitySourceProperty(activity, ActivitySource);
ActivityInstrumentationHelper.SetKindProperty(activity, ActivityKind.Server);

// see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/data-semantic-conventions.md
var path = requestValues.Path;
activity.DisplayName = path;

if (request.Url.Port == 80 || request.Url.Port == 443)
{
activity.SetTag(SemanticConventions.AttributeHttpHost, request.Url.Host);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public void AspNetRequestsAreCollectedSuccessfully(
{
options.Filter = httpContext =>
{
Assert.True(Activity.Current.IsAllDataRequested);
if (string.IsNullOrEmpty(filter))
{
return true;
Expand Down Expand Up @@ -341,6 +342,7 @@ private static Action<Activity, string, object> GetEnrichmentAction(Status statu

enrichAction = (activity, method, obj) =>
{
Assert.True(activity.IsAllDataRequested);
switch (method)
{
case "OnStartActivity":
Expand Down

0 comments on commit 6d2a37a

Please sign in to comment.