Skip to content

Commit 3406b39

Browse files
khanayan123claude
andcommitted
Add test for inherited root session ID
- Add RuntimeId.ResetForTests() to allow resetting cached state - Add RootSessionId_InheritsFromEnvVar test Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 6385c47 commit 3406b39

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

tracer/src/Datadog.Trace/Util/RuntimeId.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ internal static class RuntimeId
2020

2121
public static string GetRootSessionId() => LazyInitializer.EnsureInitialized(ref _rootSessionId, () => GetRootSessionIdImpl());
2222

23+
internal static void ResetForTests() => _rootSessionId = null;
24+
2325
private static string GetImpl()
2426
{
2527
if (NativeLoader.TryGetRuntimeIdFromNative(out var runtimeId))

tracer/test/Datadog.Trace.Tests/Util/RuntimeIdTests.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,32 @@ namespace Datadog.Trace.Tests.Util
1414
public class RuntimeIdTests
1515
{
1616
[Fact]
17-
public void RootSessionId_DefaultsToRuntimeId()
17+
public void RootSessionId_UsesRuntimeIdWhenNotInherited_AndInheritsWhenSet()
1818
{
19-
var rootSessionId = RuntimeId.GetRootSessionId();
20-
rootSessionId.Should().Be(RuntimeId.Get());
21-
}
19+
try
20+
{
21+
// When no env var is set, root session ID should default to runtime ID
22+
RuntimeId.ResetForTests();
23+
Environment.SetEnvironmentVariable(ConfigurationKeys.Telemetry.RootSessionId, null);
2224

23-
[Fact]
24-
public void RootSessionId_SetsEnvVar()
25-
{
26-
var rootSessionId = RuntimeId.GetRootSessionId();
27-
Environment.GetEnvironmentVariable(ConfigurationKeys.Telemetry.RootSessionId)
28-
.Should().Be(rootSessionId);
25+
var rootSessionId = RuntimeId.GetRootSessionId();
26+
rootSessionId.Should().Be(RuntimeId.Get());
27+
28+
// When env var is pre-set (simulating a child process), root session ID
29+
// should return the inherited value instead of the current runtime ID
30+
var inherited = "inherited-root-session-id";
31+
RuntimeId.ResetForTests();
32+
Environment.SetEnvironmentVariable(ConfigurationKeys.Telemetry.RootSessionId, inherited);
33+
34+
RuntimeId.GetRootSessionId().Should().Be(inherited);
35+
RuntimeId.GetRootSessionId().Should().NotBe(RuntimeId.Get());
36+
}
37+
finally
38+
{
39+
RuntimeId.ResetForTests();
40+
Environment.SetEnvironmentVariable(ConfigurationKeys.Telemetry.RootSessionId, null);
41+
RuntimeId.GetRootSessionId();
42+
}
2943
}
3044
}
3145
}

0 commit comments

Comments
 (0)