feat(cmd): add healthy and ready check url. #1000296298

Signed-off-by: Victor1319 <zengxuewei@oppo.com>
(cherry picked from commit 2802e5e486)
This commit is contained in:
Victor1319 2025-08-21 10:03:48 +08:00 committed by chihe
parent 6283610ad4
commit 99e41928dd

View File

@ -15,6 +15,7 @@
package main
import (
"encoding/json"
"flag"
"fmt"
syslog "log"
@ -404,6 +405,10 @@ func main() {
os.Exit(1)
}
http.HandleFunc("/healthyz", liveCheck)
http.HandleFunc("/livez", liveCheck)
http.HandleFunc("/readyz", readyCheck)
daemonize.SignalOutcome(nil)
// Block main goroutine until server shutdown.
@ -438,3 +443,32 @@ func startDaemon() error {
return nil
}
func writeJSONResponse(w http.ResponseWriter, status string, message string) {
resp := map[string]interface{}{
"status": status,
"message": message,
"time": time.Now().Format(time.RFC3339),
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.LogErrorf("failed to encode response, err %s", err.Error())
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
}
func liveCheck(w http.ResponseWriter, r *http.Request) {
log.LogInfof("livez check request received")
writeJSONResponse(w, "livez", "service is running")
log.LogInfo("livez check response sent successfully")
}
func readyCheck(w http.ResponseWriter, r *http.Request) {
log.LogInfof("ready check request received")
writeJSONResponse(w, "ready", "service is ready")
log.LogInfo("ready check response sent successfully")
}