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

[ASM] Native CallTarget definitions #6252

Open
wants to merge 14 commits into
base: dani/apm/NativeCallSites
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions Datadog.Trace.sln
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Generated", "Generated", "{
tracer\build\PackageVersionsLatestMajors.g.props = tracer\build\PackageVersionsLatestMajors.g.props
tracer\build\PackageVersionsLatestMinors.g.props = tracer\build\PackageVersionsLatestMinors.g.props
tracer\build\PackageVersionsLatestSpecific.g.props = tracer\build\PackageVersionsLatestSpecific.g.props
tracer\build\supported_calltargets.g.json = tracer\build\supported_calltargets.g.json
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.AWS.EventBridge", "tracer\test\test-applications\integrations\Samples.AWS.EventBridge\Samples.AWS.EventBridge.csproj", "{D6155F26-8245-4B66-8944-79C3DF9F9DA3}"
Expand Down
2 changes: 1 addition & 1 deletion tracer/build/_build/Build.Steps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,9 @@ bool RequiresThoroughTesting()

var nativeGeneratedFilesOutputPath = NativeTracerProject.Directory / "Generated";
CallSitesGenerator.GenerateCallSites(TargetFrameworks, tfm => DatadogTraceDirectory / "bin" / BuildConfiguration / tfm / Projects.DatadogTrace + ".dll", nativeGeneratedFilesOutputPath);
CallTargetsGenerator.GenerateCallTargets(TargetFrameworks, tfm => DatadogTraceDirectory / "bin" / BuildConfiguration / tfm / Projects.DatadogTrace + ".dll", nativeGeneratedFilesOutputPath, Version, BuildDirectory);
});


Target CompileTracerNativeTestsWindows => _ => _
.Unlisted()
.OnlyWhenStatic(() => IsWin)
Expand Down
3 changes: 2 additions & 1 deletion tracer/build/_build/Build.Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ partial class Build

var testDir = Solution.GetProject(Projects.ClrProfilerIntegrationTests).Directory;
var dependabotProj = TracerDirectory / "dependabot" / "Datadog.Dependabot.Integrations.csproj";
var definitionsFile = BuildDirectory / FileNames.DefinitionsJson;
var currentDependencies = DependabotFileManager.GetCurrentlyTestedVersions(dependabotProj);
var excludedFromUpdates = ((IncludePackages, ExcludePackages) switch
{
Expand All @@ -245,7 +246,7 @@ partial class Build
.Select(x => x.ToString())
.ToList();

var integrations = GenerateIntegrationDefinitions.GetAllIntegrations(assemblies);
var integrations = GenerateIntegrationDefinitions.GetAllIntegrations(assemblies, definitionsFile);
var distinctIntegrations = await DependabotFileManager.BuildDistinctIntegrationMaps(integrations, testedVersions);

await DependabotFileManager.UpdateIntegrations(dependabotProj, distinctIntegrations);
Expand Down
16 changes: 16 additions & 0 deletions tracer/build/_build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using CodeGenerators;
using Colorful;
using Nuke.Common;
using Nuke.Common.CI;
Expand Down Expand Up @@ -593,4 +594,19 @@ void DeleteReparsePoints(string path)
/// Run the default build
/// </summary>
public static int Main() => Execute<Build>(x => x.BuildTracerHome);

// For nuke step debugging, comment previous line and uncomment the following lines
/*
public static int Main() => Execute<Build>(x => x.Debug);

Target Debug => _ => _
.Unlisted()
.Executes(() =>
{
Logger.Information("Debugging...");
// Execute whatever you want to debug here
var nativeGeneratedFilesOutputPath = NativeTracerProject.Directory / "Generated";
CallTargetsGenerator.GenerateCallTargets(TargetFrameworks, tfm => DatadogTraceDirectory / "bin" / BuildConfiguration / tfm / Projects.DatadogTrace + ".dll", nativeGeneratedFilesOutputPath, Version);
});
//*/
}
24 changes: 12 additions & 12 deletions tracer/build/_build/CodeGenerators/CallSitesGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public static void GenerateCallSites(IEnumerable<TargetFramework> targetFramewor
{
Serilog.Log.Debug("Generating CallSite definitions file ...");

Dictionary<string, AspectClass> aspectClasses = new Dictionary<string, AspectClass>();
var aspectClasses = new Dictionary<string, AspectClass>();
foreach(var tfm in targetFrameworks)
{
var dllPath = getDllPath(tfm);
RetrieveCallSites(aspectClasses, dllPath, tfm);
}

GenerateCallSites(aspectClasses, outputPath);
GenerateFile(aspectClasses, outputPath);
}

internal static void RetrieveCallSites(Dictionary<string, AspectClass> aspectClasses, string dllPath, TargetFramework tfm)
Expand All @@ -37,17 +37,17 @@ internal static void RetrieveCallSites(Dictionary<string, AspectClass> aspectCla
var tfmCategory = GetCategory(tfm);

// Open dll to extract all AspectsClass attributes.
using var asmDefinition = Mono.Cecil.AssemblyDefinition.ReadAssembly(dllPath);
using var asmDefinition = AssemblyDefinition.ReadAssembly(dllPath);

foreach (var aspectClassType in asmDefinition.MainModule.Types)
foreach (var type in asmDefinition.MainModule.Types)
{
var aspectClassAttribute = aspectClassType.CustomAttributes.FirstOrDefault(IsAspectClass);
var aspectClassAttribute = type.CustomAttributes.FirstOrDefault(IsAspectClass);
if (aspectClassAttribute is null)
{
continue;
}

var aspectClassLine = $"{GetAspectLine(aspectClassAttribute, out var category)} {aspectClassType.FullName}";
var aspectClassLine = $"{GetAspectLine(aspectClassAttribute, out var category)} {type.FullName}";
if (!aspectClasses.TryGetValue(aspectClassLine, out var aspectClass))
{
aspectClass = new AspectClass();
Expand All @@ -56,7 +56,7 @@ internal static void RetrieveCallSites(Dictionary<string, AspectClass> aspectCla
}

// Retrieve aspects
foreach(var method in aspectClassType.Methods)
foreach(var method in type.Methods)
{
foreach(var aspectAttribute in method.CustomAttributes.Where(IsAspect))
{
Expand Down Expand Up @@ -235,7 +235,7 @@ static string GetArgument(Mono.Cecil.CustomAttributeArgument customAttributeArgu
}
}

internal static void GenerateCallSites(Dictionary<string, AspectClass> aspectClasses, AbsolutePath outputPath)
internal static void GenerateFile(Dictionary<string, AspectClass> aspectClasses, AbsolutePath outputPath)
{
var sb = new StringBuilder();
sb.AppendLine("""
Expand All @@ -254,11 +254,11 @@ namespace trace
{
""");

foreach (var aspectClass in aspectClasses.OrderBy(k => k.Key.ToString(), StringComparer.OrdinalIgnoreCase))
foreach (var aspectClass in aspectClasses.OrderBy(static k => k.Key.ToString(), StringComparer.OrdinalIgnoreCase))
{
sb.AppendLine(Format(aspectClass.Key + aspectClass.Value.Subfix()));

foreach (var method in aspectClass.Value.Aspects.OrderBy(k => k.Key.ToString(), StringComparer.OrdinalIgnoreCase))
foreach (var method in aspectClass.Value.Aspects.OrderBy(static k => k.Key.ToString(), StringComparer.OrdinalIgnoreCase))
{
sb.AppendLine(Format(" " + method.Key + method.Value.Subfix()));
}
Expand Down Expand Up @@ -287,7 +287,7 @@ internal static TargetFrameworks GetCategory(TargetFramework tfm)
return (TargetFrameworks)Enum.Parse<TargetFrameworks>(tfm.ToString().ToUpper().Replace('.', '_'));
}

internal struct AspectClass
internal record AspectClass
{
public AspectClass() {}

Expand All @@ -300,7 +300,7 @@ public string Subfix()
}
}

internal struct Aspect
internal record Aspect
{
public Aspect() { }

Expand Down
Loading
Loading