[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
Этот коммит содержится в:
Carlos Tadeu Panato Junior
2017-05-31 06:47:27 +02:00
коммит произвёл Corey Hulen
родитель 0e4add96ae
Коммит ddc996f33f
6 изменённых файлов: 53 добавлений и 13 удалений

Просмотреть файл

@@ -5,6 +5,7 @@ package api4
import ( import (
"net/http" "net/http"
"runtime"
"strconv" "strconv"
l4g "github.com/alecthomas/log4go" l4g "github.com/alecthomas/log4go"
@@ -17,6 +18,7 @@ func InitSystem() {
l4g.Debug(utils.T("api.system.init.debug")) l4g.Debug(utils.T("api.system.init.debug"))
BaseRoutes.System.Handle("/ping", ApiHandler(getSystemPing)).Methods("GET") BaseRoutes.System.Handle("/ping", ApiHandler(getSystemPing)).Methods("GET")
BaseRoutes.ApiRoot.Handle("/config", ApiSessionRequired(getConfig)).Methods("GET") BaseRoutes.ApiRoot.Handle("/config", ApiSessionRequired(getConfig)).Methods("GET")
BaseRoutes.ApiRoot.Handle("/config", ApiSessionRequired(updateConfig)).Methods("PUT") BaseRoutes.ApiRoot.Handle("/config", ApiSessionRequired(updateConfig)).Methods("PUT")
BaseRoutes.ApiRoot.Handle("/config/reload", ApiSessionRequired(configReload)).Methods("POST") 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) { 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) { func testEmail(c *Context, w http.ResponseWriter, r *http.Request) {

Просмотреть файл

@@ -12,13 +12,26 @@ import (
) )
func TestGetPing(t *testing.T) { func TestGetPing(t *testing.T) {
th := Setup().InitBasic() th := Setup().InitBasic().InitSystemAdmin()
defer TearDown() defer TearDown()
Client := th.Client Client := th.Client
b, _ := Client.GetPing() goRoutineHealthThreshold := *utils.Cfg.ServiceSettings.GoroutineHealthThreshold
if b == false { defer func() {
t.Fatal() *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 { if len(logMessage) == 0 {
t.Fatal("should return the log message") t.Fatal("should return the log message")
} }
} }

Просмотреть файл

@@ -12,6 +12,7 @@
"ReadTimeout": 300, "ReadTimeout": 300,
"WriteTimeout": 300, "WriteTimeout": 300,
"MaximumLoginAttempts": 10, "MaximumLoginAttempts": 10,
"GoroutineHealthThreshold": -1,
"GoogleDeveloperKey": "", "GoogleDeveloperKey": "",
"EnableOAuthServiceProvider": false, "EnableOAuthServiceProvider": false,
"EnableIncomingWebhooks": true, "EnableIncomingWebhooks": true,
@@ -279,4 +280,4 @@
"DataRetentionSettings": { "DataRetentionSettings": {
"Enable": false "Enable": false
} }
} }

Просмотреть файл

@@ -6234,5 +6234,9 @@
{ {
"id": "wsapi.webrtc.init.debug", "id": "wsapi.webrtc.init.debug",
"translation": "Initializing webrtc WebSocket API routes" "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. // GetPing will return ok if the running goRoutines are below the threshold and unhealthy for above.
func (c *Client4) GetPing() (bool, *Response) { func (c *Client4) GetPing() (string, *Response) {
if r, err := c.DoApiGet(c.GetSystemRoute()+"/ping", ""); err != nil { if r, err := c.DoApiGet(c.GetSystemRoute()+"/ping", ""); r.StatusCode == 500 {
return false, &Response{StatusCode: r.StatusCode, Error: err} defer r.Body.Close()
return "unhealthy", &Response{StatusCode: r.StatusCode, Error: err}
} else if err != nil {
return "", &Response{StatusCode: r.StatusCode, Error: err}
} else { } else {
defer closeBody(r) defer closeBody(r)
return CheckStatusOK(r), BuildResponse(r) return MapFromJson(r.Body)["status"], BuildResponse(r)
} }
} }

Просмотреть файл

@@ -126,6 +126,7 @@ type ServiceSettings struct {
ReadTimeout *int ReadTimeout *int
WriteTimeout *int WriteTimeout *int
MaximumLoginAttempts int MaximumLoginAttempts int
GoroutineHealthThreshold *int
GoogleDeveloperKey string GoogleDeveloperKey string
EnableOAuthServiceProvider bool EnableOAuthServiceProvider bool
EnableIncomingWebhooks bool EnableIncomingWebhooks bool
@@ -1170,6 +1171,11 @@ func (o *Config) SetDefaults() {
*o.RateLimitSettings.Enable = false *o.RateLimitSettings.Enable = false
} }
if o.ServiceSettings.GoroutineHealthThreshold == nil {
o.ServiceSettings.GoroutineHealthThreshold = new(int)
*o.ServiceSettings.GoroutineHealthThreshold = -1
}
if o.RateLimitSettings.MaxBurst == nil { if o.RateLimitSettings.MaxBurst == nil {
o.RateLimitSettings.MaxBurst = new(int) o.RateLimitSettings.MaxBurst = new(int)
*o.RateLimitSettings.MaxBurst = 100 *o.RateLimitSettings.MaxBurst = 100