Enhance logging, error handling, and resource management#381
Conversation
Lingering invitations led to failure to invite user, who was invited before, but removed after that
Reviewer's Guide by SourceryThis pull request enhances logging and error handling across multiple services, including accounts, console, infra, and IAM. The changes focus on improving error reporting, adding debug options, and refining the handling of memberships and invitations. Updated class diagram for logging and error handling in accounts serviceclassDiagram
class Main {
-isDev: bool
-debug: bool
+main()
}
class Logger {
+NewSlogLogger(SlogOptions): Logger
+Error(message: string, key: string, value: any)
}
class SlogOptions {
+ShowCaller: bool
+ShowDebugLogs: bool
+SetAsDefaultLogger: bool
}
Main --> Logger
Logger --> SlogOptions
Updated class diagram for IAM service error handlingclassDiagram
class GrpcService {
+UpdateMembership(ctx: context.Context, in: UpdateMembershipIn): UpdateMembershipOut
+RemoveMembership(ctx: context.Context, in: RemoveMembershipIn): RemoveMembershipOut
+findRoleBinding(ctx: context.Context, userId: ID, resourceRef: string): RoleBinding
}
class Error {
+ErrRoleBindingNotFound: error
}
GrpcService --> Error
Updated class diagram for environment handling in console serviceclassDiagram
class Domain {
+OnEnvironmentDeleteMessage(ctx: ConsoleContext, env: Env): error
+OnEnvironmentUpdateMessage(ctx: ConsoleContext, env: Env): error
}
class EnvironmentRepo {
+DeleteOne(ctx: ConsoleContext, filter: Filter): error
+PatchById(ctx: ConsoleContext, id: ID, patch: Patch): Env
}
class ResourceMappingRepo {
+DeleteMany(ctx: ConsoleContext, filter: Filter): error
}
Domain --> EnvironmentRepo
Domain --> ResourceMappingRepo
Updated class diagram for infra service BYOK cluster setupclassDiagram
class Domain {
+GetBYOKClusterSetupInstructions(ctx: InfraContext, name: string, onlyHelmValues: bool): any
}
class GlobalVPNConnection {
+findGlobalVPNConnection(ctx: InfraContext, accountName: string, clusterName: string, globalVPN: bool): GlobalVPNConnection
}
Domain --> GlobalVPNConnection
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey @nxtcoder17 - I've reviewed your changes - here's some feedback:
Overall Comments:
- In the accounts service, the invitation handling has changed from marking invitations as accepted to deleting them. Could you provide more context on this change and confirm that it doesn't break any existing functionality or reporting?
- The PR shows good use of custom error handling functions like errors.NewE and errors.Newf. Ensure that this pattern is consistently applied across all services for uniform error handling and improved debugging.
Here's what I looked at during the review
- 🟡 General issues: 2 issues found
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟡 Complexity: 1 issue found
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| return errors.NewE(err) | ||
| } | ||
|
|
||
| if err := d.resourceMappingRepo.DeleteMany(ctx, repos.Filter{ |
There was a problem hiding this comment.
suggestion: Consider handling partial failure scenarios
The addition of resource mapping deletion is good for data integrity. However, consider wrapping this operation along with the environment deletion in a transaction, or implement a rollback mechanism to handle scenarios where one operation succeeds and the other fails.
tx, err := d.db.BeginTx(ctx, nil)
if err != nil {
return errors.NewE(err)
}
defer tx.Rollback()
if err := d.resourceMappingRepo.DeleteMany(ctx, repos.Filter{
fc.ResourceMappingResourceHeirarchy: entities.ResourceHeirarchyEnvironment,
fc.EnvironmentName: env.Name,
}, tx); err != nil {
return errors.NewE(err)
}
|
|
||
| if err := d.addMembership(ctx, accountName, ctx.UserId, inv.UserRole); err != nil { | ||
| // INFO: invitation accepted, removing invite | ||
| if err := d.invitationRepo.DeleteById(ctx, inv.Id); err != nil { |
There was a problem hiding this comment.
suggestion: Consider potential inconsistency in invitation acceptance process
The change to delete the invitation after acceptance is good for database cleanliness. However, consider the order of operations and potential failure scenarios. If the system crashes after adding the membership but before deleting the invitation, it could lead to an inconsistent state. Consider using a transactional approach if possible, or implement a mechanism to handle such edge cases.
if err := d.db.Transaction(func(tx *gorm.DB) error {
if err := d.addMembership(ctx, accountName, ctx.UserId, inv.UserRole); err != nil {
return err
}
return d.invitationRepo.DeleteById(ctx, inv.Id)
}); err != nil {
return false, errors.NewE(err)
}
| debug = true | ||
| } | ||
|
|
||
| logger := logging.NewSlogLogger(logging.SlogOptions{ |
There was a problem hiding this comment.
issue (complexity): Consider consolidating logger initialization to reduce code duplication and complexity.
The changes introduce useful new functionality, but the logger initialization process has become overly complex. We suggest unifying the logger creation to reduce duplication while maintaining the new debugging options. Here's a proposed simplification:
func main() {
start := time.Now()
var isDev, debug bool
flag.BoolVar(&isDev, "dev", false, "--dev")
flag.BoolVar(&debug, "debug", false, "--debug")
flag.Parse()
if isDev {
debug = true
}
logger, err := logging.New(&logging.Options{
Name: "accounts-api",
Dev: isDev,
ShowDebugLog: debug,
SlogOptions: logging.SlogOptions{
ShowCaller: true,
ShowDebugLogs: debug,
SetAsDefaultLogger: true,
},
})
if err != nil {
panic(err)
}
app := fx.New(
fx.NopLogger,
fx.Supply(logger),
// ... rest of the providers
)
// ... rest of the main function
}This approach:
- Unifies logger creation, eliminating the need for two separate logger initializations.
- Maintains the new debug flag functionality.
- Keeps the performance measurement feature.
- Simplifies the fx.Provide for the logger by directly supplying it.
These changes reduce complexity while preserving the new features, making the code more maintainable and easier to understand.
Enhance logging, error handling, and resource management
Summary by Sourcery
Enhance logging and error handling across multiple services by introducing a new logger configuration, adding a debug flag, and improving error handling mechanisms. Refactor environment and invitation handling processes for better resource management and cleanup. Introduce a new error type for role binding not found to improve error clarity in the IAM service.
New Features:
Enhancements: