-
Notifications
You must be signed in to change notification settings - Fork 4
Return ScanResult with metrics from async scan methods #299
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
Open
trosenblatt
wants to merge
6
commits into
main
Choose a base branch
from
trosenblatt/return_io_duration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This commit adds a CPU duration histogram metric to track scanning time excluding I/O wait operations. This is the first PR in a series to improve timing observability. ## Changes 1. **I/O Duration Tracking**: - Added `io_duration` field to `AsyncRuleInfo` to track async I/O time - Modified `process_async` to measure duration using `Instant::now()` - `internal_scan` aggregates I/O duration from all async jobs 2. **CPU Duration Histogram Metric**: - Added `cpu_duration` histogram field to `ScannerMetrics` using highcard labels - Metric name: `scanning.cpu_duration` (in nanoseconds) - Calculated as: `total_duration - io_duration` to exclude I/O wait time - Recorded in `internal_scan_with_metrics` after each scan 3. **Highcard Labels Support**: - Added `highcard_labels` field to `Scanner` and `ScannerBuilder` - Added `highcard_labels()` builder method for configuration - `ScannerMetrics::new()` now accepts both regular and highcard labels 4. **Internal Changes**: - `internal_scan` now returns `(Vec<RuleMatch>, Duration)` tuple - Scan method signatures remain unchanged for backward compatibility ## Why This Approach This PR is intentionally minimal and doesn't change public scan method signatures. This ensures that bumping the version in sds_shared_library will be a no-op with no breaking changes. The histogram metric provides visibility into CPU scanning time while maintaining API stability. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
This PR builds on the previous CPU duration histogram metric PR to expose
timing information to callers through the async scan method APIs.
## Changes
### 1. New Public Types
**ScanMetrics** - Contains timing information:
- `total_duration`: Total time for the entire scan
- `io_duration`: Time spent in async I/O operations
- `num_async_rules`: Number of async rules that were executed
**ScanResult** - Returned by async scan methods:
- `matches`: Vec<RuleMatch> - The list of rule matches
- `metrics`: ScanMetrics - Timing metrics for the scan
### 2. Updated Async Scan Methods
- `scan_async()` now returns `Result<ScanResult, ScannerError>`
- `scan_async_with_options()` now returns `Result<ScanResult, ScannerError>`
**Breaking Change**: Callers need to access matches via `result.matches` instead of using the result directly.
### 3. Synchronous Methods Unchanged
- `scan()` and `scan_with_options()` still return `Result<Vec<RuleMatch>, ScannerError>`
- No breaking changes for synchronous callers
### 4. Internal Changes
- `internal_scan` now returns `(Vec<RuleMatch>, Duration, usize)` tuple
- `internal_scan_with_metrics` constructs and returns `ScanResult`
- Updated async tests to use new `result.matches` API
## Migration Guide
### Before:
```rust
let matches = scanner.scan_async(&mut event).await?;
for match in matches { ... }
```
### After:
```rust
let result = scanner.scan_async(&mut event).await?;
for match in result.matches { ... }
// Access metrics
println!("Total: {:?}", result.metrics.total_duration);
println!("I/O: {:?}", result.metrics.io_duration);
```
## Testing
- All 293 tests passing
- Updated async tests to use new API
- Backward compatibility maintained for sync methods
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
Added two tests to verify the cpu_duration histogram metric is properly populated: 1. **should_submit_cpu_duration_metric_non_async**: Tests non-async rules - Verifies that CPU duration is recorded and greater than 0 - Ensures the histogram metric is emitted for regular synchronous scanning 2. **should_submit_cpu_duration_metric_with_async_rule**: Tests async rule with I/O - Creates a custom async rule that sleeps for 100ms to simulate I/O - Verifies that CPU duration is less than 10ms - Confirms that I/O wait time is correctly excluded from CPU duration Both tests validate that the cpu_duration histogram metric correctly tracks CPU-only processing time by excluding I/O wait operations. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR builds on the previous CPU duration histogram metric PR to expose timing information to callers through the async scan method APIs.
Changes
1. New Public Types
ScanMetrics - Contains timing information:
total_duration: Total time for the entire scanio_duration: Time spent in async I/O operationsnum_async_rules: Number of async rules that were executedScanResult - Returned by async scan methods:
matches: Vec - The list of rule matchesmetrics: ScanMetrics - Timing metrics for the scan2. Updated Async Scan Methods
scan_async()now returnsResult<ScanResult, ScannerError>scan_async_with_options()now returnsResult<ScanResult, ScannerError>Breaking Change: Callers need to access matches via
result.matchesinstead of using the result directly.3. Synchronous Methods Unchanged
scan()andscan_with_options()still returnResult<Vec<RuleMatch>, ScannerError>4. Internal Changes
internal_scannow returns(Vec<RuleMatch>, Duration, usize)tupleinternal_scan_with_metricsconstructs and returnsScanResultresult.matchesAPIMigration Guide
Before:
After:
Testing
🤖 Generated with Claude Code