Skip to content
This repository has been archived by the owner on Jul 5, 2020. It is now read-only.

Fix race condition in generic diagnostic listener #948

Merged
merged 4 commits into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Version 2.7.0-beta3
- [Fix: SerializationException resolving Activity in cross app-domain calls](https://github.com/Microsoft/ApplicationInsights-dotnet-server/issues/613)
- [Fix: Race condition in generic diagnostic source listener](https://github.com/Microsoft/ApplicationInsights-dotnet-server/pull/948)

## Version 2.7.0-beta1
- [Add operation details for HTTP and SQL operation to the dependency telemetry.](https://github.com/Microsoft/ApplicationInsights-dotnet-server/issues/900)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,25 @@ public void TestInitialize()

#region Subscribtion tests

[TestMethod]
public void TelemetryDiagnosticSourceListenerOnCreatedListener()
{
DiagnosticListener listener = new DiagnosticListener("Test.A");
var inclusionList = new[] { "Test.A" }.ToList();
using (var dl = new TelemetryDiagnosticSourceListener(this.configuration, inclusionList))
{
dl.Subscribe();
Assert.IsTrue(listener.IsEnabled(), "There is a subscriber for a new diagnostic source");
}
}

[TestMethod]
public void TelemetryDiagnosticSourceListenerCapturesAllActivitiesByDefault()
{
var inclusionList = new[] { "Test.A" }.ToList();
using (new TelemetryDiagnosticSourceListener(this.configuration, inclusionList))
using (var dl = new TelemetryDiagnosticSourceListener(this.configuration, inclusionList))
{
dl.Subscribe();
DiagnosticListener listener = new DiagnosticListener("Test.A");
Activity activity = new Activity("Test.A.Client.Monitoring");

Expand Down Expand Up @@ -74,8 +87,9 @@ public void TelemetryDiagnosticSourceListenerCapturesAllActivitiesByDefault()
public void TelemetryDiagnosticSourceListenerIgnoresNotIncludedSources()
{
var inclusionList = new[] { "Test.B" }.ToList();
using (new TelemetryDiagnosticSourceListener(this.configuration, inclusionList))
using (var dl = new TelemetryDiagnosticSourceListener(this.configuration, inclusionList))
{
dl.Subscribe();
// Diagnostic Source A is ignored
DiagnosticListener listenerA = new DiagnosticListener("Test.A");
Activity activityA = new Activity("Test.A.Client.Monitoring");
Expand Down Expand Up @@ -114,8 +128,9 @@ public void TelemetryDiagnosticSourceListenerIgnoresNotIncludedSources()
public void TelemetryDiagnosticSourceListenerIgnoresNotIncludedActivities()
{
var inclusionList = new[] { "Test.A:Test.A.Client.Monitoring" }.ToList();
using (new TelemetryDiagnosticSourceListener(this.configuration, inclusionList))
using (var dl = new TelemetryDiagnosticSourceListener(this.configuration, inclusionList))
{
dl.Subscribe();
// Diagnostic Source is not ignored
DiagnosticListener listener = new DiagnosticListener("Test.A");

Expand Down Expand Up @@ -167,8 +182,9 @@ public void TelemetryDiagnosticSourceListenerIgnoresNotIncludedActivities()
public void TelemetryDiagnosticSourceListenerCollectsTelemetryFromRawActivity()
{
var inclusionList = new[] { "Test.A" }.ToList();
using (new TelemetryDiagnosticSourceListener(this.configuration, inclusionList))
using (var dl = new TelemetryDiagnosticSourceListener(this.configuration, inclusionList))
{
dl.Subscribe();
DiagnosticListener listener = new DiagnosticListener("Test.A");

// generic example
Expand Down Expand Up @@ -231,6 +247,7 @@ public void TelemetryDiagnosticSourceListenerCallsCustomHandlersWhenEnabled()
var inclusionList = new[] { "Test.A:Send", "Test.B" }.ToList();
using (var telemetryListener = new TelemetryDiagnosticSourceListener(this.configuration, inclusionList))
{
telemetryListener.Subscribe();
TestableEventHandler ahandler = new TestableEventHandler();
TestableEventHandler bhandler = new TestableEventHandler();
telemetryListener.RegisterHandler("Test.A", ahandler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public void Initialize(TelemetryConfiguration configuration)
this.telemetryDiagnosticSourceListener = new TelemetryDiagnosticSourceListener(configuration, this.IncludeDiagnosticSourceActivities);
this.telemetryDiagnosticSourceListener.RegisterHandler(EventHubsDiagnosticsEventHandler.DiagnosticSourceName, new EventHubsDiagnosticsEventHandler(configuration));
this.telemetryDiagnosticSourceListener.RegisterHandler(ServiceBusDiagnosticsEventHandler.DiagnosticSourceName, new ServiceBusDiagnosticsEventHandler(configuration));
this.telemetryDiagnosticSourceListener.Subscribe();
}

this.sqlClientDiagnosticSourceListener = new SqlClientDiagnosticSourceListener(configuration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@ internal abstract class DiagnosticSourceListenerBase<TContext> : IObserver<Diagn
protected readonly TelemetryClient Client;
protected readonly TelemetryConfiguration Configuration;

private readonly IDisposable listenerSubscription;
private IDisposable listenerSubscription;
private List<IDisposable> individualSubscriptions;

protected DiagnosticSourceListenerBase(TelemetryConfiguration configuration)
Copy link
Member

Choose a reason for hiding this comment

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

Because the Subscribe method is required, should we make a reference to it in the constructor?
I don't know how often a developer creates a new DiagnosticSourceListener (probably not often :) ), but I wonder that they may not know to look for the Subscribe method.

Copy link
Member Author

Choose a reason for hiding this comment

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

this is an internal class, it's only us who call this method and just once. I can add the comment, though

{
this.Configuration = configuration;
this.Client = new TelemetryClient(configuration);
}

public void Subscribe()
Copy link
Member

Choose a reason for hiding this comment

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

for this public method, can you please add a

describing What this is, Why this is?

{
if (this.listenerSubscription != null)
{
return;
}

try
{
Expand Down