Skip to content

Commit b458e10

Browse files
committed
Preserve ambient trace context
Co-authored-by: Copilot App <[email protected]> Copilot-Session: 23d8c6ac-3d44-4b96-adfe-1e8509594f79
1 parent c3dd64c commit b458e10

3 files changed

Lines changed: 49 additions & 3 deletions

File tree

‎src/OpenClaw.Connection/GatewayConnectionManager.cs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,8 +2293,7 @@ outcome is "failure" or "pairing_rejected"
22932293
: outcome == "success"
22942294
? ActivityStatusCode.Ok
22952295
: ActivityStatusCode.Unset);
2296-
activity.Stop();
2297-
activity.Dispose();
2296+
OpenClawTelemetry.StopDetachedActivity(activity);
22982297
}
22992298

23002299
private void RecordTelemetryStateTransitions(GatewayConnectionSnapshot snapshot)

‎src/OpenClaw.Shared/Telemetry/OpenClawTelemetry.cs‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ public static class OpenClawTelemetry
4545
/// </summary>
4646
/// <remarks>
4747
/// Use this for operations that begin in one asynchronous callback and finish in another.
48-
/// The caller owns the returned activity and must stop or dispose it.
48+
/// The caller owns the returned activity and must finish it with
49+
/// <see cref="StopDetachedActivity(Activity?)"/> so stopping it cannot replace a newer
50+
/// ambient activity with the context captured when this span started.
4951
/// </remarks>
5052
public static Activity? StartDetachedActivity(
5153
string spanName,
@@ -90,6 +92,26 @@ public static class OpenClawTelemetry
9092
}
9193
}
9294

95+
/// <summary>
96+
/// Stops and disposes a detached activity without changing the caller's ambient activity.
97+
/// </summary>
98+
public static void StopDetachedActivity(Activity? activity)
99+
{
100+
if (activity == null)
101+
return;
102+
103+
var current = Activity.Current;
104+
try
105+
{
106+
activity.Stop();
107+
activity.Dispose();
108+
}
109+
finally
110+
{
111+
Activity.Current = current;
112+
}
113+
}
114+
93115
/// <summary>
94116
/// Runs a synchronous action inside a span and automatically marks success, cancellation, or failure.
95117
/// </summary>

‎tests/OpenClaw.Shared.Tests/Telemetry/OpenClawTelemetryTests.cs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,31 @@ public void StartDetachedActivity_WithExplicitParent_CreatesChildAndPreservesAmb
9393
Assert.Same(ambient, Activity.Current);
9494
}
9595

96+
[Fact]
97+
public void StopDetachedActivity_PreservesNewerAmbientActivity()
98+
{
99+
using var collector = ActivityCollector.Listen(OpenClawActivitySourceName.OpenClaw.ToTelemetryName());
100+
using var original = new Activity("original").Start();
101+
var detached = OpenClawTelemetry.StartDetachedActivity("test.detached");
102+
using var newer = new Activity("newer").Start();
103+
104+
OpenClawTelemetry.StopDetachedActivity(detached);
105+
106+
Assert.NotNull(detached);
107+
Assert.True(detached!.IsStopped);
108+
Assert.Same(newer, Activity.Current);
109+
}
110+
111+
[Fact]
112+
public void StopDetachedActivity_WithNull_IsSafe()
113+
{
114+
using var ambient = new Activity("ambient").Start();
115+
116+
OpenClawTelemetry.StopDetachedActivity(null);
117+
118+
Assert.Same(ambient, Activity.Current);
119+
}
120+
96121
[Fact]
97122
public void Trace_WithListener_RecordsSuccessAndTags()
98123
{

0 commit comments

Comments
 (0)