Repository navigation
refactor: order of subject customer lookup - #3207
Conversation
📝 WalkthroughWalkthroughRefactors customer lookup in subject hook to first query by usage attribution, then by subject key while excluding deleted customers. Adjusts error handling to return generic conflict and not-found errors with context. Removes redundant second usage-attribution lookup and simplifies local variable usage. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
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. 🔧 golangci-lint (2.2.2)level=warning msg="[runner] Can't run linter goanalysis_metalinter: inspect: failed to load package router: could not load export data: no export data for "github.com/openmeterio/openmeter/openmeter/watermill/router"" ✨ Finishing Touches
🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
openmeter/customer/service/hooks/subject.go(2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
openmeter/customer/service/hooks/subject.go (2)
openmeter/customer/customer.go (1)
GetCustomerByUsageAttributionInput(143-146)pkg/models/errors.go (3)
IsGenericNotFoundError(57-65)NewGenericConflictError(68-70)NewGenericNotFoundError(38-40)
🔇 Additional comments (2)
openmeter/customer/service/hooks/subject.go (2)
192-203: Reordered lookup by usage attribution: LGTMQuerying by usage attribution first, short-circuiting on non-deleted hits, and only treating non-generic-not-found as errors is correct and simplifies the flow. No issues spotted.
228-229: Unified not-found error with context: LGTMReturning a GenericNotFound with explicit namespace and subject.key context is clear and consistent with the rest of the codebase.
| if cus != nil && cus.DeletedAt == nil { | ||
| if lo.Contains(cus.UsageAttribution.SubjectKeys, sub.Key) { | ||
| return cus, nil | ||
| } | ||
|
|
||
| return nil, ErrCustomerKeyConflict | ||
| return nil, models.NewGenericConflictError(ErrCustomerKeyConflict) | ||
| } |
There was a problem hiding this comment.
💡 Verification agent
🧩 Analysis chain
Conflict wrapping: verify errors.Is compatibility and add context
Returning a GenericConflictError is fine, but EnsureCustomer uses errors.Is(err, ErrCustomerKeyConflict). Please verify that GenericConflictError implements Unwrap() so errors.Is will match the sentinel. Also, consider preserving context in the wrapped error to aid diagnostics.
Suggested improvement to include context while preserving the sentinel:
- return nil, models.NewGenericConflictError(ErrCustomerKeyConflict)
+ return nil, models.NewGenericConflictError(fmt.Errorf(
+ "customer key conflict (customer found by key but subject not attributed) [namespace=%s subject.key=%s customer.id=%s]: %w",
+ sub.Namespace, sub.Key, cus.ID, ErrCustomerKeyConflict))Run this to confirm GenericConflictError supports unwrapping (and thus errors.Is):
🏁 Script executed:
#!/bin/bash
set -euo pipefail
# Show GenericConflictError definition and check for Unwrap method
rg -n "type GenericConflictError" -A 30
rg -n $'func \\(.*\\*GenericConflictError\\) Unwrap\\(\\) error'
# Bonus: also show GenericNotFoundError for consistency
rg -n "type GenericNotFoundError" -A 30
rg -n $'func \\(.*\\*GenericNotFoundError\\) Unwrap\\(\\) error'Length of output: 3210
Conflict wrapping is correct—add context for diagnostics
Verified that GenericConflictError in pkg/models/errors.go implements an Unwrap() method, so
errors.Is(err, ErrCustomerKeyConflict) will work as intended. To improve troubleshooting, wrap the sentinel with contextual details:
• File: openmeter/customer/service/hooks/subject.go
Lines: 220–226
Suggested diff:
- return nil, models.NewGenericConflictError(ErrCustomerKeyConflict)
+ return nil, models.NewGenericConflictError(fmt.Errorf(
+ "customer key conflict (namespace=%s subject.key=%s customer.id=%s): %w",
+ sub.Namespace, sub.Key, cus.ID, ErrCustomerKeyConflict))📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if cus != nil && cus.DeletedAt == nil { | |
| if lo.Contains(cus.UsageAttribution.SubjectKeys, sub.Key) { | |
| return cus, nil | |
| } | |
| return nil, ErrCustomerKeyConflict | |
| return nil, models.NewGenericConflictError(ErrCustomerKeyConflict) | |
| } | |
| if cus != nil && cus.DeletedAt == nil { | |
| if lo.Contains(cus.UsageAttribution.SubjectKeys, sub.Key) { | |
| return cus, nil | |
| } | |
| - return nil, models.NewGenericConflictError(ErrCustomerKeyConflict) | |
| + return nil, models.NewGenericConflictError(fmt.Errorf( | |
| + "customer key conflict (namespace=%s subject.key=%s customer.id=%s): %w", | |
| + sub.Namespace, sub.Key, cus.ID, ErrCustomerKeyConflict)) | |
| } |
🤖 Prompt for AI Agents
In openmeter/customer/service/hooks/subject.go around lines 220 to 226, the
conflict return should wrap the sentinel ErrCustomerKeyConflict with contextual
details for diagnostics; replace the current return nil,
models.NewGenericConflictError(ErrCustomerKeyConflict) with a wrapped error that
includes customer id and subject key, e.g. return nil, fmt.Errorf("customer %s
already has subject key %s: %w", cus.ID, sub.Key, ErrCustomerKeyConflict), and
add the fmt import if missing.
Overview
Reorder the lookup process for Customer by Subject.
Summary by CodeRabbit