Skip to content

Commit c576b95

Browse files
authored
Generate new Go runtime metrics for go 1.19 (#1105)
* Generate new Go runtime metrics Fix generation script Signed-off-by: Kemal Akkoyun <[email protected]> * Address review issues Signed-off-by: Kemal Akkoyun <[email protected]>
1 parent 618194d commit c576b95

File tree

6 files changed

+87
-26
lines changed

6 files changed

+87
-26
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- name: install Go
2121
uses: actions/setup-go@v2
2222
with:
23-
go-version: 1.18.x
23+
go-version-file: .go-version
2424
- name: Install snmp_exporter/generator dependencies
2525
run: sudo apt-get update && sudo apt-get -y install libsnmp-dev
2626
if: github.repository == 'prometheus/snmp_exporter'

.go-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.18

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,11 @@ test: deps common-test
1818

1919
.PHONY: test-short
2020
test-short: deps common-test-short
21+
22+
.PHONY: generate-go-collector-test-files
23+
VERSIONS := 1.17 1.18 1.19
24+
generate-go-collector-test-files:
25+
for GO_VERSION in $(VERSIONS); do \
26+
docker run --rm -v $(PWD):/workspace -w /workspace golang:$$GO_VERSION go run prometheus/gen_go_collector_metrics_set.go; \
27+
mv -f go_collector_metrics* prometheus; \
28+
done

prometheus/gen_go_collector_metrics_set.go

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,42 @@ import (
2525
"os"
2626
"runtime"
2727
"runtime/metrics"
28-
"strconv"
2928
"strings"
3029
"text/template"
3130

3231
"github.com/prometheus/client_golang/prometheus"
3332
"github.com/prometheus/client_golang/prometheus/internal"
33+
34+
"github.com/hashicorp/go-version"
3435
)
3536

3637
func main() {
38+
var givenVersion string
39+
toolVersion := runtime.Version()
3740
if len(os.Args) != 2 {
38-
log.Fatal("requires Go version (e.g. go1.17) as an argument")
41+
log.Printf("requires Go version (e.g. go1.17) as an argument. Since it is not specified, assuming %s.", toolVersion)
42+
givenVersion = toolVersion
43+
} else {
44+
givenVersion = os.Args[1]
3945
}
40-
toolVersion := runtime.Version()
41-
mtv := majorVersion(toolVersion)
42-
mv := majorVersion(os.Args[1])
43-
if mtv != mv {
44-
log.Fatalf("using Go version %q but expected Go version %q", mtv, mv)
46+
log.Printf("given version for Go: %s", givenVersion)
47+
log.Printf("tool version for Go: %s", toolVersion)
48+
49+
tv, err := version.NewVersion(strings.TrimPrefix(givenVersion, "go"))
50+
if err != nil {
51+
log.Fatal(err)
4552
}
46-
version, err := parseVersion(mv)
53+
gv, err := version.NewVersion(strings.TrimPrefix(toolVersion, "go"))
4754
if err != nil {
48-
log.Fatalf("parsing Go version: %v", err)
55+
log.Fatal(err)
56+
}
57+
if !gv.Equal(tv) {
58+
log.Fatalf("using Go version %q but expected Go version %q", tv, gv)
4959
}
5060

61+
v := goVersion(gv.Segments()[1])
62+
log.Printf("generating metrics for Go version %q", v)
63+
5164
// Generate code.
5265
var buf bytes.Buffer
5366
err = testFile.Execute(&buf, struct {
@@ -56,7 +69,7 @@ func main() {
5669
Cardinality int
5770
}{
5871
Descriptions: metrics.All(),
59-
GoVersion: version,
72+
GoVersion: v,
6073
Cardinality: rmCardinality(),
6174
})
6275
if err != nil {
@@ -70,7 +83,7 @@ func main() {
7083
}
7184

7285
// Write it to a file.
73-
fname := fmt.Sprintf("go_collector_metrics_%s_test.go", version.Abbr())
86+
fname := fmt.Sprintf("go_collector_metrics_%s_test.go", v.Abbr())
7487
if err := os.WriteFile(fname, result, 0o644); err != nil {
7588
log.Fatalf("writing file: %v", err)
7689
}
@@ -86,19 +99,6 @@ func (g goVersion) Abbr() string {
8699
return fmt.Sprintf("go1%d", g)
87100
}
88101

89-
func parseVersion(s string) (goVersion, error) {
90-
i := strings.IndexRune(s, '.')
91-
if i < 0 {
92-
return goVersion(-1), fmt.Errorf("bad Go version format")
93-
}
94-
i, err := strconv.Atoi(s[i+1:])
95-
return goVersion(i), err
96-
}
97-
98-
func majorVersion(v string) string {
99-
return v[:strings.LastIndexByte(v, '.')]
100-
}
101-
102102
func rmCardinality() int {
103103
cardinality := 0
104104

@@ -123,6 +123,7 @@ func rmCardinality() int {
123123
name[strings.IndexRune(name, ':')+1:],
124124
)
125125
cardinality += len(buckets) + 3 // Plus total count, sum, and the implicit infinity bucket.
126+
126127
// runtime/metrics bucket boundaries are lower-bound-inclusive, but
127128
// always represents each actual *boundary* so Buckets is always
128129
// 1 longer than Counts, while in Prometheus the mapping is one-to-one,
@@ -134,6 +135,12 @@ func rmCardinality() int {
134135
// We already counted the infinity bucket separately.
135136
cardinality--
136137
}
138+
// Prometheus also doesn't have buckets for -Inf, so they need to be omitted.
139+
// See the following PR for more information:
140+
// https://github.com/prometheus/client_golang/pull/1049
141+
if buckets[0] == math.Inf(-1) {
142+
cardinality--
143+
}
137144
}
138145

139146
return cardinality

prometheus/go_collector_latest_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func TestMemStatsEquivalence(t *testing.T) {
269269
}
270270

271271
func TestExpectedRuntimeMetrics(t *testing.T) {
272-
goMetrics := collectGoMetrics(t, goRuntimeMetricsCollection)
272+
goMetrics := collectGoMetrics(t, goRuntimeMemStatsCollection|goRuntimeMetricsCollection)
273273
goMetricSet := make(map[string]Metric)
274274
for _, m := range goMetrics {
275275
goMetricSet[m.Desc().fqName] = m

prometheus/go_collector_metrics_go119_test.go

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)