perf: add SetTraceIdWithValue, cache OTEL span lookup#25
Conversation
Add SetTraceIdWithValue that returns (context.Context, string), avoiding a separate GetTraceId call after SetTraceId. SetTraceId delegates to it with //go:inline for zero overhead on existing callers. Cache oteltrace.SpanFromContext result in a local variable — called once instead of up to 3 times per invocation.
📝 WalkthroughWalkthroughThe notifier package's trace ID handling was refactored by introducing Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 |
Replace SetTraceId+GetTraceId+AddToLogContext pattern with a single SetTraceIdWithValue call in both ServerErrorInterceptor and ServerErrorStreamInterceptor. SetTraceIdWithValue returns the trace ID directly and already adds it to the log context, eliminating two redundant context lookups per request. Depends on: go-coldbrew/errors#25
There was a problem hiding this comment.
Pull request overview
This PR optimizes trace ID resolution on the notifier hot path by adding a new API that returns the resolved trace ID alongside the updated context, and by reducing repeated OpenTelemetry span lookups.
Changes:
- Add
SetTraceIdWithValue(ctx) (context.Context, string)to return the resolved trace ID without requiring a follow-upGetTraceIdcall. - Refactor
SetTraceIdto delegate toSetTraceIdWithValue. - Add unit tests covering the new API and OTEL attribute behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| notifier/notifier.go | Introduces SetTraceIdWithValue, caches span lookup, and refactors SetTraceId to delegate. |
| notifier/notifier_test.go | Adds tests for SetTraceIdWithValue (returned value, existing trace ID, OTEL attribute). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@notifier/notifier.go`:
- Line 580: Remove the invalid compiler directive "//go:inline"—locate the
literal comment "//go:inline" in notifier.go and delete it (do not replace with
any nonstandard directive); if you intended to control inlining behavior use
supported directives like "//go:noinline" or "//go:noescape" instead, but
otherwise just remove the "//go:inline" line so the code relies on the
compiler's automatic inlining.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 6163925c-9912-45be-8679-5d5acdb3eaf5
📒 Files selected for processing (2)
notifier/notifier.gonotifier/notifier_test.go
…e doc Address review feedback: - Remove //go:inline (not a valid Go compiler directive) - Add doc note that callers must use the returned context
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…31) * perf: use SetTraceIdWithValue to eliminate redundant context lookups Replace SetTraceId+GetTraceId+AddToLogContext pattern with a single SetTraceIdWithValue call in both ServerErrorInterceptor and ServerErrorStreamInterceptor. SetTraceIdWithValue returns the trace ID directly and already adds it to the log context, eliminating two redundant context lookups per request. Depends on: go-coldbrew/errors#25 * chore: bump go-coldbrew/errors to v0.2.10 Required for SetTraceIdWithValue API added in errors#25.
Summary
SetTraceIdWithValuereturning(context.Context, string)to avoid a separateGetTraceIdcall afterSetTraceId— eliminates redundant context lookups on the hot pathSetTraceIddelegates toSetTraceIdWithValue— existing callers are unaffected (public API preserved)oteltrace.SpanFromContext(ctx)result: called once instead of up to 3 times per invocationRef: ROADMAP items 1.2, 1.3
Test plan
TestSetTraceId_*tests pass unchanged (public API preserved)TestSetTraceIdWithValue_*tests verify returned trace ID matchesGetTraceIdSetTraceIdWithValuego test -race ./notifier/...passes