Merge pull request #708 from prymitive/test-metrics

chore(backend): add test coverage for metrics handler
This commit is contained in:
Łukasz Mierzwa
2019-05-12 08:07:05 +01:00
committed by GitHub
2 changed files with 21 additions and 3 deletions

10
main.go
View File

@@ -100,6 +100,12 @@ func setupRouter(router *gin.Engine) {
router.NoRoute(notFound)
}
func setupMetrics(router *gin.Engine) {
prom := ginprometheus.NewPrometheus("gin")
prom.MetricsPath = getViewURL("/metrics")
prom.Use(router)
}
func setupUpstreams() {
for _, s := range config.Config.Alertmanager.Servers {
@@ -207,9 +213,7 @@ func main() {
t = loadTemplate(t, "ui/build/index.html")
router.SetHTMLTemplate(t)
prom := ginprometheus.NewPrometheus("gin")
prom.MetricsPath = getViewURL("/metrics")
prom.Use(router)
setupMetrics(router)
if config.Config.Debug {
ginpprof.Wrapper(router)

View File

@@ -1,6 +1,8 @@
package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/prymitive/karma/internal/config"
@@ -26,3 +28,15 @@ func TestLogConfig(t *testing.T) {
}
}
}
func TestMetrics(t *testing.T) {
mockConfig()
r := ginTestEngine()
setupMetrics(r)
req := httptest.NewRequest("GET", "/metrics", nil)
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
t.Errorf("GET /metrics returned status %d", resp.Code)
}
}