@@ -42,25 +42,31 @@ type xychart struct {
42
42
}
43
43
44
44
type githubSummary struct {
45
- client api.Client
46
- Pods []string
45
+ client api.Client
46
+ firingAlerts bool
47
+ Pods []string
47
48
}
48
49
49
50
func NewSummary (c api.Client , pods ... string ) githubSummary {
50
51
return githubSummary {
51
- client : c ,
52
- Pods : pods ,
52
+ client : c ,
53
+ Pods : pods ,
54
+ firingAlerts : false ,
53
55
}
54
56
}
55
57
58
+ func (s * githubSummary ) FiringAlerts () bool {
59
+ return s .firingAlerts
60
+ }
61
+
56
62
// PerformanceQuery queries the prometheus server and generates a mermaid xychart with the data.
57
63
// title - Display name of the xychart
58
64
// pod - Pod name with which to filter results from prometheus
59
65
// query - Prometheus query
60
66
// yLabel - Label of the Y axis i.e. "KB/s", "MB", etc.
61
67
// scaler - Constant by which to scale the results. For instance, cpu usage is more human-readable
62
68
// as "mCPU" vs "CPU", so we scale the results by a factor of 1,000.
63
- func (s githubSummary ) PerformanceQuery (title , pod , query string , yLabel string , scaler float64 ) (string , error ) {
69
+ func (s * githubSummary ) PerformanceQuery (title , pod , query , yLabel string , scaler float64 ) (string , error ) {
64
70
v1api := v1 .NewAPI (s .client )
65
71
ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
66
72
defer cancel ()
@@ -115,7 +121,7 @@ func (s githubSummary) PerformanceQuery(title, pod, query string, yLabel string,
115
121
116
122
// Alerts queries the prometheus server for alerts and generates markdown output for anything found.
117
123
// If no alerts are found, the alerts section will contain only "None." in the final output.
118
- func (s githubSummary ) Alerts () (string , error ) {
124
+ func (s * githubSummary ) Alerts () (string , error ) {
119
125
v1api := v1 .NewAPI (s .client )
120
126
ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
121
127
defer cancel ()
@@ -136,6 +142,7 @@ func (s githubSummary) Alerts() (string, error) {
136
142
switch a .State {
137
143
case v1 .AlertStateFiring :
138
144
firingAlerts = append (firingAlerts , aConv )
145
+ s .firingAlerts = true
139
146
case v1 .AlertStatePending :
140
147
pendingAlerts = append (pendingAlerts , aConv )
141
148
// Ignore AlertStateInactive; the alerts endpoint doesn't return them
@@ -173,27 +180,31 @@ func executeTemplate(templateFile string, obj any) (string, error) {
173
180
// template. This allows us to add or change queries (hopefully) without needing to
174
181
// touch code. The summary will be output to a file supplied by the env target.
175
182
func PrintSummary (envTarget string ) error {
183
+ path := os .Getenv (envTarget )
184
+ if path == "" {
185
+ fmt .Printf ("No summary output specified; skipping" )
186
+ return nil
187
+ }
188
+
176
189
client , err := api .NewClient (api.Config {
177
190
Address : defaultPromUrl ,
178
191
})
179
192
if err != nil {
180
- fmt .Printf ("Error creating prometheus client: %v\n " , err )
181
- os .Exit (1 )
193
+ return err
182
194
}
183
195
184
196
summary := NewSummary (client , "operator-controller" , "catalogd" )
185
- summaryMarkdown , err := executeTemplate (summaryTemplate , summary )
197
+ summaryMarkdown , err := executeTemplate (summaryTemplate , & summary )
186
198
if err != nil {
187
199
return err
188
200
}
189
- if path := os .Getenv (envTarget ); path != "" {
190
- err = os .WriteFile (path , []byte (summaryMarkdown ), 0o600 )
191
- if err != nil {
192
- return err
193
- }
194
- fmt .Printf ("Test summary output to %s successful\n " , envTarget )
195
- } else {
196
- fmt .Printf ("No summary output specified; skipping" )
201
+ err = os .WriteFile (path , []byte (summaryMarkdown ), 0o600 )
202
+ if err != nil {
203
+ return err
204
+ }
205
+ fmt .Printf ("Test summary output to %s successful\n " , envTarget )
206
+ if summary .FiringAlerts () {
207
+ return fmt .Errorf ("Alert(s) encountered during test run; see summary for details" )
197
208
}
198
209
return nil
199
210
}
0 commit comments