2017-08-23 18:46:03 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
2020-03-05 14:10:07 +00:00
|
|
|
"bytes"
|
|
|
|
"strings"
|
2017-08-23 18:46:03 +00:00
|
|
|
"testing"
|
2020-03-05 14:10:07 +00:00
|
|
|
"time"
|
2017-08-23 18:46:03 +00:00
|
|
|
|
|
|
|
"github.com/go-kit/kit/metrics"
|
2020-03-05 14:10:07 +00:00
|
|
|
"github.com/go-kit/kit/metrics/generic"
|
2017-08-23 18:46:03 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-03-05 14:10:07 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-08-23 18:46:03 +00:00
|
|
|
)
|
|
|
|
|
2020-03-05 14:10:07 +00:00
|
|
|
func TestScalableHistogram(t *testing.T) {
|
|
|
|
h := generic.NewHistogram("test", 1)
|
|
|
|
sh, err := NewHistogramWithScale(h, time.Millisecond)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ticker := time.NewTicker(500 * time.Millisecond)
|
|
|
|
<-ticker.C
|
|
|
|
sh.StartAt(time.Now())
|
|
|
|
<-ticker.C
|
|
|
|
sh.ObserveDuration()
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
h.Print(&b)
|
|
|
|
|
|
|
|
extractedDurationString := strings.Split(strings.Split(b.String(), "\n")[1], " ")
|
|
|
|
measuredDuration, err := time.ParseDuration(extractedDurationString[0] + "ms")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.InDelta(t, 500*time.Millisecond, measuredDuration, float64(1*time.Millisecond))
|
|
|
|
}
|
|
|
|
|
2017-08-23 18:46:03 +00:00
|
|
|
func TestNewMultiRegistry(t *testing.T) {
|
|
|
|
registries := []Registry{newCollectingRetryMetrics(), newCollectingRetryMetrics()}
|
|
|
|
registry := NewMultiRegistry(registries)
|
|
|
|
|
2019-07-18 19:36:05 +00:00
|
|
|
registry.ServiceReqsCounter().With("key", "requests").Add(1)
|
2020-03-05 14:10:07 +00:00
|
|
|
registry.ServiceReqDurationHistogram().With("key", "durations").Observe(float64(2))
|
2019-07-18 19:36:05 +00:00
|
|
|
registry.ServiceRetriesCounter().With("key", "retries").Add(3)
|
2017-08-23 18:46:03 +00:00
|
|
|
|
|
|
|
for _, collectingRegistry := range registries {
|
2019-07-18 19:36:05 +00:00
|
|
|
cReqsCounter := collectingRegistry.ServiceReqsCounter().(*counterMock)
|
|
|
|
cReqDurationHistogram := collectingRegistry.ServiceReqDurationHistogram().(*histogramMock)
|
|
|
|
cRetriesCounter := collectingRegistry.ServiceRetriesCounter().(*counterMock)
|
2017-08-23 18:46:03 +00:00
|
|
|
|
|
|
|
wantCounterValue := float64(1)
|
|
|
|
if cReqsCounter.counterValue != wantCounterValue {
|
|
|
|
t.Errorf("Got value %f for ReqsCounter, want %f", cReqsCounter.counterValue, wantCounterValue)
|
|
|
|
}
|
|
|
|
wantHistogramValue := float64(2)
|
|
|
|
if cReqDurationHistogram.lastHistogramValue != wantHistogramValue {
|
|
|
|
t.Errorf("Got last observation %f for ReqDurationHistogram, want %f", cReqDurationHistogram.lastHistogramValue, wantHistogramValue)
|
|
|
|
}
|
|
|
|
wantCounterValue = float64(3)
|
|
|
|
if cRetriesCounter.counterValue != wantCounterValue {
|
|
|
|
t.Errorf("Got value %f for RetriesCounter, want %f", cRetriesCounter.counterValue, wantCounterValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, []string{"key", "requests"}, cReqsCounter.lastLabelValues)
|
|
|
|
assert.Equal(t, []string{"key", "durations"}, cReqDurationHistogram.lastLabelValues)
|
|
|
|
assert.Equal(t, []string{"key", "retries"}, cRetriesCounter.lastLabelValues)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCollectingRetryMetrics() Registry {
|
|
|
|
return &standardRegistry{
|
2019-07-18 19:36:05 +00:00
|
|
|
serviceReqsCounter: &counterMock{},
|
|
|
|
serviceReqDurationHistogram: &histogramMock{},
|
|
|
|
serviceRetriesCounter: &counterMock{},
|
2017-08-23 18:46:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type counterMock struct {
|
|
|
|
counterValue float64
|
|
|
|
lastLabelValues []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *counterMock) With(labelValues ...string) metrics.Counter {
|
|
|
|
c.lastLabelValues = labelValues
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *counterMock) Add(delta float64) {
|
|
|
|
c.counterValue += delta
|
|
|
|
}
|
|
|
|
|
|
|
|
type histogramMock struct {
|
|
|
|
lastHistogramValue float64
|
|
|
|
lastLabelValues []string
|
|
|
|
}
|
|
|
|
|
2020-03-05 14:10:07 +00:00
|
|
|
func (c *histogramMock) With(labelValues ...string) ScalableHistogram {
|
2017-08-23 18:46:03 +00:00
|
|
|
c.lastLabelValues = labelValues
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-03-05 14:10:07 +00:00
|
|
|
func (c *histogramMock) Start() {}
|
|
|
|
|
|
|
|
func (c *histogramMock) StartAt(t time.Time) {}
|
|
|
|
|
|
|
|
func (c *histogramMock) ObserveDuration() {}
|
|
|
|
|
|
|
|
func (c *histogramMock) Observe(v float64) {
|
|
|
|
c.lastHistogramValue = v
|
2017-08-23 18:46:03 +00:00
|
|
|
}
|