traefik/middlewares/prometheus_test.go

190 lines
5.4 KiB
Go
Raw Normal View History

2017-01-12 13:34:54 +00:00
package middlewares
import (
"fmt"
2017-04-18 06:22:06 +00:00
"io/ioutil"
2017-01-12 13:34:54 +00:00
"net/http"
"net/http/httptest"
"strings"
"testing"
2017-06-03 12:58:35 +00:00
"github.com/containous/traefik/testhelpers"
2017-01-12 13:34:54 +00:00
"github.com/containous/traefik/types"
2017-03-08 14:17:07 +00:00
"github.com/prometheus/client_golang/prometheus"
2017-01-12 13:34:54 +00:00
"github.com/prometheus/client_golang/prometheus/promhttp"
2017-03-08 14:17:07 +00:00
dto "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/assert"
"github.com/urfave/negroni"
2017-01-12 13:34:54 +00:00
)
func TestPrometheus(t *testing.T) {
2017-04-18 06:22:06 +00:00
defer resetPrometheusValues()
metricsFamilies, err := prometheus.DefaultGatherer.Gather()
2017-03-08 14:17:07 +00:00
if err != nil {
t.Fatalf("could not gather metrics family: %s", err)
}
2017-04-18 06:22:06 +00:00
initialMetricsFamilyCount := len(metricsFamilies)
2017-03-08 14:17:07 +00:00
2017-01-12 13:34:54 +00:00
recorder := httptest.NewRecorder()
2017-06-03 12:58:35 +00:00
req1 := testhelpers.MustNewRequest(http.MethodGet, "http://localhost:3000/ok", ioutil.NopCloser(nil))
req2 := testhelpers.MustNewRequest(http.MethodGet, "http://localhost:3000/metrics", ioutil.NopCloser(nil))
2017-04-18 06:22:06 +00:00
httpHandler := setupTestHTTPHandler()
httpHandler.ServeHTTP(recorder, req1)
httpHandler.ServeHTTP(recorder, req2)
2017-01-12 13:34:54 +00:00
body := recorder.Body.String()
2017-04-18 06:22:06 +00:00
if !strings.Contains(body, reqsTotalName) {
t.Errorf("body does not contain request total entry '%s'", reqsTotalName)
2017-01-12 13:34:54 +00:00
}
2017-04-18 06:22:06 +00:00
if !strings.Contains(body, reqDurationName) {
t.Errorf("body does not contain request duration entry '%s'", reqDurationName)
}
if !strings.Contains(body, retriesTotalName) {
t.Errorf("body does not contain total retries entry '%s'", retriesTotalName)
2017-03-08 14:17:07 +00:00
}
2017-04-18 06:22:06 +00:00
metricsFamilies, err = prometheus.DefaultGatherer.Gather()
2017-03-08 14:17:07 +00:00
if err != nil {
2017-04-18 06:22:06 +00:00
t.Fatalf("could not gather metrics families: %s", err)
2017-03-08 14:17:07 +00:00
}
tests := []struct {
name string
labels map[string]string
assert func(*dto.MetricFamily)
}{
{
2017-04-18 06:22:06 +00:00
name: reqsTotalName,
2017-03-08 14:17:07 +00:00
labels: map[string]string{
"code": "200",
2017-06-03 12:58:35 +00:00
"method": http.MethodGet,
2017-03-08 14:17:07 +00:00
"service": "test",
},
assert: func(family *dto.MetricFamily) {
cv := uint(family.Metric[0].Counter.GetValue())
2017-04-18 06:22:06 +00:00
expectedCv := uint(2)
if cv != expectedCv {
t.Errorf("gathered metrics do not contain correct value for total requests, got %d expected %d", cv, expectedCv)
2017-03-08 14:17:07 +00:00
}
},
},
{
2017-04-18 06:22:06 +00:00
name: reqDurationName,
2017-03-08 14:17:07 +00:00
labels: map[string]string{
"service": "test",
"code": "200",
2017-03-08 14:17:07 +00:00
},
assert: func(family *dto.MetricFamily) {
sc := family.Metric[0].Histogram.GetSampleCount()
2017-04-18 06:22:06 +00:00
expectedSc := uint64(2)
if sc != expectedSc {
t.Errorf("gathered metrics do not contain correct sample count for request duration, got %d expected %d", sc, expectedSc)
}
},
},
{
name: retriesTotalName,
labels: map[string]string{
"service": "test",
},
assert: func(family *dto.MetricFamily) {
cv := uint(family.Metric[0].Counter.GetValue())
expectedCv := uint(1)
if cv != expectedCv {
t.Errorf("gathered metrics do not contain correct value for total retries, got '%d' expected '%d'", cv, expectedCv)
2017-03-08 14:17:07 +00:00
}
},
},
}
2017-06-03 12:58:35 +00:00
assert.Equal(t, len(tests), len(metricsFamilies)-initialMetricsFamilyCount, "gathered Traefik metrics count does not match tests count")
2017-03-08 14:17:07 +00:00
for _, test := range tests {
2017-04-18 06:22:06 +00:00
family := findMetricFamily(test.name, metricsFamilies)
2017-03-08 14:17:07 +00:00
if family == nil {
t.Errorf("gathered metrics do not contain '%s'", test.name)
continue
}
for _, label := range family.Metric[0].Label {
val, ok := test.labels[*label.Name]
if !ok {
t.Errorf("'%s' metric contains unexpected label '%s'", test.name, label)
} else if val != *label.Value {
t.Errorf("label '%s' in metric '%s' has wrong value '%s'", label, test.name, *label.Value)
}
}
test.assert(family)
}
}
2017-04-18 06:22:06 +00:00
func TestPrometheusRegisterMetricsMultipleTimes(t *testing.T) {
defer resetPrometheusValues()
recorder := httptest.NewRecorder()
2017-06-03 12:58:35 +00:00
req1 := testhelpers.MustNewRequest(http.MethodGet, "http://localhost:3000/ok", ioutil.NopCloser(nil))
2017-04-18 06:22:06 +00:00
httpHandler := setupTestHTTPHandler()
httpHandler.ServeHTTP(recorder, req1)
httpHandler = setupTestHTTPHandler()
httpHandler.ServeHTTP(recorder, req1)
metricsFamilies, err := prometheus.DefaultGatherer.Gather()
if err != nil {
t.Fatalf("could not gather metrics families: %s", err)
}
reqsTotalFamily := findMetricFamily(reqsTotalName, metricsFamilies)
if reqsTotalFamily == nil {
t.Fatalf("gathered metrics do not contain '%s'", reqsTotalName)
}
cv := uint(reqsTotalFamily.Metric[0].Counter.GetValue())
expectedCv := uint(2)
if cv != expectedCv {
t.Errorf("wrong counter value when registering metrics multiple times, got '%d' expected '%d'", cv, expectedCv)
}
}
func setupTestHTTPHandler() http.Handler {
serveMux := http.NewServeMux()
serveMux.Handle("/metrics", promhttp.Handler())
serveMux.Handle("/ok", &networkFailingHTTPHandler{failAtCalls: []int{1}, netErrorRecorder: &DefaultNetErrorRecorder{}})
2017-04-18 06:22:06 +00:00
metrics, _ := newPrometheusMetrics()
n := negroni.New()
n.Use(NewMetricsWrapper(metrics))
n.UseHandler(NewRetry(2, serveMux, NewMetricsRetryListener(metrics)))
return n
}
func resetPrometheusValues() {
_, collectors := newPrometheusMetrics()
for _, collector := range collectors {
prometheus.Unregister(collector)
}
}
func newPrometheusMetrics() (*Prometheus, []prometheus.Collector) {
prom, collectors, err := NewPrometheus("test", &types.Prometheus{})
if err != nil {
panic(fmt.Sprintf("Error creating Prometheus Metrics: %s", err))
}
return prom, collectors
}
2017-03-08 14:17:07 +00:00
func findMetricFamily(name string, families []*dto.MetricFamily) *dto.MetricFamily {
for _, family := range families {
if family.GetName() == name {
return family
}
2017-01-12 13:34:54 +00:00
}
2017-03-08 14:17:07 +00:00
return nil
2017-01-12 13:34:54 +00:00
}