Skip to content

Commit

Permalink
feat: expose metrics from expvar (#52)
Browse files Browse the repository at this point in the history
* feat: expose metrics from expvar

* adding tests for metrics

* small changes after PR
  • Loading branch information
pawels-optimizely authored Oct 22, 2019
1 parent 18b420c commit 445c9d5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/admin/handlers/admin_entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package handlers

import (
"expvar"
"net/http"
"os"

Expand Down Expand Up @@ -107,3 +108,8 @@ func (a Admin) AppInfoHeader(next http.Handler) http.Handler {
}
return http.HandlerFunc(fn)
}

// Metrics returns expvar info
func (a Admin) Metrics(w http.ResponseWriter, r *http.Request) {
expvar.Handler().ServeHTTP(w, r)
}
23 changes: 23 additions & 0 deletions pkg/admin/handlers/admin_entities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package handlers

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -157,3 +158,25 @@ func TestAppInfoHeaderHandler(t *testing.T) {
assert.Equal(t, res.Header["Author"], []string{"2"})
assert.Equal(t, res.Header["App-Name"], []string{"3"})
}

func TestMetrics(t *testing.T) {

req, _ := http.NewRequest("GET", "/metrics", nil)

rr := httptest.NewRecorder()
a := NewAdmin("1", "2", "3", nil)
http.HandlerFunc(a.Metrics).ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code, "Status code differs")

var expVarMap JSON
err := json.Unmarshal(rr.Body.Bytes(), &expVarMap)
assert.Nil(t, err)

memStatsMap, ok := expVarMap["memstats"]
assert.True(t, ok)

assert.Contains(t, memStatsMap, "Alloc")
assert.Contains(t, memStatsMap, "BySize")
assert.Contains(t, memStatsMap, "BuckHashSys")
}
1 change: 1 addition & 0 deletions pkg/admin/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func NewRouter(srvcs []handlers.HealthChecker) *chi.Mux {

r.Get("/health", optlyAdmin.Health)
r.Get("/info", optlyAdmin.AppInfo)
r.Get("/metrics", optlyAdmin.Metrics)

return r
}

0 comments on commit 445c9d5

Please sign in to comment.