@@ -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
3637func 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-
102102func 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
0 commit comments