chore: 增加健康检测功能

This commit is contained in:
2022-01-16 12:21:02 +08:00
parent cbc392ce08
commit fb29788734
129 changed files with 14041 additions and 203 deletions

View File

@@ -0,0 +1,71 @@
package checkgrp
import (
"encoding/json"
"go.uber.org/zap"
"net/http"
"os"
)
type Handlers struct {
Build string
Log *zap.SugaredLogger
}
func (h Handlers) Readiness(w http.ResponseWriter, r *http.Request) {
data := struct {
Status string `json:"status"`
}{
Status: "ok",
}
statusCode := http.StatusOK
if err := response(w, statusCode, data); err != nil {
h.Log.Errorw("readiness", "ERROR", err)
}
h.Log.Infow("readiness", "statusCode", statusCode, "method", r.Method, "path",
r.URL.Path, "remoteAddr", r.RemoteAddr)
}
func (h Handlers) Liveliness(w http.ResponseWriter, r *http.Request) {
host, err := os.Hostname()
if err != nil {
host = "unavailable"
}
data := struct {
Status string `json:"status"`
Build string `json:"build"`
Host string `json:"host"`
Pod string `json:"pod"`
PodIP string `json:"pod_ip"`
Node string `json:"node"`
Namespace string `json:"namespace"`
}{
Status: "up",
Build: h.Build,
Host: host,
Pod: os.Getenv("KUBERNETES_PODNAME"),
PodIP: os.Getenv("KUBERNETES_NAMESPACE_POD_IP"),
Node: os.Getenv("KUBERNETES_NODENAME"),
Namespace: os.Getenv("KUBERNETES_NAMESPACE"),
}
statusCode := http.StatusOK
if err := response(w, statusCode, data); err != nil {
h.Log.Errorw("liveness", "ERROR", err)
}
h.Log.Infow("readiness", "statusCode", statusCode, "method", r.Method, "path",
r.URL.Path, "remoteAddr", r.RemoteAddr)
}
func response(w http.ResponseWriter, statusCode int, data interface{}) error {
jsonData, err := json.Marshal(data)
if err != nil {
return err
}
w.Header().Set("Content-type", "application/json")
w.WriteHeader(statusCode)
if _, err := w.Write(jsonData); err != nil {
return err
}
return err
}

View File

@@ -0,0 +1,55 @@
package handlers
import (
"encoding/json"
"expvar"
"git.hongxiaowei.com/xiaowei/service/app/services/sales-api/handlers/debug/checkgrp"
"github.com/dimfeld/httptreemux/v5"
"go.uber.org/zap"
"net/http"
"net/http/pprof"
"os"
)
func DebugStandardLibaryMux() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
mux.Handle("/debug/vars", expvar.Handler())
return mux
}
func DebugMux(build string, log *zap.SugaredLogger) http.Handler {
mux := DebugStandardLibaryMux()
cgh := checkgrp.Handlers{
Build: build,
Log: log,
}
mux.HandleFunc("/debug/readiness", cgh.Readiness)
mux.HandleFunc("/debug/liveliness", cgh.Liveliness)
return mux
}
type APIMuxConfig struct {
Shutdown chan os.Signal
Log *zap.SugaredLogger
}
func APIMux(cfg APIMuxConfig) *httptreemux.ContextMux {
mux := httptreemux.NewContextMux()
h := func(w http.ResponseWriter, r *http.Request) {
status := struct {
Status string
}{
Status: "ok",
}
json.NewEncoder(w).Encode(&status)
}
mux.Handle(http.MethodGet, "/test", h)
return mux
}