|
| 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() |
| 82 | + groupedMetrics := groupMetrics(descriptions) |
| 83 | + |
| 84 | + // Generate code. |
| 85 | + var buf bytes.Buffer |
| 86 | + err = testFile.Execute(&buf, struct { |
| 87 | + GoVersion goVersion |
| 88 | + Groups []metricGroup |
| 89 | + }{ |
| 90 | + GoVersion: v, |
| 91 | + Groups: groupedMetrics, |
| 92 | + }) |
| 93 | + if err != nil { |
| 94 | + log.Fatalf("executing template: %v", err) |
| 95 | + } |
| 96 | + |
| 97 | + // Format it. |
| 98 | + result, err := format.Source(buf.Bytes()) |
| 99 | + if err != nil { |
| 100 | + log.Fatalf("formatting code: %v", err) |
| 101 | + } |
| 102 | + |
| 103 | + // Write it to a file. |
| 104 | + fname := fmt.Sprintf("go_collector_%s_test.go", v.Abbr()) |
| 105 | + if err := os.WriteFile(fname, result, 0o644); err != nil { |
| 106 | + log.Fatalf("writing file: %v", err) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func computeMetricsList() []string { |
| 111 | + var metricsList []string |
| 112 | + for _, d := range metrics.All() { |
| 113 | + if trans := rm2prom(d); trans != "" { |
| 114 | + metricsList = append(metricsList, trans) |
| 115 | + } |
| 116 | + } |
| 117 | + return metricsList |
| 118 | +} |
| 119 | + |
| 120 | +func rm2prom(d metrics.Description) string { |
| 121 | + ns, ss, n, ok := internal.RuntimeMetricsToProm(&d) |
| 122 | + if !ok { |
| 123 | + return "" |
| 124 | + } |
| 125 | + return prometheus.BuildFQName(ns, ss, n) |
| 126 | +} |
| 127 | + |
| 128 | +func groupMetrics(metricsList []string) []metricGroup { |
| 129 | + var groupedMetrics []metricGroup |
| 130 | + for _, group := range metricGroups { |
| 131 | + var matchedMetrics []string |
| 132 | + for _, metric := range metricsList { |
| 133 | + if group.Regex == nil || group.Regex.MatchString(metric) { |
| 134 | + matchedMetrics = append(matchedMetrics, metric) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // Scheduler metrics is `sched` regex plus base metrics |
| 139 | + // List of base metrics are taken from here: https://github.com/prometheus/client_golang/blob/26e3055e5133a9d64e8e5a07a7cf026875d5f55d/prometheus/go_collector.go#L208 |
| 140 | + if group.Name == "withSchedulerMetrics" { |
| 141 | + baseMatrices := []string{ |
| 142 | + "go_gc_duration_seconds", |
| 143 | + "go_goroutines", |
| 144 | + "go_info", |
| 145 | + "go_memstats_last_gc_time_seconds", |
| 146 | + "go_threads", |
| 147 | + } |
| 148 | + matchedMetrics = append(matchedMetrics, baseMatrices...) |
| 149 | + } |
| 150 | + sort.Strings(matchedMetrics) |
| 151 | + if len(matchedMetrics) > 0 { |
| 152 | + groupedMetrics = append(groupedMetrics, metricGroup{ |
| 153 | + Name: group.Name, |
| 154 | + Regex: group.Regex, |
| 155 | + Metrics: matchedMetrics, |
| 156 | + }) |
| 157 | + } |
| 158 | + } |
| 159 | + return groupedMetrics |
| 160 | +} |
| 161 | + |
| 162 | +type goVersion int |
| 163 | + |
| 164 | +func (g goVersion) String() string { |
| 165 | + return fmt.Sprintf("go1.%d", g) |
| 166 | +} |
| 167 | + |
| 168 | +func (g goVersion) Abbr() string { |
| 169 | + return fmt.Sprintf("go1%d", g) |
| 170 | +} |
| 171 | + |
| 172 | +var testFile = template.Must(template.New("testFile").Funcs(map[string]interface{}{ |
| 173 | + "nextVersion": func(version goVersion) string { |
| 174 | + return (version + goVersion(1)).String() |
| 175 | + }, |
| 176 | + "needsBaseMetrics": func(groupName string) bool { |
| 177 | + return groupName == "withAllMetrics" || groupName == "withGCMetrics" || groupName == "withMemoryMetrics" |
| 178 | + }, |
| 179 | +}).Parse(`// Copyright 2022 The Prometheus Authors |
| 180 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 181 | +// you may not use this file except in compliance with the License. |
| 182 | +// You may obtain a copy of the License at |
| 183 | +// |
| 184 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 185 | +// |
| 186 | +// Unless required by applicable law or agreed to in writing, software |
| 187 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 188 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 189 | +// See the License for the specific language governing permissions and |
| 190 | +// limitations under the License. |
| 191 | +
|
| 192 | +//go:build {{.GoVersion}} && !{{nextVersion .GoVersion}} |
| 193 | +// +build {{.GoVersion}},!{{nextVersion .GoVersion}} |
| 194 | +
|
| 195 | +package collectors |
| 196 | +
|
| 197 | +{{- range .Groups }} |
| 198 | +func {{ .Name }}() []string { |
| 199 | + {{- if needsBaseMetrics .Name }} |
| 200 | + return withBaseMetrics([]string{ |
| 201 | + {{- range $metric := .Metrics }} |
| 202 | + {{ $metric | printf "%q" }}, |
| 203 | + {{- end }} |
| 204 | + }) |
| 205 | + {{- else }} |
| 206 | + return []string{ |
| 207 | + {{- range $metric := .Metrics }} |
| 208 | + {{ $metric | printf "%q" }}, |
| 209 | + {{- end }} |
| 210 | + } |
| 211 | + {{- end }} |
| 212 | +} |
| 213 | +{{ end }} |
| 214 | +`)) |
0 commit comments