fix(ai): roll up OpenTelemetry usage across iterations#928
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (5)
📝 WalkthroughWalkthroughThe OpenTelemetry middleware now accumulates usage across chat iterations and applies it once to the root span on finish, error, or abort. Unit and E2E tests cover multi-iteration tool loops, per-iteration spans, metrics, and aggregated root attributes. ChangesOpenTelemetry root usage rollup
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant E2ETest
participant OtelUsageRoute
participant Chat
participant otelMiddleware
participant SpanExporter
E2ETest->>OtelUsageRoute: POST provider=tool-loop
OtelUsageRoute->>Chat: invoke chat with weatherTool
Chat->>otelMiddleware: emit usage for each iteration
otelMiddleware->>otelMiddleware: accumulate root usage
Chat->>otelMiddleware: finish after tool loop
otelMiddleware->>SpanExporter: export iteration spans and rolled-up root span
SpanExporter-->>E2ETest: return captured spans
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
testing/e2e/tests/middleware.spec.tsParsing error: "parserOptions.project" has been provided for Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
onSpanEndcallback on finish, error, and abort pathsProvenance
Weco supplied the request-level accumulator direction. The strict evaluator rejected its initial field mapping, so the final implementation was rebuilt through the existing
usageAttributes()contract and hardened across every terminal path. Public autoresearch trajectoryValidation
root_usage_abs_error_tokensimproved from550to0; finish, error, and abort fixtures passedpnpm --filter @tanstack/ai test:lib --run tests/middlewares/otel.test.ts(50tests passed)pnpm --filter @tanstack/ai test:lib(1,137tests passed)pnpm --filter @tanstack/ai test:typespnpm --filter @tanstack/ai test:eslint(no errors; existing warnings only)pnpm --filter @tanstack/ai-e2e test:types347tests with one skipped, one retry-only flaky test, and one environment-only MiniflareEPIPEfailure that reproduces on the unchanged baseCI=1 pnpm test:prgit diff --checkConflict Review
PR #896 changes adjacent usage-attribute tests and E2E helpers but does not implement request-level root-span rollup. It may require a small test/helper rebase if merged first; it is not a behavioral duplicate of this fix.
Fixes #916
Summary by CodeRabbit
Bug Fixes
Tests