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

Commit

Permalink
Fix race condition in generic diagnostic listener (#948)
Browse files Browse the repository at this point in the history
Fix race condiftion in generic diagnostic listener
  • Loading branch information
Liudmila Molkova authored Jul 10, 2018
1 parent 8d846c6 commit 0b30363
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
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,31 @@ 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;

/// <summary>
/// Creates DiagnosticSourceListenerBase. To finish the initialization and subscribe to all enabled sources,
/// call <see cref="Subscribe"/>
/// </summary>
/// <param name="configuration"><see cref="TelemetryConfiguration"/> instance.
/// The listener tracks dependency calls and uses configuration to construct <see cref="TelemetryClient"/></param>
protected DiagnosticSourceListenerBase(TelemetryConfiguration configuration)
{
this.Configuration = configuration;
this.Client = new TelemetryClient(configuration);
}

/// <summary>
/// Subscribes the listener to all enabled sources. This method must be called
/// to enable dependency calls collection.
/// </summary>
public void Subscribe()
{
if (this.listenerSubscription != null)
{
return;
}

try
{
Expand Down

0 comments on commit 0b30363

Please sign in to comment.