@@ -91,39 +91,40 @@ func (c *CheckRunner) Run(ctx context.Context, checks []string) error {
9191 })
9292 }
9393
94- type errorCheck struct {
95- check string
96- err error
97- }
98-
99- var errors []errorCheck
94+ checkResults := make ([]checkResult , 0 , len (validatedChecks ))
95+ hasFailures := false
10096
97+ // run checks
10198 for _ , check := range validatedChecks {
102- c .logger .WithField ("type" , check .typeName ).Infof ("running check: %s" , check .name )
103- c .logger .Debugf ("check options: %+v" , check .options )
104-
105- if err := check .Run (ctx , c .cluster ); err != nil {
106- c .logger .WithField ("type" , check .typeName ).Errorf ("check '%s' failed: %v" , check .name , err )
107- errors = append (errors , errorCheck {
108- check : check .name ,
109- err : fmt .Errorf ("check %s failed: %w" , check .name , err ),
110- })
99+ c .logger .WithFields (map [string ]interface {}{
100+ "type" : check .typeName ,
101+ "options" : fmt .Sprintf ("%+v" , check .options ),
102+ }).Infof ("running check: %s" , check .name )
103+
104+ err := check .Run (ctx , c .cluster )
105+ if err != nil {
106+ hasFailures = true
107+ c .logger .WithFields (map [string ]interface {}{
108+ "type" : check .typeName ,
109+ "error" : err ,
110+ }).Errorf ("'%s' check failed" , check .name )
111111 } else {
112- c .logger .WithField ("type" , check .typeName ).Infof ("%s check completed successfully" , check .name )
112+ c .logger .WithField ("type" , check .typeName ).Infof ("'%s' check completed successfully" , check .name )
113113 }
114+
115+ // append check result
116+ checkResults = append (checkResults , checkResult {
117+ check : check .name ,
118+ err : err ,
119+ timestamp : time .Now (),
120+ })
114121 }
115122
116- if len (errors ) == 1 {
117- return errors [0 ].err
118- } else if len (errors ) > 1 {
119- var errStrings []string
120- for _ , e := range errors {
121- errStrings = append (errStrings , fmt .Sprintf ("[%s]: {%v}" , e .check , e .err ))
122- c .logger .Errorf ("%s: %v" , e .check , e .err )
123- }
124- return fmt .Errorf ("multiple checks failed: %s" , strings .Join (errStrings , "; " ))
123+ if hasFailures {
124+ return formatErrorReport (checkResults )
125125 }
126126
127+ c .logger .WithField ("total_checks" , len (checkResults )).Info ("All checks completed successfully" )
127128 return nil
128129}
129130
@@ -166,3 +167,52 @@ func createChildContext(ctx context.Context, timeout *time.Duration) (context.Co
166167 }
167168 return context .WithCancel (ctx )
168169}
170+
171+ type checkResult struct {
172+ check string
173+ err error
174+ timestamp time.Time
175+ }
176+
177+ func formatErrorReport (results []checkResult ) error {
178+ var failedChecks []string
179+ var failedDetails []string
180+
181+ // if there is only one error, return it directly
182+ if len (results ) == 1 {
183+ return results [0 ].err
184+ }
185+
186+ for _ , result := range results {
187+ if result .err != nil {
188+ failedChecks = append (failedChecks , result .check )
189+ failedDetails = append (failedDetails , result .DetailString ())
190+ }
191+ }
192+
193+ totalChecks := len (results )
194+ failedCount := len (failedChecks )
195+
196+ return fmt .Errorf ("CHECK_FAILED | %d/%d checks failed | Checks: %s | %s" ,
197+ failedCount ,
198+ totalChecks ,
199+ strings .Join (failedChecks , "," ),
200+ strings .Join (failedDetails , " | " ))
201+ }
202+
203+ func (e checkResult ) String () string {
204+ if e .err != nil {
205+ return fmt .Sprintf ("%s: %v" , e .check , e .err )
206+ }
207+ return fmt .Sprintf ("%s: success" , e .check )
208+ }
209+
210+ func (e checkResult ) DetailString () string {
211+ timestamp := e .timestamp .Format ("2006-01-02T15:04:05" )
212+ if e .err != nil {
213+ return fmt .Sprintf (`{"check":"%s","time":"%s","error":"%v"}` ,
214+ e .check , timestamp , e .err )
215+ }
216+ return fmt .Sprintf (`{"check":"%s","time":"%s","status":"success"}` ,
217+ e .check , timestamp )
218+ }
0 commit comments