| 1 |
// Copyright 2021 The Prometheus Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
//go:build ignore |
| 15 |
// +build ignore |
| 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 |
// Find default metrics. |
| 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 |
// Generate code. |
| 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 |
// Format it. |
| 95 |
result, err := format.Source(buf.Bytes()) |
| 96 |
if err != nil { |
| 97 |
log.Fatalf("formatting code: %v", err) |
| 98 |
} |
| 99 |
|
| 100 |
// Write it to a file. |
| 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 |
// Collect all histogram samples so that we can get their buckets. |
| 121 |
// The API guarantees that the buckets are always fixed for the lifetime |
| 122 |
// of the process. |
| 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 |
// Handle histograms. |
| 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 // Plus total count, sum, and the implicit infinity bucket. |
| 141 |
|
| 142 |
// runtime/metrics bucket boundaries are lower-bound-inclusive, but |
| 143 |
// always represents each actual *boundary* so Buckets is always |
| 144 |
// 1 longer than Counts, while in Prometheus the mapping is one-to-one, |
| 145 |
// as the bottom bucket extends to -Inf, and the top infinity bucket is |
| 146 |
// implicit. Therefore, we should have one fewer bucket than is listed |
| 147 |
// above. |
| 148 |
cardinality-- |
| 149 |
if buckets[len(buckets)-1] == math.Inf(1) { |
| 150 |
// We already counted the infinity bucket separately. |
| 151 |
cardinality-- |
| 152 |
} |
| 153 |
// Prometheus also doesn't have buckets for -Inf, so they need to be omitted. |
| 154 |
// See the following PR for more information: |
| 155 |
// https://github.com/prometheus/client_golang/pull/1049 |
| 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 |
`)) |