mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-13 09:50:00 +08:00
add custom health response information (#4034)
Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
This commit is contained in:
@@ -19,7 +19,7 @@ func TestRedisMetric(t *testing.T) {
|
|||||||
cfg := devserver.Config{}
|
cfg := devserver.Config{}
|
||||||
_ = conf.FillDefault(&cfg)
|
_ = conf.FillDefault(&cfg)
|
||||||
server := devserver.NewServer(cfg)
|
server := devserver.NewServer(cfg)
|
||||||
server.StartAsync()
|
server.StartAsync(cfg)
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
metricReqDur.Observe(8, "test-cmd")
|
metricReqDur.Observe(8, "test-cmd")
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ func TestSqlxMetric(t *testing.T) {
|
|||||||
_ = conf.FillDefault(&cfg)
|
_ = conf.FillDefault(&cfg)
|
||||||
cfg.Port = 6480
|
cfg.Port = 6480
|
||||||
server := devserver.NewServer(cfg)
|
server := devserver.NewServer(cfg)
|
||||||
server.StartAsync()
|
server.StartAsync(cfg)
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
metricReqDur.Observe(8, "test-cmd")
|
metricReqDur.Observe(8, "test-cmd")
|
||||||
|
|||||||
@@ -2,11 +2,12 @@ package devserver
|
|||||||
|
|
||||||
// Config is config for inner http server.
|
// Config is config for inner http server.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Enabled bool `json:",default=true"`
|
Enabled bool `json:",default=true"`
|
||||||
Host string `json:",optional"`
|
Host string `json:",optional"`
|
||||||
Port int `json:",default=6060"`
|
Port int `json:",default=6060"`
|
||||||
MetricsPath string `json:",default=/metrics"`
|
MetricsPath string `json:",default=/metrics"`
|
||||||
HealthPath string `json:",default=/healthz"`
|
HealthPath string `json:",default=/healthz"`
|
||||||
EnableMetrics bool `json:",default=true"`
|
EnableMetrics bool `json:",default=true"`
|
||||||
EnablePprof bool `json:",default=true"`
|
EnablePprof bool `json:",default=true"`
|
||||||
|
HealthRespInfo string `json:",default=OK"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,13 +32,13 @@ func NewServer(config Config) *Server {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) addRoutes() {
|
func (s *Server) addRoutes(c Config) {
|
||||||
// route path, routes list
|
// route path, routes list
|
||||||
s.handleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
|
s.handleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
|
||||||
_ = json.NewEncoder(w).Encode(s.routes)
|
_ = json.NewEncoder(w).Encode(s.routes)
|
||||||
})
|
})
|
||||||
// health
|
// health
|
||||||
s.handleFunc(s.config.HealthPath, health.CreateHttpHandler())
|
s.handleFunc(s.config.HealthPath, health.CreateHttpHandler(c.HealthRespInfo))
|
||||||
|
|
||||||
// metrics
|
// metrics
|
||||||
if s.config.EnableMetrics {
|
if s.config.EnableMetrics {
|
||||||
@@ -62,8 +62,8 @@ func (s *Server) handleFunc(pattern string, handler http.HandlerFunc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// StartAsync start inner http server background.
|
// StartAsync start inner http server background.
|
||||||
func (s *Server) StartAsync() {
|
func (s *Server) StartAsync(c Config) {
|
||||||
s.addRoutes()
|
s.addRoutes(c)
|
||||||
threading.GoSafe(func() {
|
threading.GoSafe(func() {
|
||||||
addr := fmt.Sprintf("%s:%d", s.config.Host, s.config.Port)
|
addr := fmt.Sprintf("%s:%d", s.config.Host, s.config.Port)
|
||||||
logx.Infof("Starting dev http server at %s", addr)
|
logx.Infof("Starting dev http server at %s", addr)
|
||||||
@@ -78,7 +78,7 @@ func StartAgent(c Config) {
|
|||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
if c.Enabled {
|
if c.Enabled {
|
||||||
s := NewServer(c)
|
s := NewServer(c)
|
||||||
s.StartAsync()
|
s.StartAsync(c)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,10 +44,10 @@ func AddProbe(probe Probe) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateHttpHandler create health http handler base on given probe.
|
// CreateHttpHandler create health http handler base on given probe.
|
||||||
func CreateHttpHandler() http.HandlerFunc {
|
func CreateHttpHandler(healthRespInfo string) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, _ *http.Request) {
|
return func(w http.ResponseWriter, _ *http.Request) {
|
||||||
if defaultHealthManager.IsReady() {
|
if defaultHealthManager.IsReady() {
|
||||||
_, _ = w.Write([]byte("OK"))
|
_, _ = w.Write([]byte(healthRespInfo))
|
||||||
} else {
|
} else {
|
||||||
http.Error(w, "Service Unavailable\n"+defaultHealthManager.verboseInfo(),
|
http.Error(w, "Service Unavailable\n"+defaultHealthManager.verboseInfo(),
|
||||||
http.StatusServiceUnavailable)
|
http.StatusServiceUnavailable)
|
||||||
|
|||||||
@@ -121,7 +121,7 @@ func TestAddGlobalProbes(t *testing.T) {
|
|||||||
|
|
||||||
func TestCreateHttpHandler(t *testing.T) {
|
func TestCreateHttpHandler(t *testing.T) {
|
||||||
cleanupForTest(t)
|
cleanupForTest(t)
|
||||||
srv := httptest.NewServer(CreateHttpHandler())
|
srv := httptest.NewServer(CreateHttpHandler("OK"))
|
||||||
defer srv.Close()
|
defer srv.Close()
|
||||||
|
|
||||||
resp, err := http.Get(srv.URL)
|
resp, err := http.Get(srv.URL)
|
||||||
|
|||||||
Reference in New Issue
Block a user