prometheus/promql
Jorge Creixell e9e3d64b7c
PromQL engine: Delay deletion of __name__ label to the end of the query evaluation (#14477)
PromQL engine: Delay deletion of __name__ label to the end of the query evaluation

  - This change allows optionally preserving the `__name__` label via the `label_replace` and `label_join` functions, and helps prevent the dreaded "vector cannot contain metrics with the same labelset" error.
  - The implementation extends the `Series` and `Sample` structs with a boolean flag indicating whether the `__name__` label should be deleted at the end of the query evaluation.
  - The `label_replace` and `label_join` functions can still access the value of the `__name__` label, even if it has been previously marked for deletion. If  `__name__` is used as target label, it won't be dropped at the end of the query evaluation.
  - Fixes https://github.com/prometheus/prometheus/issues/11397
  - See https://github.com/jcreixell/prometheus/pull/2 for previous discussion, including the decision to create this PR and benchmark it before considering other alternatives (like refactoring `labels.Labels`).
  - See https://github.com/jcreixell/prometheus/pull/1 for an alternative implementation using a special label instead of boolean flags.
  - Note: a feature flag  `promql-delayed-name-removal` has been added as it changes the behavior of some "weird" queries (see https://github.com/prometheus/prometheus/issues/11397#issuecomment-1451998792)

Example (this always fails, as `__name__` is being dropped by `count_over_time`):

```
count_over_time({__name__!=""}[1m])

=> Error executing query: vector cannot contain metrics with the same labelset
```

Before:

```
label_replace(count_over_time({__name__!=""}[1m]), "__name__", "count_$1", "__name__", "(.+)")

=> Error executing query: vector cannot contain metrics with the same labelset
```

After:

```
label_replace(count_over_time({__name__!=""}[1m]), "__name__", "count_$1", "__name__", "(.+)")

=>
count_go_gc_cycles_automatic_gc_cycles_total{instance="localhost:9090", job="prometheus"} 1
count_go_gc_cycles_forced_gc_cycles_total{instance="localhost:9090", job="prometheus"} 1
...
```

Signed-off-by: Jorge Creixell <jcreixell@gmail.com>

---------

Signed-off-by: Jorge Creixell <jcreixell@gmail.com>
Signed-off-by: Björn Rabenstein <github@rabenste.in>
2024-08-29 15:50:39 +02:00
..
fuzz-data
parser fix(utf8): ensure correct validation when legacy mode turned on (#14736) 2024-08-28 17:15:42 +02:00
promqltest PromQL engine: Delay deletion of __name__ label to the end of the query evaluation (#14477) 2024-08-29 15:50:39 +02:00
bench_test.go promql: add avg aggregation benchmark 2024-07-04 18:59:30 +02:00
engine_internal_test.go refactor: extract some PromQL Engine tests which use unexported structs 2024-05-08 16:27:17 +01:00
engine_test.go PromQL engine: Delay deletion of __name__ label to the end of the query evaluation (#14477) 2024-08-29 15:50:39 +02:00
engine.go PromQL engine: Delay deletion of __name__ label to the end of the query evaluation (#14477) 2024-08-29 15:50:39 +02:00
functions_internal_test.go Fix issue where summation of +/- infinity returns NaN instead of infinity 2024-06-28 11:26:54 +10:00
functions_test.go test: move most PromQL tests into separate test package 2024-05-08 16:28:56 +01:00
functions.go PromQL engine: Delay deletion of __name__ label to the end of the query evaluation (#14477) 2024-08-29 15:50:39 +02:00
fuzz_test.go remove obsolete build tag 2024-01-17 22:26:32 +08:00
fuzz.go textparse: Implement CreatedTimestamp() in openmetricsparse.go (#14356) 2024-08-08 12:35:35 +01:00
histogram_stats_iterator_test.go Add more test cases 2024-07-29 14:53:32 +02:00
histogram_stats_iterator.go Use CopyTo when resetting histogram in stats iterator 2024-07-31 11:18:47 +02:00
promql_test.go feat: add limitk() and limit_ratio() operators (#12503) 2024-07-03 22:18:57 +02:00
quantile_test.go PromQL: ignore small errors for bucketQuantile (#13153) 2023-11-25 00:05:38 +01:00
quantile.go Merge branch 'main' into nhcb 2024-05-14 16:20:15 +08:00
query_logger_test.go promql.ActiveQueryTracker: Unmap mmapped file when done 2024-05-09 14:27:29 +02:00
query_logger.go Join errors 2024-05-27 17:14:17 +02:00
value_test.go test: move most PromQL tests into separate test package 2024-05-08 16:28:56 +01:00
value.go PromQL engine: Delay deletion of __name__ label to the end of the query evaluation (#14477) 2024-08-29 15:50:39 +02:00