| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package main |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"fmt" |
| 22 |
"go/format" |
| 23 |
"log" |
| 24 |
"math" |
| 25 |
"os" |
| 26 |
"runtime" |
| 27 |
"runtime/metrics" |
| 28 |
"strings" |
| 29 |
"text/template" |
| 30 |
|
| 31 |
"github.com/prometheus/client_golang/prometheus" |
| 32 |
"github.com/prometheus/client_golang/prometheus/internal" |
| 33 |
|
| 34 |
version "github.com/hashicorp/go-version" |
| 35 |
) |
| 36 |
|
| 37 |
func main() { |
| 38 |
var givenVersion string |
| 39 |
toolVersion := runtime.Version() |
| 40 |
if len(os.Args) != 2 { |
| 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] |
| 45 |
} |
| 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) |
| 52 |
} |
| 53 |
|
| 54 |
toolVersion = strings.Split(strings.TrimPrefix(toolVersion, "go"), " ")[0] |
| 55 |
gv, err := version.NewVersion(toolVersion) |
| 56 |
if err != nil { |
| 57 |
log.Fatal(err) |
| 58 |
} |
| 59 |
if !gv.Equal(tv) { |
| 60 |
log.Fatalf("using Go version %q but expected Go version %q", tv, gv) |
| 61 |
} |
| 62 |
|
| 63 |
v := goVersion(gv.Segments()[1]) |
| 64 |
log.Printf("generating metrics for Go version %q", v) |
| 65 |
|
| 66 |
allDesc := metrics.All() |
| 67 |
|
| 68 |
|
| 69 |
var defaultDesc []metrics.Description |
| 70 |
for _, d := range allDesc { |
| 71 |
if !internal.GoCollectorDefaultRuntimeMetrics.MatchString(d.Name) { |
| 72 |
continue |
| 73 |
} |
| 74 |
defaultDesc = append(defaultDesc, d) |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
var buf bytes.Buffer |
| 79 |
err = testFile.Execute(&buf, struct { |
| 80 |
AllDescriptions []metrics.Description |
| 81 |
DefaultDescriptions []metrics.Description |
| 82 |
GoVersion goVersion |
| 83 |
Cardinality int |
| 84 |
}{ |
| 85 |
AllDescriptions: allDesc, |
| 86 |
DefaultDescriptions: defaultDesc, |
| 87 |
GoVersion: v, |
| 88 |
Cardinality: rmCardinality(), |
| 89 |
}) |
| 90 |
if err != nil { |
| 91 |
log.Fatalf("executing template: %v", err) |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
result, err := format.Source(buf.Bytes()) |
| 96 |
if err != nil { |
| 97 |
log.Fatalf("formatting code: %v", err) |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
fname := fmt.Sprintf("go_collector_metrics_%s_test.go", v.Abbr()) |
| 102 |
if err := os.WriteFile(fname, result, 0o644); err != nil { |
| 103 |
log.Fatalf("writing file: %v", err) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
type goVersion int |
| 108 |
|
| 109 |
func (g goVersion) String() string { |
| 110 |
return fmt.Sprintf("go1.%d", g) |
| 111 |
} |
| 112 |
|
| 113 |
func (g goVersion) Abbr() string { |
| 114 |
return fmt.Sprintf("go1%d", g) |
| 115 |
} |
| 116 |
|
| 117 |
func rmCardinality() int { |
| 118 |
cardinality := 0 |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
var histograms []metrics.Sample |
| 124 |
for _, d := range metrics.All() { |
| 125 |
if d.Kind == metrics.KindFloat64Histogram { |
| 126 |
histograms = append(histograms, metrics.Sample{Name: d.Name}) |
| 127 |
} else { |
| 128 |
cardinality++ |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
metrics.Read(histograms) |
| 134 |
for i := range histograms { |
| 135 |
name := histograms[i].Name |
| 136 |
buckets := internal.RuntimeMetricsBucketsForUnit( |
| 137 |
histograms[i].Value.Float64Histogram().Buckets, |
| 138 |
name[strings.IndexRune(name, ':')+1:], |
| 139 |
) |
| 140 |
cardinality += len(buckets) + 3 |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
cardinality-- |
| 149 |
if buckets[len(buckets)-1] == math.Inf(1) { |
| 150 |
|
| 151 |
cardinality-- |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
if buckets[0] == math.Inf(-1) { |
| 157 |
cardinality-- |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
return cardinality |
| 162 |
} |
| 163 |
|
| 164 |
var testFile = template.Must(template.New("testFile").Funcs(map[string]interface{}{ |
| 165 |
"rm2prom": func(d metrics.Description) string { |
| 166 |
ns, ss, n, ok := internal.RuntimeMetricsToProm(&d) |
| 167 |
if !ok { |
| 168 |
return "" |
| 169 |
} |
| 170 |
return prometheus.BuildFQName(ns, ss, n) |
| 171 |
}, |
| 172 |
"nextVersion": func(version goVersion) string { |
| 173 |
return (version + goVersion(1)).String() |
| 174 |
}, |
| 175 |
}).Parse(`// Code generated by gen_go_collector_metrics_set.go; DO NOT EDIT. |
| 176 |
//go:generate go run gen_go_collector_metrics_set.go {{.GoVersion}} |
| 177 |
|
| 178 |
//go:build {{.GoVersion}} && !{{nextVersion .GoVersion}} |
| 179 |
// +build {{.GoVersion}},!{{nextVersion .GoVersion}} |
| 180 |
|
| 181 |
package prometheus |
| 182 |
|
| 183 |
var ( |
| 184 |
expectedRuntimeMetrics = map[string]string{ |
| 185 |
{{- range .AllDescriptions -}} |
| 186 |
{{- $trans := rm2prom . -}} |
| 187 |
{{- if ne $trans "" }} |
| 188 |
{{.Name | printf "%q"}}: {{$trans | printf "%q"}}, |
| 189 |
{{- end -}} |
| 190 |
{{end}} |
| 191 |
} |
| 192 |
|
| 193 |
expMetrics = map[string]string{ |
| 194 |
{{- range .DefaultDescriptions -}} |
| 195 |
{{- $trans := rm2prom . -}} |
| 196 |
{{- if ne $trans "" }} |
| 197 |
{{.Name | printf "%q"}}: {{$trans | printf "%q"}}, |
| 198 |
{{- end -}} |
| 199 |
{{end}} |
| 200 |
} |
| 201 |
) |
| 202 |
|
| 203 |
const expectedRuntimeMetricsCardinality = {{.Cardinality}} |
| 204 |
`)) |