Skip to content

Commit 3f1d126

Browse files
authored
Add process tags to dynamic instrumentation snapshots (#7839)
## Summary of changes add a process tags field next to where the service name is added in the snapshot json. dotnet version of DataDog/dd-trace-java#8779 ## Reason for change AIDM-195 ## Implementation details I wanted to inject the setting from https://github.com/DataDog/dd-trace-dotnet/blob/79b266b0c7a329eae91584356a1cfc7c5599c1ee/tracer/src/Datadog.Trace/Debugger/DebuggerFactory.cs#L30 but looking at the call chain to bring it to the snapshot creator, it was quite tedious and messy. Another option was to put it in the debugger settings, but it felt out of place as those settings are specific to the live debugger, not a repeat of global tracer settings. In the end I decided to inject _a little bit_ so that I could specify the value in tests. ## Test coverage just made sure the test that checks json correctness includes this ## Other details <!-- Fixes #{issue} --> <!-- ⚠️ Note: Where possible, please obtain 2 approvals prior to merging. Unless CODEOWNERS specifies otherwise, for external teams it is typically best to have one review from a team member, and one review from apm-dotnet. Trivial changes do not require 2 reviews. MergeQueue is NOT enabled in this repository. If you have write access to the repo, the PR has 1-2 approvals (see above), and all of the required checks have passed, you can use the Squash and Merge button to merge the PR. If you don't have write access, or you need help, reach out in the #apm-dotnet channel in Slack. -->
1 parent 3c85e98 commit 3f1d126

File tree

6 files changed

+24
-16
lines changed

6 files changed

+24
-16
lines changed

tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/ExceptionReplaySnapshotCreator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ namespace Datadog.Trace.Debugger.ExceptionAutoInstrumentation
1414
{
1515
internal class ExceptionReplaySnapshotCreator : DebuggerSnapshotCreator
1616
{
17-
public ExceptionReplaySnapshotCreator(bool isFullSnapshot, ProbeLocation location, bool hasCondition, string[] tags, CaptureLimitInfo limitInfo)
18-
: base(isFullSnapshot, location, hasCondition, tags, limitInfo)
17+
public ExceptionReplaySnapshotCreator(bool isFullSnapshot, ProbeLocation location, bool hasCondition, string[] tags, CaptureLimitInfo limitInfo, bool withProcessTags)
18+
: base(isFullSnapshot, location, hasCondition, tags, limitInfo, withProcessTags)
1919
{
2020
}
2121

22-
public ExceptionReplaySnapshotCreator(bool isFullSnapshot, ProbeLocation location, bool hasCondition, string[] tags, MethodScopeMembers methodScopeMembers, CaptureLimitInfo limitInfo)
23-
: base(isFullSnapshot, location, hasCondition, tags, methodScopeMembers, limitInfo)
22+
public ExceptionReplaySnapshotCreator(bool isFullSnapshot, ProbeLocation location, bool hasCondition, string[] tags, MethodScopeMembers methodScopeMembers, CaptureLimitInfo limitInfo, bool withProcessTags)
23+
: base(isFullSnapshot, location, hasCondition, tags, methodScopeMembers, limitInfo, withProcessTags)
2424
{
2525
}
2626

tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/TrackedStackFrameNode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private string CreateSnapshot()
134134
MaxFieldCount: DebuggerSettings.DefaultMaxNumberOfFieldsToCopy,
135135
MaxLength: DebuggerSettings.DefaultMaxStringLength);
136136

137-
using var snapshotCreator = new ExceptionReplaySnapshotCreator(isFullSnapshot: true, location: ProbeLocation.Method, hasCondition: false, Array.Empty<string>(), members, limitInfo: limitInfo);
137+
using var snapshotCreator = new ExceptionReplaySnapshotCreator(isFullSnapshot: true, ProbeLocation.Method, hasCondition: false, [], members, limitInfo, Tracer.Instance.Settings.PropagateProcessTags);
138138

139139
_snapshotId = snapshotCreator.SnapshotId;
140140

tracer/src/Datadog.Trace/Debugger/Expressions/ProbeProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public IProbeProcessor UpdateProbeProcessor(ProbeDefinition probe)
121121

122122
public IDebuggerSnapshotCreator CreateSnapshotCreator()
123123
{
124-
return new DebuggerSnapshotCreator(ProbeInfo.IsFullSnapshot, ProbeInfo.ProbeLocation, ProbeInfo.HasCondition, ProbeInfo.Tags, ProbeInfo.CaptureLimitInfo);
124+
return new DebuggerSnapshotCreator(ProbeInfo.IsFullSnapshot, ProbeInfo.ProbeLocation, ProbeInfo.HasCondition, ProbeInfo.Tags, ProbeInfo.CaptureLimitInfo, Tracer.Instance.Settings.PropagateProcessTags);
125125
}
126126

127127
private void SetExpressions(ProbeDefinition probe)

tracer/src/Datadog.Trace/Debugger/Snapshots/DebuggerSnapshotCreator.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ internal class DebuggerSnapshotCreator : IDebuggerSnapshotCreator, IDisposable
3333
private readonly bool _isFullSnapshot;
3434
private readonly ProbeLocation _probeLocation;
3535
private readonly CaptureLimitInfo _limitInfo;
36+
private readonly bool _injectProcessTags;
3637

3738
private long _lastSampledTime;
3839
private TimeSpan _accumulatedDuration;
@@ -42,7 +43,7 @@ internal class DebuggerSnapshotCreator : IDebuggerSnapshotCreator, IDisposable
4243
private string _snapshotId;
4344
private ObjectPool<MethodScopeMembers, MethodScopeMembersParameters> _scopeMembersPool;
4445

45-
public DebuggerSnapshotCreator(bool isFullSnapshot, ProbeLocation location, bool hasCondition, string[] tags, CaptureLimitInfo limitInfo)
46+
public DebuggerSnapshotCreator(bool isFullSnapshot, ProbeLocation location, bool hasCondition, string[] tags, CaptureLimitInfo limitInfo, bool withProcessTags)
4647
{
4748
_isFullSnapshot = isFullSnapshot;
4849
_probeLocation = location;
@@ -55,13 +56,14 @@ public DebuggerSnapshotCreator(bool isFullSnapshot, ProbeLocation location, bool
5556
ProbeHasCondition = hasCondition;
5657
Tags = tags;
5758
_limitInfo = limitInfo;
59+
_injectProcessTags = withProcessTags;
5860
_accumulatedDuration = new TimeSpan(0, 0, 0, 0, 0);
5961
_scopeMembersPool = new ObjectPool<MethodScopeMembers, MethodScopeMembersParameters>();
6062
Initialize();
6163
}
6264

63-
public DebuggerSnapshotCreator(bool isFullSnapshot, ProbeLocation location, bool hasCondition, string[] tags, MethodScopeMembers methodScopeMembers, CaptureLimitInfo limitInfo)
64-
: this(isFullSnapshot, location, hasCondition, tags, limitInfo)
65+
public DebuggerSnapshotCreator(bool isFullSnapshot, ProbeLocation location, bool hasCondition, string[] tags, MethodScopeMembers methodScopeMembers, CaptureLimitInfo limitInfo, bool withProcessTags)
66+
: this(isFullSnapshot, location, hasCondition, tags, limitInfo, withProcessTags)
6567
{
6668
MethodScopeMembers = methodScopeMembers;
6769
}
@@ -730,7 +732,7 @@ internal void FinalizeSnapshot(string methodName, string typeFullName, string pr
730732
.EndSnapshot()
731733
.EndDebugger()
732734
.AddLoggerInfo(methodName, typeFullName, probeFilePath)
733-
.AddGeneralInfo(DebuggerManager.Instance.ServiceName, traceId, spanId)
735+
.AddGeneralInfo(DebuggerManager.Instance.ServiceName, ProcessTags.SerializedTags, traceId, spanId)
734736
.AddMessage()
735737
.Complete();
736738
}
@@ -868,11 +870,17 @@ internal DebuggerSnapshotCreator AddLoggerInfo(string methodName, string typeFul
868870
return this;
869871
}
870872

871-
internal DebuggerSnapshotCreator AddGeneralInfo(string service, string traceId, string spanId)
873+
internal DebuggerSnapshotCreator AddGeneralInfo(string service, string processTags, string traceId, string spanId)
872874
{
873875
JsonWriter.WritePropertyName("service");
874876
JsonWriter.WriteValue(service ?? UnknownValue);
875877

878+
if (_injectProcessTags && !string.IsNullOrEmpty(processTags))
879+
{
880+
JsonWriter.WritePropertyName("process_tags");
881+
JsonWriter.WriteValue(processTags);
882+
}
883+
876884
JsonWriter.WritePropertyName("ddsource");
877885
JsonWriter.WriteValue(DebuggerTags.DDSource);
878886

tracer/test/Datadog.Trace.Tests/Debugger/DebuggerSnapshotCreatorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public async Task SpecialType_LazyInitialized()
9696
/// </summary>
9797
internal async Task ValidateSingleValue(object local)
9898
{
99-
var snapshot = SnapshotHelper.GenerateSnapshot(local);
99+
var snapshot = SnapshotHelper.GenerateSnapshot(local, withProcessTags: true);
100100

101101
output.WriteLine("Snapshot: " + snapshot);
102102
var verifierSettings = new VerifySettings();

tracer/test/Datadog.Trace.Tests/Debugger/SnapshotHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,23 @@ static SnapshotHelper()
3131
DebuggerSnapshotSerializer.SetConfig(testSettings);
3232
}
3333

34-
internal static string GenerateSnapshot(object instance, bool prettify = true)
34+
internal static string GenerateSnapshot(object instance, bool prettify = true, bool withProcessTags = false)
3535
{
36-
return GenerateSnapshot(null, new object[] { }, new object[] { instance }, prettify);
36+
return GenerateSnapshot(instance: null, new object[] { }, new object[] { instance }, prettify, withProcessTags);
3737
}
3838

3939
/// <summary>
4040
/// Generate a debugger snapshot by simulating the same flow of method calls as our instrumentation produces for a method probe.
4141
/// </summary>
42-
private static string GenerateSnapshot(object instance, object[] args, object[] locals, bool prettify)
42+
private static string GenerateSnapshot(object instance, object[] args, object[] locals, bool prettify, bool withProcessTags)
4343
{
4444
var maxInfo = new CaptureLimitInfo(
4545
MaxReferenceDepth: DebuggerSettings.DefaultMaxDepthToSerialize,
4646
MaxCollectionSize: DebuggerSettings.DefaultMaxNumberOfItemsInCollectionToCopy,
4747
MaxFieldCount: DebuggerSettings.DefaultMaxNumberOfFieldsToCopy,
4848
MaxLength: DebuggerSettings.DefaultMaxStringLength);
4949

50-
var snapshotCreator = new DebuggerSnapshotCreator(isFullSnapshot: true, ProbeLocation.Method, hasCondition: false, new[] { "Tag1", "Tag2" }, maxInfo);
50+
var snapshotCreator = new DebuggerSnapshotCreator(isFullSnapshot: true, ProbeLocation.Method, hasCondition: false, new[] { "Tag1", "Tag2" }, maxInfo, withProcessTags);
5151
{
5252
// method entry
5353
snapshotCreator.StartEntry();

0 commit comments

Comments
 (0)