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

Enforce not referencing Datadog.Trace directly in sample projects #5683

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions tracer/test/test-applications/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,13 @@
</Otherwise>
</Choose>

<!-- Ensure that we don't reference Datadog.Trace directly (unless explicitly handled) -->
<!-- Note that you need to ensure the buid logic is updated to exclude the project if you take this approach -->
<Target Name="ValidateDisallowedReferences" BeforeTargets="CoreCompile">
<Error
Condition="'%(ProjectReference.FileName)' == 'Datadog.Trace' AND '$(AllowDatadogTraceReference)' != 'true'"
Text="You should not directly reference Datadog.Trace in this project" />
</Target>

<Import Project=".\Samples.Shared\Samples.Shared.projitems" Label="Shared" Condition="'$(IncludeSharedSampleHelpers)' != 'false'" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class SampleHelpers
private static readonly Type ProcessHelpersType = Type.GetType("Datadog.Trace.Util.ProcessHelpers, Datadog.Trace");
private static readonly Type UserDetailsType = Type.GetType("Datadog.Trace.UserDetails, Datadog.Trace");
private static readonly Type SpanExtensionsType = Type.GetType("Datadog.Trace.SpanExtensions, Datadog.Trace");
public static readonly Type IpcClientType = Type.GetType("Datadog.Trace.Ci.Ipc.IpcClient, Datadog.Trace");
private static readonly PropertyInfo GetTracerManagerProperty = TracerType?.GetProperty("TracerManager", BindingFlags.NonPublic | BindingFlags.Instance);
private static readonly MethodInfo GetNativeTracerVersionMethod = InstrumentationType?.GetMethod("GetNativeTracerVersion");
private static readonly MethodInfo GetTracerInstance = TracerType?.GetProperty("Instance", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)?.GetMethod;
Expand Down Expand Up @@ -57,7 +58,6 @@ public class SampleHelpers
#endif
private static readonly FieldInfo TracerThreePartVersionField = TracerConstantsType?.GetField("ThreePartVersion");


static SampleHelpers()
{
if (TracerType is null)
Expand Down
9 changes: 9 additions & 0 deletions tracer/test/test-applications/aspnet/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,13 @@
</PropertyGroup>

<Import Project="..\Samples.Shared\Samples.Shared.projitems" Label="Shared" />

<!-- Ensure that we don't reference Datadog.Trace directly (unless explicitly handled) -->
<!-- Note that you need to ensure the buid logic is updated to exclude the project if you take this approach -->
<Target Name="ValidateDisallowedReferences" BeforeTargets="CoreCompile">
<Error
Condition="'%(ProjectReference.FileName)' == 'Datadog.Trace' AND '$(AllowDatadogTraceReference)' != 'true'"
Text="You should not directly reference Datadog.Trace in this project" />
</Target>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@
</PropertyGroup>

<Import Project="..\Samples.Shared\Samples.Shared.projitems" Label="Shared" />

<!-- Ensure that we don't reference Datadog.Trace directly (unless explicitly handled) -->
<!-- Note that you need to ensure the buid logic is updated to exclude the project if you take this approach -->
<Target Name="ValidateDisallowedReferences" BeforeTargets="CoreCompile">
<Error
Condition="'%(ProjectReference.FileName)' == 'Datadog.Trace' AND '$(AllowDatadogTraceReference)' != 'true'"
Text="You should not directly reference Datadog.Trace in this project" />
</Target>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@
<!-- Hide warnings for EOL .NET Core targets (e.g. netcoreapp3.0) -->
<CheckEolTargetFramework>false</CheckEolTargetFramework>
</PropertyGroup>

<!-- Ensure that we don't reference Datadog.Trace directly (unless explicitly handled) -->
<!-- Note that you need to ensure the buid logic is updated to exclude the project if you take this approach -->
<Target Name="ValidateDisallowedReferences" BeforeTargets="CoreCompile">
<Error
Condition="'%(ProjectReference.FileName)' == 'Datadog.Trace' AND '$(AllowDatadogTraceReference)' != 'true'"
Text="You should not directly reference Datadog.Trace in this project" />
</Target>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)..\..\..\..\..\Datadog.Trace.snk</AssemblyOriginatorKeyFile>
<DelaySign>false</DelaySign>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<AllowDatadogTraceReference>true</AllowDatadogTraceReference>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Memory" Version="4.5.5" />
<ProjectReference Include="..\..\..\..\src\Datadog.Trace\Datadog.Trace.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static CallTargetState OnMethodBegin<TTarget>(TTarget instance, ref strin
ref var readOnlySpanValue = ref arg02.DangerousGetReadOnlySpan<char>(out success);
if (success)
{
readOnlySpanValue = "World";
readOnlySpanValue = "World".AsSpan();
}

ref var spanValue = ref arg03.DangerousGetSpan<char>(out success);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static CallTargetState OnMethodBegin<TTarget, TArg1>(TTarget instance, re
ref var readOnlySpanValue = ref callTargetRefStruct.DangerousGetReadOnlySpan<char>(out success);
if (success)
{
readOnlySpanValue = "Hello World";
readOnlySpanValue = "Hello World".AsSpan();
return returnValue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static CallTargetState OnMethodBegin<TTarget, TArg1, TArg2>(TTarget insta
ref var readOnlySpanValue = ref callTargetRefStruct.DangerousGetReadOnlySpan<char>(out success);
if (success)
{
readOnlySpanValue = "Hello";
readOnlySpanValue = "Hello".AsSpan();
goto secondArgument;
}

Expand All @@ -41,7 +41,7 @@ public static CallTargetState OnMethodBegin<TTarget, TArg1, TArg2>(TTarget insta
ref var readOnlySpanValue = ref callTargetRefStruct2.DangerousGetReadOnlySpan<char>(out success);
if (success)
{
readOnlySpanValue = "World";
readOnlySpanValue = "World".AsSpan();
goto returnValue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ private static void WithRefStructArguments()
var wRefStructArg = new WithRefStructArguments();

Console.WriteLine($"{typeof(WithRefArguments).FullName} | ReadOnlySpan<char> methods");
RunMethod(() => wRefStructArg.VoidReadOnlySpanMethod("BadParam"));
RunMethod(() => wRefStructArg.VoidReadOnlySpanMethod("BadParam".AsSpan()));
RunMethod(() =>
{
var arg1 = "BadParam".AsSpan();
wRefStructArg.VoidReadOnlySpanMethod(ref arg1);
if (arg1 != "Hello World") throw new Exception("Error modifying arg1 value.");
if (!arg1.SequenceEqual("Hello World".AsSpan())) throw new Exception("Error modifying arg1 value.");
});
RunMethod(() =>
{
Expand All @@ -27,15 +27,18 @@ private static void WithRefStructArguments()
var arg1 = "BadParam".AsSpan();
var arg2 = "BadParam".AsSpan();
wRefStructArg.Void2ReadOnlySpanMethod(ref arg1, ref arg2);
if (arg1 != "Hello") throw new Exception("Error modifying arg1 value.");
if (arg2 != "World") throw new Exception("Error modifying arg2 value.");
if (!arg1.SequenceEqual("Hello".AsSpan())) throw new Exception("Error modifying arg1 value.");
if (!arg2.SequenceEqual("World".AsSpan())) throw new Exception("Error modifying arg2 value.");
});
RunMethod(() =>
{
var arg1 = "BadParam".AsSpan();
var arg2 = "BadParam".AsSpan();
wRefStructArg.Void2ReadOnlySpanMethod(arg1, ref arg2);
if (arg2 != "World") throw new Exception("Error modifying arg2 value.");
if (!arg2.SequenceEqual("World".AsSpan()))
{
throw new Exception("Error modifying arg2 value.");
}
});

Console.WriteLine($"{typeof(WithRefArguments).FullName} | Span<char> methods");
Expand Down Expand Up @@ -118,7 +121,7 @@ private static void WithRefStructArguments()
var arg4 = new ReadOnlyRefStruct("BadParam");
wRefStructArg.VoidMixedMethod(ref arg1, ref arg2, ref arg3, ref arg4);
if (arg1 != "Hello") throw new Exception("Error modifying arg1 value.");
if (arg2 != "World") throw new Exception("Error modifying arg2 value.");
if (!arg2.SequenceEqual("World".AsSpan())) throw new Exception("Error modifying arg2 value.");
if (arg3.ToString() != "Hello") throw new Exception("Error modifying arg3 value.");
if (arg4.Value != "World") throw new Exception("Error modifying arg4 value.");
});
Expand All @@ -129,7 +132,7 @@ private static void WithRefStructArguments()
var arg3 = new Span<char>(['B', 'a', 'd', 'P', 'a', 'r', 'a', 'm']);
var arg4 = new ReadOnlyRefStruct("BadParam");
wRefStructArg.VoidMixedMethod(arg1, ref arg2, arg3, ref arg4);
if (arg2 != "World") throw new Exception("Error modifying arg2 value.");
if (!arg2.SequenceEqual("World".AsSpan())) throw new Exception("Error modifying arg2 value.");
if (arg4.Value != "World") throw new Exception("Error modifying arg4 value.");
});
}
Expand All @@ -141,30 +144,30 @@ internal class WithRefStructArguments

public void VoidReadOnlySpanMethod(ReadOnlySpan<char> arg1)
{
if (arg1 != "Hello World") throw new Exception("Error modifying arg1 value.");
if (!arg1.SequenceEqual("Hello World".AsSpan())) throw new Exception("Error modifying arg1 value.");
}

public void VoidReadOnlySpanMethod(ref ReadOnlySpan<char> arg1)
{
if (arg1 != "Hello World") throw new Exception("Error modifying arg1 value.");
if (!arg1.SequenceEqual("Hello World".AsSpan())) throw new Exception("Error modifying arg1 value.");
}

public void Void2ReadOnlySpanMethod(ReadOnlySpan<char> arg1, ReadOnlySpan<char> arg2)
{
if (arg1 != "Hello") throw new Exception("Error modifying arg1 value.");
if (arg2 != "World") throw new Exception("Error modifying arg2 value.");
if (!arg1.SequenceEqual("Hello".AsSpan())) throw new Exception("Error modifying arg1 value.");
if (!arg2.SequenceEqual("World".AsSpan())) throw new Exception("Error modifying arg2 value.");
}

public void Void2ReadOnlySpanMethod(ref ReadOnlySpan<char> arg1, ref ReadOnlySpan<char> arg2)
{
if (arg1 != "Hello") throw new Exception("Error modifying arg1 value.");
if (arg2 != "World") throw new Exception("Error modifying arg2 value.");
if (!arg1.SequenceEqual("Hello".AsSpan())) throw new Exception("Error modifying arg1 value.");
if (!arg2.SequenceEqual("World".AsSpan())) throw new Exception("Error modifying arg2 value.");
}

public void Void2ReadOnlySpanMethod(ReadOnlySpan<char> arg1, ref ReadOnlySpan<char> arg2)
{
if (arg1 != "Hello") throw new Exception("Error modifying arg1 value.");
if (arg2 != "World") throw new Exception("Error modifying arg2 value.");
if (!arg1.SequenceEqual("Hello".AsSpan())) throw new Exception("Error modifying arg1 value.");
if (!arg2.SequenceEqual("World".AsSpan())) throw new Exception("Error modifying arg2 value.");
}

// *** Span<char> arguments ***
Expand Down Expand Up @@ -232,23 +235,23 @@ public void Void2ReadOnlyRefStructMethod(ReadOnlyRefStruct arg1, ref ReadOnlyRef
public void VoidMixedMethod(string arg1, ReadOnlySpan<char> arg2, Span<char> arg3, ReadOnlyRefStruct arg4)
{
if (arg1 != "Hello") throw new Exception("Error modifying arg1 value.");
if (arg2 != "World") throw new Exception("Error modifying arg2 value.");
if (!arg2.SequenceEqual("World".AsSpan())) throw new Exception("Error modifying arg2 value.");
if (arg3.ToString() != "Hello") throw new Exception("Error modifying arg3 value.");
if (arg4.Value != "World") throw new Exception("Error modifying arg4 value.");
}

public void VoidMixedMethod(ref string arg1, ref ReadOnlySpan<char> arg2, ref Span<char> arg3, ref ReadOnlyRefStruct arg4)
{
if (arg1 != "Hello") throw new Exception("Error modifying arg1 value.");
if (arg2 != "World") throw new Exception("Error modifying arg2 value.");
if (!arg2.SequenceEqual("World".AsSpan())) throw new Exception("Error modifying arg2 value.");
if (arg3.ToString() != "Hello") throw new Exception("Error modifying arg3 value.");
if (arg4.Value != "World") throw new Exception("Error modifying arg4 value.");
}

public void VoidMixedMethod(string arg1, ref ReadOnlySpan<char> arg2, Span<char> arg3, ref ReadOnlyRefStruct arg4)
{
if (arg1 != "Hello") throw new Exception("Error modifying arg1 value.");
if (arg2 != "World") throw new Exception("Error modifying arg2 value.");
if (!arg2.SequenceEqual("World".AsSpan())) throw new Exception("Error modifying arg2 value.");
if (arg3.ToString() != "Hello") throw new Exception("Error modifying arg3 value.");
if (arg4.Value != "World") throw new Exception("Error modifying arg4 value.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<LangVersion>latest</LangVersion>
<AllowDatadogTraceReference>true</AllowDatadogTraceReference>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static void Main(string[] args)

Console.WriteLine(@"Using SessionId: " + sessionId);

var ipcClientType = typeof(Datadog.Trace.Tracer).Assembly.GetType("Datadog.Trace.Ci.Ipc.IpcClient")!;
var ipcClientType = Samples.SampleHelpers.IpcClientType!;
var setMessageReceivedCallbackMethod = ipcClientType.GetMethod("SetMessageReceivedCallback");
var trySendMessageMethod = ipcClientType.GetMethod("TrySendMessage");
using var ipcClient = (IDisposable)Activator.CreateInstance(ipcClientType, sessionId)!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,4 @@
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\src\Datadog.Trace\Datadog.Trace.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- Just temporary, until v3 -->
<AllowDatadogTraceReference>true</AllowDatadogTraceReference>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\src\Datadog.Trace\Datadog.Trace.csproj" />
</ItemGroup>
Expand Down
Loading