Skip to content

Commit 5df80e1

Browse files
Automatically instrument methods decorated with [Trace] (Datadog.Trace.Annotations.TraceAttribute) (DataDog#2606)
* Implement feature to customize span operation and resource name in TraceAnnotationsIntegration spans by decorating a method specified by DD_TRACE_METHODS with a Datadog.Trace.TraceAttribute Next: Have the profiler automatically find methods with the Datadog.Trace.TraceAttribute and instrument them with the TraceAnnotationsIntegration * Test: Update tests with a brand new type that isn't not specified in DD_TRACE_METHODS, decorate it with the Datadog.Trace.TraceAttribute, and assert that we have a new span. Also apply the attribute to methods already specified by DD_TRACE_METHODS and assert that we have the same number of spans and that the attribute's ResourceName and OperationName properties get applied to the generated span. Product: - Modify the ModuleLoadFinished callback to look through a module (so long as we don't intentionally skip it) to look for methods decorated with Datadog.Trace.TraceAttribute and request ReJIT on them. If the integrations have not been loaded yet, they're put on a queue to be requested for ReJIT at a later ModuleLoadFinished callback. - Add a new PInvoke in the profiler to add the TraceAnnotationsIntegration type earlier in the startup code path than the InitializeTraceMethods PInvoke, which occurs after Tracer initialization * Remove Samples.TraceAnnotations.Program[RunTestsAsync] from DD_TRACE_METHODS and mark it with [Trace] to demonstrate that we can instrument early methods (except for the entrypoint Main) at this point * Update GetIntegrationsFromTraceMethodsConfiguration signature to accept a TypeReference argument so we can re-use the one trace_annotation_integration_type TypeReference * Add documentation to Datadog.Trace.TraceAttribute and update public api test * Add log message in TraceAnnotationInfoFactory * Try to remove whitespace changes * Fix typo in test case * Improve logging in cor_profiler.cpp * Handle Datadog.Trace.TraceAttribute in managed code only by doing reflection, do not depend on having a type reference to it * - Rename Datadog.Trace.TraceAttribute to Datadog.Trace.Annotations.TraceAttribute and move it to a new Datadog.Trace.Annotations assembly/package - In Samples.TraceAnnotations, test the official Datadog.Trace.Annotations.TraceAttribute type from the Datadog.Trace.Annotations assembly under alias OfficialTraceAttribute and test a custom-built Datadog.Trace.Annotations.TraceAttribute type from the Samples.TraceAnnotations assembly under alias CustomTraceAttribute * Add new public API test for Datadog.Trace.Annotations assembly and do some small refactorin * Add public api tests for Datadog.Trace.AspNet * Fix snapshots after rebase * Update docs/Datadog.Trace.Annotations/README.md Co-authored-by: Andrew Lock <andrew.lock@datadoghq.com> * Delete public API test for Datadog.Trace.AspNet since we don't expose this to developers * Implement PR feedback - Apply nullable at project level for Datadog.Trace.Annotations - Add attribute to property setter to demonstrate how to use the trace attribute on property accessors methods - Create a constant field for WStr("Datadog.Trace.Annotations.TraceAttribute") and a static field for its c_string - Fix comments - Fix public API snapshots for net6.0 * Fix transient issue with public API test where the test would fail if running multiple tests at once * Additional docs update to mention the existence of the Datadog.Trace.Annotations NuGet package from the Datadog.Trace README. Also add further documentation in the Datadog.Trace.Annotations README. * Add code snippet for TraceAttribute into README * Add missing Datadog.OpenTracing public api test * Update trace annotations snapshots * Optimization: Do not search for trace attributes in assemblies that have the following prefixes - "System." - "Microsoft." - "Datadog." * Use an updated version of the Datadog.Trace package for the Samples.TraceAnnotations.VersionMismatch.NewerNuGet test application that contains the logic to scan for [Trace] * Add feature flag DD_TRACE_ANNOTATIONS_ENABLED to control whether the trace annotations feature is enabled, since it incurs an added runtime cost. The default is true so users can immediately take advantage of the feature Co-authored-by: Andrew Lock <andrew.lock@datadoghq.com>
1 parent 9024d66 commit 5df80e1

File tree

54 files changed

+1509
-265
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1509
-265
lines changed

Datadog.Trace.sln

+15
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samples.GrpcDotNet", "trace
476476
EndProject
477477
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samples.GrpcLegacy", "tracer\test\test-applications\integrations\Samples.GrpcLegacy\Samples.GrpcLegacy.csproj", "{754F73E1-F7A4-47C7-A3F7-DC59ADA5105A}"
478478
EndProject
479+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Datadog.Trace.Annotations", "tracer\src\Datadog.Trace.Annotations\Datadog.Trace.Annotations.csproj", "{4067EAF6-28C5-4B04-9C8A-80720C0541E6}"
480+
EndProject
479481
Global
480482
GlobalSection(SharedMSBuildProjectFiles) = preSolution
481483
tracer\test\test-applications\Samples.Shared\Samples.Shared.projitems*{24d3e547-8897-4111-baa2-b92f50cc13d4}*SharedItemsImports = 5
@@ -2366,6 +2368,18 @@ Global
23662368
{754F73E1-F7A4-47C7-A3F7-DC59ADA5105A}.Release|x64.Build.0 = Release|x64
23672369
{754F73E1-F7A4-47C7-A3F7-DC59ADA5105A}.Release|x86.ActiveCfg = Release|x86
23682370
{754F73E1-F7A4-47C7-A3F7-DC59ADA5105A}.Release|x86.Build.0 = Release|x86
2371+
{4067EAF6-28C5-4B04-9C8A-80720C0541E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2372+
{4067EAF6-28C5-4B04-9C8A-80720C0541E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
2373+
{4067EAF6-28C5-4B04-9C8A-80720C0541E6}.Debug|x64.ActiveCfg = Debug|Any CPU
2374+
{4067EAF6-28C5-4B04-9C8A-80720C0541E6}.Debug|x64.Build.0 = Debug|Any CPU
2375+
{4067EAF6-28C5-4B04-9C8A-80720C0541E6}.Debug|x86.ActiveCfg = Debug|Any CPU
2376+
{4067EAF6-28C5-4B04-9C8A-80720C0541E6}.Debug|x86.Build.0 = Debug|Any CPU
2377+
{4067EAF6-28C5-4B04-9C8A-80720C0541E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
2378+
{4067EAF6-28C5-4B04-9C8A-80720C0541E6}.Release|Any CPU.Build.0 = Release|Any CPU
2379+
{4067EAF6-28C5-4B04-9C8A-80720C0541E6}.Release|x64.ActiveCfg = Release|Any CPU
2380+
{4067EAF6-28C5-4B04-9C8A-80720C0541E6}.Release|x64.Build.0 = Release|Any CPU
2381+
{4067EAF6-28C5-4B04-9C8A-80720C0541E6}.Release|x86.ActiveCfg = Release|Any CPU
2382+
{4067EAF6-28C5-4B04-9C8A-80720C0541E6}.Release|x86.Build.0 = Release|Any CPU
23692383
EndGlobalSection
23702384
GlobalSection(SolutionProperties) = preSolution
23712385
HideSolutionNode = FALSE
@@ -2543,6 +2557,7 @@ Global
25432557
{2CC63AEB-0098-4D3B-9606-F07692C03E90} = {BAF8F246-3645-42AD-B1D0-0F7EAFBAB34A}
25442558
{DEACDE01-95FE-4777-B70A-F20A96AABEA7} = {BAF8F246-3645-42AD-B1D0-0F7EAFBAB34A}
25452559
{754F73E1-F7A4-47C7-A3F7-DC59ADA5105A} = {BAF8F246-3645-42AD-B1D0-0F7EAFBAB34A}
2560+
{4067EAF6-28C5-4B04-9C8A-80720C0541E6} = {9E5F0022-0A50-40BF-AC6A-C3078585ECAB}
25462561
EndGlobalSection
25472562
GlobalSection(ExtensibilityGlobals) = postSolution
25482563
SolutionGuid = {160A1D00-1F5B-40F8-A155-621B4459D78F}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Datadog.Trace.Annotations NuGet package
2+
3+
This package contains custom attribute types to enable additional features of the Datadog APM instrumentation library.
4+
5+
> Note: Automatic instrumentation is required for the attributes in this package to take effect. Please [read our documentation](https://docs.datadoghq.com/tracing/setup/dotnet) for details on how to install the tracer for automatic instrumentation.
6+
7+
> Note: If you are unable to add new package references to your application, you may still enable this functionality by defining types inside your application whose full name and type members match the definitions in this package.
8+
9+
## Attributes
10+
### Datadog.Trace.Annotations.TraceAttribute
11+
An attribute that marks the decorated method to be instrumented by Datadog automatic instrumentation. [Source](https://github.com/DataDog/dd-trace-dotnet/tree/master/tracer/src/Datadog.Trace.Annotations/TraceAttribute.cs)
12+
13+
```csharp
14+
using System;
15+
16+
namespace Datadog.Trace.Annotations;
17+
18+
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
19+
public class TraceAttribute : Attribute
20+
{
21+
public string OperationName { get; set; }
22+
23+
public string ResourceName { get; set; }
24+
}
25+
```
26+
27+
## Get in touch
28+
29+
If you have questions, feedback, or feature requests, reach our [support](https://docs.datadoghq.com/help).

docs/Datadog.Trace/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ This package contains the Datadog .NET APM tracer for configuring custom instrum
44

55
> If you are only using automatic instrumentation, **you do not need this package**. Please [read our documentation](https://docs.datadoghq.com/tracing/setup/dotnet) for details on how to install the tracer for automatic instrumentation.
66
7+
> If you are using automatic instrumentation and would like to interact with APM only through C# attributes, see the [Datadog.Trace.Annotations](https://www.nuget.org/packages/Datadog.Trace.Annotations/) NuGet package.
8+
79
## Getting Started
810

911
1. Configure the Datadog agent for APM [as described in our documentation](https://docs.datadoghq.com/tracing/setup_overview/setup/dotnet-core#configure-the-datadog-agent-for-apm).

tracer/build/_build/Build.Steps.cs

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ partial class Build
9393
{
9494
Solution.GetProject(Projects.DatadogTrace),
9595
Solution.GetProject(Projects.DatadogTraceOpenTracing),
96+
Solution.GetProject(Projects.DatadogTraceAnnotations),
9697
};
9798

9899
Project[] ParallelIntegrationTests => new[]

tracer/build/_build/Projects.cs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ public static class Projects
22
{
33
public const string DatadogMonitoringDistribution = "Datadog.Monitoring.Distribution";
44
public const string DatadogTrace = "Datadog.Trace";
5+
public const string DatadogTraceAnnotations = "Datadog.Trace.Annotations";
56
public const string DatadogTraceAspNet = "Datadog.Trace.AspNet";
67
public const string DatadogTraceOpenTracing = "Datadog.Trace.OpenTracing";
78
public const string DatadogTraceMsBuild = "Datadog.Trace.MSBuild";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
5+
<Nullable>enable</Nullable>
6+
7+
<!-- NuGet -->
8+
<Version>1.0.0</Version>
9+
<Title>Datadog APM Annotations</Title>
10+
<Description>Annotations to enable additional features of the Datadog APM instrumentation library.</Description>
11+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
12+
<EmbedUntrackedSources>true</EmbedUntrackedSources>
13+
<IncludeSymbols>true</IncludeSymbols>
14+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
15+
<PackageReadmeFile>README.md</PackageReadmeFile>
16+
</PropertyGroup>
17+
18+
<ItemGroup>
19+
<None Include="..\..\..\docs\Datadog.Trace.Annotations\README.md" Pack="true" PackagePath="\" />
20+
</ItemGroup>
21+
22+
<ItemGroup>
23+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
24+
</ItemGroup>
25+
26+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// <copyright file="TraceAttribute.cs" company="Datadog">
2+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
4+
// </copyright>
5+
6+
using System;
7+
8+
namespace Datadog.Trace.Annotations
9+
{
10+
/// <summary>
11+
/// Attribute that marks the decorated method to be instrumented
12+
/// by Datadog automatic instrumentation.
13+
/// </summary>
14+
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
15+
public class TraceAttribute : Attribute
16+
{
17+
/// <summary>
18+
/// Gets or sets the span operation name
19+
/// </summary>
20+
public string? OperationName { get; set; }
21+
22+
/// <summary>
23+
/// Gets or sets the span resource name
24+
/// </summary>
25+
public string? ResourceName { get; set; }
26+
}
27+
}

tracer/src/Datadog.Trace.ClrProfiler.Native/Datadog.Trace.ClrProfiler.Native.def

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ EXPORTS
99
EnableByRefInstrumentation
1010
EnableCallTargetStateByRef
1111
AddDerivedInstrumentations
12+
AddTraceAttributeInstrumentation
1213
InitializeTraceMethods

0 commit comments

Comments
 (0)