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

Add option for configuring initial fields on the logger #244

Merged
merged 2 commits into from
Oct 10, 2024

Conversation

jirwin
Copy link
Contributor

@jirwin jirwin commented Oct 10, 2024

calling something like:

sdkLogging.WithInitialFields(map[string]interface{}{
			"tenant_id":    p.TenantId,
			"connector_id": connector.Id,
			"app_id":       connector.AppId,
		}),

wil be sure that the logger we create for the connector has static values included on logs.

Summary by CodeRabbit

  • New Features

    • Introduced the ability to specify initial fields for logger configuration, enhancing logging customization.
  • Bug Fixes

    • No bug fixes were included in this release.

Copy link

coderabbitai bot commented Oct 10, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

The changes introduce a new function named WithInitialFields to the logging package in pkg/logging/logging.go. This function allows users to provide a map of string keys to interface values, which modifies the InitialFields property of the zap.Config struct. The existing logging functions remain unchanged, and the Init function continues to utilize the new option alongside the existing ones.

Changes

File Change Summary
pkg/logging/logging.go Added WithInitialFields(fields map[string]interface{}) Option method.

Poem

In the forest where we play,
A new log function joins the fray.
With fields to set, oh what a sight,
Our logs now shine, so clear and bright!
Hopping 'round with joy we cheer,
For logging's better, oh dear, oh dear! 🐇✨


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 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.

@jirwin jirwin requested review from ggreer and mgaeta October 10, 2024 22:13
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: 1

🧹 Outside diff range and nitpick comments (1)
pkg/logging/logging.go (1)

46-50: LGTM! Consider minor enhancements for robustness.

The WithInitialFields function is well-implemented and aligns perfectly with the existing Option pattern. It provides the desired functionality of allowing users to set initial fields for the logger.

To further improve robustness, consider the following suggestions:

  1. Add input validation to handle nil or empty maps:

    func WithInitialFields(fields map[string]interface{}) Option {
        return func(c *zap.Config) {
            if fields != nil && len(fields) > 0 {
                if c.InitialFields == nil {
                    c.InitialFields = make(map[string]interface{})
                }
                for k, v := range fields {
                    c.InitialFields[k] = v
                }
            }
        }
    }
  2. This approach merges the new fields with existing ones (if any) instead of overwriting them, which might be more flexible in some scenarios.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 2bdcdc6 and 44a5265.

📒 Files selected for processing (1)
  • pkg/logging/logging.go (1 hunks)
🧰 Additional context used
🔇 Additional comments (1)
pkg/logging/logging.go (1)

46-50: Summary: PR objectives met with well-implemented changes

This pull request successfully introduces the ability to configure initial fields for the logger within the Baton SDK. The implementation:

  1. Adds a new WithInitialFields option that integrates seamlessly with the existing logging configuration system.
  2. Maintains backward compatibility by not modifying existing functions.
  3. Enhances log context and traceability by allowing static values (like tenant_id, connector_id, and app_id) to be included in all log entries.

The changes align well with the PR objectives and follow good coding practices. Minor suggestions for improvement have been provided to enhance robustness.

To fully validate the implementation:

  1. Ensure that the verification script is run to check usage across the codebase.
  2. Consider adding unit tests for the new WithInitialFields function.
  3. Update documentation to reflect this new configuration option.

Overall, this is a valuable addition to the SDK that will improve logging capabilities for connectors.

Comment on lines +46 to +50
func WithInitialFields(fields map[string]interface{}) Option {
return func(c *zap.Config) {
c.InitialFields = fields
}
}
Copy link

Choose a reason for hiding this comment

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

💡 Codebase verification

WithInitialFields is not used in the codebase.

  • No instances of WithInitialFields found in any logging.Init function calls.
🔗 Analysis chain

Integration looks good. Verify usage in the codebase.

The WithInitialFields function integrates seamlessly with the existing codebase. It follows the established Option pattern and can be used alongside other options in the Init function without any changes to the existing code.

To ensure proper usage across the codebase:

This script will help verify that the new WithInitialFields option is being used correctly throughout the project.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of WithInitialFields in the codebase

# Test 1: Check for imports of the logging package
echo "Checking for imports of the logging package:"
rg --type go 'import\s+\([^)]*"github\.com/ConductorOne/baton-sdk/pkg/logging"[^)]*\)' || rg --type go 'import\s+"github\.com/ConductorOne/baton-sdk/pkg/logging"'

# Test 2: Look for usage of WithInitialFields
echo "Checking for usage of WithInitialFields:"
rg --type go 'logging\.WithInitialFields\('

# Test 3: Examine Init function calls with options
echo "Examining Init function calls with options:"
rg --type go 'logging\.Init\(.*\)' -A 5

Length of output: 1106

@@ -43,6 +43,12 @@ func WithOutputPaths(paths []string) Option {
}
}

func WithInitialFields(fields map[string]interface{}) Option {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd add a docstring explaining what this does:

// WithInitialFields - ensure that the logger we create for the connector has static values included on logs.
func WithInitialFields(fields map[string]interface{}) Option {
    ...
}

@jirwin jirwin enabled auto-merge (squash) October 10, 2024 22:25
@jirwin jirwin merged commit 79182ad into main Oct 10, 2024
4 checks passed
@jirwin jirwin deleted the jirwin/custom-log-fields branch October 10, 2024 22:26
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