1
- // gather package contains common gathering logic for all gatherers
1
+ // Package gather contains common gathering logic for all gatherers
2
2
package gather
3
3
4
4
import (
@@ -20,11 +20,14 @@ import (
20
20
"github.com/openshift/insights-operator/pkg/insights/insightsclient"
21
21
"github.com/openshift/insights-operator/pkg/record"
22
22
"github.com/openshift/insights-operator/pkg/recorder"
23
+ "github.com/openshift/insights-operator/pkg/types"
23
24
"github.com/openshift/insights-operator/pkg/utils"
24
25
)
25
26
26
27
// norevive
27
28
const (
29
+ // AllGatherersConst is used to specify in the config that we want to enable
30
+ // all gathering functions from all gatherers
28
31
AllGatherersConst = "ALL"
29
32
)
30
33
@@ -36,6 +39,7 @@ type GathererFunctionReport struct {
36
39
Duration int64 `json:"duration_in_ms"`
37
40
RecordsCount int `json:"records_count"`
38
41
Errors []string `json:"errors"`
42
+ Warnings []string `json:"warnings"`
39
43
Panic interface {} `json:"panic"`
40
44
}
41
45
@@ -78,57 +82,114 @@ func CollectAndRecordGatherer(
78
82
rec recorder.Interface ,
79
83
configurator configobserver.Configurator ,
80
84
) ([]GathererFunctionReport , error ) {
85
+ startTime := time .Now ()
86
+ reports , totalNumberOfRecords , errs := collectAndRecordGatherer (ctx , gatherer , rec , configurator )
87
+ reports = append (reports , GathererFunctionReport {
88
+ FuncName : gatherer .GetName (),
89
+ Duration : time .Since (startTime ).Milliseconds (),
90
+ RecordsCount : totalNumberOfRecords ,
91
+ Errors : utils .ErrorsToStrings (errs ),
92
+ })
93
+
94
+ return reports , utils .SumErrors (errs )
95
+ }
96
+
97
+ func collectAndRecordGatherer (
98
+ ctx context.Context ,
99
+ gatherer gatherers.Interface ,
100
+ rec recorder.Interface ,
101
+ configurator configobserver.Configurator ,
102
+ ) (reports []GathererFunctionReport , totalNumberOfRecords int , allErrors []error ) {
81
103
resultsChan , err := startGatheringConcurrently (ctx , gatherer , configurator .Config ().Gather )
82
104
if err != nil {
83
- return nil , err
105
+ allErrors = append (allErrors , err )
106
+ return reports , totalNumberOfRecords , allErrors
84
107
}
85
108
86
- gathererName := gatherer .GetName ()
109
+ for result := range resultsChan {
110
+ report , errs := recordGatheringFunctionResult (rec , & result , gatherer .GetName ())
111
+ allErrors = append (allErrors , errs ... )
112
+ reports = append (reports , report )
113
+ totalNumberOfRecords += report .RecordsCount
114
+ }
115
+
116
+ return reports , totalNumberOfRecords , allErrors
117
+ }
87
118
88
- var errs []error
89
- var functionReports []GathererFunctionReport
119
+ func recordGatheringFunctionResult (
120
+ rec recorder.Interface , result * GatheringFunctionResult , gathererName string ,
121
+ ) (GathererFunctionReport , []error ) {
122
+ var allErrors []error
123
+ var recordWarnings []error
124
+ var recordErrs []error
90
125
91
- for result := range resultsChan {
92
- if result . Panic != nil {
93
- klog .Error (fmt .Errorf (
94
- " gatherer %v's function %v panicked with error: %v" ,
95
- gathererName , result .FunctionName , result .Panic ,
96
- ))
97
- result . Errs = append (result . Errs , fmt .Errorf ("%v" , result .Panic ))
98
- }
126
+ if result . Panic != nil {
127
+ recordErrs = append ( recordErrs , fmt . Errorf ( "panic: %v" , result . Panic ))
128
+ klog .Error (fmt .Errorf (
129
+ ` gatherer "%v" function "%v" panicked with the error: %v` ,
130
+ gathererName , result .FunctionName , result .Panic ,
131
+ ))
132
+ allErrors = append (allErrors , fmt .Errorf (`function "%v" panicked` , result .FunctionName ))
133
+ }
99
134
100
- for _ , err := range result .Errs {
101
- errs = append (errs , fmt .Errorf (
102
- "gatherer %v's function %v failed with error: %v" ,
135
+ for _ , err := range result .Errs {
136
+ if w , isWarning := err .(* types.Warning ); isWarning {
137
+ recordWarnings = append (recordWarnings , w )
138
+ klog .Warningf (
139
+ `gatherer "%v" function "%v" produced the warning: %v` , gathererName , result .FunctionName , w ,
140
+ )
141
+ } else {
142
+ recordErrs = append (recordErrs , err )
143
+ klog .Errorf (
144
+ `gatherer "%v" function "%v" failed with the error: %v` ,
103
145
gathererName , result .FunctionName , err ,
104
- ))
146
+ )
147
+ allErrors = append (allErrors , fmt .Errorf (`function "%v" failed with an error` , result .FunctionName ))
105
148
}
106
- recordedRecs := 0
107
- for _ , r := range result .Records {
108
- if err := rec .Record (r ); err != nil {
109
- result .Errs = append (result .Errs , fmt .Errorf (
110
- "unable to record gatherer %v function %v' result %v because of error: %v" ,
111
- gathererName , result .FunctionName , r .Name , err ,
112
- ))
113
- continue
149
+ }
150
+
151
+ recordedRecs := 0
152
+ for _ , r := range result .Records {
153
+ wasRecorded := true
154
+ if errs := rec .Record (r ); len (errs ) > 0 {
155
+ for _ , err := range errs {
156
+ if w , isWarning := err .(* types.Warning ); isWarning {
157
+ recordWarnings = append (recordWarnings , w )
158
+ klog .Warningf (
159
+ `issue recording gatherer "%v" function "%v" result "%v" because of the warning: %v` ,
160
+ gathererName , result .FunctionName , r .GetFilename (), w ,
161
+ )
162
+ } else {
163
+ recordErrs = append (recordErrs , err )
164
+ klog .Errorf (
165
+ `error recording gatherer "%v" function "%v" result "%v" because of the error: %v` ,
166
+ gathererName , result .FunctionName , r .GetFilename (), err ,
167
+ )
168
+ allErrors = append (allErrors , fmt .Errorf (
169
+ `unable to record function "%v" record "%v"` , result .FunctionName , r .GetFilename (),
170
+ ))
171
+ wasRecorded = false
172
+ }
114
173
}
174
+ }
175
+ if wasRecorded {
115
176
recordedRecs ++
116
177
}
117
-
118
- klog .Infof (
119
- "Gather %v's function %v took %v to process %v records" ,
120
- gathererName , result .FunctionName , result .TimeElapsed , len (result .Records ),
121
- )
122
-
123
- functionReports = append (functionReports , GathererFunctionReport {
124
- FuncName : fmt .Sprintf ("%v/%v" , gathererName , result .FunctionName ),
125
- Duration : result .TimeElapsed .Milliseconds (),
126
- RecordsCount : recordedRecs ,
127
- Errors : utils .ErrorsToStrings (result .Errs ),
128
- Panic : result .Panic ,
129
- })
130
178
}
131
- return functionReports , utils .SumErrors (errs )
179
+
180
+ klog .Infof (
181
+ `gatherer "%v" function "%v" took %v to process %v records` ,
182
+ gathererName , result .FunctionName , result .TimeElapsed , len (result .Records ),
183
+ )
184
+
185
+ return GathererFunctionReport {
186
+ FuncName : fmt .Sprintf ("%v/%v" , gathererName , result .FunctionName ),
187
+ Duration : result .TimeElapsed .Milliseconds (),
188
+ RecordsCount : recordedRecs ,
189
+ Errors : utils .ErrorsToStrings (recordErrs ),
190
+ Warnings : utils .ErrorsToStrings (recordWarnings ),
191
+ Panic : result .Panic ,
192
+ }, allErrors
132
193
}
133
194
134
195
// RecordArchiveMetadata records info about archive and gatherers' reports
@@ -149,8 +210,8 @@ func RecordArchiveMetadata(
149
210
IsGlobalObfuscationEnabled : anonymizer != nil ,
150
211
}},
151
212
}
152
- if err := rec .Record (archiveMetadata ); err != nil {
153
- return fmt .Errorf ("unable to record archive metadata because of error : %v" , err )
213
+ if errs := rec .Record (archiveMetadata ); len ( errs ) > 0 {
214
+ return fmt .Errorf ("unable to record archive metadata because of the errors : %v" , errs )
154
215
}
155
216
156
217
return nil
0 commit comments