Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AzureSdkDiagnosticListener from crashing user app #2294

Merged
merged 2 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,32 @@ public void CleanUp()
}
}

[TestMethod]
public void AzureSdkListenerDoesNotThrowAfterInitialization()
{
using (var listener = new DiagnosticListener("Azure.SomeClient"))
using (var module = new DependencyTrackingTelemetryModule())
{
module.Initialize(this.configuration);

// Dispose config after initialize
// If AzureSdkListener attempted to create
// new TelemetryClient after initialize, it'd throw.
this.configuration.Dispose();

Activity sendActivity = new Activity("Azure.SomeClient.Send");
sendActivity.AddTag("kind", "client");

listener.StartActivity(sendActivity, null);
listener.StopActivity(sendActivity, null);

var listenerAnother = new DiagnosticListener("Azure.AnotherClient");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these statements represent customer code (typically when they use some Azure sdks) which would have thrown unhandled exception, without the fix in this PR.
This would have resulted in exception, only if the DiagnosticListener was created after SDK initialization, because any DiagnosticListener created before SDK initialization will get callback within the subscribe code itself, which is try..catch..wrapped. (https://github.com/microsoft/ApplicationInsights-dotnet/blob/develop/WEB/Src/DependencyCollector/DependencyCollector/Implementation/DiagnosticSourceListenerBase.cs#L52-L58).

Any listener created afterwards goes here (https://github.com/microsoft/ApplicationInsights-dotnet/blob/develop/WEB/Src/DependencyCollector/DependencyCollector/Implementation/DiagnosticSourceListenerBase.cs#L72-L97) which has no try..catch. Will do a follow up to make sure SDK is protected in all those scenarios as well.

var listenerYetAnother = new DiagnosticListener("Azure.YetAnotherClient");

Assert.AreEqual(0, this.sentItems.Count);
}
}

[TestMethod]
public void AzureClientSpansNotCollectedWhenDisabled()
{
Expand Down Expand Up @@ -93,7 +119,7 @@ public void AzureClientSpansAreCollected()
Assert.AreEqual(sendActivity.SpanId.ToHexString(), telemetry.Id);

Assert.AreEqual("v1", telemetry.Properties["k1"]);

Assert.IsTrue(telemetry.Properties.TryGetValue("tracestate", out var tracestate));
Assert.AreEqual("state=some", tracestate);
}
Expand Down Expand Up @@ -847,7 +873,7 @@ public void AzureClientSpansAreCollectedForHttp()
.AddTag("serviceRequestId", "service-request-id");

listener.StopActivity(httpActivity, payload);

var telemetry = this.sentItems.Last() as DependencyTelemetry;

Assert.IsNotNull(telemetry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal override bool IsActivityEnabled(string evnt, object context)

protected override IDiagnosticEventHandler GetEventHandler(string diagnosticListenerName)
{
return new AzureSdkDiagnosticsEventHandler(this.Configuration);
return new AzureSdkDiagnosticsEventHandler(this.Client);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal class AzureSdkDiagnosticsEventHandler : DiagnosticsEventHandlerBase
// fetcher is created per AzureSdkDiagnosticsEventHandler and AzureSdkDiagnosticsEventHandler is created per DiagnosticSource
private readonly PropertyFetcher linksPropertyFetcher = new PropertyFetcher("Links");

public AzureSdkDiagnosticsEventHandler(TelemetryConfiguration configuration) : base(configuration)
public AzureSdkDiagnosticsEventHandler(TelemetryClient client) : base(client)
{
}

Expand Down Expand Up @@ -159,7 +159,7 @@ private static bool TryGetAverageTimeInQueueForBatch(IEnumerable<Activity> links
// instrumentation does not consistently report enqueued time, ignoring whole span
return false;
}

long startEpochTime = 0;
#if NET452
startEpochTime = (long)(requestStartTime - EpochStart).TotalMilliseconds;
Expand Down Expand Up @@ -318,7 +318,7 @@ private static void SetEventHubsProperties(Activity activity, OperationTelemetry
return;
}

// Target uniquely identifies the resource, we use both: queueName and endpoint
// Target uniquely identifies the resource, we use both: queueName and endpoint
// with schema used for SQL-dependencies
string separator = "/";
if (endpoint.EndsWith(separator, StringComparison.Ordinal))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ protected DiagnosticsEventHandlerBase(TelemetryConfiguration configuration)
this.TelemetryClient = new TelemetryClient(configuration);
}

protected DiagnosticsEventHandlerBase(TelemetryClient client)
{
this.TelemetryClient = client;
}

public virtual bool IsEventEnabled(string evnt, object arg1, object arg2)
{
return !evnt.EndsWith(TelemetryDiagnosticSourceListener.ActivityStartNameSuffix, StringComparison.Ordinal);
Expand Down