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

Enable SDK to create package for auto-instrumentation #2365

Merged
merged 12 commits into from Aug 18, 2021
Merged
39 changes: 39 additions & 0 deletions .github/workflows/redfield-sanity-check.yml
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

only building Base sdk?

Copy link
Member

Choose a reason for hiding this comment

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

Running our full test suite here will fail.
We have too many flaky tests and Github Actions does not have a mechanism for retries.
(I'm investigating this in #2345)
If we want to run more test projects here, we must filter those tests.


- 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 }}
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions .props/_GlobalStaticVersion.props
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
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
}
}
}
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
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"
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
/// 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
}
}
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
Expand Up @@ -52,7 +52,11 @@ private static bool Initialize()
{
try
{
#if REDFIELD
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you open an issue to completely remove this class. it should not be required.

Copy link
Member Author

Choose a reason for hiding this comment

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

Investigated a little and found methods from this class is called in several places.
For example Tryrun is called in OperationCorrelationTelemetryInitializer.cs.

Did I understand your comment incorrectly?

Copy link
Contributor

Choose a reason for hiding this comment

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

yea all the places where this ActivityExtensions is used can be modified to directly use Activity API, instead of doing it via this class.

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
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";
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
#else
private const string EventProviderName = "Microsoft-ApplicationInsights-Data";
#endif

/// <summary>
/// Initializes a new instance of the RichPayloadEventSource class.
Expand Down
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
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
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
Expand Up @@ -13,7 +13,12 @@
internal class TraceSourceForEventSource : TraceSource, IEventListener, IDisposable
{
private const long AllKeyword = -1;

#if REDFIELD
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
private const string TraceSourceName = "Redfield.Microsoft.ApplicationInsights.Extensibility.TraceSource";
#else
private const string TraceSourceName = "Microsoft.ApplicationInsights.Extensibility.TraceSource";
#endif

private DiagnosticsEventListener listener;

Expand Down
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
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>
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
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>
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
<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
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
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
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
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
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
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
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
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