mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
[PLT-5465/APIV4] GET /system/health - Improve ping health check to have limits (#6331)
* implement PLT-5465 - Improve ping health check to have limits * update /ping and delete /health * remove permission check
This commit is contained in:
committed by
Corey Hulen
parent
0e4add96ae
commit
ddc996f33f
@@ -5,6 +5,7 @@ package api4
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"runtime"
|
||||
"strconv"
|
||||
|
||||
l4g "github.com/alecthomas/log4go"
|
||||
@@ -17,6 +18,7 @@ func InitSystem() {
|
||||
l4g.Debug(utils.T("api.system.init.debug"))
|
||||
|
||||
BaseRoutes.System.Handle("/ping", ApiHandler(getSystemPing)).Methods("GET")
|
||||
|
||||
BaseRoutes.ApiRoot.Handle("/config", ApiSessionRequired(getConfig)).Methods("GET")
|
||||
BaseRoutes.ApiRoot.Handle("/config", ApiSessionRequired(updateConfig)).Methods("PUT")
|
||||
BaseRoutes.ApiRoot.Handle("/config/reload", ApiSessionRequired(configReload)).Methods("POST")
|
||||
@@ -34,7 +36,19 @@ func InitSystem() {
|
||||
}
|
||||
|
||||
func getSystemPing(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
ReturnStatusOK(w)
|
||||
|
||||
actualGoroutines := runtime.NumGoroutine()
|
||||
if *utils.Cfg.ServiceSettings.GoroutineHealthThreshold <= 0 || actualGoroutines <= *utils.Cfg.ServiceSettings.GoroutineHealthThreshold {
|
||||
ReturnStatusOK(w)
|
||||
} else {
|
||||
rdata := map[string]string{}
|
||||
rdata["status"] = "unhealthy"
|
||||
|
||||
l4g.Warn(utils.T("api.system.go_routines"), actualGoroutines, *utils.Cfg.ServiceSettings.GoroutineHealthThreshold)
|
||||
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(model.MapToJson(rdata)))
|
||||
}
|
||||
}
|
||||
|
||||
func testEmail(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -12,13 +12,26 @@ import (
|
||||
)
|
||||
|
||||
func TestGetPing(t *testing.T) {
|
||||
th := Setup().InitBasic()
|
||||
th := Setup().InitBasic().InitSystemAdmin()
|
||||
defer TearDown()
|
||||
Client := th.Client
|
||||
|
||||
b, _ := Client.GetPing()
|
||||
if b == false {
|
||||
t.Fatal()
|
||||
goRoutineHealthThreshold := *utils.Cfg.ServiceSettings.GoroutineHealthThreshold
|
||||
defer func() {
|
||||
*utils.Cfg.ServiceSettings.GoroutineHealthThreshold = goRoutineHealthThreshold
|
||||
}()
|
||||
|
||||
status, resp := Client.GetPing()
|
||||
CheckNoError(t, resp)
|
||||
if status != "OK" {
|
||||
t.Fatal("should return OK")
|
||||
}
|
||||
|
||||
*utils.Cfg.ServiceSettings.GoroutineHealthThreshold = 10
|
||||
status, resp = th.SystemAdminClient.GetPing()
|
||||
CheckInternalErrorStatus(t, resp)
|
||||
if status != "unhealthy" {
|
||||
t.Fatal("should return unhealthy")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -342,5 +355,4 @@ func TestPostLog(t *testing.T) {
|
||||
if len(logMessage) == 0 {
|
||||
t.Fatal("should return the log message")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
"ReadTimeout": 300,
|
||||
"WriteTimeout": 300,
|
||||
"MaximumLoginAttempts": 10,
|
||||
"GoroutineHealthThreshold": -1,
|
||||
"GoogleDeveloperKey": "",
|
||||
"EnableOAuthServiceProvider": false,
|
||||
"EnableIncomingWebhooks": true,
|
||||
@@ -279,4 +280,4 @@
|
||||
"DataRetentionSettings": {
|
||||
"Enable": false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6234,5 +6234,9 @@
|
||||
{
|
||||
"id": "wsapi.webrtc.init.debug",
|
||||
"translation": "Initializing webrtc WebSocket API routes"
|
||||
},
|
||||
{
|
||||
"id": "api.system.go_routines",
|
||||
"translation": "The number of running goroutines is over the health threshold %v of %v"
|
||||
}
|
||||
]
|
||||
|
||||
@@ -1737,15 +1737,18 @@ func (c *Client4) GetFileInfosForPost(postId string, etag string) ([]*FileInfo,
|
||||
}
|
||||
}
|
||||
|
||||
// General Section
|
||||
// General/System Section
|
||||
|
||||
// GetPing will ping the server and to see if it is up and running.
|
||||
func (c *Client4) GetPing() (bool, *Response) {
|
||||
if r, err := c.DoApiGet(c.GetSystemRoute()+"/ping", ""); err != nil {
|
||||
return false, &Response{StatusCode: r.StatusCode, Error: err}
|
||||
// GetPing will return ok if the running goRoutines are below the threshold and unhealthy for above.
|
||||
func (c *Client4) GetPing() (string, *Response) {
|
||||
if r, err := c.DoApiGet(c.GetSystemRoute()+"/ping", ""); r.StatusCode == 500 {
|
||||
defer r.Body.Close()
|
||||
return "unhealthy", &Response{StatusCode: r.StatusCode, Error: err}
|
||||
} else if err != nil {
|
||||
return "", &Response{StatusCode: r.StatusCode, Error: err}
|
||||
} else {
|
||||
defer closeBody(r)
|
||||
return CheckStatusOK(r), BuildResponse(r)
|
||||
return MapFromJson(r.Body)["status"], BuildResponse(r)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -126,6 +126,7 @@ type ServiceSettings struct {
|
||||
ReadTimeout *int
|
||||
WriteTimeout *int
|
||||
MaximumLoginAttempts int
|
||||
GoroutineHealthThreshold *int
|
||||
GoogleDeveloperKey string
|
||||
EnableOAuthServiceProvider bool
|
||||
EnableIncomingWebhooks bool
|
||||
@@ -1170,6 +1171,11 @@ func (o *Config) SetDefaults() {
|
||||
*o.RateLimitSettings.Enable = false
|
||||
}
|
||||
|
||||
if o.ServiceSettings.GoroutineHealthThreshold == nil {
|
||||
o.ServiceSettings.GoroutineHealthThreshold = new(int)
|
||||
*o.ServiceSettings.GoroutineHealthThreshold = -1
|
||||
}
|
||||
|
||||
if o.RateLimitSettings.MaxBurst == nil {
|
||||
o.RateLimitSettings.MaxBurst = new(int)
|
||||
*o.RateLimitSettings.MaxBurst = 100
|
||||
|
||||
Reference in New Issue
Block a user