Merge pull request #3326 from mfaizanse/health_endpoint_params

Added param options to /healthz endpoint
This commit is contained in:
Ivan Kozlovic
2022-08-09 08:49:22 -06:00
committed by GitHub
5 changed files with 66 additions and 8 deletions

View File

@@ -2603,6 +2603,12 @@ type JSzOptions struct {
Limit int `json:"limit,omitempty"`
}
// HealthzOptions are options passed to Healthz
type HealthzOptions struct {
JSEnabled bool `json:"js-enabled,omitempty"`
JSServerOnly bool `json:"js-server-only,omitempty"`
}
type StreamDetail struct {
Name string `json:"name"`
Cluster *ClusterInfo `json:"cluster,omitempty"`
@@ -2928,7 +2934,19 @@ func (s *Server) HandleHealthz(w http.ResponseWriter, r *http.Request) {
s.httpReqStats[HealthzPath]++
s.mu.Unlock()
hs := s.healthz()
jsEnabled, err := decodeBool(w, r, "js-enabled")
if err != nil {
return
}
jsServerOnly, err := decodeBool(w, r, "js-server-only")
if err != nil {
return
}
hs := s.healthz(&HealthzOptions{
JSEnabled: jsEnabled,
JSServerOnly: jsServerOnly,
})
if hs.Error != _EMPTY_ {
s.Warnf("Healthcheck failed: %q", hs.Error)
w.WriteHeader(http.StatusServiceUnavailable)
@@ -2942,9 +2960,14 @@ func (s *Server) HandleHealthz(w http.ResponseWriter, r *http.Request) {
}
// Generate health status.
func (s *Server) healthz() *HealthStatus {
func (s *Server) healthz(opts *HealthzOptions) *HealthStatus {
var health = &HealthStatus{Status: "ok"}
// set option defaults
if opts == nil {
opts = &HealthzOptions{}
}
if err := s.readyForConnections(time.Millisecond); err != nil {
health.Status = "error"
health.Error = err.Error()
@@ -2954,6 +2977,12 @@ func (s *Server) healthz() *HealthStatus {
// Check JetStream
js := s.getJetStream()
if js == nil {
// If JetStream should be enabled then return error status.
if opts.JSEnabled {
health.Status = "unavailable"
health.Error = NewJSNotEnabledError().Error()
return health
}
return health
}
@@ -2985,6 +3014,11 @@ func (s *Server) healthz() *HealthStatus {
return health
}
// If JSServerOnly is true, then do not check further accounts, streams and consumers.
if opts.JSServerOnly {
return health
}
// Range across all accounts, the streams assigned to them, and the consumers.
// If they are assigned to this server check their status.
for acc, asa := range cc.streams {