Title: Start timestamp synthesis for cumulative metrics
PR Link: prometheus/prometheus#18279
Author: Ridwan Sharif
Branch: [branch] β main
This PR introduces a mechanism to synthesize start timestamps for cumulative metrics (Counters, Histograms) when they are missing in the exposition (e.g., OpenMetrics without start timestamps). It uses a "subtract initial" approach (similar to OpenTelemetry) to establish a baseline and then produce delta values or synthesized start times for TSDB ingestion, improving accuracy for rate queries in short-lived environments.
- Added
st-synthesisfeature flag incmd/prometheus/main.go. - Implemented
stCacheinscrape/st_synthesis.goto track state for counters and histograms. - Integrated synthesis logic into
scrapeLoopinscrape/scrape_append_v2.go. - Added unit and integration tests in
scrape/st_synthesis_test.goandscrape/scrape_test.go.
I evaluated the code independently based on the fetched diff. No external discussion context was available due to environment limitations.
Based on the type of changes in this PR, reviewers should focus on:
| Area | Priority | What to Check |
|---|---|---|
scrape/st_synthesis.go |
π΄ High | Correctness of reset detection and histogram subtraction logic. Precision loss in float conversion. |
scrape/scrape_append_v2.go |
π΄ High | Integration with scrape loop, correct handling of missing start times, and staleness tracking. |
| Tests | π‘ Medium | Coverage of edge cases (resets, first scrape behavior). |
Issues are sorted by severity (Critical β High β Medium β Low β Nitpick).
None identified.
None identified.
Precision Loss in Integer Histogram Subtraction
- File with exact line number:
scrape/st_synthesis.go:L831(approximate based on diff) - Problem: The implementation converts integer histograms to float histograms to perform subtraction and then casts back to integers.
- Impact: Potential precision loss or rounding errors in histogram bucket counts.
- Suggestion: Implement native subtraction for integer histograms if possible, as noted in the author's TODO on line 831.
First Sample Skipped for Anchoring
- File with exact line number:
scrape/st_synthesis.go:L785(and similar for histograms) - Problem: The first sample of a new series is used to anchor the start time and is skipped (not appended to TSDB).
- Impact: New series won't have data points on the first scrape. This is a design choice but should be explicitly documented for users.
- Suggestion: Ensure this behavior is documented in the feature flag description or user guide.
None identified.
| Test File | Type | Coverage |
|---|---|---|
scrape/st_synthesis_test.go |
unit | Tests stCache methods with various data (counters, histograms) and resets. |
scrape/scrape_test.go |
integration | Tests synthesis integration in the scrape loop. |
- New code tested: Yes -
stCachemethods are covered. - Edge cases covered: Partial - Resets are tested, but complex histogram edge cases might need more coverage.
- Regression tests: N/A - This is a new feature.
- Test with very small increments to check for float precision issues in histogram conversion.
- Design choice for skipping first sample: Is there any alternative that allows appending the first sample (e.g., assuming it started at 0 at
prevTsif available)? - Precision impact: How significant is the precision loss in practice for typical histogram use cases?
| Aspect | Status | Notes |
|---|---|---|
| Code Quality | π’ | Clean implementation. |
| Security | π’ | No issues found. |
| Performance | π’ | Benchmarks added, seems fine. |
| Test Coverage | π’ | Good unit and integration tests. |
| Documentation | π‘ | Need to ensure the first-sample-skip behavior is clear to users. |
Overall: APPROVE with suggestions (Experimental feature, monitoring precision is advised).