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

Print DB log #480

Merged
merged 2 commits into from
Feb 12, 2025
Merged

Print DB log #480

merged 2 commits into from
Feb 12, 2025

Conversation

mkysel
Copy link
Collaborator

@mkysel mkysel commented Feb 11, 2025

Make it easier to find the DB name in deployed environments.

Summary by CodeRabbit

  • New Features

    • Enhanced logging during database initialization now captures critical events and errors to improve system observability and reliability.
  • Tests

    • Logging consistency has been integrated into testing workflows to help verify stability and streamline diagnostics.

@mkysel mkysel requested a review from a team as a code owner February 11, 2025 21:10
Copy link

coderabbitai bot commented Feb 11, 2025

Walkthrough

This pull request introduces a new logger parameter to the db.NewNamespacedDB function. The change is applied in the main application, the database package, and related test functions. In each location, the *zap.Logger parameter is added to the function signature and corresponding calls. This enables logging during database initialization and connection without altering the underlying connection logic.

Changes

File(s) Change Summary
cmd/replication/main.go Added the logger argument to the call of db.NewNamespacedDB in the main function.
pkg/db/pgx.go Updated the NewNamespacedDB function signature to include a *zap.Logger parameter and inserted logging for a successful DB connection.
pkg/db/pgx_test.go Modified test function signatures to include a logger parameter and initialized a zap logger (zap.NewDevelopment()) for consistent logging.

Possibly related PRs

Suggested reviewers

  • richardhuaaa
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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: 0

🧹 Nitpick comments (2)
pkg/db/pgx.go (1)

143-144: Enhance logging with more context and precision.

The log message could be more precise and include additional context:

  1. Clarify which step was successful (namespace creation vs. DB connection)
  2. Include more context like DSN (with sensitive info redacted)
-    logger.Info("Successfully connected to DB", zap.String("namespace", namespace))
+    logger.Info("Successfully created database namespace",
+        zap.String("namespace", namespace),
+        zap.String("dsn", redactDSN(config.ConnConfig.ConnString())),
+    )

Add this helper function at the package level:

// redactDSN removes sensitive information from the DSN
func redactDSN(dsn string) string {
    // Use a regex to replace password and sensitive query params
    return regexp.MustCompile(`password=([^&\s]+)`).ReplaceAllString(dsn, "password=xxxxx")
}

Also consider adding another log message after successful DB connection:

    db, err := newPGXDB(ctx, config, waitForDB)
    if err != nil {
        return nil, err
    }
+   logger.Info("Successfully connected to database", zap.String("namespace", namespace))
pkg/db/pgx_test.go (1)

19-20: Reduce logger initialization duplication and ensure proper cleanup.

The logger initialization is duplicated across test functions. Consider:

  1. Creating a helper function to initialize the logger
  2. Using a test-specific logger configuration
  3. Ensuring proper cleanup

Add this helper function at the package level:

func newTestLogger(t *testing.T) *zap.Logger {
    // Use nop logger for tests to reduce noise
    config := zap.NewDevelopmentConfig()
    config.Level = zap.NewAtomicLevelAt(zap.WarnLevel)
    logger, err := config.Build()
    require.NoError(t, err)
    t.Cleanup(func() {
        _ = logger.Sync()
    })
    return logger
}

Then replace all logger initializations with:

-    logger, err := zap.NewDevelopment()
-    require.NoError(t, err)
+    logger := newTestLogger(t)

Also applies to: 47-48, 77-78, 91-92, 143-144

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c5f81ec and f3d8a3b.

📒 Files selected for processing (3)
  • cmd/replication/main.go (1 hunks)
  • pkg/db/pgx.go (3 hunks)
  • pkg/db/pgx_test.go (8 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Test (Node)
🔇 Additional comments (3)
cmd/replication/main.go (1)

75-77: LGTM!

The logger is properly initialized and passed to the NewNamespacedDB function, enhancing the visibility of database operations.

pkg/db/pgx.go (1)

8-8: LGTM!

The zap logger import is correctly added.

pkg/db/pgx_test.go (1)

6-6: LGTM!

The zap logger import is correctly added.

@mkysel mkysel merged commit 76fe82d into main Feb 12, 2025
7 checks passed
@mkysel mkysel deleted the mkysel/print-db-log branch February 12, 2025 13:22
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