Skip to content

Commit

Permalink
handlers: log successful heartbeat checks with latency
Browse files Browse the repository at this point in the history
fixes: #326 #328
  • Loading branch information
Greg Guthe committed Sep 23, 2019
1 parent 83f4c84 commit 7c151d1
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions handlers.go
Expand Up @@ -251,31 +251,39 @@ func (a *autographer) handleHeartbeat(w http.ResponseWriter, r *http.Request) {
result = map[string]bool{}
status = http.StatusOK
requestContext = r.Context()
rid = getRequestID(r)
)

// try to fetch the private key from the HSM for the first
// signer conf with a non-PEM private key that we saved on
// server start
if a.heartbeatConf.hsmSignerConf != nil {
var (
err error
conf = a.heartbeatConf.hsmSignerConf
checkResult = make(chan error, 1)
err error
hsmSignerConf = a.heartbeatConf.hsmSignerConf
hsmHBTimeout = a.heartbeatConf.HSMCheckTimeout
checkResult = make(chan error, 1)
hsmHeartbeatStartTs = time.Now()
)
go func() {
checkResult <- conf.CheckHSMConnection()
checkResult <- hsmSignerConf.CheckHSMConnection()
}()
select {
case <-time.After(a.heartbeatConf.HSMCheckTimeout):
err = fmt.Errorf("Checking HSM connection for signer %s private key timed out", conf.ID)
case <-time.After(hsmHBTimeout):
err = fmt.Errorf("Checking HSM connection for signer %s private key timed out", hsmSignerConf.ID)
case err = <-checkResult:
}

if err == nil {
log.WithFields(log.Fields{
"rid": rid,
"t": int32(time.Since(hsmHeartbeatStartTs) / time.Millisecond),
"timeout": fmt.Sprintf("%s", hsmHBTimeout),
}).Info("HSM heartbeat completed successfully")
result["hsmAccessible"] = true
status = http.StatusOK
} else {
log.Errorf("error checking HSM connection for signer %s: %s", conf.ID, err)
log.Errorf("error checking HSM connection for signer %s: %s", hsmSignerConf.ID, err)
result["hsmAccessible"] = false
status = http.StatusInternalServerError
}
Expand All @@ -285,10 +293,16 @@ func (a *autographer) handleHeartbeat(w http.ResponseWriter, r *http.Request) {
// don't fail the heartbeat since we only care about DB
// connectivity on server start
if a.db != nil {
dbHeartbeatStartTs := time.Now()
dbCheckCtx, dbCancel := context.WithTimeout(requestContext, a.heartbeatConf.DBCheckTimeout)
defer dbCancel()
err := a.db.CheckConnectionContext(dbCheckCtx)
if err == nil {
log.WithFields(log.Fields{
"rid": rid,
"t": int32(time.Since(dbHeartbeatStartTs) / time.Millisecond),
"timeout": fmt.Sprintf("%s", a.heartbeatConf.DBCheckTimeout),
}).Info("DB heartbeat completed successfully")
result["dbAccessible"] = true
} else {
log.Errorf("error checking DB connection: %s", err)
Expand Down

0 comments on commit 7c151d1

Please sign in to comment.