-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
201 lines (168 loc) · 5.6 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
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"github.com/poblish/boulevard/generation"
"golang.org/x/tools/go/packages"
"gopkg.in/yaml.v2"
)
type packagesList []string
type extraLabels []string
var packageFlags packagesList
var rulesOutputPath string
var rulesOutputFormat string
var dashboardOutputPath string
var dashboardUid string
var dashboardTitle string
var metricsLabelsPath string
var sourcePath string
var defaultMetricsPrefix string
var alertExtraLabels extraLabels
var alertManagerOutputFormat = "alertManager"
var defaultRulesOutputFileName = "alert_rules.yaml"
var defaultGrafanaDashboardFileName = "grafana_dashboard.json"
func main() {
currentDir, err := os.Getwd()
if err == nil {
currentDir = "."
}
state := BoulevardState{}
// See if there's any upstream state to pick up
stateFileBytes, err := os.ReadFile(filepath.Join(sourcePath, ".boulevard_state"))
if err == nil {
if err := yaml.Unmarshal(stateFileBytes, &state); err != nil {
log.Fatalf("Bad state %s", err)
}
}
flag.Var(&packageFlags, "pkg", "Packages to scan")
flag.StringVar(&sourcePath, "sourcePath", "", "Source path")
flag.StringVar(&rulesOutputPath, "rulesOutputPath", "", "Rules output path")
flag.StringVar(&rulesOutputFormat, "rulesOutputFormat", "", "Rules output format")
flag.StringVar(&dashboardOutputPath, "dashboardOutputPath", "", "Dashboard output path")
flag.StringVar(&dashboardUid, "dashboardUid", "", "Override default Dashboard id")
flag.StringVar(&dashboardTitle, "dashboardTitle", "", "Override default Dashboard title")
flag.StringVar(&metricsLabelsPath, "metricsLabelsPath", "", "Metrics labels path")
flag.Var(&alertExtraLabels, "alertExtraLabels", "Extra alert labels (key=value)")
flag.StringVar(&defaultMetricsPrefix, "defaultMetricsPrefix", "", "Metrics prefix fallback/default")
flag.Parse()
if rulesOutputPath == "" {
if state.GeneratedChartDir != "" {
rulesOutputPath = fmt.Sprintf("%s/includes/prometheus-rules/%s", state.GeneratedChartDir, defaultRulesOutputFileName)
} else {
rulesOutputPath = defaultRulesOutputFileName
}
}
if dashboardOutputPath == "" {
if state.GeneratedChartDir != "" {
dashboardOutputPath = fmt.Sprintf("%s/includes/dashboards/%s", state.GeneratedChartDir, defaultGrafanaDashboardFileName)
} else {
dashboardOutputPath = defaultGrafanaDashboardFileName
}
}
if sourcePath == "" {
if state.SourcePath != "" {
sourcePath = state.SourcePath
} else {
sourcePath = currentDir
}
}
if len(packageFlags) == 0 && state.DefaultPkg != "" {
packageFlags = []string{state.DefaultPkg}
}
if rulesOutputFormat == "" {
if state.RulesOutputFormat != "" {
rulesOutputFormat = state.RulesOutputFormat
} else {
rulesOutputPath = alertManagerOutputFormat
}
}
if metricsLabelsPath == "" {
if state.MetricsLabelsPath != "" {
metricsLabelsPath = state.MetricsLabelsPath
}
}
if defaultMetricsPrefix == "" {
defaultMetricsPrefix = state.DefaultMetricsPrefix
}
if dashboardUid == "" {
dashboardUid = state.DashboardUidOverride
}
if dashboardTitle == "" {
dashboardTitle = state.DashboardTitleOverride
}
if len(alertExtraLabels) == 0 {
alertExtraLabels = state.AlertExtraLabels
}
var alertRuleFormat int
switch rulesOutputFormat {
case alertManagerOutputFormat:
alertRuleFormat = generation.PrometheusAlertManagerFormat
case "operator":
alertRuleFormat = generation.PrometheusOperatorFormat
default:
log.Fatalf("Unsupported rules output format %s", rulesOutputFormat)
}
fmt.Printf("Examining packages %v from repository dir: %s\n", packageFlags, generation.FriendlyFileName(sourcePath))
////////////////////////////////////////////
conf := packages.Config{
Dir: sourcePath,
Mode: packages.NeedName | packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo,
Tests: false,
}
loadedPkgs, err := packages.Load(&conf, packageFlags...)
if err != nil {
log.Fatalf("Could not load packages %s", err)
}
generator := &generation.DashboardGenerator{DefaultMetricsPrefix: defaultMetricsPrefix, DashboardUid: dashboardUid, DashboardTitle: dashboardTitle}
metrics, err := generator.DiscoverMetrics(loadedPkgs)
if err != nil {
log.Fatalf("Metrics discovery failed %s", err)
}
if len(metrics) > 0 {
// FIXME Hardcoded name
alertMetrics, err := generator.GenerateAlertRules(rulesOutputPath, generation.OutputOptions{AlertRuleFormat: alertRuleFormat, ExtraLabels: alertExtraLabels})
if err != nil {
log.Fatalf("Alert rule generation failed %s", err)
}
if metricsLabelsPath != "" {
metricsOutput := generation.AlertMetricsOutput{AlertsCount: alertMetrics.Count, UniqueMetricsCount: len(metrics)}
metricsOutput.WriteToFile(metricsLabelsPath)
}
}
if len(metrics) > 0 || len(state.ExternalMetricNames) > 0 {
err = generator.GenerateGrafanaDashboard(dashboardOutputPath, metrics, state.DashboardTags, state.ExternalMetricNames)
if err != nil {
log.Fatalf("Generation failed %s", err)
}
}
}
func (i *packagesList) String() string {
return "my string representation"
}
func (i *packagesList) Set(value string) error {
*i = append(*i, value)
return nil
}
func (i *extraLabels) String() string {
return "my string representation"
}
func (i *extraLabels) Set(value string) error {
*i = append(*i, value)
return nil
}
type BoulevardState struct {
SourcePath string
GeneratedChartDir string
DefaultPkg string
DefaultMetricsPrefix string
RulesOutputFormat string
MetricsLabelsPath string
DashboardUidOverride string
DashboardTitleOverride string
DashboardTags []string
AlertExtraLabels []string
ExternalMetricNames []string
}