| 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 |
"os" |
| 25 |
"regexp" |
| 26 |
"runtime" |
| 27 |
"runtime/metrics" |
| 28 |
"sort" |
| 29 |
"strings" |
| 30 |
"text/template" |
| 31 |
|
| 32 |
"github.com/prometheus/client_golang/prometheus" |
| 33 |
"github.com/prometheus/client_golang/prometheus/internal" |
| 34 |
|
| 35 |
version "github.com/hashicorp/go-version" |
| 36 |
) |
| 37 |
|
| 38 |
type metricGroup struct { |
| 39 |
Name string |
| 40 |
Regex *regexp.Regexp |
| 41 |
Metrics []string |
| 42 |
} |
| 43 |
|
| 44 |
var metricGroups = []metricGroup{ |
| 45 |
{"withAllMetrics", nil, nil}, |
| 46 |
{"withGCMetrics", regexp.MustCompile("^go_gc_.*"), nil}, |
| 47 |
{"withMemoryMetrics", regexp.MustCompile("^go_memory_classes_.*"), nil}, |
| 48 |
{"withSchedulerMetrics", regexp.MustCompile("^go_sched_.*"), nil}, |
| 49 |
{"withDebugMetrics", regexp.MustCompile("^go_godebug_non_default_behavior_.*"), nil}, |
| 50 |
} |
| 51 |
|
| 52 |
func main() { |
| 53 |
var givenVersion string |
| 54 |
toolVersion := runtime.Version() |
| 55 |
if len(os.Args) != 2 { |
| 56 |
log.Printf("requires Go version (e.g. go1.17) as an argument. Since it is not specified, assuming %s.", toolVersion) |
| 57 |
givenVersion = toolVersion |
| 58 |
} else { |
| 59 |
givenVersion = os.Args[1] |
| 60 |
} |
| 61 |
log.Printf("given version for Go: %s", givenVersion) |
| 62 |
log.Printf("tool version for Go: %s", toolVersion) |
| 63 |
|
| 64 |
tv, err := version.NewVersion(strings.TrimPrefix(givenVersion, "go")) |
| 65 |
if err != nil { |
| 66 |
log.Fatal(err) |
| 67 |
} |
| 68 |
|
| 69 |
toolVersion = strings.Split(strings.TrimPrefix(toolVersion, "go"), " ")[0] |
| 70 |
gv, err := version.NewVersion(toolVersion) |
| 71 |
if err != nil { |
| 72 |
log.Fatal(err) |
| 73 |
} |
| 74 |
if !gv.Equal(tv) { |
| 75 |
log.Fatalf("using Go version %q but expected Go version %q", tv, gv) |
| 76 |
} |
| 77 |
|
| 78 |
v := goVersion(gv.Segments()[1]) |
| 79 |
log.Printf("generating metrics for Go version %q", v) |
| 80 |
|
| 81 |
descriptions := computeMetricsList(metrics.All()) |
| 82 |
groupedMetrics := groupMetrics(descriptions) |
| 83 |
|
| 84 |
// Find default metrics. |
| 85 |
var defaultRuntimeDesc []metrics.Description |
| 86 |
for _, d := range metrics.All() { |
| 87 |
if !internal.GoCollectorDefaultRuntimeMetrics.MatchString(d.Name) { |
| 88 |
continue |
| 89 |
} |
| 90 |
defaultRuntimeDesc = append(defaultRuntimeDesc, d) |
| 91 |
} |
| 92 |
|
| 93 |
defaultRuntimeMetricsList := computeMetricsList(defaultRuntimeDesc) |
| 94 |
|
| 95 |
onlyGCDefRuntimeMetricsList := []string{} |
| 96 |
onlySchedDefRuntimeMetricsList := []string{} |
| 97 |
|
| 98 |
for _, m := range defaultRuntimeMetricsList { |
| 99 |
if strings.HasPrefix(m, "go_gc") { |
| 100 |
onlyGCDefRuntimeMetricsList = append(onlyGCDefRuntimeMetricsList, m) |
| 101 |
} |
| 102 |
if strings.HasPrefix(m, "go_sched") { |
| 103 |
onlySchedDefRuntimeMetricsList = append(onlySchedDefRuntimeMetricsList, m) |
| 104 |
} else { |
| 105 |
continue |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// Generate code. |
| 110 |
var buf bytes.Buffer |
| 111 |
err = testFile.Execute(&buf, struct { |
| 112 |
GoVersion goVersion |
| 113 |
Groups []metricGroup |
| 114 |
DefaultRuntimeMetricsList []string |
| 115 |
OnlyGCDefRuntimeMetricsList []string |
| 116 |
OnlySchedDefRuntimeMetricsList []string |
| 117 |
}{ |
| 118 |
GoVersion: v, |
| 119 |
Groups: groupedMetrics, |
| 120 |
DefaultRuntimeMetricsList: defaultRuntimeMetricsList, |
| 121 |
OnlyGCDefRuntimeMetricsList: onlyGCDefRuntimeMetricsList, |
| 122 |
OnlySchedDefRuntimeMetricsList: onlySchedDefRuntimeMetricsList, |
| 123 |
}) |
| 124 |
if err != nil { |
| 125 |
log.Fatalf("executing template: %v", err) |
| 126 |
} |
| 127 |
|
| 128 |
// Format it. |
| 129 |
result, err := format.Source(buf.Bytes()) |
| 130 |
if err != nil { |
| 131 |
log.Fatalf("formatting code: %v", err) |
| 132 |
} |
| 133 |
|
| 134 |
// Write it to a file. |
| 135 |
fname := fmt.Sprintf("go_collector_%s_test.go", v.Abbr()) |
| 136 |
if err := os.WriteFile(fname, result, 0o644); err != nil { |
| 137 |
log.Fatalf("writing file: %v", err) |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
func computeMetricsList(descs []metrics.Description) []string { |
| 142 |
var metricsList []string |
| 143 |
for _, d := range descs { |
| 144 |
if trans := rm2prom(d); trans != "" { |
| 145 |
metricsList = append(metricsList, trans) |
| 146 |
} |
| 147 |
} |
| 148 |
return metricsList |
| 149 |
} |
| 150 |
|
| 151 |
func rm2prom(d metrics.Description) string { |
| 152 |
ns, ss, n, ok := internal.RuntimeMetricsToProm(&d) |
| 153 |
if !ok { |
| 154 |
return "" |
| 155 |
} |
| 156 |
return prometheus.BuildFQName(ns, ss, n) |
| 157 |
} |
| 158 |
|
| 159 |
func groupMetrics(metricsList []string) []metricGroup { |
| 160 |
var groupedMetrics []metricGroup |
| 161 |
for _, group := range metricGroups { |
| 162 |
matchedMetrics := make([]string, 0) |
| 163 |
for _, metric := range metricsList { |
| 164 |
if group.Regex == nil || group.Regex.MatchString(metric) { |
| 165 |
matchedMetrics = append(matchedMetrics, metric) |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
sort.Strings(matchedMetrics) |
| 170 |
groupedMetrics = append(groupedMetrics, metricGroup{ |
| 171 |
Name: group.Name, |
| 172 |
Regex: group.Regex, |
| 173 |
Metrics: matchedMetrics, |
| 174 |
}) |
| 175 |
} |
| 176 |
return groupedMetrics |
| 177 |
} |
| 178 |
|
| 179 |
type goVersion int |
| 180 |
|
| 181 |
func (g goVersion) String() string { |
| 182 |
return fmt.Sprintf("go1.%d", g) |
| 183 |
} |
| 184 |
|
| 185 |
func (g goVersion) Abbr() string { |
| 186 |
return fmt.Sprintf("go1%d", g) |
| 187 |
} |
| 188 |
|
| 189 |
var testFile = template.Must(template.New("testFile").Funcs(map[string]interface{}{ |
| 190 |
"nextVersion": func(version goVersion) string { |
| 191 |
return (version + goVersion(1)).String() |
| 192 |
}, |
| 193 |
}).Parse(`// Copyright 2022 The Prometheus Authors |
| 194 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 195 |
// you may not use this file except in compliance with the License. |
| 196 |
// You may obtain a copy of the License at |
| 197 |
// |
| 198 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 199 |
// |
| 200 |
// Unless required by applicable law or agreed to in writing, software |
| 201 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 202 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 203 |
// See the License for the specific language governing permissions and |
| 204 |
// limitations under the License. |
| 205 |
|
| 206 |
//go:build {{.GoVersion}} && !{{nextVersion .GoVersion}} |
| 207 |
// +build {{.GoVersion}},!{{nextVersion .GoVersion}} |
| 208 |
|
| 209 |
package collectors |
| 210 |
|
| 211 |
{{- range .Groups }} |
| 212 |
func {{ .Name }}() []string { |
| 213 |
return withBaseMetrics([]string{ |
| 214 |
{{- range $metric := .Metrics }} |
| 215 |
{{ $metric | printf "%q" }}, |
| 216 |
{{- end }} |
| 217 |
}) |
| 218 |
} |
| 219 |
{{ end }} |
| 220 |
|
| 221 |
var ( |
| 222 |
defaultRuntimeMetrics = []string{ |
| 223 |
{{- range $metric := .DefaultRuntimeMetricsList }} |
| 224 |
{{ $metric | printf "%q"}}, |
| 225 |
{{- end }} |
| 226 |
} |
| 227 |
onlyGCDefRuntimeMetrics = []string{ |
| 228 |
{{- range $metric := .OnlyGCDefRuntimeMetricsList }} |
| 229 |
{{ $metric | printf "%q"}}, |
| 230 |
{{- end }} |
| 231 |
} |
| 232 |
onlySchedDefRuntimeMetrics = []string{ |
| 233 |
{{- range $metric := .OnlySchedDefRuntimeMetricsList }} |
| 234 |
{{ $metric | printf "%q"}}, |
| 235 |
{{- end }} |
| 236 |
} |
| 237 |
) |
| 238 |
`)) |