fix(diagnostics): chain run traces to request scope · openclaw/openclaw@410783c
vincentkoc
·
2026-04-27
·
via Recent Commits to openclaw:main
File tree
agents/pi-embedded-runner/run
| Original file line number | Diff line number | Diff line change |
|---|
@@ -12,8 +12,8 @@ import { filterHeartbeatPairs } from "../../../auto-reply/heartbeat-filter.js";
|
12 | 12 | import { resolveChannelCapabilities } from "../../../config/channel-capabilities.js"; |
13 | 13 | import { emitTrustedDiagnosticEvent } from "../../../infra/diagnostic-events.js"; |
14 | 14 | import { |
15 | | -createDiagnosticTraceContext, |
16 | 15 | createChildDiagnosticTraceContext, |
| 16 | +createDiagnosticTraceContextFromActiveScope, |
17 | 17 | freezeDiagnosticTraceContext, |
18 | 18 | } from "../../../infra/diagnostic-trace-context.js"; |
19 | 19 | import { isEmbeddedMode } from "../../../infra/embedded-mode.js"; |
@@ -648,7 +648,9 @@ export async function runEmbeddedAttempt(
|
648 | 648 | const sessionLabel = params.sessionKey ?? params.sessionId; |
649 | 649 | const contextInjectionMode = resolveContextInjectionMode(params.config); |
650 | 650 | const agentDir = params.agentDir ?? resolveOpenClawAgentDir(); |
651 | | -const diagnosticTrace = freezeDiagnosticTraceContext(createDiagnosticTraceContext()); |
| 651 | +const diagnosticTrace = freezeDiagnosticTraceContext( |
| 652 | +createDiagnosticTraceContextFromActiveScope(), |
| 653 | +); |
652 | 654 | const runTrace = freezeDiagnosticTraceContext( |
653 | 655 | createChildDiagnosticTraceContext(diagnosticTrace), |
654 | 656 | ); |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -2,6 +2,7 @@ import { afterEach, describe, expect, it } from "vitest";
|
2 | 2 | import { |
3 | 3 | createChildDiagnosticTraceContext, |
4 | 4 | createDiagnosticTraceContext, |
| 5 | +createDiagnosticTraceContextFromActiveScope, |
5 | 6 | freezeDiagnosticTraceContext, |
6 | 7 | formatDiagnosticTraceparent, |
7 | 8 | getActiveDiagnosticTraceContext, |
@@ -158,4 +159,31 @@ describe("diagnostic-trace-context", () => {
|
158 | 159 | |
159 | 160 | expect(getActiveDiagnosticTraceContext()).toBeUndefined(); |
160 | 161 | }); |
| 162 | + |
| 163 | +it("creates child trace contexts from the active request scope", () => { |
| 164 | +const requestTrace = createDiagnosticTraceContext({ |
| 165 | +traceId: TRACE_ID, |
| 166 | +spanId: SPAN_ID, |
| 167 | +traceFlags: "00", |
| 168 | +}); |
| 169 | + |
| 170 | +runWithDiagnosticTraceContext(requestTrace, () => { |
| 171 | +const scoped = createDiagnosticTraceContextFromActiveScope({ |
| 172 | +spanId: CHILD_SPAN_ID, |
| 173 | +}); |
| 174 | + |
| 175 | +expect(scoped).toEqual({ |
| 176 | +traceId: TRACE_ID, |
| 177 | +spanId: CHILD_SPAN_ID, |
| 178 | +parentSpanId: SPAN_ID, |
| 179 | +traceFlags: "00", |
| 180 | +}); |
| 181 | +}); |
| 182 | + |
| 183 | +expect(createDiagnosticTraceContextFromActiveScope({ spanId: CHILD_SPAN_ID })).toEqual({ |
| 184 | +traceId: expect.stringMatching(/^[0-9a-f]{32}$/), |
| 185 | +spanId: CHILD_SPAN_ID, |
| 186 | +traceFlags: "01", |
| 187 | +}); |
| 188 | +}); |
161 | 189 | }); |
| Original file line number | Diff line number | Diff line change |
|---|
@@ -198,6 +198,16 @@ export function createChildDiagnosticTraceContext(
|
198 | 198 | }); |
199 | 199 | } |
200 | 200 | |
| 201 | +export function createDiagnosticTraceContextFromActiveScope( |
| 202 | +input: Omit<DiagnosticTraceContextInput, "traceId" | "traceparent"> = {}, |
| 203 | +): DiagnosticTraceContext { |
| 204 | +const active = getActiveDiagnosticTraceContext(); |
| 205 | +if (!active) { |
| 206 | +return createDiagnosticTraceContext(input); |
| 207 | +} |
| 208 | +return createChildDiagnosticTraceContext(active, input); |
| 209 | +} |
| 210 | + |
201 | 211 | export function freezeDiagnosticTraceContext( |
202 | 212 | context: DiagnosticTraceContext, |
203 | 213 | ): DiagnosticTraceContext { |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。