Skip to content

Commit

Permalink
Enable SDK to create package for auto-instrumentation (#2365)
Browse files Browse the repository at this point in the history
* Create downgraded DiagnosticSource version for Redfield

* Remove Settings.Designer.cs, created by mistake

* Fix tests

* Fix tests

* Fix tests

* Remove hard-coded Redfield property

* PR Comments

* Rename yml file
  • Loading branch information
rajkumar-rangaraj authored Aug 18, 2021
1 parent 91e1217 commit 0c25b41
Show file tree
Hide file tree
Showing 36 changed files with 226 additions and 15 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/redfield-sanity-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Syntax: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions
# .NET CLI: https://docs.microsoft.com/dotnet/core/tools/
# Description: The purpose of this build is to build and test with redfield flag.

name: Redfield Sanity Check

on:
push:
branches: [ master, develop ]
pull_request:
branches: [ master, develop ]

jobs:
build-test:

runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
framework: [netcoreapp3.1,net5.0]
include:
- os: ubuntu-latest
args: "--filter TestCategory!=WindowsOnly"

steps:
- uses: actions/checkout@v2

- name: restore (Base sln)
run: dotnet restore ./BASE/Microsoft.ApplicationInsights.sln

- name: Build
run: dotnet build -p:Redfield=True ./BASE/Microsoft.ApplicationInsights.sln --configuration Release --no-restore

- name: Test
id: test1
continue-on-error: true
run: dotnet test ./BASE/Microsoft.ApplicationInsights.sln --framework ${{ matrix.framework }} --configuration Release --no-build --logger:"console;verbosity=detailed" ${{ matrix.args }}
1 change: 1 addition & 0 deletions .props/_GlobalStaticVersion.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<SemanticVersionMinor>18</SemanticVersionMinor> <!-- If changing the Minor version, also update the Date value. -->
<SemanticVersionPatch>0</SemanticVersionPatch>
<PreReleaseMilestone></PreReleaseMilestone> <!--Valid values: beta1, beta2, EMPTY for stable -->
<PreReleaseMilestone Condition="'$(Redfield)' == 'True'">redfield</PreReleaseMilestone>
<PreReleaseMilestone Condition="'$(NightlyBuild)' == 'True'">nightly</PreReleaseMilestone> <!-- Overwrite this property for nightly builds from the DEVELOP branch. -->
<!--
Date when Semantic Version was changed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public void GetOperationNameReturnsFirstAddedOperationName()
var activity = new Activity("test");
activity.AddTag("OperationName", "test me 1");
activity.AddTag("OperationName", "test me 2");

#if !REDFIELD
Assert.AreEqual("test me 1", activity.GetOperationName());
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ protected override void OnEventWritten(EventWrittenEventArgs eventData)

protected override void OnEventSourceCreated(EventSource eventSource)
{
#if REDFIELD
if (string.Equals(eventSource.Name, "Redfield-Microsoft-ApplicationInsights-Core", StringComparison.Ordinal))
#else
if (string.Equals(eventSource.Name, "Microsoft-ApplicationInsights-Core", StringComparison.Ordinal))
#endif
{
var eventCounterArguments = new Dictionary<string, string>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.ApplicationInsights.TestFramework;
using System.Diagnostics.Tracing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;

[TestClass]
public class CoreEventSourceTest
Expand All @@ -25,5 +26,30 @@ private static EventAttribute GetEventAttribute(string methodName)
MethodInfo method = typeof(CoreEventSource).GetMethod(methodName);
return method.GetCustomAttribute<EventAttribute>();
}

#if REDFIELD
/// <summary>
/// This is a sanitiy check.
/// The 'Redfield' compilation flag should switch the name of EventSource class.
/// Devs can review the test log and confirm that this test runs and passes.
/// This will serve as a verification that the Redfield compilation flag worked as expected.
///
/// To run this test:
/// dotnet build /p:Redfield=True ".\dotnet\BASE\Microsoft.ApplicationInsights.sln"
/// dotnet test ".\bin\Debug\test\Microsoft.ApplicationInsights.Tests\net5.0\Microsoft.ApplicationInsights.Tests.dll" --filter Name~VerifyRedfieldEventSourceName
/// </summary>
[TestMethod]
public void VerifyRedfieldEventSourceName()
{
var expectedName = "Redfield-Microsoft-ApplicationInsights-Core";

var eventSourceAttribute = typeof(CoreEventSource)
.GetCustomAttributes(typeof(EventSourceAttribute))
.Single() as EventSourceAttribute;

Assert.IsNotNull(eventSourceAttribute);
Assert.AreEqual(expectedName, eventSourceAttribute.Name);
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
{
using System.Diagnostics.Tracing;

#if REDFIELD
[EventSource(Name = "Redfield-Microsoft-ApplicationInsights-Extensibility-Test")]
#else
[EventSource(Name = "Microsoft-ApplicationInsights-Extensibility-Test")]
#endif
internal class TestEventSource : EventSource
{
[Event(1, Message = "Error: {0}", Level = EventLevel.Error)]
Expand Down
4 changes: 4 additions & 0 deletions BASE/src/Microsoft.ApplicationInsights/ActivityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ private static bool Initialize()
{
try
{
#if REDFIELD
Assembly.Load(new AssemblyName("System.Diagnostics.DiagnosticSource, Version=4.0.5.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51"));
#else
Assembly.Load(new AssemblyName("System.Diagnostics.DiagnosticSource, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51"));
#endif
return true;
}
catch (System.IO.FileNotFoundException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ internal sealed partial class RichPayloadEventSource : IDisposable
internal readonly EventSource EventSourceInternal;

/// <summary>Event provider name.</summary>
#if REDFIELD
private const string EventProviderName = "Redfield-Microsoft-ApplicationInsights-Data";
#else
private const string EventProviderName = "Microsoft-ApplicationInsights-Data";
#endif

/// <summary>
/// Initializes a new instance of the RichPayloadEventSource class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Tracing;

#if REDFIELD
[EventSource(Name = "Redfield-Microsoft-ApplicationInsights-Core")]
#else
[EventSource(Name = "Microsoft-ApplicationInsights-Core")]
#endif
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", Justification = "appDomainName is required")]
internal sealed class CoreEventSource : EventSource
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,38 @@ protected override void OnEventSourceCreated(EventSource eventSource)
/// </summary>
private static bool ShouldSubscribe(EventSource eventSource)
{
#if REDFIELD
if (eventSource.Name.StartsWith("Redfield-Microsoft-A", StringComparison.Ordinal))
{
switch (eventSource.Name)
{
case "Redfield-Microsoft-ApplicationInsights-Core":
case "Redfield-Microsoft-ApplicationInsights-WindowsServer-TelemetryChannel":

case "Redfield-Microsoft-ApplicationInsights-Extensibility-AppMapCorrelation-Dependency":
case "Redfield-Microsoft-ApplicationInsights-Extensibility-AppMapCorrelation-Web":

case "Redfield-Microsoft-ApplicationInsights-Extensibility-DependencyCollector":
case "Redfield-Microsoft-ApplicationInsights-Extensibility-EventCounterCollector":
case "Redfield-Microsoft-ApplicationInsights-Extensibility-PerformanceCollector":
case "Redfield-Microsoft-ApplicationInsights-Extensibility-PerformanceCollector-QuickPulse":
case "Redfield-Microsoft-ApplicationInsights-Extensibility-Web":
case "Redfield-Microsoft-ApplicationInsights-Extensibility-WindowsServer":
case "Redfield-Microsoft-ApplicationInsights-WindowsServer-Core":
case "Redfield-Microsoft-ApplicationInsights-Extensibility-EventSourceListener":
case "Redfield-Microsoft-ApplicationInsights-AspNetCore":
case "Redfield-Microsoft-ApplicationInsights-LoggerProvider":
return true;
default:
return false;
}
}

if (eventSource.Name == "Microsoft-AspNet-Telemetry-Correlation")
{
return true;
}
#else
if (eventSource.Name.StartsWith("Microsoft-A", StringComparison.Ordinal))
{
switch (eventSource.Name)
Expand All @@ -96,7 +128,7 @@ private static bool ShouldSubscribe(EventSource eventSource)
// AppMapCorrelation has a shared partial class: https://github.com/microsoft/ApplicationInsights-dotnet/blob/master/WEB/Src/Common/AppMapCorrelationEventSource.cs
case "Microsoft-ApplicationInsights-Extensibility-AppMapCorrelation-Dependency": // https://github.com/microsoft/ApplicationInsights-dotnet/blob/master/WEB/Src/DependencyCollector/DependencyCollector/Implementation/AppMapCorrelationEventSource.cs
case "Microsoft-ApplicationInsights-Extensibility-AppMapCorrelation-Web": // https://github.com/microsoft/ApplicationInsights-dotnet/blob/master/WEB/Src/Web/Web/Implementation/AppMapCorrelationEventSource.cs

case "Microsoft-ApplicationInsights-Extensibility-DependencyCollector": // https://github.com/microsoft/ApplicationInsights-dotnet/blob/master/WEB/Src/DependencyCollector/DependencyCollector/Implementation/DependencyCollectorEventSource.cs
case "Microsoft-ApplicationInsights-Extensibility-EventCounterCollector": // https://github.com/microsoft/ApplicationInsights-dotnet/blob/master/WEB/Src/EventCounterCollector/EventCounterCollector/EventCounterCollectorEventSource.cs
case "Microsoft-ApplicationInsights-Extensibility-PerformanceCollector": // https://github.com/microsoft/ApplicationInsights-dotnet/blob/master/WEB/Src/PerformanceCollector/PerformanceCollector/Implementation/PerformanceCollectorEventSource.cs
Expand All @@ -113,6 +145,7 @@ private static bool ShouldSubscribe(EventSource eventSource)
return false;
}
}
#endif

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
/// </summary>
internal class DiagnosticsEventListener : EventListener
{
#if REDFIELD
private const string EventSourceNamePrefix = "Redfield-Microsoft-ApplicationInsights-";
#else
private const string EventSourceNamePrefix = "Microsoft-ApplicationInsights-";

#endif
private readonly EventKeywords keywords;

private readonly EventLevel logLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
internal class TraceSourceForEventSource : TraceSource, IEventListener, IDisposable
{
private const long AllKeyword = -1;

#if REDFIELD
private const string TraceSourceName = "Redfield.Microsoft.ApplicationInsights.Extensibility.TraceSource";
#else
private const string TraceSourceName = "Microsoft.ApplicationInsights.Extensibility.TraceSource";
#endif

private DiagnosticsEventListener listener;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
/// </summary>
internal class SelfDiagnosticsEventListener : EventListener
{
#if REDFIELD
private const string EventSourceNamePrefix = "Redfield-Microsoft-ApplicationInsights-";
#else
private const string EventSourceNamePrefix = "Microsoft-ApplicationInsights-";

#endif
// Buffer size of the log line. A UTF-16 encoded character in C# can take up to 4 bytes if encoded in UTF-8.
private const int BUFFERSIZE = 4 * 5120;
private readonly ThreadLocal<byte[]> writeBuffer = new ThreadLocal<byte[]>(() => null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<PackageReference Include="Microsoft.Diagnostics.Tracing.EventRegister" Version="1.1.28" Condition="$(OS) == 'Windows_NT'">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="5.0.0" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="$(SystemDiagnosticsDiagnosticSourcePkgVer)" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' != 'netstandard2.0'">
Expand All @@ -34,5 +34,5 @@
<AdditionalFiles Include="$(PublicApiRoot)\$(AssemblyName).dll\$(TargetFramework)\PublicAPI.Shipped.txt" />
<AdditionalFiles Include="$(PublicApiRoot)\$(AssemblyName).dll\$(TargetFramework)\PublicAPI.Unshipped.txt" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Tracing;

#if REDFIELD
[EventSource(Name = "Redfield-Microsoft-ApplicationInsights-WindowsServer-TelemetryChannel")]
#else
[EventSource(Name = "Microsoft-ApplicationInsights-WindowsServer-TelemetryChannel")]
#endif
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", Justification = "appDomainName is required")]
internal sealed class TelemetryChannelEventSource : EventSource
{
Expand Down
17 changes: 17 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,25 @@

<!-- This is used to disable some build properties. -->
<IsExamplesSolution Condition="'$(SolutionName)' == 'Examples' ">true</IsExamplesSolution>

<!-- This is used to change EventSource names. -->
<DefineConstants Condition="'$(Redfield)' == 'True'">$(DefineConstants);REDFIELD</DefineConstants>
</PropertyGroup>

<PropertyGroup Label="Package versions for System.Diagnostics.DiagnosticSource">
<SystemDiagnosticsDiagnosticSourcePkgVer>5.0.0</SystemDiagnosticsDiagnosticSourcePkgVer>
<SystemDiagnosticsDiagnosticSourcePkgVer Condition="'$(Redfield)' == 'True'">4.7.0</SystemDiagnosticsDiagnosticSourcePkgVer>
</PropertyGroup>

<Target Name="Info_Redfield" BeforeTargets="Build" Condition="'$(Redfield)' == 'True'">
<!--
This flag is reserved for Codeless Attach products.
Redfield has some unique code changes to avoid conflicting with the real AI SDK.
To use: dotnet build /p:Redfield=True
-->
<Message Text="Directory.Build.props: Redfield build detected." Importance="high"/>
</Target>

<Target Name="Info_InternalSettings" BeforeTargets="Build">
<Message Text="Directory.Build.props: Internal_Logging is set to $(Internal_Logging)." Importance="high"/>
</Target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<ItemGroup>
<ProjectReference Include="..\..\..\BASE\src\Microsoft.ApplicationInsights\Microsoft.ApplicationInsights.csproj" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="5.0.0" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="$(SystemDiagnosticsDiagnosticSourcePkgVer)" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ namespace Microsoft.ApplicationInsights.TraceEvent.Shared.Implementation
[EventSource(Name = ProviderName)]
internal sealed class EventSourceListenerEventSource : EventSource
{
#if REDFIELD
public const string ProviderName = "Redfield-Microsoft-ApplicationInsights-Extensibility-EventSourceListener";
#else
public const string ProviderName = "Microsoft-ApplicationInsights-Extensibility-EventSourceListener";
#endif
public static readonly EventSourceListenerEventSource Log = new EventSourceListenerEventSource();

public readonly string ApplicationName;
Expand Down
4 changes: 4 additions & 0 deletions LOGGING/src/ILogger/ApplicationInsightsLoggerEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ namespace Microsoft.Extensions.Logging.ApplicationInsights
/// <summary>
/// EventSource for reporting errors and warnings from Logging module.
/// </summary>
#if REDFIELD
[EventSource(Name = "Redfield-Microsoft-ApplicationInsights-LoggerProvider")]
#else
[EventSource(Name = "Microsoft-ApplicationInsights-LoggerProvider")]
#endif
internal sealed class ApplicationInsightsLoggerEventSource : EventSource
{
public static readonly ApplicationInsightsLoggerEventSource Log = new ApplicationInsightsLoggerEventSource();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ protected override void OnEventWritten(EventWrittenEventArgs eventData)

protected override void OnEventSourceCreated(EventSource eventSource)
{
#if REDFIELD
if (string.Equals(eventSource.Name, "Redfield-Microsoft-ApplicationInsights-Extensibility-EventSourceListener", System.StringComparison.Ordinal))
#else
if (string.Equals(eventSource.Name, "Microsoft-ApplicationInsights-Extensibility-EventSourceListener", System.StringComparison.Ordinal))
#endif
{
EnableEvents(eventSource, EventLevel.LogAlways);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ protected override void OnEventWritten(EventWrittenEventArgs eventData)

protected override void OnEventSourceCreated(EventSource eventSource)
{
#if REDFIELD
if (string.Equals(eventSource.Name, "Redfield-Microsoft-ApplicationInsights-Extensibility-EventSourceListener", StringComparison.Ordinal))
#else
if (string.Equals(eventSource.Name, "Microsoft-ApplicationInsights-Extensibility-EventSourceListener", StringComparison.Ordinal))
#endif
{
EnableEvents(eventSource, EventLevel.LogAlways);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
/// <summary>
/// Event source for Application Insights ASP.NET Core SDK.
/// </summary>
#if REDFIELD
[EventSource(Name = "Redfield-Microsoft-ApplicationInsights-AspNetCore")]
#else
[EventSource(Name = "Microsoft-ApplicationInsights-AspNetCore")]
#endif
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", Justification = "appDomainName is required")]
[SuppressMessage("", "SA1611:ElementParametersMustBeDocumented", Justification = "Internal only class.")]
internal sealed class AspNetCoreEventSource : EventSource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
/// <summary>
/// Event source for Application Insights Worker Service SDK.
/// </summary>
#if REDFIELD
[EventSource(Name = "Redfield-Microsoft-ApplicationInsights-WorkerService")]
#else
[EventSource(Name = "Microsoft-ApplicationInsights-WorkerService")]
#endif
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", Justification = "appDomainName is required")]
[SuppressMessage("", "SA1611:ElementParametersMustBeDocumented", Justification = "Internal only class.")]
internal sealed class WorkerServiceEventSource : EventSource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<ItemGroup>
<!--Common Dependencies-->
<ProjectReference Include="..\..\..\..\BASE\src\Microsoft.ApplicationInsights\Microsoft.ApplicationInsights.csproj" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="5.0.0" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="$(SystemDiagnosticsDiagnosticSourcePkgVer)" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net452'">
Expand Down
Loading

0 comments on commit 0c25b41

Please sign in to comment.