Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate new Go runtime metrics for go 1.19 #1105

Merged
merged 2 commits into from Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: install Go
uses: actions/setup-go@v2
with:
go-version: 1.18.x
go-version-file: .go-version
- name: Install snmp_exporter/generator dependencies
run: sudo apt-get update && sudo apt-get -y install libsnmp-dev
if: github.repository == 'prometheus/snmp_exporter'
Expand Down
1 change: 1 addition & 0 deletions .go-version
@@ -0,0 +1 @@
1.18
8 changes: 8 additions & 0 deletions Makefile
Expand Up @@ -18,3 +18,11 @@ test: deps common-test

.PHONY: test-short
test-short: deps common-test-short

.PHONY: generate-go-collector-test-files
VERSIONS := 1.17 1.18 1.19
generate-go-collector-test-files:
for GO_VERSION in $(VERSIONS); do \
docker run --rm -v $(PWD):/workspace -w /workspace golang:$$GO_VERSION go run prometheus/gen_go_collector_metrics_set.go; \
mv -f go_collector_metrics* prometheus; \
done
55 changes: 31 additions & 24 deletions prometheus/gen_go_collector_metrics_set.go
Expand Up @@ -25,29 +25,42 @@ import (
"os"
"runtime"
"runtime/metrics"
"strconv"
"strings"
"text/template"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/internal"

"github.com/hashicorp/go-version"
)

func main() {
var givenVersion string
toolVersion := runtime.Version()
if len(os.Args) != 2 {
log.Fatal("requires Go version (e.g. go1.17) as an argument")
log.Printf("requires Go version (e.g. go1.17) as an argument. Since it is not specified, assuming %s.", toolVersion)
givenVersion = toolVersion
} else {
givenVersion = os.Args[1]
}
toolVersion := runtime.Version()
mtv := majorVersion(toolVersion)
mv := majorVersion(os.Args[1])
if mtv != mv {
log.Fatalf("using Go version %q but expected Go version %q", mtv, mv)
log.Printf("given version for Go: %s", givenVersion)
log.Printf("tool version for Go: %s", toolVersion)

tv, err := version.NewVersion(strings.TrimPrefix(givenVersion, "go"))
if err != nil {
log.Fatal(err)
}
version, err := parseVersion(mv)
gv, err := version.NewVersion(strings.TrimPrefix(toolVersion, "go"))
if err != nil {
log.Fatalf("parsing Go version: %v", err)
log.Fatal(err)
}
if !gv.Equal(tv) {
log.Fatalf("using Go version %q but expected Go version %q", tv, gv)
}

v := goVersion(gv.Segments()[1])
log.Printf("generating metrics for Go version %q", v)

// Generate code.
var buf bytes.Buffer
err = testFile.Execute(&buf, struct {
Expand All @@ -56,7 +69,7 @@ func main() {
Cardinality int
}{
Descriptions: metrics.All(),
GoVersion: version,
GoVersion: v,
Cardinality: rmCardinality(),
})
if err != nil {
Expand All @@ -70,7 +83,7 @@ func main() {
}

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

func parseVersion(s string) (goVersion, error) {
i := strings.IndexRune(s, '.')
if i < 0 {
return goVersion(-1), fmt.Errorf("bad Go version format")
}
i, err := strconv.Atoi(s[i+1:])
return goVersion(i), err
}

func majorVersion(v string) string {
return v[:strings.LastIndexByte(v, '.')]
}

func rmCardinality() int {
cardinality := 0

Expand All @@ -123,6 +123,7 @@ func rmCardinality() int {
name[strings.IndexRune(name, ':')+1:],
)
cardinality += len(buckets) + 3 // Plus total count, sum, and the implicit infinity bucket.

// runtime/metrics bucket boundaries are lower-bound-inclusive, but
// always represents each actual *boundary* so Buckets is always
// 1 longer than Counts, while in Prometheus the mapping is one-to-one,
Expand All @@ -134,6 +135,12 @@ func rmCardinality() int {
// We already counted the infinity bucket separately.
cardinality--
}
// Prometheus also doesn't have buckets for -Inf, so they need to be omitted.
// See the following PR for more information:
// https://github.com/prometheus/client_golang/pull/1049
if buckets[0] == math.Inf(-1) {
cardinality--
}
}

return cardinality
Expand Down
2 changes: 1 addition & 1 deletion prometheus/go_collector_latest_test.go
Expand Up @@ -269,7 +269,7 @@ func TestMemStatsEquivalence(t *testing.T) {
}

func TestExpectedRuntimeMetrics(t *testing.T) {
goMetrics := collectGoMetrics(t, goRuntimeMetricsCollection)
goMetrics := collectGoMetrics(t, goRuntimeMemStatsCollection|goRuntimeMetricsCollection)
goMetricSet := make(map[string]Metric)
for _, m := range goMetrics {
goMetricSet[m.Desc().fqName] = m
Expand Down
45 changes: 45 additions & 0 deletions prometheus/go_collector_metrics_go119_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.