Repository navigation
Attach user context to traces and Sentry errors - #908
Merged
Merged
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (13)
📝 WalkthroughWalkthroughChangesObservability instrumentation
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant resolveRequestAuth
participant callerContextFields
participant MCPTool
participant logMcpEvent
participant Sentry
resolveRequestAuth->>Sentry: setUser({ id: stableUserId })
callerContextFields->>MCPTool: provide userId
MCPTool->>logMcpEvent: include userId in outcome payload
MCPTool->>Sentry: report failure with userId
sequenceDiagram
participant recordUsage
participant WorkersTracing
participant UsageSink
recordUsage->>WorkersTracing: enterSpan(kody.usage.{eventType})
recordUsage->>UsageSink: persist usage event
Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 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 |
kody-bot
marked this pull request as ready for review
July 24, 2026 14:06
Contributor
|
🔎 Preview deployed: https://kody-pr-908.kody-a99.workers.dev Worker: Mocks:
|
5 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Makes traces and error reports attributable to users and app-level features, following up on the OTel tracing rollout (#905, #906).
recordUsage(packages/worker/src/usage/record-usage.ts) now emits akody.usage.{eventType}trace span per metered event, nested under the active platform span, withkody.user_id,kody.event_type,kody.outcome, and optionalkody.entity_id/kody.duration_ms/kody.bytesattributes. The metering chokepoints (execute, package export, job run, workflow run, service runtime, outbound fetch, email send/receive) are exactly the app-level units worth finding in traces, so this single funnel makes every trace searchable by user and feature without touching the chokepoints. Emission follows the same never-throws contract as metering, and guards for runtimes withouttracing.sendDefaultPiistays false):McpObservabilityPayloadgainsuserId,callerContextFieldssupplies it from the caller context, andreportMcpFailureToSentrysetsscope.setUser({ id }). TheuserIdalso lands on the structuredmcp-eventlog lines. Call sites indefine-capability.ts,execute.ts, andsearch-tool-runner.tspass it through.resolveRequestAuth(packages/worker/src/app/request-auth-cache.ts) callsSentry.setUser({ id: stableUserId })once per request after auth resolution, so uncaught app-route errors are user-attributed.cloudflare-workers-stub.tsgains atracingstub; the Sentry stub gainssetUser;package-service.node.test.ts's module mock now spreads the original module.usage-metering.mddocuments the span contract;request-lifecycle.mdpoints at both hooks.Why
Cloudflare's automatic tracing only knows platform operations — traces had no user or feature identity, and Sentry errors (reporting now enabled in production) had none either. Per-user isolation is the core invariant of this repo, which makes per-user traceability the natural debugging axis.
Verification
record-usage.node.test.tscovers span emission, attribute shape, skip-on-missing-user, and optional-attribute omission (3 tests).observability.workers.test.tsextended forcallerContextFieldsuser id and payload serialization.npm run typecheckpasses; real-workerd usage/observability suites pass (thecloudflare:workersnamespace import withtracingaccess works in vitest-pool-workers).System recap — extends usage metering and MCP observability (medium risk)
Mode: recap · Base:
main@66d6e32d· Head: branch tipClassification: extends — the usage-metering helper contract gains a span-emission side effect and the MCP observability payload gains a field; no new primitives.
Primitives touched
usage-meteringrecordUsageemitskody.usage.*spansmcp-serverscope.setUser;mcp-eventlogs gainuserIdapp-sessionsSentry.setUserafter auth resolutionSystem map
User identity flows from auth/caller context into trace spans and Sentry error scopes because of this PR.
Legend: green = composes (wiring only) · amber = extended by this PR · red = new primitive · gray = context (unchanged).
flowchart LR appSessions["app-sessions<br/>Browser sessions"]:::touched mcpServer["mcp-server<br/>MCP endpoint"]:::extended usageMetering["usage-metering<br/>recordUsage()"]:::extended tracing["Workers tracing<br/>(platform)"]:::untouched sentry["Sentry<br/>(errors)"]:::untouched appSessions -->|"setUser({id}) after auth"| sentry mcpServer -->|"userId on mcp-event + failure scope"| sentry usageMetering -->|"kody.usage.* span with kody.user_id"| tracing classDef touched fill:#1a7f37,color:#fff classDef extended fill:#9a6700,color:#fff classDef added fill:#cf222e,color:#fff classDef untouched fill:#57606a,color:#fffInvariants
Per-user isolation is unaffected: user ids flow only into Kody's own telemetry (first-party spans/logs/Sentry), never across users. Only the stable user id is sent to Sentry — never email or display name.
Summary by CodeRabbit