forked from machbase/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdedup_test.go
154 lines (134 loc) · 4.6 KB
/
dedup_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package dedup
import (
"github.com/stretchr/testify/require"
"testing"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/metric"
)
func createMetric(name string, value int64, when time.Time) telegraf.Metric {
m, _ := metric.New(name,
map[string]string{"tag": "tag_value"},
map[string]interface{}{"value": value},
when,
)
return m
}
func createDedup(initTime time.Time) Dedup {
return Dedup{
DedupInterval: internal.Duration{Duration: 10 * time.Minute},
FlushTime: initTime,
Cache: make(map[uint64]telegraf.Metric),
}
}
func assertCacheRefresh(t *testing.T, proc *Dedup, item telegraf.Metric) {
id := item.HashID()
name := item.Name()
// cache is not empty
require.NotEqual(t, 0, len(proc.Cache))
// cache has metric with proper id
cache, present := proc.Cache[id]
require.True(t, present)
// cache has metric with proper name
require.Equal(t, name, cache.Name())
// cached metric has proper field
cValue, present := cache.GetField("value")
require.True(t, present)
iValue, _ := item.GetField("value")
require.Equal(t, cValue, iValue)
// cached metric has proper timestamp
require.Equal(t, cache.Time(), item.Time())
}
func assertCacheHit(t *testing.T, proc *Dedup, item telegraf.Metric) {
id := item.HashID()
name := item.Name()
// cache is not empty
require.NotEqual(t, 0, len(proc.Cache))
// cache has metric with proper id
cache, present := proc.Cache[id]
require.True(t, present)
// cache has metric with proper name
require.Equal(t, name, cache.Name())
// cached metric has proper field
cValue, present := cache.GetField("value")
require.True(t, present)
iValue, _ := item.GetField("value")
require.Equal(t, cValue, iValue)
// cached metric did NOT change timestamp
require.NotEqual(t, cache.Time(), item.Time())
}
func assertMetricPassed(t *testing.T, target []telegraf.Metric, source telegraf.Metric) {
// target is not empty
require.NotEqual(t, 0, len(target))
// target has metric with proper name
require.Equal(t, "m1", target[0].Name())
// target metric has proper field
tValue, present := target[0].GetField("value")
require.True(t, present)
sValue, present := source.GetField("value")
require.Equal(t, tValue, sValue)
// target metric has proper timestamp
require.Equal(t, target[0].Time(), source.Time())
}
func assertMetricSuppressed(t *testing.T, target []telegraf.Metric, source telegraf.Metric) {
// target is empty
require.Equal(t, 0, len(target))
}
func TestProcRetainsMetric(t *testing.T) {
deduplicate := createDedup(time.Now())
source := createMetric("m1", 1, time.Now())
target := deduplicate.Apply(source)
assertCacheRefresh(t, &deduplicate, source)
assertMetricPassed(t, target, source)
}
func TestSuppressRepeatedValue(t *testing.T) {
deduplicate := createDedup(time.Now())
// Create metric in the past
source := createMetric("m1", 1, time.Now().Add(-1*time.Second))
target := deduplicate.Apply(source)
source = createMetric("m1", 1, time.Now())
target = deduplicate.Apply(source)
assertCacheHit(t, &deduplicate, source)
assertMetricSuppressed(t, target, source)
}
func TestPassUpdatedValue(t *testing.T) {
deduplicate := createDedup(time.Now())
// Create metric in the past
source := createMetric("m1", 1, time.Now().Add(-1*time.Second))
target := deduplicate.Apply(source)
source = createMetric("m1", 2, time.Now())
target = deduplicate.Apply(source)
assertCacheRefresh(t, &deduplicate, source)
assertMetricPassed(t, target, source)
}
func TestPassAfterCacheExpire(t *testing.T) {
deduplicate := createDedup(time.Now())
// Create metric in the past
source := createMetric("m1", 1, time.Now().Add(-1*time.Hour))
target := deduplicate.Apply(source)
source = createMetric("m1", 1, time.Now())
target = deduplicate.Apply(source)
assertCacheRefresh(t, &deduplicate, source)
assertMetricPassed(t, target, source)
}
func TestCacheRetainsMetrics(t *testing.T) {
deduplicate := createDedup(time.Now())
// Create metric in the past 3sec
source := createMetric("m1", 1, time.Now().Add(-3*time.Hour))
deduplicate.Apply(source)
// Create metric in the past 2sec
source = createMetric("m1", 1, time.Now().Add(-2*time.Hour))
deduplicate.Apply(source)
source = createMetric("m1", 1, time.Now())
deduplicate.Apply(source)
assertCacheRefresh(t, &deduplicate, source)
}
func TestCacheShrink(t *testing.T) {
// Time offset is more than 2 * DedupInterval
deduplicate := createDedup(time.Now().Add(-2 * time.Hour))
// Time offset is more than 1 * DedupInterval
source := createMetric("m1", 1, time.Now().Add(-1*time.Hour))
deduplicate.Apply(source)
require.Equal(t, 0, len(deduplicate.Cache))
}