Repository navigation
feat(server,do,ai): post-hoc span attributes + traced rag embed cost - #170
Conversation
Let a ctx.trace body attach attributes only known after it resolves via a new SpanHandle second callback arg (setAttribute/setAttributes), merged over the start attributes at record time (post-hoc wins on a key clash). Additive and backward-compatible — the handle is a trailing parameter, so every existing (trace) => ... body still compiles and behaves unchanged. Wire it through @lunora/ai/rag: the embed span now carries gen_ai.usage .input_tokens (from the AI SDK embed result usage) and gen_ai.usage.cost (defensively probed from providerMetadata / AI Gateway, omitted when absent). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0197KjhmBDB3PXAc6TXiPqZ9
✅ Deploy Preview for lunorash ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: ⛔ Files ignored due to path filters (7)
📒 Files selected for processing (7)
WalkthroughThe tracing APIs now provide span handles for post-hoc attributes. RAG embedding calls use them to record model identifiers, token usage, and available provider costs, while untraced calls retain their vector-only return path. ChangesEmbedding tracing
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant defineRag
participant ctxTrace as ctx.trace
participant embeddingModel as Embedding model
participant spanHandle as SpanHandle
defineRag->>ctxTrace: Start generation span with model id
ctxTrace->>embeddingModel: Execute embedding
embeddingModel-->>defineRag: Return vectors, usage, metadata
defineRag->>spanHandle: Record tokens and optional cost
ctxTrace-->>defineRag: Return embedding vectors
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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 |
|
Thank you for following the naming conventions! 🙏 |
|
Thank you for confirming the Contributor License Agreement! 🙏 |
Merging this PR will improve performance by 10.28%
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ⚡ | hit: cache.run returns memoized result |
73.2 µs | 66.4 µs | +10.28% |
Tip
Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.
Comparing feat/post-hoc-span-attributes (1de1b33) with alpha (0804b38)2
Footnotes
-
1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports. ↩
-
No successful run was found on
alpha(d71f8f1) during the generation of this report, so 0804b38 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩
Regenerates the server/do/lunora api-snapshots to record the new SpanHandle interface and the LunoraTracer/ContextTracer second `span` callback arg. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0197KjhmBDB3PXAc6TXiPqZ9
|
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Trace-fusion, piece 1 — post-hoc span attributes (the shippable, portable piece)
ctx.trace(name, fn, attributes?)captured attributes only at span start. This lets the wrapped body set attributes that are only known after it resolves (an AI call's token usage / dollar cost). Additive and backward-compatible.SpanHandle(shared/span-event.ts):setAttribute(key, value)/setAttributes(fields), exported from@lunora/server+@lunora/do.LunoraTracergains a second body arg —(trace, span) => …— mirrored asContextTracerin@lunora/do, kept in lockstep by thectx-telemetry-contractdrift guard. Start attributes still normalize up front (invariant preserved); post-hoc attrs merge at record time (post-hoc wins on key clash), emitted only when non-empty.(trace) => …bodies stay assignable (trailing-optional-param rule). Only one call site changed — the@lunora/testingpassthroughTracestub (it assigns toLunoraTracer, so needs the param). Allctx.trace(...)bodies compile unchanged.First consumer — RAG embed cost:
define-rag.ts(already traced since #160/#162) now attaches post-hocgen_ai.usage.input_tokens(from the embedusage) andgen_ai.usage.cost(defensiveproviderMetadataprobe — a no-op until AI-Gateway cost routing from #165 lands). Untraced path unchanged.Verify: do 1186 / ai 94 / testing 81 / server 419 tests pass; tsc clean across server/do/ai/testing (+ agent/runtime consumers); eslint clean.
Follow-up: trace-fusion piece 2 (fuse
ctx.traceinto Cloudflare's now-GA Workers custom-spans tree viatracing.enterSpan) is deferred behind aShardDOprototype — double-export risk + async-context-in-DO unverified.otlpSinkstays the portable default.🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Documentation