From 0234594908756cebd2390969ecac5402753476ac Mon Sep 17 00:00:00 2001 From: Arthur Silva Sens Date: Wed, 10 Apr 2024 13:03:44 -0300 Subject: [PATCH] Add support for go 1.20 (#617) * Test with go 1.20 Signed-off-by: Arthur Silva Sens * LabelSet.String(): use different implementation for go 1.20 Signed-off-by: Arthur Silva Sens --------- Signed-off-by: Arthur Silva Sens --- .circleci/config.yml | 1 + assets/go.mod | 2 +- go.mod | 2 +- go.sum | 4 - model/labelset.go | 26 ------ model/labelset_go120_test.go | 144 +++++++++++++++++++++++++++++++++ model/labelset_string.go | 45 +++++++++++ model/labelset_string_go120.go | 39 +++++++++ model/labelset_test.go | 2 + sigv4/go.mod | 2 +- sigv4/go.sum | 4 - 11 files changed, 234 insertions(+), 37 deletions(-) create mode 100644 model/labelset_go120_test.go create mode 100644 model/labelset_string.go create mode 100644 model/labelset_string_go120.go diff --git a/.circleci/config.yml b/.circleci/config.yml index 66bc9681..64b08fc8 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -95,6 +95,7 @@ workflows: matrix: parameters: go_version: + - "1.20" - "1.21" - "1.22" - test-assets: diff --git a/assets/go.mod b/assets/go.mod index 4f73955c..1f13afcc 100644 --- a/assets/go.mod +++ b/assets/go.mod @@ -1,3 +1,3 @@ module github.com/prometheus/common/assets -go 1.21 +go 1.20 diff --git a/go.mod b/go.mod index 68f8917b..d27efad9 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/prometheus/common -go 1.21 +go 1.20 require ( github.com/alecthomas/kingpin/v2 v2.4.0 diff --git a/go.sum b/go.sum index b09ceb0b..df797878 100644 --- a/go.sum +++ b/go.sum @@ -8,7 +8,6 @@ github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU= github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0= 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 github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= -github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= 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 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= -github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc= github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU= 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= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/model/labelset.go b/model/labelset.go index ec738e62..d0ad88da 100644 --- a/model/labelset.go +++ b/model/labelset.go @@ -14,12 +14,9 @@ package model import ( - "bytes" "encoding/json" "fmt" - "slices" "sort" - "strconv" ) // A LabelSet is a collection of LabelName and LabelValue pairs. The LabelSet @@ -131,29 +128,6 @@ func (l LabelSet) Merge(other LabelSet) LabelSet { return result } -// String will look like `{foo="bar", more="less"}`. Names are sorted alphabetically. -func (l LabelSet) String() string { - var lna [32]LabelName // On stack to avoid memory allocation for sorting names. - labelNames := lna[:0] - for name := range l { - labelNames = append(labelNames, name) - } - slices.Sort(labelNames) - var bytea [1024]byte // On stack to avoid memory allocation while building the output. - b := bytes.NewBuffer(bytea[:0]) - b.WriteByte('{') - for i, name := range labelNames { - if i > 0 { - b.WriteString(", ") - } - b.WriteString(string(name)) - b.WriteByte('=') - b.Write(strconv.AppendQuote(b.AvailableBuffer(), string(l[name]))) - } - b.WriteByte('}') - return b.String() -} - // Fingerprint returns the LabelSet's fingerprint. func (ls LabelSet) Fingerprint() Fingerprint { return labelSetToFingerprint(ls) diff --git a/model/labelset_go120_test.go b/model/labelset_go120_test.go new file mode 100644 index 00000000..d3c00e8f --- /dev/null +++ b/model/labelset_go120_test.go @@ -0,0 +1,144 @@ +// Copyright 2024 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build !go1.21 + +package model + +import ( + "encoding/json" + "testing" +) + +func TestUnmarshalJSONLabelSet(t *testing.T) { + type testConfig struct { + LabelSet LabelSet `yaml:"labelSet,omitempty"` + } + + // valid LabelSet JSON + labelSetJSON := `{ + "labelSet": { + "monitor": "codelab", + "foo": "bar", + "foo2": "bar", + "abc": "prometheus", + "foo11": "bar11" + } +}` + var c testConfig + err := json.Unmarshal([]byte(labelSetJSON), &c) + if err != nil { + t.Errorf("unexpected error while marshalling JSON : %s", err.Error()) + } + + labelSetString := c.LabelSet.String() + + expected := `{abc="prometheus", foo="bar", foo11="bar11", foo2="bar", monitor="codelab"}` + + if expected != labelSetString { + t.Errorf("expected %s but got %s", expected, labelSetString) + } + + // invalid LabelSet JSON + invalidlabelSetJSON := `{ + "labelSet": { + "1nvalid_23name": "codelab", + "foo": "bar" + } +}` + + NameValidationScheme = LegacyValidation + err = json.Unmarshal([]byte(invalidlabelSetJSON), &c) + expectedErr := `"1nvalid_23name" is not a valid label name` + if err == nil || err.Error() != expectedErr { + t.Errorf("expected an error with message '%s' to be thrown", expectedErr) + } +} + +func TestLabelSetClone(t *testing.T) { + labelSet := LabelSet{ + "monitor": "codelab", + "foo": "bar", + "bar": "baz", + } + + cloneSet := labelSet.Clone() + + if len(labelSet) != len(cloneSet) { + t.Errorf("expected the length of the cloned Label set to be %d, but got %d", + len(labelSet), len(cloneSet)) + } + + for ln, lv := range labelSet { + expected := cloneSet[ln] + if expected != lv { + t.Errorf("expected to get LabelValue %s, but got %s for LabelName %s", expected, lv, ln) + } + } +} + +func TestLabelSetMerge(t *testing.T) { + labelSet := LabelSet{ + "monitor": "codelab", + "foo": "bar", + "bar": "baz", + } + + labelSet2 := LabelSet{ + "monitor": "codelab", + "dolor": "mi", + "lorem": "ipsum", + } + + expectedSet := LabelSet{ + "monitor": "codelab", + "foo": "bar", + "bar": "baz", + "dolor": "mi", + "lorem": "ipsum", + } + + mergedSet := labelSet.Merge(labelSet2) + + if len(mergedSet) != len(expectedSet) { + t.Errorf("expected the length of the cloned Label set to be %d, but got %d", + len(expectedSet), len(mergedSet)) + } + + for ln, lv := range mergedSet { + expected := expectedSet[ln] + if expected != lv { + t.Errorf("expected to get LabelValue %s, but got %s for LabelName %s", expected, lv, ln) + } + } +} + +// Benchmark Results for LabelSet's String() method +// --------------------------------------------------------------------------------------------------------- +// goos: linux +// goarch: amd64 +// pkg: github.com/prometheus/common/model +// cpu: 11th Gen Intel(R) Core(TM) i5-1145G7 @ 2.60GHz +// BenchmarkLabelSetStringMethod-8 732376 1532 ns/op + +func BenchmarkLabelSetStringMethod(b *testing.B) { + ls := make(LabelSet) + ls["monitor"] = "codelab" + ls["foo2"] = "bar" + ls["foo"] = "bar" + ls["abc"] = "prometheus" + ls["foo11"] = "bar11" + for i := 0; i < b.N; i++ { + _ = ls.String() + } +} diff --git a/model/labelset_string.go b/model/labelset_string.go new file mode 100644 index 00000000..174753ed --- /dev/null +++ b/model/labelset_string.go @@ -0,0 +1,45 @@ +// Copyright 2024 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build go1.21 + +package model + +import ( + "bytes" + "sort" + "strconv" +) + +// String will look like `{foo="bar", more="less"}`. Names are sorted alphabetically. +func (l LabelSet) String() string { + var lna [32]string // On stack to avoid memory allocation for sorting names. + labelNames := lna[:0] + for name := range l { + labelNames = append(labelNames, string(name)) + } + sort.Strings(labelNames) + var bytea [1024]byte // On stack to avoid memory allocation while building the output. + b := bytes.NewBuffer(bytea[:0]) + b.WriteByte('{') + for i, name := range labelNames { + if i > 0 { + b.WriteString(", ") + } + b.WriteString(name) + b.WriteByte('=') + b.Write(strconv.AppendQuote(b.AvailableBuffer(), string(l[LabelName(name)]))) + } + b.WriteByte('}') + return b.String() +} diff --git a/model/labelset_string_go120.go b/model/labelset_string_go120.go new file mode 100644 index 00000000..c4212685 --- /dev/null +++ b/model/labelset_string_go120.go @@ -0,0 +1,39 @@ +// Copyright 2024 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build !go1.21 + +package model + +import ( + "fmt" + "sort" + "strings" +) + +// String was optimized using functions not available for go 1.20 +// or lower. We keep the old implementation for compatibility with client_golang. +// Once client golang drops support for go 1.20 (scheduled for August 2024), this +// file can be removed. +func (l LabelSet) String() string { + labelNames := make([]string, 0, len(l)) + for name := range l { + labelNames = append(labelNames, string(name)) + } + sort.Strings(labelNames) + lstrs := make([]string, 0, len(l)) + for _, name := range labelNames { + lstrs = append(lstrs, fmt.Sprintf("%s=%q", name, l[LabelName(name)])) + } + return fmt.Sprintf("{%s}", strings.Join(lstrs, ", ")) +} diff --git a/model/labelset_test.go b/model/labelset_test.go index 3f8824d6..785886ce 100644 --- a/model/labelset_test.go +++ b/model/labelset_test.go @@ -11,6 +11,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//go:build go1.21 + package model import ( diff --git a/sigv4/go.mod b/sigv4/go.mod index cf23cdfb..14112644 100644 --- a/sigv4/go.mod +++ b/sigv4/go.mod @@ -1,6 +1,6 @@ module github.com/prometheus/common/sigv4 -go 1.21 +go 1.20 replace github.com/prometheus/common => ../ diff --git a/sigv4/go.sum b/sigv4/go.sum index 5f12db28..0ad7a43e 100644 --- a/sigv4/go.sum +++ b/sigv4/go.sum @@ -14,7 +14,6 @@ github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= -github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= @@ -22,7 +21,6 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfC github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= -github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= 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 github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo= github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= -github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= 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 google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= -gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=