Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace logrus with slog #56

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft

Replace logrus with slog #56

wants to merge 2 commits into from

Conversation

majst01
Copy link
Collaborator

@majst01 majst01 commented Sep 6, 2024

Logrus is not very well maintained and proven to be slow and allocates. Slog is part of go stdlib since go-1.21 and is known to be fast and does not allocate.

See: https://betterstack.com/community/guides/logging/best-golang-logging-libraries/

Summary by CodeRabbit

  • New Features

    • Transitioned logging from logrus to slog, enhancing structured logging capabilities across the application.
    • Simplified logging configuration by removing customizable log formats.
  • Bug Fixes

    • Improved clarity and structure of log messages for better readability and maintainability.
  • Documentation

    • Updated comments and formatting for improved clarity throughout the codebase.
  • Chores

    • Removed unused logging functions related to logrus in gRPC utilities.

Copy link

coderabbitai bot commented Sep 6, 2024

Walkthrough

Walkthrough

The changes involve a comprehensive transition from the logrus logging library to the slog logging library across multiple files in the codebase. This transition affects function signatures, logging methods, and configuration settings, resulting in a unified logging approach. Additionally, the LogFormat configuration has been removed from various components, indicating a shift towards a default logging format. The modifications streamline the logging implementation throughout the application.

Changes

Files Change Summary
go.mod, main.go, pkg/driver/controller_test.go, test/csi-sanity/csi-sanity_test.go Removed github.com/sirupsen/logrus dependency and LogFormat configuration from various components, simplifying logging setup.
pkg/driver/backend/dsc/discovery_client.go, pkg/driver/backend/registry.go, pkg/driver/backend/wrapper.go, pkg/driver/controller.go, pkg/driver/driver.go, pkg/driver/luks.go, pkg/driver/node.go, pkg/driver/status.go, pkg/lb/lbgrpc/client.go, pkg/lb/lbgrpc/client_test.go Transitioned logging from logrus to slog, updating function signatures and logging methods to reflect the new library's structure and capabilities.
pkg/grpcutil/grpcutil.go Removed functions that mapped gRPC status codes to logrus log levels, indicating a shift in logging strategy for gRPC responses.
pkg/driver/csi_md.go Minor adjustments to comments for clarity and a slight change in logging statement format, with no functional changes.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@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: 5

Outside diff range, codebase verification and nitpick comments (1)
pkg/driver/controller_test.go (1)

Update documentation in main.go regarding LogFormat.

The LogFormat field has been removed from the getDriver function, but it is still mentioned in the configuration documentation within main.go. Please update this documentation to reflect the current state of the codebase and avoid potential confusion. Ensure that no other parts of the codebase rely on LogFormat for functionality.

  • main.go: Update or remove references to LogFormat in the configuration documentation.
Analysis chain

Line range hint 178-178: Verify the impact of removing LogFormat.

The removal of the LogFormat field from the getDriver function simplifies the logging configuration. Please ensure that this change aligns with the new logging library's capabilities and does not adversely affect any logging behavior or dependent functionalities.

Run the following script to verify the usage of the LogFormat field in other parts of the codebase:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of `LogFormat` field in other parts of the codebase.

# Test: Search for the `LogFormat` field usage. Expect: No occurrences outside the removed instance.
rg --type go 'LogFormat'

Length of output: 102


Script:

#!/bin/bash
# Description: Investigate the usage of `LogFormat` in `main.go`.

# Search for the context around `LogFormat` in `main.go` to understand its usage.
rg --type go -A 5 -B 5 'LogFormat' main.go

Length of output: 864

@@ -1132,7 +1098,7 @@ func (c *Client) doUpdateVolume(
// expired for the CO to [presumably] retry.
return nil, nil
case codes.InvalidArgument:
log.Errorf("volume update refused by LB on bad arg: %s", st.Message())
log.Error("volume update refused by LB on bad arg", "error", st.Message())
Copy link

Choose a reason for hiding this comment

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

Issue: Handle error logging for volume update refusal in UpdateVolume.

The logging of errors when a volume update is refused due to bad arguments is crucial for diagnosing issues. However, the log level should be adjusted to Warn instead of Error since this is not a system error but a user or request error.

Consider changing the log level to reflect the nature of the error:

- log.Error("volume update refused by LB on bad arg", "error", st.Message())
+ log.Warn("volume update refused by LB on bad arg", "error", st.Message())
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.

Suggested change
log.Error("volume update refused by LB on bad arg", "error", st.Message())
log.Warn("volume update refused by LB on bad arg", "error", st.Message())

@@ -1373,16 +1335,15 @@ func (c *Client) DeleteSnapshot(
code := st.Code()
switch code { //nolint:exhaustive
case codes.InvalidArgument:
c.log.Errorf("delete snapshot refused by LB on bad arg: %s", st.Message())
c.log.Error("delete snapshot refused by LB on bad arg", "error", st.Message())
Copy link

Choose a reason for hiding this comment

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

Issue: Handle error logging for snapshot deletion refusal in DeleteSnapshot.

The logging of errors when snapshot deletion is refused due to bad arguments is crucial for diagnosing issues. However, the log level should be adjusted to Warn instead of Error since this is not a system error but a user or request error.

Consider changing the log level to reflect the nature of the error:

- c.log.Error("delete snapshot refused by LB on bad arg", "error", st.Message())
+ c.log.Warn("delete snapshot refused by LB on bad arg", "error", st.Message())
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.

Suggested change
c.log.Error("delete snapshot refused by LB on bad arg", "error", st.Message())
c.log.Warn("delete snapshot refused by LB on bad arg", "error", st.Message())

@@ -1259,16 +1224,15 @@ func (c *Client) CreateSnapshot(
code := st.Code()
switch code { //nolint:exhaustive
case codes.InvalidArgument:
c.log.Errorf("create snapshot refused by LB on bad arg: %s", st.Message())
c.log.Error("create snapshot refused by LB on bad arg", "message", st.Message())
Copy link

Choose a reason for hiding this comment

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

Issue: Handle error logging for snapshot creation refusal in CreateSnapshot.

The logging of errors when snapshot creation is refused due to bad arguments is crucial for diagnosing issues. However, the log level should be adjusted to Warn instead of Error since this is not a system error but a user or request error.

Consider changing the log level to reflect the nature of the error:

- c.log.Error("create snapshot refused by LB on bad arg", "message", st.Message())
+ c.log.Warn("create snapshot refused by LB on bad arg", "message", st.Message())
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.

Suggested change
c.log.Error("create snapshot refused by LB on bad arg", "message", st.Message())
c.log.Warn("create snapshot refused by LB on bad arg", "message", st.Message())

Comment on lines 274 to +276
opts = append(opts, grpc.WithInsecure())
} else if mgmtScheme == "grpcs" {
logger.Infof("connecting securely")
log.Info("connecting securely")
Copy link

Choose a reason for hiding this comment

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

Issue: Use of deprecated grpc.WithInsecure.

The use of grpc.WithInsecure() is deprecated. It's recommended to use WithTransportCredentials and insecure.NewCredentials() instead. This change is necessary to avoid future compatibility issues and adhere to best practices.

Consider replacing the deprecated method with the recommended approach:

- opts = append(opts, grpc.WithInsecure())
+ opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
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.

Suggested change
opts = append(opts, grpc.WithInsecure())
} else if mgmtScheme == "grpcs" {
logger.Infof("connecting securely")
log.Info("connecting securely")
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
} else if mgmtScheme == "grpcs" {
log.Info("connecting securely")
Tools
golangci-lint

274-274: SA1019: grpc.WithInsecure is deprecated: use WithTransportCredentials and insecure.NewCredentials() instead. Will be supported throughout 1.x.

(staticcheck)


interceptors := []grpc.UnaryServerInterceptor{
grpc_ctxtags.UnaryServerInterceptor(ctxTagOpts...),
grpc_logrus.UnaryServerInterceptor(d.log, logrusOpts...),
// TODO add slog interceptor
Copy link

Choose a reason for hiding this comment

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

TODO noted for adding slog interceptor.

The TODO comment in the Run function suggests that an slog interceptor for gRPC logging is planned but not yet implemented. It's important to track this task in the project's issue tracker to ensure it's completed in future updates.

Would you like me to open a GitHub issue to track the implementation of the slog interceptor?

Copy link

sonarqubecloud bot commented Sep 6, 2024

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.

1 participant