Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 4dc34ab

Browse files
Merge pull request #206 from karolz-ms/develop
Disable Microsoft-ApplicationInsights-Data EventSource by default
2 parents 03bf8a9 + fa9b3a5 commit 4dc34ab

4 files changed

Lines changed: 41 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Version 2.7.1
44
- [NLog can perform Layout of InstrumentationKey](https://github.com/Microsoft/ApplicationInsights-dotnet-logging/pull/203)
55
- Upgrade `System.Diagnostics.DiagnosticSource` to version 4.5.0
6+
- [Event Source telemetry module: Microsoft-ApplicationInsights-Data id disabled by default to work around CLR bug](https://github.com/Microsoft/ApplicationInsights-dotnet-logging/pull/206)
67

78
### Version 2.6.4
89
- [Log4Net new supports NetStandard 1.3!](https://github.com/Microsoft/ApplicationInsights-dotnet-logging/pull/167)

src/EventSourceListener/EventSourceListeningRequestBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public abstract class EventSourceListeningRequestBase
1717
public string Name { get; set; }
1818

1919
/// <summary>
20-
/// Gets or sets a value indicating whether allows wildcards in <see cref="Name" />.
20+
/// Gets or sets a value indicating whether the value of the <see cref="Name"/> property should match the name of an EventSource exactly, or should the value be treated as EventSource name prefix.
2121
/// </summary>
2222
public bool PrefixMatch { get; set; }
2323

src/EventSourceListener/EventSourceTelemetryModule.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ namespace Microsoft.ApplicationInsights.EventSourceListener
2929
/// </summary>
3030
public class EventSourceTelemetryModule : EventListener, ITelemetryModule
3131
{
32+
private const string AppInsightsDataEventSource = "Microsoft-ApplicationInsights-Data";
33+
3234
private readonly OnEventWrittenHandler onEventWrittenHandler;
3335
private OnEventWrittenHandler eventWrittenHandlerPicker;
3436

@@ -90,7 +92,7 @@ public void Initialize(TelemetryConfiguration configuration)
9092
if (this.Sources.Count == 0)
9193
{
9294
EventSourceListenerEventSource.Log.NoSourcesConfigured(nameof(EventSourceListener.EventSourceTelemetryModule));
93-
return;
95+
// Continue--we need to be prepared for handling disabled sources.
9496
}
9597

9698
try
@@ -105,6 +107,16 @@ public void Initialize(TelemetryConfiguration configuration)
105107
}
106108
}
107109

110+
// Special case: because of .NET bug https://github.com/dotnet/coreclr/issues/14434, using Microsoft-ApplicationInsights-Data will result in infinite loop.
111+
// So we will disable it by default, unless there is explicit configuration for this EventSource.
112+
bool hasExplicitConfigForAiDataSource =
113+
this.Sources.Any(req => req.Name?.StartsWith(AppInsightsDataEventSource, StringComparison.Ordinal) ?? false) ||
114+
this.DisabledSources.Any(req => req.Name?.StartsWith(AppInsightsDataEventSource, StringComparison.Ordinal) ?? false);
115+
if (!hasExplicitConfigForAiDataSource)
116+
{
117+
this.DisabledSources.Add(new DisableEventSourceRequest { Name = AppInsightsDataEventSource });
118+
}
119+
108120
// Set the initialized flag now to ensure that we do not miss any sources that came online as we are executing the initialization
109121
// (OnEventSourceCreated() might have been called on a separate thread). Worst case we will attempt to enable the same source twice
110122
// (with same settings), but that is OK, as the semantics of EnableEvents() is really "update what is being tracked", so it is fine

test/EventSourceListener.netcoreapp10.Tests/EventSourceTelemetryModuleTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,32 @@ public void DoNotReportTplEvents()
411411
}
412412
}
413413

414+
[TestMethod]
415+
[TestCategory("EventSourceListener")]
416+
public void DisablesAppInsightsDataByDefault()
417+
{
418+
using (var module = new EventSourceTelemetryModule())
419+
{
420+
module.Initialize(GetTestTelemetryConfiguration());
421+
422+
Assert.AreEqual(1, module.DisabledSources.Count);
423+
Assert.AreEqual(new DisableEventSourceRequest { Name = "Microsoft-ApplicationInsights-Data" }, module.DisabledSources[0]);
424+
}
425+
}
426+
427+
[TestMethod]
428+
[TestCategory("EventSourceListener")]
429+
public void DoesNotDisableAppInsightsDataIfExplicitlyEnabled()
430+
{
431+
using (var module = new EventSourceTelemetryModule())
432+
{
433+
module.Sources.Add(new EventSourceListeningRequest { Name = "Microsoft-ApplicationInsights-Data" });
434+
module.Initialize(GetTestTelemetryConfiguration());
435+
436+
Assert.AreEqual(0, module.DisabledSources.Count);
437+
}
438+
}
439+
414440
private Task PerformActivityAsync(int requestId)
415441
{
416442
return Task.Run(async () =>

0 commit comments

Comments
 (0)