Repository navigation
fix(diagnostics-otel): keep model usage spans inside the turn trace - #235
Conversation
model.usage is emitted after the run settles, so its parent span is often no longer tracked or retained by the exporter. Resolving the parent through the trusted-only lookup then failed and the span started a brand-new trace, detaching token counts and cost from the turn they belong to. Fall back to the event's own trace context (remote parent) the way message delivery spans already do. The retained-context cleanup tests now assert the late span links through a remote parent rather than a retained one.
|
Claude encountered an error —— View job I'll analyze this and get back to you. |
There was a problem hiding this comment.
All reported issues were addressed across 2 files
Reply with feedback, questions, or to request a fix.
Fix all with cubic | Re-trigger cubic
…e control The late model.usage event cannot carry a runId (DiagnosticUsageEvent has no such field), so it resolves retained run contexts through the plain traceId:spanId key. Emit one usage event while the run context is still retained and assert it resolves the retained (non-remote) span context, so the post-cleanup isRemote assertion fails if drain or stop cleanup regresses. Also drop the invalid runId from the new fallback test (CI test-types).
augusteo
left a comment
There was a problem hiding this comment.
Review (sweep pickup from #pr-review-request)
Read the change and the helpers at head b6c7b6cf:
activeInternalOrTrustedContext(service.ts:2113) falls back exactly as described: active trusted span → retained span context →internalOrTrustedParentContext, which builds a remote span context from the event's own trace context (parentSpanId). The updated tests assertisRemote: truewith the event'straceId/CHILD_SPAN_ID, which matches that path.- This makes
openclaw.model.usageconsistent with the delivery spans (lines 2607/2626), which already use the same helper. Consistency argument holds. - Security: no new data exposure — the span attributes were already recorded before this change; only the parenting changes, so token/cost data lands in the turn trace instead of a detached one.
- Tests were updated, not deleted: the two old assertions (
setSpanContextnot called, no parent) were inverted into positive assertions of the new fallback, plus a new dedicated test for the untracked-parent case.
Could not execute the jest suite locally (review runner disk-full), so the claimed 96/96 exporter suite is taken from CI/author. The change is one call-site line plus tests and reads correctly. No blocking issues.
There was a problem hiding this comment.
All reported issues were addressed across 1 file (changes from recent commits).
Reply with feedback, questions, or to request a fix.
Fix all with cubic | Re-trigger cubic
augusteo
left a comment
There was a problem hiding this comment.
Re-reviewed at head 0b7c88d (supersedes my earlier approval on b6c7b6c).
Production change is a one-call-site fix: recordModelUsage now resolves its parent with activeInternalOrTrustedContext instead of activeTrustedParentContext. Verified the helper at head: it tries (1) the event's own spanId in active/aliased trusted spans, (2) retained span contexts, (3) falls back to the event's own remote parent context — exactly what delivery spans already do. Since model.usage is emitted after the run settles, the old code returned undefined and startSpan created a detached root trace; the fallback keeps tokens/cost in the turn trace.
The two commits since the prior approval are test-only: a positive control asserting the retained (non-remote) context wins while the run context is still retained, and a flush-before-read determinism fix. Tests updated, not deleted; no production behavior change since the last pass.
No new data exposure — span attributes are unchanged; only parenting changes.
|
Independent verification at
|
augusteo
left a comment
There was a problem hiding this comment.
Review: approved
Re-reviewed the exact current head 0b7c88d6. The production change correctly uses activeInternalOrTrustedContext so late model.usage events retain the turn trace through an active/retained context when available, then fall back to the event trace context instead of creating a detached root.
Verified:
- the new untracked-parent regression test asserts the same trace and parent span;
- the cleanup tests now include a positive retained-context control and explicit event flushing before assertions;
- both review threads are resolved at this head;
- exact-head targeted checks are green, including OpenGrep, Workflow Sanity, Security Sensitive Guard, Dependency Guard, CodeQL, Periphery,
check-test-types,check-lint, andcheck-additional-extension-bundled.
The large matrix CI workflow is still running with no failures observed. This approval is for the code; no merge or deployment was performed.
…238) Root version, 135 plugin manifests synced via scripts/sync-plugin-versions.ts, changed npm-shrinkwrap.json files regenerated, CHANGELOG section for boon.46. Shipping: #237 (diagnostics-otel owns its tracer and meter providers instead of the global ones, so it stops handing spans to a co-running SDK), #235 (model.usage stays inside the turn trace), #233 (a plugin's scheduled turn is no longer rolled back by an unrelated registry becoming active), #229 (browser-handoff recheck scheduling failure is logged), #212 (dev-only vitest bump in ui). Base = 2026.6.11-boon.45. Gateway plus @openclaw/slack, @openclaw/msteams and @openclaw/diagnostics-prometheus bumped in lockstep; no separately published plugin source changed in this train, so a gateway roll delivers all of it.
What Problem This Solves
openclaw.model.usage(token counts, cost) often landed in a brand-new trace instead of the turn it belongs to. In the local E2E for #227 the usage span appeared as a separate root with its own trace id whileopenclaw.run/openclaw.model.callshared the turn trace. Drilling into a slow turn in Tempo therefore showed timings but no usage.Why This Change Was Made
model.usageis emitted byagent-runnerafter the run settles, as a child ofrunResult.diagnosticTrace. By then the parent span is usually no longer tracked or retained by the exporter, andrecordModelUsageresolved its parent withactiveTrustedParentContext, which returnsundefinedwhen the parent is not tracked, sotracer.startSpancreated a new root trace.recordMessageDeliveryalready usesactiveInternalOrTrustedContext, which falls back to the event's own trace context as a remote parent; the usage span now does the same.User Impact
Tokens and cost are attached to the same trace as the run, model calls, tools, and delivery. No config or protocol change; spans keep their names and attributes.
Evidence
undefined), green after.isRemote: true, parent = the run span id) instead of asserting no linkage at all, which preserves their intent (retained contexts released) without forcing a detached trace.pnpm test extensions/diagnostics-otel/src/service.test.ts96/96;pnpm tsgo:extensionsclean; oxlint clean;git diff --checkclean.Summary by cubic
Keeps
openclaw.model.usagespans (token counts and cost) in the same trace as the rest of the turn instead of starting a detached trace.The usage span is emitted after the run settles, so its parent span is often no longer tracked by the exporter;
activeTrustedParentContextthen returnedundefinedand the span started a brand-new root trace. Resolving the parent now usesactiveInternalOrTrustedContext, which falls back to the event's own trace context as a remote parent — the same approachrecordMessageDeliveryalready uses. Spans keep their names and attributes, and no config or protocol change is required.Tests
Written for commit 0b7c88d. Summary will update on new commits.