Skip to content

Commit

Permalink
Generate new Go runtime metrics
Browse files Browse the repository at this point in the history
Fix generation script

Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
  • Loading branch information
kakkoyun committed Aug 5, 2022
1 parent 618194d commit 6591fa2
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 26 deletions.
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
54 changes: 30 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,11 @@ func rmCardinality() int {
// We already counted the infinity bucket separately.
cardinality--
}
// https://github.com/prometheus/client_golang/pull/1049/files
if buckets[0] == math.Inf(-1) {
// We already counted the infinity bucket separately.
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.

0 comments on commit 6591fa2

Please sign in to comment.