Skip to content

Commit

Permalink
Merge pull request #708 from prymitive/test-metrics
Browse files Browse the repository at this point in the history
chore(backend): add test coverage for metrics handler
  • Loading branch information
prymitive authored May 12, 2019
2 parents 0211873 + 5180461 commit 4ea7abb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
10 changes: 7 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/prymitive/karma/internal/config"
Expand All @@ -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)
}
}

0 comments on commit 4ea7abb

Please sign in to comment.