-
Notifications
You must be signed in to change notification settings - Fork 107
/
main.go
273 lines (243 loc) · 6.66 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// extract-metrics extracts prometheus metrics from .go source
package main
import (
"encoding/json"
"fmt"
"go/ast"
"go/parser"
"go/token"
"html"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
CfgMarkdown = "markdown"
CfgMarkdownTplFile = "markdown.template.file"
CfgMarkdownTplPlaceholder = "markdown.template.placeholder"
CfgCodebasePath = "codebase.path"
)
var (
scriptName = filepath.Base(os.Args[0])
rootCmd = &cobra.Command{
Use: scriptName,
Short: "Extracts Prometheus metrics from .go code.",
Long: `This tool parses .go source files in the given codebase path
and generates a set of registered Prometheus metrics. By default it outputs JSON formatted metrics
map. You can also provide --markdown flag and it will print a Markdown-formatted table of metrics
useful for embedding into other Markdown files. Additionally, you can use --markdown.template.file
and it will embed the table in place of the placeholder in the provided template file.`,
Example: "./extract-metrics --codebase.path ../.. --markdown",
Run: doExtractMetrics,
}
)
type Metric struct {
Name string `json:"name"`
Type string `json:"type"`
Help string `json:"help"`
Labels []string `json:"labels"`
Filename string `json:"filename"`
Line int `json:"line"`
Vec bool `json:"vec"`
}
func markdownTable(metrics map[string]Metric) string {
var ordKeys []string
for k := range metrics {
ordKeys = append(ordKeys, k)
}
sort.Slice(ordKeys, func(i, j int) bool {
return metrics[ordKeys[i]].Name < metrics[ordKeys[j]].Name
})
tplDir := viper.GetString(CfgCodebasePath)
if viper.IsSet(CfgMarkdownTplFile) {
tplDir = filepath.Dir(viper.GetString(CfgMarkdownTplFile))
}
mdTable := "Name | Type | Description | Labels | Package\n"
mdTable += "-----|------|-------------|--------|--------\n"
for _, k := range ordKeys {
m := metrics[k]
pkg, _ := filepath.Rel(viper.GetString(CfgCodebasePath), m.Filename)
pkg = filepath.Dir(pkg)
fileURL, _ := filepath.Rel(tplDir, m.Filename)
desc := html.EscapeString(m.Help)
labels := strings.Join(m.Labels, ", ")
mdTable += fmt.Sprintf("%s | %s | %s | %s | [%s](%s)\n", m.Name, m.Type, desc,
labels, pkg, fileURL)
}
return mdTable
}
func printMarkdown(metrics map[string]Metric) {
mdTable := markdownTable(metrics)
if !viper.IsSet(CfgMarkdownTplFile) {
// Print Markdown table only.
fmt.Print(mdTable)
return
}
md, err := ioutil.ReadFile(viper.GetString(CfgMarkdownTplFile))
if err != nil {
panic(err)
}
mdStr := fmt.Sprintf("<!--- DO NOT EDIT. This file was generated by %s -->\n", scriptName)
mdStr += string(md)
mdStr = strings.Replace(mdStr, viper.GetString(CfgMarkdownTplPlaceholder)+"\n", mdTable, 1)
fmt.Print(mdStr)
}
func printJSON(m map[string]Metric) {
data, err := json.Marshal(m)
if err != nil {
panic(err)
}
fmt.Printf("%s", data)
}
var metrics = map[string]Metric{}
func doExtractMetrics(cmd *cobra.Command, args []string) {
searchDir := viper.GetString(CfgCodebasePath)
fset := token.NewFileSet() // positions are relative to fset
err := filepath.Walk(searchDir, func(path string, f os.FileInfo, err error) error {
if err != nil {
log.Fatal(err)
}
if f.IsDir() {
return nil
}
if !strings.HasSuffix(f.Name(), ".go") {
return nil
}
src, err := parser.ParseFile(fset, path, nil, 0)
if err != nil {
return err
}
ast.Inspect(src, func(n ast.Node) bool {
m, ok := checkNewPrometheusMetric(fset, n)
if ok {
m.Filename = path
metrics[m.Name] = m
}
return true
})
return nil
})
if err != nil {
log.Fatal(err)
}
if viper.GetBool(CfgMarkdown) {
printMarkdown(metrics)
} else {
printJSON(metrics)
}
}
// checkNewPrometheusMetric checks the given node in AST, if it contains Prometheus metric.
//
// Example code in go:
//
// ```
// rhpLatency = prometheus.NewSummaryVec(
// prometheus.SummaryOpts{
// Name: "oasis_rhp_latency",
// Help: "Runtime Host call latency (seconds).",
// },
// []string{"call"},
// )
// ```
//
// Identifiers for Name and Help fields in Opts are also unwound, for example:
//
// ```
// const MetricCPUUTimeSeconds = "oasis_node_cpu_utime_seconds"
// ...
// utimeGauge = prometheus.NewGauge(
// prometheus.GaugeOpts{
// Name: MetricCPUUTimeSeconds,
// Help: "CPU user time spent by worker as reported by /proc/<PID>/stat (seconds).",
// },
// )
// ```
func checkNewPrometheusMetric(f *token.FileSet, n ast.Node) (m Metric, ok bool) {
c, ok := n.(*ast.CallExpr)
if !ok {
return
}
sel, ok := c.Fun.(*ast.SelectorExpr)
if !ok {
return
}
pkg, ok := sel.X.(*ast.Ident)
if !ok || pkg.Name != "prometheus" {
return m, false
}
re := regexp.MustCompile(`New(.*)`)
if !re.MatchString(sel.Sel.String()) {
return m, false
}
// Call expression is of form prometheus.New<metric Name>(...) or
// prometheus.New<metric Name>Vec().
m.Type = re.FindStringSubmatch(sel.Sel.String())[1]
if strings.HasSuffix(m.Type, "Vec") {
m.Vec = true
m.Type = m.Type[:len(m.Type)-3]
}
m.Line = f.Position(c.Pos()).Line
// Obtain metric Name and Help values.
ast.Inspect(c.Args[0], func(n ast.Node) bool {
// Find metrics Name: and Help: attributes.
kv, okKV := n.(*ast.KeyValueExpr)
if !okKV {
return true
}
if kv.Key.(*ast.Ident).Name == "Name" {
m.Name = extractValue(kv.Value)
}
if kv.Key.(*ast.Ident).Name == "Help" {
m.Help = extractValue(kv.Value)
}
return true
})
// If labels are defined, extract them.
if len(c.Args) > 1 {
l, okL := c.Args[1].(*ast.CompositeLit)
if !okL {
return
}
for _, e := range l.Elts {
m.Labels = append(m.Labels, extractValue(e))
}
}
return
}
// extractValue returns string value of the identifier or literal.
func extractValue(n ast.Expr) string {
lit, ok := n.(*ast.BasicLit)
if ok {
// Strip quotes.
return lit.Value[1 : len(lit.Value)-1]
}
ident, ok := n.(*ast.Ident)
if !ok || ident.Obj == nil {
return ""
}
decl, ok := ident.Obj.Decl.(*ast.ValueSpec)
if !ok || len(decl.Values) != 1 {
return ""
}
val, ok := decl.Values[0].(*ast.BasicLit)
if !ok {
return ""
}
// Strip quotes.
return val.Value[1 : len(val.Value)-1]
}
func main() {
rootCmd.Flags().Bool(CfgMarkdown, false, "print metrics in markdown format")
rootCmd.Flags().String(CfgCodebasePath, "", "path to Go codebase")
rootCmd.Flags().String(CfgMarkdownTplFile, "", "path to Markdown template file")
rootCmd.Flags().String(CfgMarkdownTplPlaceholder, "<!--- OASIS_METRICS -->", "placeholder for Markdown table in the template")
_ = cobra.MarkFlagRequired(rootCmd.Flags(), CfgCodebasePath)
_ = viper.BindPFlags(rootCmd.Flags())
_ = rootCmd.Execute()
}