diff --git a/internal/healthcheck/dns.go b/internal/healthcheck/dns.go index 9f5b611..1cfca87 100644 --- a/internal/healthcheck/dns.go +++ b/internal/healthcheck/dns.go @@ -2,7 +2,6 @@ package healthcheck import ( "context" - "fmt" "net" "net/http" "time" @@ -34,18 +33,16 @@ func (h *Healthcheck) DNSCheckLoop() { ips, err := r.LookupIP(context.TODO(), "ip", hostname) if err != nil { log.Printf("Fetching dns records failed: %s\n", err.Error()) - h.dnsOK = false return } if len(ips) > 0 { log.Println("Received at least 1 IP. Ready!") - h.dnsOK = true + h.lastDNSTime = time.Now() return } log.Println("Received NO IPs. Not Ready!") - h.dnsOK = false }() time.Sleep(30 * time.Second) @@ -55,18 +52,6 @@ func (h *Healthcheck) DNSCheckLoop() { // seederHealthcheck endpoint for the seeder service as a whole (Are we sending DNS responses) func (h *Healthcheck) seederHealthcheck() func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { - if h.dnsOK { - w.WriteHeader(http.StatusOK) - _, err := fmt.Fprintf(w, "Ok") - if err != nil { - log.Errorf("Error writing healthcheck response %s\n", err.Error()) - } - } else { - w.WriteHeader(http.StatusInternalServerError) - _, err := fmt.Fprintf(w, "Not OK") - if err != nil { - log.Errorf("Error writing healthcheck response %s\n", err.Error()) - } - } + timeMetricHealthcheckHelper(h.lastDNSTime, w, r) } } diff --git a/internal/healthcheck/fullnode.go b/internal/healthcheck/fullnode.go new file mode 100644 index 0000000..9e16e88 --- /dev/null +++ b/internal/healthcheck/fullnode.go @@ -0,0 +1,42 @@ +package healthcheck + +import ( + "encoding/json" + "net/http" + "time" + + log "github.com/sirupsen/logrus" + + "github.com/chia-network/go-chia-libs/pkg/types" +) + +func (h *Healthcheck) fullNodeReceive(resp *types.WebsocketResponse) { + var blockHeight uint32 + + if resp.Command != "get_blockchain_state" { + return + } + + block := &types.WebsocketBlockchainState{} + err := json.Unmarshal(resp.Data, block) + if err != nil { + log.Errorf("Error unmarshalling: %s\n", err.Error()) + return + } + blockHeight = block.BlockchainState.Peak.OrEmpty().Height + + // Edge case, but we should be sure block height is increasing + if blockHeight <= h.lastHeight { + return + } + + h.lastHeight = blockHeight + h.lastHeightTime = time.Now() +} + +// Healthcheck endpoint for the full node service as a whole +func (h *Healthcheck) fullNodeHealthcheck() func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + timeMetricHealthcheckHelper(h.lastHeightTime, w, r) + } +} diff --git a/internal/healthcheck/healthcheck.go b/internal/healthcheck/healthcheck.go index 33ae76c..ba0a913 100644 --- a/internal/healthcheck/healthcheck.go +++ b/internal/healthcheck/healthcheck.go @@ -1,7 +1,6 @@ package healthcheck import ( - "encoding/json" "fmt" "net/http" "net/url" @@ -25,8 +24,8 @@ type Healthcheck struct { // Time we received the last block height lastHeightTime time.Time - // dnsOK Are we currently serving DNS responses? - dnsOK bool + // Last time we got a successful DNS response + lastDNSTime time.Time } // NewHealthcheck returns a new instance of healthcheck @@ -35,7 +34,6 @@ func NewHealthcheck(port uint16, logLevel log.Level) (*Healthcheck, error) { healthcheck := &Healthcheck{ healthcheckPort: port, - dnsOK: false, } log.SetLevel(logLevel) @@ -103,30 +101,6 @@ func (h *Healthcheck) websocketReceive(resp *types.WebsocketResponse, err error) } } -func (h *Healthcheck) fullNodeReceive(resp *types.WebsocketResponse) { - var blockHeight uint32 - - if resp.Command != "get_blockchain_state" { - return - } - - block := &types.WebsocketBlockchainState{} - err := json.Unmarshal(resp.Data, block) - if err != nil { - log.Errorf("Error unmarshalling: %s\n", err.Error()) - return - } - blockHeight = block.BlockchainState.Peak.OrEmpty().Height - - // Edge case, but we should be sure block height is increasing - if blockHeight <= h.lastHeight { - return - } - - h.lastHeight = blockHeight - h.lastHeightTime = time.Now() -} - func (h *Healthcheck) walletReceive(resp *types.WebsocketResponse) {} func (h *Healthcheck) crawlerReceive(resp *types.WebsocketResponse) {} @@ -150,21 +124,19 @@ func (h *Healthcheck) reconnectHandler() { } } -// Healthcheck endpoint for the full node service as a whole -func (h *Healthcheck) fullNodeHealthcheck() func(http.ResponseWriter, *http.Request) { - return func(w http.ResponseWriter, r *http.Request) { - if time.Since(h.lastHeightTime) < viper.GetDuration("healthcheck-threshold") { - w.WriteHeader(http.StatusOK) - _, err := fmt.Fprintf(w, "Ok") - if err != nil { - log.Errorf("Error writing healthcheck response %s\n", err.Error()) - } - } else { - w.WriteHeader(http.StatusInternalServerError) - _, err := fmt.Fprintf(w, "Not OK") - if err != nil { - log.Errorf("Error writing healthcheck response %s\n", err.Error()) - } +func timeMetricHealthcheckHelper(lastTime time.Time, w http.ResponseWriter, r *http.Request) { + if time.Since(lastTime) < viper.GetDuration("healthcheck-threshold") { + w.WriteHeader(http.StatusOK) + _, err := fmt.Fprintf(w, "Ok") + if err != nil { + log.Errorf("Error writing healthcheck response %s\n", err.Error()) + } + } else { + w.WriteHeader(http.StatusInternalServerError) + _, err := fmt.Fprintf(w, "Not OK") + if err != nil { + log.Errorf("Error writing healthcheck response %s\n", err.Error()) } } } +