Skip to content

Commit 0234594

Browse files
author
Arthur Silva Sens
authoredApr 10, 2024··
Add support for go 1.20 (#617)
* Test with go 1.20 Signed-off-by: Arthur Silva Sens <arthur.sens@coralogix.com> * LabelSet.String(): use different implementation for go 1.20 Signed-off-by: Arthur Silva Sens <arthur.sens@coralogix.com> --------- Signed-off-by: Arthur Silva Sens <arthur.sens@coralogix.com>
1 parent 4433931 commit 0234594

11 files changed

+234
-37
lines changed
 

‎.circleci/config.yml

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ workflows:
9595
matrix:
9696
parameters:
9797
go_version:
98+
- "1.20"
9899
- "1.21"
99100
- "1.22"
100101
- test-assets:

‎assets/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/prometheus/common/assets
22

3-
go 1.21
3+
go 1.20

‎go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/prometheus/common
22

3-
go 1.21
3+
go 1.20
44

55
require (
66
github.com/alecthomas/kingpin/v2 v2.4.0

‎go.sum

-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj
88
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
99
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1010
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
11-
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1211
github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU=
1312
github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0=
1413
github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
@@ -26,7 +25,6 @@ github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4d
2625
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
2726
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
2827
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
29-
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
3028
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
3129
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
3230
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
@@ -45,7 +43,6 @@ github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncj
4543
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
4644
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
4745
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
48-
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
4946
github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
5047
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
5148
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@@ -76,4 +73,3 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
7673
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
7774
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
7875
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
79-
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

‎model/labelset.go

-26
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@
1414
package model
1515

1616
import (
17-
"bytes"
1817
"encoding/json"
1918
"fmt"
20-
"slices"
2119
"sort"
22-
"strconv"
2320
)
2421

2522
// A LabelSet is a collection of LabelName and LabelValue pairs. The LabelSet
@@ -131,29 +128,6 @@ func (l LabelSet) Merge(other LabelSet) LabelSet {
131128
return result
132129
}
133130

134-
// String will look like `{foo="bar", more="less"}`. Names are sorted alphabetically.
135-
func (l LabelSet) String() string {
136-
var lna [32]LabelName // On stack to avoid memory allocation for sorting names.
137-
labelNames := lna[:0]
138-
for name := range l {
139-
labelNames = append(labelNames, name)
140-
}
141-
slices.Sort(labelNames)
142-
var bytea [1024]byte // On stack to avoid memory allocation while building the output.
143-
b := bytes.NewBuffer(bytea[:0])
144-
b.WriteByte('{')
145-
for i, name := range labelNames {
146-
if i > 0 {
147-
b.WriteString(", ")
148-
}
149-
b.WriteString(string(name))
150-
b.WriteByte('=')
151-
b.Write(strconv.AppendQuote(b.AvailableBuffer(), string(l[name])))
152-
}
153-
b.WriteByte('}')
154-
return b.String()
155-
}
156-
157131
// Fingerprint returns the LabelSet's fingerprint.
158132
func (ls LabelSet) Fingerprint() Fingerprint {
159133
return labelSetToFingerprint(ls)

‎model/labelset_go120_test.go

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// Copyright 2024 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
//go:build !go1.21
15+
16+
package model
17+
18+
import (
19+
"encoding/json"
20+
"testing"
21+
)
22+
23+
func TestUnmarshalJSONLabelSet(t *testing.T) {
24+
type testConfig struct {
25+
LabelSet LabelSet `yaml:"labelSet,omitempty"`
26+
}
27+
28+
// valid LabelSet JSON
29+
labelSetJSON := `{
30+
"labelSet": {
31+
"monitor": "codelab",
32+
"foo": "bar",
33+
"foo2": "bar",
34+
"abc": "prometheus",
35+
"foo11": "bar11"
36+
}
37+
}`
38+
var c testConfig
39+
err := json.Unmarshal([]byte(labelSetJSON), &c)
40+
if err != nil {
41+
t.Errorf("unexpected error while marshalling JSON : %s", err.Error())
42+
}
43+
44+
labelSetString := c.LabelSet.String()
45+
46+
expected := `{abc="prometheus", foo="bar", foo11="bar11", foo2="bar", monitor="codelab"}`
47+
48+
if expected != labelSetString {
49+
t.Errorf("expected %s but got %s", expected, labelSetString)
50+
}
51+
52+
// invalid LabelSet JSON
53+
invalidlabelSetJSON := `{
54+
"labelSet": {
55+
"1nvalid_23name": "codelab",
56+
"foo": "bar"
57+
}
58+
}`
59+
60+
NameValidationScheme = LegacyValidation
61+
err = json.Unmarshal([]byte(invalidlabelSetJSON), &c)
62+
expectedErr := `"1nvalid_23name" is not a valid label name`
63+
if err == nil || err.Error() != expectedErr {
64+
t.Errorf("expected an error with message '%s' to be thrown", expectedErr)
65+
}
66+
}
67+
68+
func TestLabelSetClone(t *testing.T) {
69+
labelSet := LabelSet{
70+
"monitor": "codelab",
71+
"foo": "bar",
72+
"bar": "baz",
73+
}
74+
75+
cloneSet := labelSet.Clone()
76+
77+
if len(labelSet) != len(cloneSet) {
78+
t.Errorf("expected the length of the cloned Label set to be %d, but got %d",
79+
len(labelSet), len(cloneSet))
80+
}
81+
82+
for ln, lv := range labelSet {
83+
expected := cloneSet[ln]
84+
if expected != lv {
85+
t.Errorf("expected to get LabelValue %s, but got %s for LabelName %s", expected, lv, ln)
86+
}
87+
}
88+
}
89+
90+
func TestLabelSetMerge(t *testing.T) {
91+
labelSet := LabelSet{
92+
"monitor": "codelab",
93+
"foo": "bar",
94+
"bar": "baz",
95+
}
96+
97+
labelSet2 := LabelSet{
98+
"monitor": "codelab",
99+
"dolor": "mi",
100+
"lorem": "ipsum",
101+
}
102+
103+
expectedSet := LabelSet{
104+
"monitor": "codelab",
105+
"foo": "bar",
106+
"bar": "baz",
107+
"dolor": "mi",
108+
"lorem": "ipsum",
109+
}
110+
111+
mergedSet := labelSet.Merge(labelSet2)
112+
113+
if len(mergedSet) != len(expectedSet) {
114+
t.Errorf("expected the length of the cloned Label set to be %d, but got %d",
115+
len(expectedSet), len(mergedSet))
116+
}
117+
118+
for ln, lv := range mergedSet {
119+
expected := expectedSet[ln]
120+
if expected != lv {
121+
t.Errorf("expected to get LabelValue %s, but got %s for LabelName %s", expected, lv, ln)
122+
}
123+
}
124+
}
125+
126+
// Benchmark Results for LabelSet's String() method
127+
// ---------------------------------------------------------------------------------------------------------
128+
// goos: linux
129+
// goarch: amd64
130+
// pkg: github.com/prometheus/common/model
131+
// cpu: 11th Gen Intel(R) Core(TM) i5-1145G7 @ 2.60GHz
132+
// BenchmarkLabelSetStringMethod-8 732376 1532 ns/op
133+
134+
func BenchmarkLabelSetStringMethod(b *testing.B) {
135+
ls := make(LabelSet)
136+
ls["monitor"] = "codelab"
137+
ls["foo2"] = "bar"
138+
ls["foo"] = "bar"
139+
ls["abc"] = "prometheus"
140+
ls["foo11"] = "bar11"
141+
for i := 0; i < b.N; i++ {
142+
_ = ls.String()
143+
}
144+
}

‎model/labelset_string.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2024 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
//go:build go1.21
15+
16+
package model
17+
18+
import (
19+
"bytes"
20+
"sort"
21+
"strconv"
22+
)
23+
24+
// String will look like `{foo="bar", more="less"}`. Names are sorted alphabetically.
25+
func (l LabelSet) String() string {
26+
var lna [32]string // On stack to avoid memory allocation for sorting names.
27+
labelNames := lna[:0]
28+
for name := range l {
29+
labelNames = append(labelNames, string(name))
30+
}
31+
sort.Strings(labelNames)
32+
var bytea [1024]byte // On stack to avoid memory allocation while building the output.
33+
b := bytes.NewBuffer(bytea[:0])
34+
b.WriteByte('{')
35+
for i, name := range labelNames {
36+
if i > 0 {
37+
b.WriteString(", ")
38+
}
39+
b.WriteString(name)
40+
b.WriteByte('=')
41+
b.Write(strconv.AppendQuote(b.AvailableBuffer(), string(l[LabelName(name)])))
42+
}
43+
b.WriteByte('}')
44+
return b.String()
45+
}

‎model/labelset_string_go120.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2024 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
//go:build !go1.21
15+
16+
package model
17+
18+
import (
19+
"fmt"
20+
"sort"
21+
"strings"
22+
)
23+
24+
// String was optimized using functions not available for go 1.20
25+
// or lower. We keep the old implementation for compatibility with client_golang.
26+
// Once client golang drops support for go 1.20 (scheduled for August 2024), this
27+
// file can be removed.
28+
func (l LabelSet) String() string {
29+
labelNames := make([]string, 0, len(l))
30+
for name := range l {
31+
labelNames = append(labelNames, string(name))
32+
}
33+
sort.Strings(labelNames)
34+
lstrs := make([]string, 0, len(l))
35+
for _, name := range labelNames {
36+
lstrs = append(lstrs, fmt.Sprintf("%s=%q", name, l[LabelName(name)]))
37+
}
38+
return fmt.Sprintf("{%s}", strings.Join(lstrs, ", "))
39+
}

‎model/labelset_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14+
//go:build go1.21
15+
1416
package model
1517

1618
import (

‎sigv4/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/prometheus/common/sigv4
22

3-
go 1.21
3+
go 1.20
44

55
replace github.com/prometheus/common => ../
66

‎sigv4/go.sum

-4
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@ github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg
1414
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
1515
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
1616
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
17-
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
1817
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
1918
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
2019
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
2120
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
2221
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
2322
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
2423
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
25-
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
2624
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
2725
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
2826
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU=
@@ -36,7 +34,6 @@ github.com/prometheus/client_model v0.6.0/go.mod h1:NTQHnmxFpouOD0DpvP4XujX3CdOA
3634
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
3735
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
3836
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
39-
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
4037
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
4138
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
4239
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
@@ -63,7 +60,6 @@ google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGm
6360
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
6461
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
6562
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
66-
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
6763
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
6864
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
6965
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

0 commit comments

Comments
 (0)
Please sign in to comment.