Merge pull request #1156 from prymitive/health

feat(backend): add /health endpoint for healthcheck probes
This commit is contained in:
Łukasz Mierzwa
2019-11-14 12:46:24 +00:00
committed by GitHub
4 changed files with 34 additions and 0 deletions

View File

@@ -194,6 +194,11 @@ apply as with `make run`. Example:
make PORT=5000 ALERTMANAGER_URI=https://alertmanager.example.com run-docker
### Health checks
`/health` endpoint can be used for health check probes, it always responds with
`200 OK` code and `Pong` response body.
## Configuration
Please see [CONFIGURATION](/docs/CONFIGURATION.md) for full list of available

View File

@@ -98,6 +98,7 @@ func setupRouter(router *gin.Engine) {
}))
router.GET(getViewURL("/"), index)
router.GET(getViewURL("/health"), pong)
router.GET(getViewURL("/alerts.json"), alerts)
router.GET(getViewURL("/autocomplete.json"), autocomplete)
router.GET(getViewURL("/labelNames.json"), knownLabelNames)

View File

@@ -33,6 +33,10 @@ func noCache(c *gin.Context) {
c.Header("Cache-Control", "no-cache, no-store, must-revalidate")
}
func pong(c *gin.Context) {
c.String(200, "Pong")
}
func compressResponse(data []byte) ([]byte, error) {
var b bytes.Buffer
gz, err := gzip.NewWriterLevel(&b, 3)

View File

@@ -49,6 +49,30 @@ func ginTestEngine() *gin.Engine {
return r
}
func TestHealth(t *testing.T) {
mockConfig()
r := ginTestEngine()
req := httptest.NewRequest("GET", "/health", nil)
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
t.Errorf("GET /health returned status %d", resp.Code)
}
}
func TestHealthPrefix(t *testing.T) {
os.Setenv("LISTEN_PREFIX", "/prefix")
defer os.Unsetenv("LISTEN_PREFIX")
mockConfig()
r := ginTestEngine()
req := httptest.NewRequest("GET", "/prefix/health", nil)
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
t.Errorf("GET /prefix/health returned status %d", resp.Code)
}
}
func TestIndex(t *testing.T) {
mockConfig()
r := ginTestEngine()