2017-04-18 06:22:06 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
2017-08-28 10:50:02 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2017-08-23 18:46:03 +00:00
|
|
|
"reflect"
|
2017-04-18 06:22:06 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/go-kit/kit/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMetricsRetryListener(t *testing.T) {
|
2017-08-28 10:50:02 +00:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
2017-08-23 18:46:03 +00:00
|
|
|
retryMetrics := newCollectingRetryMetrics()
|
|
|
|
retryListener := NewMetricsRetryListener(retryMetrics, "backendName")
|
2017-08-28 10:50:02 +00:00
|
|
|
retryListener.Retried(req, 1)
|
|
|
|
retryListener.Retried(req, 2)
|
2017-04-18 06:22:06 +00:00
|
|
|
|
|
|
|
wantCounterValue := float64(2)
|
|
|
|
if retryMetrics.retryCounter.counterValue != wantCounterValue {
|
|
|
|
t.Errorf("got counter value of %d, want %d", retryMetrics.retryCounter.counterValue, wantCounterValue)
|
|
|
|
}
|
2017-08-23 18:46:03 +00:00
|
|
|
|
|
|
|
wantLabelValues := []string{"backend", "backendName"}
|
|
|
|
if !reflect.DeepEqual(retryMetrics.retryCounter.lastLabelValues, wantLabelValues) {
|
|
|
|
t.Errorf("wrong label values %v used, want %v", retryMetrics.retryCounter.lastLabelValues, wantLabelValues)
|
|
|
|
}
|
2017-04-18 06:22:06 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 18:46:03 +00:00
|
|
|
// collectingRetryMetrics is an implementation of the retryMetrics interface that can be used inside tests to collect the times Add() was called.
|
2017-04-18 06:22:06 +00:00
|
|
|
type collectingRetryMetrics struct {
|
|
|
|
retryCounter *collectingCounter
|
|
|
|
}
|
|
|
|
|
2017-08-23 18:46:03 +00:00
|
|
|
func newCollectingRetryMetrics() collectingRetryMetrics {
|
2017-04-18 06:22:06 +00:00
|
|
|
return collectingRetryMetrics{retryCounter: &collectingCounter{}}
|
|
|
|
}
|
|
|
|
|
2017-08-23 18:46:03 +00:00
|
|
|
func (metrics collectingRetryMetrics) RetriesCounter() metrics.Counter {
|
2017-04-18 06:22:06 +00:00
|
|
|
return metrics.retryCounter
|
|
|
|
}
|
|
|
|
|
|
|
|
type collectingCounter struct {
|
2017-08-23 18:46:03 +00:00
|
|
|
counterValue float64
|
|
|
|
lastLabelValues []string
|
2017-04-18 06:22:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *collectingCounter) With(labelValues ...string) metrics.Counter {
|
2017-08-23 18:46:03 +00:00
|
|
|
c.lastLabelValues = labelValues
|
|
|
|
return c
|
2017-04-18 06:22:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *collectingCounter) Add(delta float64) {
|
|
|
|
c.counterValue += delta
|
|
|
|
}
|