Skip to content

Optimizing "grafana generated" regex matchers#12375

Merged
jesusvazquez merged 1 commit intoprometheus:mainfrom
alanprot:opmize/group-regex
May 30, 2023
Merged

Optimizing "grafana generated" regex matchers#12375
jesusvazquez merged 1 commit intoprometheus:mainfrom
alanprot:opmize/group-regex

Conversation

@alanprot
Copy link
Contributor

When using a multi-value variable on Grafana Dashboards the default behavior is to produce regex in the format (a|b). See https://grafana.com/docs/grafana/latest/dashboards/variables/variable-syntax/#regex

This PR is expanding the optimization Prometheus already have on regexes like a|b to regexes like (a|b) and improve the query time and cpu utilization for dashboards with high cardinality variables substantially.

PS: Nothing will change for other types of queries even if they starts and ends with (), like (a|b)|(c|d)

goos: linux
goarch: amd64
pkg: github.com/prometheus/prometheus/tsdb
cpu: Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz
                                                             │     /tmp/old     │              /tmp/new               │
                                                             │      sec/op      │    sec/op     vs base               │
Querier/Head/PostingsForMatchers/i=~"(1|2|3|4|5|6|20|55)"-32   18796.184µ ± ∞ ¹   2.416µ ± ∞ ¹  -99.99% (p=0.008 n=5)
Querier/Head/PostingsForMatchers/i!~"(1|2|3|4|5|6|20|55)"-32      18.450m ± ∞ ¹   2.238m ± ∞ ¹  -87.87% (p=0.008 n=5)
geomean                                                            18.62m         73.53µ        -99.61%
¹ need >= 6 samples for confidence interval at level 0.95

                                                             │    /tmp/old     │               /tmp/new               │
                                                             │      B/op       │     B/op	vs base               │
Querier/Head/PostingsForMatchers/i=~"(1|2|3|4|5|6|20|55)"-32   1606349.0 ± ∞ ¹     848.0 ± ∞ ¹  -99.95% (p=0.008 n=5)
Querier/Head/PostingsForMatchers/i!~"(1|2|3|4|5|6|20|55)"-32     1.531Mi ± ∞ ¹   1.532Mi ± ∞ ¹   +0.04% (p=0.008 n=5)
geomean                                                          1.532Mi         36.04Ki        -97.70%
¹ need >= 6 samples for confidence interval at level 0.95

                                                             │  /tmp/old   │               /tmp/new               │
                                                             │  allocs/op  │  allocs/op    vs base                │
Querier/Head/PostingsForMatchers/i=~"(1|2|3|4|5|6|20|55)"-32   4.000 ± ∞ ¹   31.000 ± ∞ ¹  +675.00% (p=0.008 n=5)
Querier/Head/PostingsForMatchers/i!~"(1|2|3|4|5|6|20|55)"-32   7.000 ± ∞ ¹   35.000 ± ∞ ¹  +400.00% (p=0.008 n=5)
geomean                                                        5.292          32.94        +522.49%

@alanprot alanprot requested a review from jesusvazquez as a code owner May 17, 2023 18:05
@alanprot alanprot force-pushed the opmize/group-regex branch from d49bbb6 to fdbfa64 Compare May 17, 2023 18:06
Signed-off-by: Alan Protasio <alanprot@gmail.com>
@alanprot alanprot force-pushed the opmize/group-regex branch from 62431fb to c062711 Compare May 19, 2023 22:48
@alanprot
Copy link
Contributor Author

@jesusvazquez 🥺 :D

Copy link
Member

@jesusvazquez jesusvazquez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you are on a mission to improve a lot of the regexes cases 💪

@jesusvazquez jesusvazquez merged commit 73078bf into prometheus:main May 30, 2023
@alanprot
Copy link
Contributor Author

Thanks!! :D

bboreham pushed a commit to bboreham/prometheus that referenced this pull request Jul 24, 2023
Signed-off-by: Alan Protasio <alanprot@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants