Skip to content

feat: add tracing to cli argument#1509

Merged
HerringtonDarkholme merged 3 commits intomainfrom
tracing
Oct 5, 2024
Merged

feat: add tracing to cli argument#1509
HerringtonDarkholme merged 3 commits intomainfrom
tracing

Conversation

@HerringtonDarkholme
Copy link
Copy Markdown
Member

@HerringtonDarkholme HerringtonDarkholme commented Oct 5, 2024

Summary by CodeRabbit

  • New Features

    • Introduced enhanced tracing capabilities for runtime statistics in both running and scanning processes.
    • Users can now specify tracing levels via command-line arguments for more detailed output.
  • Bug Fixes

    • Improved handling of runtime arguments for better execution management.
  • Documentation

    • Updated documentation for new tracing functionality to clarify usage and output behavior.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Oct 5, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

The pull request introduces modifications to enhance the handling of runtime statistics during execution. A new tracing field has been added to the OutputArgs struct in both run.rs and scan.rs, allowing for improved tracking of execution statistics. The Tracing enum in tracing.rs has been updated with a new variant and methods to facilitate this functionality. Additionally, methods in the RunWithInferredLang, RunWithSpecificLang, and ScanWithConfig structures have been modified to utilize the new tracing capabilities, ensuring that runtime statistics are accurately reported.

Changes

File Path Change Summary
crates/cli/src/run.rs Added tracing field in OutputArgs. Modified run_pattern_with_printer to utilize tracing for statistics management. Updated consume_items to print statistics after processing.
crates/cli/src/scan.rs Added tracing field in OutputArgs. Adjusted run_with_config to use tracing for statistics. Updated consume_items to print statistics.
crates/cli/src/utils/tracing.rs Modified Tracing enum to add Nothing variant. Added methods run_stats and scan_stats. Updated SummaryStats to include level field. Removed Debug derive from FileStats, SummaryStats, and RuleStats.

Possibly related PRs

  • feat: add tracing level summary #1500: The changes in this PR enhance tracing capabilities, which is directly related to the new tracing field added in the main PR's OutputArgs structure, indicating a connection in functionality regarding runtime statistics and tracing.

Poem

🐇 In the fields where rabbits play,
New tracing paths now light the way.
With stats to guide our joyful hops,
We track our steps, no need for stops!
So let us leap and dance with glee,
For every trace brings joy to me! ✨


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.

@codecov
Copy link
Copy Markdown

codecov bot commented Oct 5, 2024

Codecov Report

Attention: Patch coverage is 81.48148% with 5 lines in your changes missing coverage. Please review.

Project coverage is 72.59%. Comparing base (563357f) to head (e6c47e6).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
crates/cli/src/run.rs 57.14% 3 Missing ⚠️
crates/cli/src/scan.rs 50.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1509      +/-   ##
==========================================
- Coverage   72.60%   72.59%   -0.02%     
==========================================
  Files          87       87              
  Lines        5935     5947      +12     
==========================================
+ Hits         4309     4317       +8     
- Misses       1626     1630       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Copy Markdown
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

🧹 Outside diff range and nitpick comments (5)
crates/cli/src/utils/args.rs (2)

131-138: LGTM: New tracing field added with clear documentation

The new tracing field in the OutputArgs struct is well-implemented and documented. It provides useful functionality for debugging and understanding the program's behavior. The #[clap(...)] attribute is correctly configured, and the field placement maintains backward compatibility.

Consider adding an example of how to use the tracing option in the documentation comment. This could help users understand how to set different tracing levels. For instance:

/// Show tracing information for file/rule discovery and scanning.
///
/// This flag helps users inspect ast-grep's internal filtering of files and rules.
/// Tracing will output how many and why files and rules are scanned and skipped.
/// Tracing information outputs to stderr and does not affect the result of the search.
///
/// Example usage:
/// ```
/// ast-grep ... --tracing=verbose
/// ```
#[clap(long, default_value = "nothing", value_name = "LEVEL")]
pub tracing: Tracing,

Line range hint 1-238: Summary: Tracing functionality added successfully

The changes to this file are minimal and focused, adding a new tracing field to the OutputArgs struct along with the necessary import. The implementation is clean, well-documented, and properly integrated into the existing code structure. These changes enhance the CLI's functionality by providing users with the ability to trace file and rule discovery during execution, which can be valuable for debugging and understanding the program's behavior.

As the codebase grows, consider creating a separate module for tracing-related functionality if it becomes more complex. This would help maintain a clean separation of concerns and make the code more modular.

crates/cli/src/scan.rs (1)

331-331: LGTM! Consider adding documentation for the new tracing field.

The addition of the tracing field to OutputArgs aligns with the PR objective of adding tracing to CLI arguments. This change allows for optional tracing functionality with a default configuration.

Consider adding a doc comment to explain the purpose and usage of the tracing field, which will help users understand its functionality when using the CLI.

crates/cli/src/run.rs (1)

Line range hint 201-405: Overall: Excellent implementation of tracing feature

The changes consistently implement the tracing feature across different run modes (RunWithInferredLang and RunWithSpecificLang), enhancing the CLI tool's ability to track and report runtime statistics. The implementation is well-integrated into the existing codebase and aligns perfectly with the PR objectives.

As a minor suggestion for improvement:

Consider adding a brief comment explaining the purpose of the new tracing field in the OutputArgs struct. This would help future maintainers understand the role of this field in the overall statistics tracking feature.

crates/cli/src/utils/tracing.rs (1)

Line range hint 52-59: Issue with serializing FileStats containing AtomicUsize

Deriving Serialize and Deserialize for FileStats may not work as expected because AtomicUsize does not implement the Serialize or Deserialize traits by default. This could lead to compilation errors or runtime issues during serialization.

Consider implementing custom serialization and deserialization for FileStats or switching to thread-safe types that support serialization, such as using Mutex<usize>. Alternatively, you can serialize the values by fetching them from the atomic fields explicitly.

use serde::{Serializer, Deserializer};

impl Serialize for FileStats {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let files_scanned = self.files_scanned.load(Ordering::Acquire);
        let files_skipped = self.files_skipped.load(Ordering::Acquire);
        let stats = (files_scanned, files_skipped);
        stats.serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for FileStats {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let (files_scanned, files_skipped): (usize, usize) = Deserialize::deserialize(deserializer)?;
        Ok(FileStats {
            files_scanned: AtomicUsize::new(files_scanned),
            files_skipped: AtomicUsize::new(files_skipped),
        })
    }
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 563357f and d62f28f.

📒 Files selected for processing (5)
  • crates/cli/src/run.rs (3 hunks)
  • crates/cli/src/scan.rs (2 hunks)
  • crates/cli/src/utils/args.rs (2 hunks)
  • crates/cli/src/utils/mod.rs (1 hunks)
  • crates/cli/src/utils/tracing.rs (3 hunks)
🔇 Additional comments (11)
crates/cli/src/utils/mod.rs (1)

10-10: LGTM! Verify the Tracing implementation.

The addition of Tracing to the public exports is consistent with the PR objective of adding tracing to the CLI argument. This change makes the Tracing item accessible to other parts of the CLI, which is likely the intended behavior.

To ensure the Tracing implementation is complete and consistent, please run the following script:

This script will help verify that Tracing is properly implemented and used within the project.

✅ Verification successful

Verified the Tracing implementation as an enum with no recursive dependencies.

The Tracing enum is correctly implemented without any unintended recursive fields, ensuring no cyclic dependencies are present. This change aligns with the PR objectives and maintains the integrity of the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the Tracing implementation and its usage

# Test 1: Check if Tracing is implemented in the tracing module
echo "Checking Tracing implementation:"
ast-grep --lang rust --pattern 'struct Tracing { $$$1 }' crates/cli/src/utils/tracing.rs

# Test 2: Look for usages of Tracing in other files
echo "Checking Tracing usage:"
rg --type rust '\bTracing\b' crates/cli

Length of output: 624


Script:

#!/bin/bash
# Description: Verify the Tracing implementation as an enum and check for recursive fields

# Test 1: Check if Tracing is implemented as an enum in the tracing module
echo "Checking if Tracing is an enum:"
ast-grep --lang rust --pattern 'enum Tracing { $$$1 }' crates/cli/src/utils/tracing.rs

# Test 2: Look for recursive usage of Tracing within its own implementation
echo "Checking for recursive Tracing fields:"
ast-grep --lang rust --pattern 'impl Tracing { $$$ }' crates/cli/src/utils/tracing.rs | rg 'level:\s*Tracing'

# Test 3: Look for usages of Tracing in other files to ensure proper implementation
echo "Checking Tracing usage in the codebase:"
rg --type rust '\bTracing\b' crates/cli

Length of output: 851


Script:

#!/bin/bash
# Description: Verify the Tracing implementation as an enum and ensure no unintended recursive dependencies

# Test 1: Check if Tracing is implemented as an enum in the tracing module
echo "Checking if Tracing is an enum:"
ast-grep --lang rust --pattern 'enum Tracing { $$$1 }' crates/cli/src/utils/tracing.rs

# Test 2: Verify that the Tracing enum does not have unintended recursive fields
echo "Verifying Tracing enum fields for unintended recursion:"
ast-grep --lang rust --pattern 'pub level: Tracing,' crates/cli/src/utils/tracing.rs

Length of output: 714


Script:

#!/bin/bash
# Description: Refine verification of the Tracing enum for recursive fields

# Test 1: Confirm Tracing is an enum
echo "Re-confirming if Tracing is an enum:"
ast-grep --lang rust --pattern 'enum Tracing { $$$1 }' crates/cli/src/utils/tracing.rs

# Test 2: Refined pattern to check for recursive field within the Tracing enum
echo "Verifying Tracing enum fields for recursive dependencies with refined pattern:"
ast-grep --lang rust --pattern 'enum Tracing {
    $$$
    pub\s+level\s*:\s*Tracing,
    $$$
}' crates/cli/src/utils/tracing.rs

Length of output: 469

crates/cli/src/utils/args.rs (1)

4-4: LGTM: New import for Tracing

The added import for Tracing is correctly placed and follows the existing import style in the file. This import is necessary for the new tracing field in the OutputArgs struct.

crates/cli/src/scan.rs (3)

Line range hint 1-394: Summary: Tracing functionality successfully integrated into the scanning process.

The changes in this file successfully implement the PR objective of adding tracing to CLI arguments. The modifications include:

  1. Adding a tracing field to the OutputArgs struct.
  2. Integrating tracing into the ScanWithConfig::try_new method for statistics management.
  3. Updating the Worker implementation to print tracing statistics after processing items.

These changes enhance the scanning process by providing additional tracing capabilities without disrupting the existing functionality. The implementation appears consistent and well-integrated into the codebase.

To ensure the robustness of these changes, consider adding unit tests specifically for the new tracing functionality, including edge cases and different configuration scenarios.


Line range hint 165-165: LGTM! Verify the implementation of the print method for ScanStats.

The addition of eprintln!("{}", self.stats.print()); ensures that the tracing statistics are reported at the end of the scanning process. This change completes the integration of tracing functionality into the scanning workflow.

To ensure the correct implementation of the print method for ScanStats, please run the following verification script:

#!/bin/bash
# Description: Verify the implementation of the print method for ScanStats.

# Test: Search for the print method definition in ScanStats
rg --type rust -g '!target/' 'impl.*ScanStats.*fn\s+print.*\('

# Test: Check for any unit tests related to ScanStats print method
rg --type rust -g '!target/' '#\[test\].*fn.*scan_stats.*print'

122-122: LGTM! Verify the implementation of scan_stats method.

The change to use arg.output.tracing.scan_stats(rule_stats) for initializing the stats field integrates the new tracing functionality into the scanning process. This modification aligns with the PR objective and enhances the statistics management capabilities.

To ensure the correct implementation of the scan_stats method, please run the following verification script:

Also applies to: 127-127

crates/cli/src/run.rs (3)

201-205: LGTM: Enhanced statistics tracking for RunWithInferredLang

The addition of the stats field initialized with arg.output.tracing.run_stats() improves the capability to track runtime statistics for the RunWithInferredLang struct. This change aligns well with the overall goal of enhancing statistics management during execution.


290-296: LGTM: Consistent statistics tracking for RunWithSpecificLang

The addition of the stats field initialized with arg.output.tracing.run_stats() for the RunWithSpecificLang struct maintains consistency with the previous change for RunWithInferredLang. This ensures uniform statistics tracking across different run modes, enhancing the overall capability to monitor runtime performance.


404-405: LGTM: Completed tracing feature with default initialization

The addition of the tracing: Default::default() field to the OutputArgs struct in the default_run_arg() function completes the implementation of the tracing feature. This ensures that the new tracing field is properly initialized with its default value when creating a default RunArg, maintaining consistency with the rest of the implementation.

crates/cli/src/utils/tracing.rs (3)

21-27: LGTM! Tracing enum modifications are appropriate

The addition of the Nothing variant with the #[default] attribute correctly sets the default tracing level. Deriving Default, Serialize, and Deserialize on the Tracing enum enhances its usability for default instantiation and serialization purposes.


33-48: LGTM! Added methods run_stats and scan_stats are correctly implemented

The run_stats and scan_stats methods provide convenient ways to initialize RunStats and ScanStats with the current tracing level and default values. The implementation is clean and aligns with the design patterns of the codebase.


93-96: LGTM! RuleStats struct modifications are appropriate

The RuleStats struct correctly derives Default, Serialize, and Deserialize. Removing the Debug derive is acceptable if it's not needed for debugging purposes.

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