healthcheck: improve logging of observers

This commit is contained in:
Andras Banki-Horvath 2024-07-25 18:09:26 +02:00
parent 04dde98edc
commit 0fd4c7d5f9
No known key found for this signature in database
GPG key ID: 80E5375C094198D8

View file

@ -234,14 +234,13 @@ func (o *Observation) monitor(shutdown shutdownFunc, quit chan struct{}) {
// the max attempts are reached. In that case we will // the max attempts are reached. In that case we will
// stop the ticker and quit. // stop the ticker and quit.
if o.retryCheck(quit, shutdown) { if o.retryCheck(quit, shutdown) {
log.Debugf("Health check: max attempts " + o.Debugf("max attempts failed, monitor exiting")
"failed, monitor exiting")
return return
} }
// Exit if we receive the instruction to shutdown. // Exit if we receive the instruction to shutdown.
case <-quit: case <-quit:
log.Debug("Health check: monitor quit") o.Debugf("monitor quit")
return return
} }
} }
@ -270,7 +269,7 @@ func (o *Observation) retryCheck(quit chan struct{},
// so we'll invoke our success callback if defined and // so we'll invoke our success callback if defined and
// then exit. // then exit.
if err == nil { if err == nil {
log.Debug("invoking success callback") o.Debugf("invoking success callback")
// Invoke the success callback. // Invoke the success callback.
o.OnSuccess() o.OnSuccess()
@ -283,7 +282,7 @@ func (o *Observation) retryCheck(quit chan struct{},
"%v", o, o.Timeout) "%v", o, o.Timeout)
case <-quit: case <-quit:
log.Debug("Health check: monitor quit") o.Debugf("monitor quit")
return false return false
} }
@ -291,17 +290,18 @@ func (o *Observation) retryCheck(quit chan struct{},
// check has failed so we'll fire the on failure callback // check has failed so we'll fire the on failure callback
// and request shutdown. // and request shutdown.
if count == o.Attempts { if count == o.Attempts {
log.Debug("invoking failure callback") o.Debugf("invoking failure callback")
o.OnFailure() o.OnFailure()
shutdown("Health check: %v failed after %v "+ shutdown("Health check: %v failed after %v calls", o,
"calls", o, o.Attempts) o.Attempts)
return true return true
} }
log.Infof("Health check: %v, call: %v failed with: %v, "+ o.Infof("failed with: %v, attempts: %v backing off for: %v",
"backing off for: %v", o, count, err, o.Backoff) err, count, o.Backoff)
// If we are still within the number of calls allowed for this // If we are still within the number of calls allowed for this
// check, we wait for our back off period to elapse, or exit if // check, we wait for our back off period to elapse, or exit if
@ -310,10 +310,22 @@ func (o *Observation) retryCheck(quit chan struct{},
case <-time.After(o.Backoff): case <-time.After(o.Backoff):
case <-quit: case <-quit:
log.Debug("Health check: monitor quit") o.Debugf("monitor quit")
return false return false
} }
} }
return false return false
} }
// Infof logs an info message for an observation prefixed with the health check
// name.
func (o *Observation) Infof(format string, params ...interface{}) {
log.Debugf(fmt.Sprintf("Health check: %v ", o)+format, params...)
}
// Debugf logs a debug message for an observation prefixed with the health check
// name.
func (o *Observation) Debugf(format string, params ...interface{}) {
log.Debugf(fmt.Sprintf("Health check: %v ", o)+format, params...)
}