summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/armon/go-metrics/metrics_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/armon/go-metrics/metrics_test.go')
-rw-r--r--vendor/github.com/armon/go-metrics/metrics_test.go151
1 files changed, 150 insertions, 1 deletions
diff --git a/vendor/github.com/armon/go-metrics/metrics_test.go b/vendor/github.com/armon/go-metrics/metrics_test.go
index f5b2a4c79..8556a0019 100644
--- a/vendor/github.com/armon/go-metrics/metrics_test.go
+++ b/vendor/github.com/armon/go-metrics/metrics_test.go
@@ -9,7 +9,7 @@ import (
func mockMetric() (*MockSink, *Metrics) {
m := &MockSink{}
- met := &Metrics{sink: m}
+ met := &Metrics{Config: Config{FilterDefault: true}, sink: m}
return m, met
}
@@ -24,6 +24,19 @@ func TestMetrics_SetGauge(t *testing.T) {
}
m, met = mockMetric()
+ labels := []Label{{"a", "b"}}
+ met.SetGaugeWithLabels([]string{"key"}, float32(1), labels)
+ if m.keys[0][0] != "key" {
+ t.Fatalf("")
+ }
+ if m.vals[0] != 1 {
+ t.Fatalf("")
+ }
+ if !reflect.DeepEqual(m.labels[0], labels) {
+ t.Fatalf("")
+ }
+
+ m, met = mockMetric()
met.HostName = "test"
met.EnableHostname = true
met.SetGauge([]string{"key"}, float32(1))
@@ -97,6 +110,19 @@ func TestMetrics_IncrCounter(t *testing.T) {
}
m, met = mockMetric()
+ labels := []Label{{"a", "b"}}
+ met.IncrCounterWithLabels([]string{"key"}, float32(1), labels)
+ if m.keys[0][0] != "key" {
+ t.Fatalf("")
+ }
+ if m.vals[0] != 1 {
+ t.Fatalf("")
+ }
+ if !reflect.DeepEqual(m.labels[0], labels) {
+ t.Fatalf("")
+ }
+
+ m, met = mockMetric()
met.EnableTypePrefix = true
met.IncrCounter([]string{"key"}, float32(1))
if m.keys[0][0] != "counter" || m.keys[0][1] != "key" {
@@ -128,6 +154,19 @@ func TestMetrics_AddSample(t *testing.T) {
}
m, met = mockMetric()
+ labels := []Label{{"a", "b"}}
+ met.AddSampleWithLabels([]string{"key"}, float32(1), labels)
+ if m.keys[0][0] != "key" {
+ t.Fatalf("")
+ }
+ if m.vals[0] != 1 {
+ t.Fatalf("")
+ }
+ if !reflect.DeepEqual(m.labels[0], labels) {
+ t.Fatalf("")
+ }
+
+ m, met = mockMetric()
met.EnableTypePrefix = true
met.AddSample([]string{"key"}, float32(1))
if m.keys[0][0] != "sample" || m.keys[0][1] != "key" {
@@ -162,6 +201,20 @@ func TestMetrics_MeasureSince(t *testing.T) {
m, met = mockMetric()
met.TimerGranularity = time.Millisecond
+ labels := []Label{{"a", "b"}}
+ met.MeasureSinceWithLabels([]string{"key"}, n, labels)
+ if m.keys[0][0] != "key" {
+ t.Fatalf("")
+ }
+ if m.vals[0] > 0.1 {
+ t.Fatalf("")
+ }
+ if !reflect.DeepEqual(m.labels[0], labels) {
+ t.Fatalf("")
+ }
+
+ m, met = mockMetric()
+ met.TimerGranularity = time.Millisecond
met.EnableTypePrefix = true
met.MeasureSince([]string{"key"}, n)
if m.keys[0][0] != "timer" || m.keys[0][1] != "key" {
@@ -260,3 +313,99 @@ func TestInsert(t *testing.T) {
t.Fatalf("bad insert %v %v", exp, out)
}
}
+
+func TestMetrics_Filter_Blacklist(t *testing.T) {
+ m := &MockSink{}
+ conf := DefaultConfig("")
+ conf.AllowedPrefixes = []string{"service", "debug.thing"}
+ conf.BlockedPrefixes = []string{"debug"}
+ conf.EnableHostname = false
+ met, err := New(conf, m)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ // Allowed by default
+ key := []string{"thing"}
+ met.SetGauge(key, 1)
+ if !reflect.DeepEqual(m.keys[0], key) {
+ t.Fatalf("key doesn't exist %v, %v", m.keys[0], key)
+ }
+ if m.vals[0] != 1 {
+ t.Fatalf("bad val: %v", m.vals[0])
+ }
+
+ // Allowed by filter
+ key = []string{"service", "thing"}
+ met.SetGauge(key, 2)
+ if !reflect.DeepEqual(m.keys[1], key) {
+ t.Fatalf("key doesn't exist")
+ }
+ if m.vals[1] != 2 {
+ t.Fatalf("bad val: %v", m.vals[1])
+ }
+
+ // Allowed by filter, subtree of a blocked entry
+ key = []string{"debug", "thing"}
+ met.SetGauge(key, 3)
+ if !reflect.DeepEqual(m.keys[2], key) {
+ t.Fatalf("key doesn't exist")
+ }
+ if m.vals[2] != 3 {
+ t.Fatalf("bad val: %v", m.vals[2])
+ }
+
+ // Blocked by filter
+ key = []string{"debug", "other-thing"}
+ met.SetGauge(key, 4)
+ if len(m.keys) != 3 {
+ t.Fatalf("key shouldn't exist")
+ }
+}
+
+func TestMetrics_Filter_Whitelist(t *testing.T) {
+ m := &MockSink{}
+ conf := DefaultConfig("")
+ conf.AllowedPrefixes = []string{"service", "debug.thing"}
+ conf.BlockedPrefixes = []string{"debug"}
+ conf.FilterDefault = false
+ conf.EnableHostname = false
+ met, err := New(conf, m)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ // Blocked by default
+ key := []string{"thing"}
+ met.SetGauge(key, 1)
+ if len(m.keys) != 0 {
+ t.Fatalf("key should not exist")
+ }
+
+ // Allowed by filter
+ key = []string{"service", "thing"}
+ met.SetGauge(key, 2)
+ if !reflect.DeepEqual(m.keys[0], key) {
+ t.Fatalf("key doesn't exist")
+ }
+ if m.vals[0] != 2 {
+ t.Fatalf("bad val: %v", m.vals[0])
+ }
+
+ // Allowed by filter, subtree of a blocked entry
+ key = []string{"debug", "thing"}
+ met.SetGauge(key, 3)
+ if !reflect.DeepEqual(m.keys[1], key) {
+ t.Fatalf("key doesn't exist")
+ }
+ if m.vals[1] != 3 {
+ t.Fatalf("bad val: %v", m.vals[1])
+ }
+
+ // Blocked by filter
+ key = []string{"debug", "other-thing"}
+ met.SetGauge(key, 4)
+ if len(m.keys) != 2 {
+ t.Fatalf("key shouldn't exist")
+ }
+}