|
| 1 | +# Implementation Summary: Code Coverage Validation for OpenTelemetry Collector Components |
| 2 | + |
| 3 | +## Issue Reference |
| 4 | + |
| 5 | +GitHub Issue: #14098 - Validate and enforce test coverage requirements |
| 6 | + |
| 7 | +Parent Issue: #14066 - Automate validation and fulfillment of criteria outlined in the "Component Stability" document |
| 8 | + |
| 9 | +## Overview |
| 10 | + |
| 11 | +This implementation adds automated validation of code coverage requirements for OpenTelemetry Collector components based on their stability level, as outlined in the Component Stability document. |
| 12 | + |
| 13 | +## What Was Implemented |
| 14 | + |
| 15 | +### 1. Metadata Schema Enhancement (`cmd/mdatagen/metadata-schema.yaml`) |
| 16 | + |
| 17 | +- Added optional `coverage_minimum` field under the `status` section |
| 18 | +- Allows components to specify a minimum coverage requirement higher than the default |
| 19 | +- Value must be between 0 and 100 |
| 20 | +- Setting it lower than stability-based minimum has no effect |
| 21 | + |
| 22 | +```yaml |
| 23 | +status: |
| 24 | + coverage_minimum: 85 # Optional: Require 85% coverage |
| 25 | +``` |
| 26 | +
|
| 27 | +### 2. Status Struct Update (`cmd/mdatagen/internal/status.go`) |
| 28 | + |
| 29 | +- Added `CoverageMinimum` field to the `Status` struct |
| 30 | +- Added validation to ensure the value is between 0 and 100 |
| 31 | +- Integrated validation into the existing `Validate()` method |
| 32 | + |
| 33 | +### 3. Coverage Validation Tool (`cmd/checkcover/`) |
| 34 | + |
| 35 | +Created a new command-line tool with the following components: |
| 36 | + |
| 37 | +#### Files Created: |
| 38 | + |
| 39 | +- `main.go` - CLI entry point with cobra command structure |
| 40 | +- `validator.go` - Core validation logic |
| 41 | +- `validator_test.go` - Unit tests |
| 42 | +- `README.md` - Documentation |
| 43 | +- `go.mod` - Go module definition |
| 44 | +- `Makefile` - Build configuration |
| 45 | + |
| 46 | +#### Features: |
| 47 | + |
| 48 | +- **Coverage Parsing**: Reads and parses coverage.txt files (generated by `go tool covdata textfmt`) |
| 49 | +- **Module Detection**: Automatically finds all components with metadata.yaml files |
| 50 | +- **Threshold Calculation**: Determines minimum coverage based on: |
| 51 | + - Stability level (80% for stable components) |
| 52 | + - Repository-wide minimum (if specified) |
| 53 | + - Component-specific coverage_minimum (if specified) |
| 54 | +- **Validation**: Compares actual coverage against required minimum |
| 55 | +- **Detailed Reporting**: Shows which components pass or fail with specific percentages |
| 56 | + |
| 57 | +#### Command-Line Interface: |
| 58 | + |
| 59 | +```bash |
| 60 | +checkcover -c coverage.txt -r /path/to/repo [-m repo-minimum] [-v] |
| 61 | +``` |
| 62 | + |
| 63 | +Flags: |
| 64 | + |
| 65 | +- `-c, --coverage-file`: Path to coverage file (default: "coverage.txt") |
| 66 | +- `-r, --repo-root`: Repository root directory (default: ".") |
| 67 | +- `-m, --repo-minimum`: Repository-wide minimum percentage (default: 0) |
| 68 | +- `-v, --verbose`: Enable verbose output |
| 69 | + |
| 70 | +### 4. CI Integration (`.github/workflows/build-and-test.yml`) |
| 71 | + |
| 72 | +Added a new step to the `test-coverage` job: |
| 73 | + |
| 74 | +```yaml |
| 75 | +- name: Validate coverage requirements |
| 76 | + run: | |
| 77 | + cd cmd/checkcover |
| 78 | + go run . -c ../../coverage.txt -r ../.. -v |
| 79 | +``` |
| 80 | + |
| 81 | +This runs after: |
| 82 | + |
| 83 | +1. Tests are executed with coverage (`make gotest-with-cover`) |
| 84 | +2. Coverage report is uploaded to Codecov |
| 85 | + |
| 86 | +### 5. Existing Functionality Verified |
| 87 | + |
| 88 | +- Confirmed that mdatagen already generates README sections with codecov badges |
| 89 | +- The badge generation respects the `disable_codecov_badge` flag |
| 90 | +- Component ID matching is handled via `GetCodeCovComponentID()` method |
| 91 | + |
| 92 | +## Coverage Requirements |
| 93 | + |
| 94 | +### Stable Components |
| 95 | + |
| 96 | +- **Minimum**: 80% coverage |
| 97 | +- **Override**: Can be increased via `coverage_minimum` in metadata.yaml |
| 98 | +- **Repository Minimum**: Applied if higher than 80% |
| 99 | + |
| 100 | +### Other Stability Levels (Alpha, Beta, Development) |
| 101 | + |
| 102 | +- **Minimum**: No default requirement |
| 103 | +- **Override**: Can set via `coverage_minimum` in metadata.yaml |
| 104 | +- **Repository Minimum**: Applied if specified |
| 105 | + |
| 106 | +### Calculation Priority |
| 107 | + |
| 108 | +The tool uses the highest of: |
| 109 | + |
| 110 | +1. Stability-based minimum (80% for stable, 0% for others) |
| 111 | +2. Repository minimum (via `-m` flag) |
| 112 | +3. Component-specific `coverage_minimum` (from metadata.yaml) |
| 113 | + |
| 114 | +## Example Usage |
| 115 | + |
| 116 | +### For a Stable Component |
| 117 | + |
| 118 | +```yaml |
| 119 | +# metadata.yaml |
| 120 | +type: otlp |
| 121 | +status: |
| 122 | + class: receiver |
| 123 | + stability: |
| 124 | + stable: [metrics, traces] |
| 125 | + # No coverage_minimum specified, uses 80% default |
| 126 | +``` |
| 127 | + |
| 128 | +**Requirement**: 80% coverage |
| 129 | + |
| 130 | +### For a Component with Custom Minimum |
| 131 | + |
| 132 | +```yaml |
| 133 | +# metadata.yaml |
| 134 | +type: custom |
| 135 | +status: |
| 136 | + class: processor |
| 137 | + stability: |
| 138 | + stable: [metrics] |
| 139 | + coverage_minimum: 90 # Higher than default |
| 140 | +``` |
| 141 | + |
| 142 | +**Requirement**: 90% coverage |
| 143 | + |
| 144 | +### For an Alpha Component |
| 145 | + |
| 146 | +```yaml |
| 147 | +# metadata.yaml |
| 148 | +type: experimental |
| 149 | +status: |
| 150 | + class: exporter |
| 151 | + stability: |
| 152 | + alpha: [metrics] |
| 153 | + coverage_minimum: 70 # Optional requirement for alpha |
| 154 | +``` |
| 155 | + |
| 156 | +**Requirement**: 70% coverage (from coverage_minimum) |
| 157 | + |
| 158 | +## Testing |
| 159 | + |
| 160 | +### Unit Tests |
| 161 | + |
| 162 | +- Coverage file parsing |
| 163 | +- Minimum coverage calculation |
| 164 | +- Module path resolution |
| 165 | +- Coverage lookup and matching |
| 166 | + |
| 167 | +### Integration with CI |
| 168 | + |
| 169 | +- Runs automatically on every PR and commit |
| 170 | +- Fails the build if any component doesn't meet requirements |
| 171 | +- Provides clear error messages showing which components failed |
| 172 | + |
| 173 | +## Benefits |
| 174 | + |
| 175 | +1. **Automated Enforcement**: No manual checking required |
| 176 | +2. **Clear Requirements**: Components know exactly what coverage they need |
| 177 | +3. **Flexible Configuration**: Components can set higher standards if desired |
| 178 | +4. **CI Integration**: Prevents merging code that doesn't meet coverage standards |
| 179 | +5. **Visibility**: All components show coverage badges in their READMEs |
| 180 | + |
| 181 | +## Future Enhancements |
| 182 | + |
| 183 | +Potential improvements that could be made in the future: |
| 184 | + |
| 185 | +1. **Beta/Alpha Requirements**: Consider adding default minimums for beta (e.g., 60%) and alpha (e.g., 40%) components |
| 186 | +2. **Progressive Requirements**: Implement a feature flag system that gradually increases requirements as components mature |
| 187 | +3. **Coverage Trends**: Track coverage changes over time and warn about decreases |
| 188 | +4. **Exemptions**: Add ability to temporarily exempt specific files or packages from coverage requirements |
| 189 | +5. **HTML Reports**: Generate detailed HTML reports showing coverage by component |
| 190 | + |
| 191 | +## Files Changed |
| 192 | + |
| 193 | +1. `cmd/mdatagen/metadata-schema.yaml` - Added coverage_minimum field |
| 194 | +2. `cmd/mdatagen/internal/status.go` - Added CoverageMinimum field and validation |
| 195 | +3. `cmd/checkcover/main.go` - New file (CLI entry point) |
| 196 | +4. `cmd/checkcover/validator.go` - New file (validation logic) |
| 197 | +5. `cmd/checkcover/validator_test.go` - New file (tests) |
| 198 | +6. `cmd/checkcover/README.md` - New file (documentation) |
| 199 | +7. `cmd/checkcover/go.mod` - New file (dependencies) |
| 200 | +8. `cmd/checkcover/Makefile` - New file (build config) |
| 201 | +9. `.github/workflows/build-and-test.yml` - Added coverage validation step |
| 202 | + |
| 203 | +## Related Documentation |
| 204 | + |
| 205 | +- [Component Stability](../../docs/component-stability.md) - Requirements for stable components |
| 206 | +- [checkcover README](../checkcover/README.md) - Tool documentation |
| 207 | +- [mdatagen README](../mdatagen/README.md) - Metadata generator documentation |
| 208 | + |
| 209 | +## Conclusion |
| 210 | + |
| 211 | +This implementation provides a complete solution for validating and enforcing code coverage requirements across all OpenTelemetry Collector components. It automates a critical aspect of the component stability criteria and ensures that stable components maintain the required 80% coverage threshold. |
0 commit comments