Repository navigation
Commit eb2439e
authored
fix(common-utils): skip metric points that have no recorded value (#3583)
## Summary
When a scraped series goes away, or one scrape fails, the collector's `prometheus` receiver sends one more point with `FLAG_NO_RECORDED_VALUE` set. This is the OTLP form of a Prometheus staleness marker. The ClickHouse exporter stores it with `Flags = 1` and zeros: `Value = 0` for gauges and sums, zero bucket counts for histograms, and `Count = 0`, `Scale = 0` and no buckets for exponential histograms. The metric queries in `renderChartConfig` did not read `Flags`, so they used the marker as a real zero. Alerts use the same queries.
Effects on `main`, measured in the new integration test:
- Gauge: at the marker minute, `min` is 0 instead of 10, `avg` is 5 instead of 10, and `count` is 2 instead of 1. This is the case from #3511.
- Sum (cumulative counter): when the series comes back after the marker, the next point is compared with the marker's 0. After one failed scrape on a counter at 120, `increase` shows 130 for the next minute instead of 10.
- Histogram: the same reset. `count` is 15 instead of 5, and p50 is 7.5 although all new observations are in (10, 30].
- Exponential histogram: the same reset. Also, the marker's scale 0 makes the quantile query downscale every series to scale 0. p50 is 3.36 instead of a value in (4, 4.36].
Changes:
- New `renderMetricWhere` in `renderChartConfig.ts` adds `bitAnd(Flags, 1) = 0` to the `WHERE` of the source CTE. The gauge, sum, histogram and exponential histogram paths use it, so single and multi-series metric charts and metric alerts all get it. The filter applies before the window functions, so the next point is compared with the last real point.
- The filter is added only when the table has a `Flags` column of a non-`Nullable` integer type (`UInt32` in the OTel schema). The gauge and sum queries already read `Flags`, but `ReqMetricTableColumns` does not require it on histogram tables, and no table needs it to be an integer. So a custom table without it, or with another type, gets the same query as before. A failed column lookup also keeps the old query. The lookup reads the `DESCRIBE` result that the same query already caches, so it adds no ClickHouse query.
Things to be aware of:
- A flagged point never has a value, so results for data without markers do not change. The existing metric integration tests and unit snapshots pass without changes.
- Not changed: PromQL charts (they read the TimeSeries tables), raw SQL charts, and the MCP tools that read only metric names, units and attribute keys. Summary metrics have no chart query.
- A `Nullable` `Flags` column is skipped on purpose: `bitAnd(NULL, 1) = 0` is `NULL`, so the filter would drop rows whose `Flags` is `NULL`.
- A counter that restarts behind a marker (120 → marker → 5) is now compared with 120, and the Sum query clamps the decrease to 0, as it does for every counter reset (see the comment on its `Source` CTE). On `main`, the marker's 0 was the baseline, so this one case read 5. A counter that comes back without a restart (one failed scrape) no longer counts its whole total again. Using the Prometheus reset rule (`Value` when it is below the previous point) would fix both, but it changes every counter reset, so I left it out of this PR.
### Tests
- New `packages/common-utils/src/__tests__/metricNoRecordedValue.int.test.ts` (ClickHouse): one table per metric type with the OTel columns, and marker rows shaped like the receiver output (checked against `receiver/prometheusreceiver/internal/metricfamily.go`). 10 tests: gauge `min`, `avg` and `count` at the marker minute, the minute before it (a control), sum `increase`, a counter restart behind a marker against the same restart without one, histogram `count` (with the minute before the marker) and p50, and exponential histogram `count` (with the minute before the marker) and p50.
- `renderChartConfig.test.ts`: the filter is in the source scan once for each metric type and aggFn when `Flags` is `UInt32`, and it is there for `UInt8`, `Int64` and `LowCardinality(UInt32)`. It is not there when a histogram table has no `Flags`, when `Flags` is `String`, `Nullable(UInt32)` or `LowCardinality(Nullable(UInt32))`, or when the lookup fails.
With `renderChartConfig.ts` from `main`, 9 of the 10 integration tests fail (the control passes), and 9 of the 17 new unit tests fail (the "same query as before" cases pass on both). Without the type check, the 3 type tests fail. `renderChartConfig.test.ts` and `builderToRawSql.test.ts` (290 tests, no snapshot changes) and `queryChartConfig.int.test.ts` (81 tests) pass. ESLint (no new warnings) and Prettier pass. I ran the integration tests on ClickHouse 26.8.14.3 and 26.8.34.13; CI uses 26.8.36.3.
I did not run the `api` integration tests (alerts, MCP), `tsc` for the whole package, or the UI steps below. I ran the SQL below against the OTel gauge schema on ClickHouse 26.8.
### How to reproduce
**Starting point:** `yarn dev`, which creates the OTel metric tables in the `default` database of the local ClickHouse.
**On `main`:**
1. Insert two series of one gauge for the last 6 minutes. Series `b` ends with a marker in the last minute:
```sql
INSERT INTO default.otel_metrics_gauge
(ServiceName, MetricName, ResourceAttributes, TimeUnix, Value, Flags)
SELECT
'repro',
'repro.cpu',
map('host', host),
toStartOfMinute(now()) - toIntervalMinute(5 - m),
if(host = 'b' AND m = 5, 0, 10),
if(host = 'b' AND m = 5, 1, 0)
FROM (SELECT arrayJoin(['a', 'b']) AS host) AS hosts
CROSS JOIN (SELECT number AS m FROM numbers(6)) AS minutes
```
2. Open **Chart Explorer**. Pick the metrics source and the gauge `repro.cpu` with the **Min** aggregation, over the last 15 minutes with a 1 minute granularity.
3. The last minute drops to 0. With **Avg** it drops to 5.
**On this branch:**
1. Do the same steps. The last minute stays at 10 for **Min** and **Avg**.
The new integration test covers the other metric types: `make dev-int-common-utils FILE=metricNoRecordedValue`.
### How to test on Vercel preview
N/A — non-UI change.
### References
- Closes #3511
- Related: #3499, #3500 and #3502 report other problems in the same metric queries. This PR does not change them.1 parent dc58efd commit eb2439e
4 files changed
Lines changed: 541 additions & 4 deletions
File tree
- .changeset
- packages/common-utils/src
- __tests__
- core
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
0 commit comments