Skip to content

Commit 2e2ce74

Browse files
committed
Enable experimental functions
Signed-off-by: arkbriar <[email protected]>
1 parent 1b94133 commit 2e2ce74

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@ go 1.23
55
require (
66
github.com/pkg/errors v0.9.1
77
github.com/prometheus/prometheus v0.301.0
8+
github.com/stretchr/testify v1.10.0
89
github.com/urfave/negroni v1.0.0
910
)
1011

1112
require (
1213
github.com/beorn7/perks v1.0.1 // indirect
1314
github.com/cespare/xxhash/v2 v2.3.0 // indirect
15+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
1416
github.com/dennwc/varint v1.0.0 // indirect
1517
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc // indirect
18+
github.com/kr/text v0.2.0 // indirect
1619
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
20+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
1721
github.com/prometheus/client_golang v1.20.5 // indirect
1822
github.com/prometheus/client_model v0.6.1 // indirect
1923
github.com/prometheus/common v0.61.0 // indirect
@@ -22,4 +26,5 @@ require (
2226
golang.org/x/sys v0.29.0 // indirect
2327
golang.org/x/text v0.21.0 // indirect
2428
google.golang.org/protobuf v1.36.2 // indirect
29+
gopkg.in/yaml.v3 v3.0.1 // indirect
2530
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
2222
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
2323
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
2424
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
25+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
2526
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
2627
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2728
github.com/dennwc/varint v1.0.0 h1:kGNFFSSw8ToIy3obO/kKr8U9GZYUAxQEVuix4zfDWzE=
@@ -54,6 +55,10 @@ github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2E
5455
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
5556
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
5657
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
58+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
59+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
60+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
61+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
5762
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
5863
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
5964
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
@@ -80,6 +85,8 @@ github.com/prometheus/prometheus v0.301.0 h1:0z8dgegmILivNomCd79RKvVkIols8vBGPKm
8085
github.com/prometheus/prometheus v0.301.0/go.mod h1:BJLjWCKNfRfjp7Q48DrAjARnCi7GhfUVvUFEAWTssZM=
8186
github.com/prometheus/sigv4 v0.1.0 h1:FgxH+m1qf9dGQ4w8Dd6VkthmpFQfGTzUeavMoQeG1LA=
8287
github.com/prometheus/sigv4 v0.1.0/go.mod h1:doosPW9dOitMzYe2I2BN0jZqUuBrGPbXrNsTScN18iU=
88+
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
89+
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
8390
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
8491
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
8592
github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc=
@@ -122,6 +129,9 @@ google.golang.org/grpc v1.69.0 h1:quSiOM1GJPmPH5XtU+BCoVXcDVJJAzNcoyfC2cCjGkI=
122129
google.golang.org/grpc v1.69.0/go.mod h1:vyjdE6jLBI76dgpDojsFGNaHlxdjXN9ghpnd2o7JGZ4=
123130
google.golang.org/protobuf v1.36.2 h1:R8FeyR1/eLmkutZOM5CWghmo5itiG9z0ktFlTVLuTmU=
124131
google.golang.org/protobuf v1.36.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
132+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
133+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
134+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
125135
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
126136
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
127137
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

proxy.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import (
1414
"github.com/prometheus/prometheus/promql/parser"
1515
)
1616

17+
func init() {
18+
parser.EnableExperimentalFunctions = true
19+
}
20+
1721
const (
1822
extraMatchesKey = "extra-match[]"
1923
)

proxy_test.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ import (
44
"testing"
55

66
"github.com/prometheus/prometheus/model/labels"
7+
"github.com/prometheus/prometheus/promql/parser"
8+
9+
"github.com/stretchr/testify/assert"
710
)
811

12+
func mustCanonicalizeQuery(s string) string {
13+
return must(parser.ParseExpr(s)).String()
14+
}
15+
916
func TestProxyRewriteQuery(t *testing.T) {
1017
testcases := map[string]struct {
1118
query string
@@ -28,17 +35,36 @@ func TestProxyRewriteQuery(t *testing.T) {
2835
},
2936
expected: `sum by (pod) (process_cpu_seconds_total{cluster="test-useast1-eks-a"})`,
3037
},
38+
"matrix selector": {
39+
query: `sum_over_time (process_cpu_seconds_total[30d:1h])`,
40+
labelMatchers: []*labels.Matcher{
41+
{
42+
Type: labels.MatchEqual,
43+
Name: "cluster",
44+
Value: "test-useast1-eks-a",
45+
},
46+
},
47+
expected: `sum_over_time (process_cpu_seconds_total{cluster="test-useast1-eks-a"}[30d:1h])`,
48+
},
49+
"experimental functions": {
50+
query: `sort_by_label (process_cpu_seconds_total{job="test"})`,
51+
labelMatchers: []*labels.Matcher{
52+
{
53+
Type: labels.MatchEqual,
54+
Name: "cluster",
55+
Value: "test-useast1-eks-a",
56+
},
57+
},
58+
expected: `sort_by_label (process_cpu_seconds_total{cluster="test-useast1-eks-a", job="test"})`,
59+
},
3160
}
3261

3362
for name, tc := range testcases {
3463
t.Run(name, func(t *testing.T) {
3564
p := &proxy{}
3665
got, err := p.rewriteQuery(tc.query, tc.labelMatchers...)
37-
if err != nil {
38-
t.Fatalf("unexpected error: %v", err)
39-
}
40-
if got != tc.expected {
41-
t.Fatalf("expected %q, got %q", tc.expected, got)
66+
if assert.NoErrorf(t, err, "unexpected error: %s") {
67+
assert.Equal(t, mustCanonicalizeQuery(tc.expected), got)
4268
}
4369
})
4470
}

0 commit comments

Comments
 (0)