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

fix log level priorty #1031

Merged

Conversation

samtholiya
Copy link
Collaborator

@samtholiya samtholiya commented Feb 5, 2025

Known Issues

  • We are creating our own flag parser which we should avoid. Maybe we will require it and there is no going arround. But not enough time was given to think about it this time.
  • The flag > env > config priority is handled for logging level and file but maybe... viper could do this for us we would have to check so that we don't have to manually check for these individually

what

  • We have to fix log level priority flag > env > config

why

  • This is what customer expects based on cli standard

references

Summary by CodeRabbit

  • New Features

    • Enhanced logging configuration, enabling environment variables and command-line flags to override configuration file settings.
    • Added dedicated configuration sections for logging and stack management.
    • Improved log parsing to support flexible flag formats and present consistent version and platform details.
  • Tests

    • Expanded test coverage to verify the correct prioritization and display of logging settings.

@mergify mergify bot added the triage Needs triage label Feb 5, 2025
@samtholiya samtholiya force-pushed the feature/dev-3020-atmos-log-flags-should-take-priority-over-env branch from a47bb51 to 568597b Compare February 6, 2025 00:04
cmd/root.go Outdated Show resolved Hide resolved
@samtholiya samtholiya marked this pull request as ready for review February 6, 2025 00:25
@samtholiya samtholiya requested a review from a team as a code owner February 6, 2025 00:25
Copy link
Contributor

coderabbitai bot commented Feb 6, 2025

📝 Walkthrough

Walkthrough

This pull request refines the logging configuration in Atmos by adjusting how command-line flags are handled. The changes conditionally update the configuration only when flags are explicitly modified, and a new parseFlags function is introduced for manual flag parsing. Updates also include a revised configuration structure in the YAML fixture with added logging and stack sections. Several snapshot files and a test case file have been updated to verify that flags take precedence over environment variables and configuration settings.

Changes

File(s) Change Summary
cmd/root.go Modified logging flag processing in PersistentPreRun and Execute; added new function parseFlags for manual flag parsing; removed incorrect default flag logging.
tests/fixtures/scenarios/priority-log-check/atmos.yaml Added logs section (with level and file) and stacks section (with base_path and included_paths).
tests/snapshots/TestCLICommands_* Updated and removed debug log statements across various snapshot files to reflect prioritized logging behavior and revised version/OS output.
tests/test-cases/log-level-validation.yaml Introduced new test cases to ensure log level and log file prioritization (flags > ENV > config).

Sequence Diagram(s)

sequenceDiagram
    participant CLI
    participant RootCmd
    participant parseFlags
    participant Env
    participant Config
    CLI->>RootCmd: Start command execution
    RootCmd->>parseFlags: Parse logging-related flags
    parseFlags-->>RootCmd: Return parsed flag values
    RootCmd->>Env: Check environment variables (ATMOS_LOGS_LEVEL, ATMOS_LOGS_FILE)
    RootCmd->>Config: Load configuration file settings
    RootCmd-->>CLI: Initialize logging (using flags > ENV > config)
Loading

Assessment against linked issues

Objective Addressed Explanation
DEV-3020: Atmos Log Flags Should Take Priority Over ENV (Flags > ENV > config; false log msg)

Possibly related PRs

Suggested labels

minor

Suggested reviewers

  • aknysh
  • osterman
✨ 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: 1

🧹 Nitpick comments (5)
tests/test-cases/log-level-validation.yaml (5)

20-39: Robust Check for Invalid Log Level in Environment Variable

This test correctly ensures that an invalid log level provided through the environment variable results in an error. For consistency with other tests (see later cases where strings are quoted), consider quoting the value on line 33 (e.g., "XTrace") to avoid any unexpected YAML parsing behavior.


92-109: Ensure Env Variable Overrides Config for Log Level

This test demonstrates that the log level provided via the environment variable (set to "Debug") takes priority over the configuration setting, as shown by the expected debug log output. Note: the test name uses "priortized"—a quick typo fix to "prioritized" would improve clarity.


110-128: Flag Supersedes Env and Config for Log Level

This test effectively checks that the command-line flag for log level overrides both the environment variable and configuration settings. As with the previous test, consider correcting "priortized" to "prioritized" in the test name.


129-147: Env Variable Correctly Prioritizes Log File Location

The test verifies that when the log file is specified through the environment variable (ATMOS_LOGS_FILE set to /dev/stdout), it directs the debug logs as expected. A minor note: please update "priortized" to "prioritized" in the test name for consistency.


148-168: Flag Overrides Env for Log File Destination

This test case confirms that the command-line flag for the log file takes precedence over the environment variable (which is set to /dev/stderr), ensuring the logs are directed to /dev/stdout as intended. A small update to fix "priortized" to "prioritized" in the test name is recommended for clarity.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 7d202f6 and f6ad650.

📒 Files selected for processing (12)
  • cmd/root.go (3 hunks)
  • tests/fixtures/scenarios/priority-log-check/atmos.yaml (1 hunks)
  • tests/snapshots/TestCLICommands_Valid_Log_Level_in_Config_File.stderr.golden (0 hunks)
  • tests/snapshots/TestCLICommands_Valid_Log_Level_in_Config_File.stdout.golden (1 hunks)
  • tests/snapshots/TestCLICommands_Valid_Log_Level_in_Environment_Variable.stderr.golden (0 hunks)
  • tests/snapshots/TestCLICommands_Valid_log_file_in_env_should_be_priortized_over_config.stdout.golden (1 hunks)
  • tests/snapshots/TestCLICommands_Valid_log_file_in_flag_should_be_priortized_over_env_and_config.stdout.golden (1 hunks)
  • tests/snapshots/TestCLICommands_Valid_log_level_in_env_should_be_priortized_over_config.stderr.golden (1 hunks)
  • tests/snapshots/TestCLICommands_Valid_log_level_in_env_should_be_priortized_over_config.stdout.golden (1 hunks)
  • tests/snapshots/TestCLICommands_Valid_log_level_in_flag_should_be_priortized_over_env_and_config.stderr.golden (1 hunks)
  • tests/snapshots/TestCLICommands_Valid_log_level_in_flag_should_be_priortized_over_env_and_config.stdout.golden (1 hunks)
  • tests/test-cases/log-level-validation.yaml (1 hunks)
💤 Files with no reviewable changes (2)
  • tests/snapshots/TestCLICommands_Valid_Log_Level_in_Environment_Variable.stderr.golden
  • tests/snapshots/TestCLICommands_Valid_Log_Level_in_Config_File.stderr.golden
✅ Files skipped from review due to trivial changes (5)
  • tests/snapshots/TestCLICommands_Valid_log_level_in_flag_should_be_priortized_over_env_and_config.stdout.golden
  • tests/snapshots/TestCLICommands_Valid_log_level_in_env_should_be_priortized_over_config.stdout.golden
  • tests/snapshots/TestCLICommands_Valid_log_level_in_env_should_be_priortized_over_config.stderr.golden
  • tests/snapshots/TestCLICommands_Valid_Log_Level_in_Config_File.stdout.golden
  • tests/snapshots/TestCLICommands_Valid_log_file_in_flag_should_be_priortized_over_env_and_config.stdout.golden
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Acceptance Tests (windows-latest, windows)
  • GitHub Check: Summary
🔇 Additional comments (9)
cmd/root.go (2)

48-57: Verify if conditional flag handling is necessary.

The conditional setting of log level and file only when flags are changed is a good practice. However, let's verify if this change is required since there's a TODO comment expressing uncertainty.

✅ Verification successful

🏁 Script executed:

#!/bin/bash
# Check if there are any instances where the flag values are used without checking if they were changed
rg -A 5 "cmd\.Flags\(\)\.GetString\(\"logs-(level|file)\"\)" --glob '!{cmd/root.go}'

Length of output: 520


Conditional flag handling appears both intentional and necessary.

Our investigation shows that outside of cmd/root.go, flags like "logs-level" are used directly (as seen in cmd/validate_editorconfig.go), whereas in cmd/root.go the conditional assignment ensures that ConfigAndStacksInfo only captures user-specified values. This prevents inadvertently overriding defaults when users don’t modify these flags. Therefore, the conditional checks are appropriate and align with the rest of the codebase.


127-161: Consider using Viper for flag management.

While the manual flag parsing implementation is thorough, using Viper would be a more robust solution as mentioned in the PR objectives. Viper provides built-in support for managing configuration priorities between flags, environment variables, and config files.

✅ Verification successful

🌐 Web query:

What are the benefits of using Viper over manual flag parsing in Go?

Length of output: 2902


Review Comment – Consider using Viper for Flag Management

The manual flag parsing implementation in cmd/root.go is solid and handles common flag patterns well. However, as the web query confirms, using Viper offers several benefits:

  • Unified Configuration Management: Viper integrates multiple configuration sources (flags, environment variables, config files, etc.) with a clear, hierarchical precedence.
  • Reduced Boilerplate and Automatic Binding: Viper seamlessly binds Cobra flags and automatically manages type conversion, reducing the manual overhead.
  • Advanced Features: Includes support for live configuration reloading, structured data parsing, and unified access via viper.Get*() methods.

Given these advantages and the PR objectives, it would be worthwhile to consider refactoring the manual flag parsing in favor of Viper.

tests/snapshots/TestCLICommands_Valid_log_level_in_flag_should_be_priortized_over_env_and_config.stderr.golden (1)

1-4: LGTM! Test snapshot validates priority order.

The debug logs correctly demonstrate that command-line arguments take precedence over environment variables, validating the priority implementation.

tests/snapshots/TestCLICommands_Valid_log_file_in_env_should_be_priortized_over_config.stdout.golden (1)

1-7: LGTM! Test snapshot validates environment variable priority.

The debug logs correctly demonstrate that environment variables take precedence over configuration file settings.

tests/fixtures/scenarios/priority-log-check/atmos.yaml (1)

1-9: LGTM! Configuration provides sensible defaults.

The configuration file is well-structured with appropriate default values for logging level and output destination.

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

2-19: Clear Validation for Invalid Log Level in Config File

This test case cleanly verifies that an invalid log level specified in the configuration file leads to a failure (exit code 1) with an appropriate error message.


40-54: Solid Test for Valid Log Level in Config File

The test confirms that when a valid log level is set in the configuration file, the command executes successfully and produces output matching the expected regex pattern. Looks good!


55-73: Effective Validation of Valid Log Level via Environment Variable

This test efficiently verifies that setting a valid log level using the environment variable is correctly picked up, as indicated by the debug log message in stderr.


74-91: Correct Prioritization for Valid Log Level via Command Line Flag

The test case neatly confirms that specifying the log level via the command line flag works as expected, taking precedence over other configurations with an empty stderr.

cmd/root.go Show resolved Hide resolved
@mergify mergify bot removed the triage Needs triage label Feb 6, 2025
@osterman osterman added the patch A minor, backward compatible change label Feb 6, 2025
@osterman osterman merged commit 110cc7a into main Feb 6, 2025
46 checks passed
@osterman osterman deleted the feature/dev-3020-atmos-log-flags-should-take-priority-over-env branch February 6, 2025 01:59
Copy link

github-actions bot commented Feb 6, 2025

These changes were released in v1.160.2.

Cerebrovinny added a commit that referenced this pull request Feb 9, 2025
* fix log level priorty

* fix unwanted flag being set

* add tests for log level priority

* added todo comment with more clearity

* Add tests for logs-file

* update comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
patch A minor, backward compatible change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants