Skip to content

feat: refactor LogFields to use shared RequestContext#24

Merged
ankurs merged 2 commits intomainfrom
feat/request-context
Apr 4, 2026
Merged

feat: refactor LogFields to use shared RequestContext#24
ankurs merged 2 commits intomainfrom
feat/request-context

Conversation

@ankurs
Copy link
Copy Markdown
Member

@ankurs ankurs commented Apr 4, 2026

Summary

  • LogFields now wraps *options.Options instead of owning its own sync.RWMutex + map
  • AddToLogContext/FromContext delegate to options.AddToLogFields/options.LogFieldsFromContext
  • Log fields are stored in the same RequestContext as options — one context.WithValue instead of two
  • Range() calls RangeSlice() to preserve slice-snapshot iteration behavior
  • Removed unused newLogFields(), legacy context key, and sync import

Depends on: go-coldbrew/options#17 (must be merged and tagged first)

Note: go.mod contains a temporary replace directive for local development. This will be removed and the options version bumped once options#17 is tagged.

Test plan

  • All log tests pass (log, loggers/slog, wrap, all backend examples)
  • make lint passes (0 issues)
  • Benchmarks show no regression
  • Downstream packages verified locally: errors, interceptors, core

Summary by CodeRabbit

Release Notes

  • New Features

    • Added new methods to LogFields for sync.Map-compatible operations: Delete, Load, Range, and Store.
  • Documentation

    • Updated logging documentation with clarified behavior for handling nil contexts and LogFields storage mechanism.
    • Updated generated API documentation links.
  • Chores

    • Updated module dependency configuration.

LogFields now wraps *options.Options and stores log fields in the shared
RequestContext, eliminating a separate context.WithValue per request.
All methods delegate to the underlying Options; Range uses RangeSlice
for slice-snapshot iteration.

Depends on go-coldbrew/options#17. The go.mod replace directive must be
removed and the options version bumped once that PR is merged and tagged.
Copilot AI review requested due to automatic review settings April 4, 2026 10:22
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 4, 2026

Warning

Rate limit exceeded

@ankurs has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 0 minutes and 55 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 0 minutes and 55 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e99e7079-b694-46c3-98bc-007e507e3627

📥 Commits

Reviewing files that changed from the base of the PR and between f92a6d0 and a570634.

📒 Files selected for processing (2)
  • loggers/README.md
  • loggers/fields.go
📝 Walkthrough

Walkthrough

This PR refactors LogFields to delegate storage and context operations to the options package, updates the module dependency mapping to use a local options path, and adjusts documentation with new method signatures and line number references.

Changes

Cohort / File(s) Summary
Dependency Management
go.mod
Added replace directive to map github.com/go-coldbrew/options to local relative path ../options for local development.
Core Logic Refactoring
loggers/fields.go
Refactored LogFields struct to wrap *options.Options instead of managing its own sync.RWMutex and map. Context operations now delegate to options package equivalents; Range simplified to use inner.RangeSlice.
Documentation Updates
loggers/README.md
Updated LogFields method signatures and descriptions to document new delegation-based behavior and added Delete, Load, Range, Store methods; clarified AddToLogContext nil context handling.
Documentation Link References
loggers/slog/README.md, wrap/README.md
Updated source code line number anchors in generated API documentation links (no functional changes).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

🐰 Delegation hops so free,
Options stored, no locks to be,
Maps replaced with lighter care,
Context flows through options' air! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title accurately describes the main refactoring: LogFields is being refactored to use a shared RequestContext via the options package, which aligns with the primary change across loggers/fields.go and related files.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/request-context

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors loggers.LogFields to share storage with the options request context, reducing per-request context allocations and centralizing context-field handling.

Changes:

  • Refactor LogFields to wrap *options.Options and delegate field operations (incl. Range() via RangeSlice()).
  • Update context helpers (AddToLogContext, FromContext) to delegate to options equivalents.
  • Regenerate gomarkdoc READMEs and adjust module metadata (go.mod/go.sum).

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
loggers/fields.go Re-implements LogFields on top of *options.Options and updates context helpers to use options APIs.
loggers/README.md Regenerated gomarkdoc output reflecting new LogFields API docs and updated line links.
loggers/slog/README.md Regenerated gomarkdoc output with updated source line links.
wrap/README.md Regenerated gomarkdoc output with updated source line links.
go.mod Adds a local replace for github.com/go-coldbrew/options (noted as temporary).
go.sum Removes checksums for github.com/go-coldbrew/options v0.2.6 (consistent with local replace usage).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread loggers/fields.go
Comment on lines +16 to 20
// Add adds or modifies a log field.
func (o *LogFields) Add(key string, value any) {
if len(key) > 0 {
o.mu.Lock()
if o.m == nil {
o.m = make(map[string]any, 2)
}
o.m[key] = value
o.mu.Unlock()
o.inner.Add(key, value)
}
Copy link

Copilot AI Apr 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LogFields is an exported type, but its methods now dereference o.inner without guarding against nil. This makes the zero value of LogFields (e.g., var lf LogFields or &LogFields{}) panic on Add, which is a behavioral/API change from the previous implementation where the zero value was usable. Consider making the methods nil-safe (no-op/empty behavior when inner is nil) or providing an exported constructor and documenting that LogFields must be obtained via FromContext/AddToLogContext only.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — all methods now guard against nil inner. The zero value of LogFields is safe (no-op). Added doc noting LogFields should be obtained via FromContext/AddToLogContext.

Comment thread go.mod
Comment on lines +5 to +6
replace github.com/go-coldbrew/options => ../options

Copy link

Copilot AI Apr 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The committed replace github.com/go-coldbrew/options => ../options will break CI/builds for anyone who doesn't have a sibling ../options directory, and it also changes the module graph for downstream consumers. Please avoid committing local-path replaces; use a go.work file for local development, or bump the github.com/go-coldbrew/options version to the tagged release once options#17 is available and remove the replace before merging.

Suggested change
replace github.com/go-coldbrew/options => ../options

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acknowledged — this replace directive is intentional for local development while options#17 is in review. It will be removed and replaced with a proper version bump once options is tagged. This PR should not be merged until then.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 `@go.mod`:
- Around line 5-8: Remove the local sibling-module replacement from go.mod (the
line `replace github.com/go-coldbrew/options => ../options`) and keep only the
proper `require github.com/go-coldbrew/options v0.2.6` entry; do not merge this
replace directive into the branch — instead leave the PR as draft until
`github.com/go-coldbrew/options` is tagged upstream and then update the required
version in go.mod to the released tag so that loggers/fields.go uses the
released API.
🪄 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: dac5979a-fca8-440e-94e8-bb2e87523390

📥 Commits

Reviewing files that changed from the base of the PR and between 7c7df83 and f92a6d0.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (5)
  • go.mod
  • loggers/README.md
  • loggers/fields.go
  • loggers/slog/README.md
  • wrap/README.md

Comment thread go.mod
The zero value of LogFields (inner == nil) is now safe — methods are
no-ops instead of panicking. Documents that LogFields should be obtained
via FromContext/AddToLogContext.
@ankurs ankurs merged commit ba2ed27 into main Apr 4, 2026
4 of 8 checks passed
@ankurs ankurs deleted the feat/request-context branch April 4, 2026 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants