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

Validate Atmos Log Levels #930

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Validate Atmos Log Levels #930

wants to merge 8 commits into from

Conversation

Cerebrovinny
Copy link
Collaborator

@Cerebrovinny Cerebrovinny commented Jan 11, 2025

what

  • Error if invalid ATMOS_LOGS_LEVEL set
  • Log levels now work hierarchically (e.g., Debug level will show Debug and Trace messages)

why

We should not accept invalid log level to be set

Evidence:

BEFORE:
Screenshot 2025-01-11 at 15 08 09

AFTER:

Screenshot 2025-01-11 at 18 51 25

references

Summary by CodeRabbit

  • New Features

    • Enhanced log level validation across configuration sources (config file, environment variables, command line)
    • Improved error handling for invalid log levels with clear guidance
  • Bug Fixes

    • Prevented assignment of invalid log levels in configuration
    • Streamlined log message formatting
  • Tests

    • Added comprehensive test cases for log level validation scenarios
    • Verified behavior with valid and invalid log level configurations

@mergify mergify bot added the triage Needs triage label Jan 11, 2025
Copy link

mergify bot commented Jan 11, 2025

Important

Cloud Posse Engineering Team Review Required

This pull request modifies files that require Cloud Posse's review. Please be patient, and a core maintainer will review your changes.

To expedite this process, reach out to us on Slack in the #pr-reviews channel.

@mergify mergify bot added the needs-cloudposse Needs Cloud Posse assistance label Jan 11, 2025
@osterman
Copy link
Member

osterman commented Jan 12, 2025

@Cerebrovinny also ensure this behavior is working with the --logs-level global flag, and a misconfigured logs.level in Atmos config.

@osterman
Copy link
Member

Add test cases for each. It's sufficient to use tests/test-cases

@Cerebrovinny Cerebrovinny marked this pull request as ready for review January 13, 2025 23:08
@Cerebrovinny Cerebrovinny requested a review from a team as a code owner January 13, 2025 23:08
Copy link
Contributor

coderabbitai bot commented Jan 13, 2025

📝 Walkthrough

Walkthrough

The pull request introduces comprehensive log level validation across the Atmos configuration system. Changes span multiple files in the pkg/config, pkg/logger, and pkg/utils directories, focusing on ensuring that only valid log levels can be set through configuration files, environment variables, and command-line arguments. The modifications enhance error handling by implementing robust log level parsing and validation mechanisms.

Changes

File Change Summary
pkg/config/utils.go Added logger import, implemented log level validation in processEnvVars and processCommandLineArgs functions
pkg/logger/logger.go Introduced logLevelOrder map, added isLevelEnabled method, updated logging method logic
pkg/utils/log_utils.go Removed newline character from error message formatting
tests/fixtures/... Added test configuration files for valid and invalid log levels
tests/test-cases/log-level-validation.yaml Created comprehensive test cases for log level validation scenarios

Assessment against linked issues

Objective Addressed Explanation
Validate log levels for ATMOS_LOGS_LEVEL [DEV-2693]
Error on invalid log levels

Possibly related PRs

Suggested labels

major

Suggested reviewers

  • osterman
  • kevcube
  • aknysh

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 or @auto-summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @auto-title anywhere in the PR title to generate the title automatically.

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
Contributor

@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 (4)
tests/fixtures/valid-log-level/atmos.yaml (1)

1-8: Fix YAML formatting issues.

The configuration is valid, but there are two formatting issues to address:

  1. Remove trailing spaces after "**/*" on line 8
  2. Add a newline at the end of the file

Apply this diff to fix the formatting:

 logs:
   level: Trace
   file: /dev/stdout

 stacks:
   base_path: stacks
   included_paths:
-    - "**/*" 
+    - "**/*"
+
🧰 Tools
🪛 yamllint (1.35.1)

[error] 8-8: no new line character at the end of file

(new-line-at-end-of-file)


[error] 8-8: trailing spaces

(trailing-spaces)

tests/fixtures/invalid-log-level/atmos.yaml (1)

8-8: Quick formatting fixes needed, warrior!

Let's maintain clean YAML formatting:

  • Add a newline at the end of file
  • Remove trailing spaces after "**/*"
-    - "**/*" 
+    - "**/*"
+
🧰 Tools
🪛 yamllint (1.35.1)

[error] 8-8: no new line character at the end of file

(new-line-at-end-of-file)


[error] 8-8: trailing spaces

(trailing-spaces)

tests/test-cases/log-level-validation.yaml (2)

36-80: Strong positive test coverage, but let's fortify it further! 🏰

The tests cover all input methods (config, env var, CLI) with valid log levels. However, based on the PR objectives, we should add:

  1. Case sensitivity tests (e.g., "DEBUG" vs "Debug")
  2. Empty log level validation
  3. Hierarchical logging tests (verify Debug shows Trace messages)

Would you like me to help generate additional test cases for these scenarios?

🧰 Tools
🪛 yamllint (1.35.1)

[error] 80-80: no new line character at the end of file

(new-line-at-end-of-file)


[error] 80-80: trailing spaces

(trailing-spaces)


80-80: Quick formatting fix needed, warrior!

Add a newline at the end of file and remove trailing spaces.

-      exit_code: 0 
+      exit_code: 0
+
🧰 Tools
🪛 yamllint (1.35.1)

[error] 80-80: no new line character at the end of file

(new-line-at-end-of-file)


[error] 80-80: trailing spaces

(trailing-spaces)

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4799f28 and 0007c6d.

📒 Files selected for processing (6)
  • pkg/config/utils.go (4 hunks)
  • pkg/logger/logger.go (4 hunks)
  • pkg/utils/log_utils.go (1 hunks)
  • tests/fixtures/invalid-log-level/atmos.yaml (1 hunks)
  • tests/fixtures/valid-log-level/atmos.yaml (1 hunks)
  • tests/test-cases/log-level-validation.yaml (1 hunks)
🧰 Additional context used
🪛 yamllint (1.35.1)
tests/fixtures/invalid-log-level/atmos.yaml

[error] 8-8: no new line character at the end of file

(new-line-at-end-of-file)


[error] 8-8: trailing spaces

(trailing-spaces)

tests/fixtures/valid-log-level/atmos.yaml

[error] 8-8: no new line character at the end of file

(new-line-at-end-of-file)


[error] 8-8: trailing spaces

(trailing-spaces)

tests/test-cases/log-level-validation.yaml

[error] 80-80: no new line character at the end of file

(new-line-at-end-of-file)


[error] 80-80: trailing spaces

(trailing-spaces)

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Acceptance Tests (windows-latest, windows)
  • GitHub Check: Summary
🔇 Additional comments (8)
pkg/logger/logger.go (4)

23-30: Well-structured log level hierarchy!

The ordering from most verbose (Trace) to least verbose (Off) is logically organized and follows standard logging practices.


57-64: Improved log level validation with helpful error messages!

The validation using a slice of valid levels is more maintainable, and the error message now includes valid options for better user experience.


Line range hint 112-120: Good catch on respecting LogLevelOff!

The additional check ensures errors are not logged when the log level is set to Off, maintaining consistent behavior with the logging configuration.


123-129: Excellent implementation of hierarchical logging!

The isLevelEnabled method elegantly implements the hierarchical log level functionality, ensuring consistent behavior across all logging methods. The implementation correctly uses the level ordering to determine if a message should be logged.

Also applies to: 132-152

pkg/utils/log_utils.go (1)

51-51: Verify log output formatting after newline removal.

The removal of the newline character might affect log readability. Please verify that the log output remains properly formatted across different logging scenarios.

✅ Verification successful

Log formatting maintains proper readability ✓

The use of Fprintln automatically adds newlines, maintaining consistent log formatting with other error logging implementations in the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check log output formatting with different error messages

# Test single-line error
echo "Testing single-line error formatting..."
rg -A 1 'logColor.Fprintln.*Error' .

# Test multi-line error
echo "Testing multi-line error formatting..."
rg -A 1 'Error logging the error:' .

Length of output: 525

pkg/config/utils.go (1)

362-366: Robust log level validation across all configuration sources!

The implementation ensures that log levels are validated consistently whether they come from environment variables, command-line arguments, or configuration files. The validation-before-assignment pattern prevents invalid log levels from being set.

Also applies to: 405-409, 488-491

tests/fixtures/invalid-log-level/atmos.yaml (1)

1-4: Strong test fixture for negative testing! 💪

The invalid log level "XTrace" is perfect for validating the error handling. This will help ensure our logging system remains robust.

tests/test-cases/log-level-validation.yaml (1)

1-35: Excellent negative test cases, commander! 🛡️

The invalid log level tests are well-structured and verify proper error messages. The error message regex \[Trace Debug Info Warning Off\] clearly communicates valid options to users.

@mergify mergify bot removed the triage Needs triage label Jan 13, 2025
LogLevelDebug: 1,
LogLevelInfo: 2,
LogLevelWarning: 3,
LogLevelOff: 4,
Copy link
Member

Choose a reason for hiding this comment

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

This is interesting. Do we expose this option as well? Let's expose it and update docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-cloudposse Needs Cloud Posse assistance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants