forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 2
/
date_test.go
199 lines (162 loc) · 5.25 KB
/
date_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package date
import (
"testing"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func MustMetric(name string, tags map[string]string, fields map[string]interface{}, metricTime time.Time) telegraf.Metric {
if tags == nil {
tags = map[string]string{}
}
if fields == nil {
fields = map[string]interface{}{}
}
m, _ := metric.New(name, tags, fields, metricTime)
return m
}
func TestTagAndField(t *testing.T) {
dateFormatTagAndField := Date{
TagKey: "month",
FieldKey: "month",
}
err := dateFormatTagAndField.Init()
require.Error(t, err)
}
func TestNoOutputSpecified(t *testing.T) {
dateFormatNoOutput := Date{}
err := dateFormatNoOutput.Init()
require.Error(t, err)
}
func TestMonthTag(t *testing.T) {
dateFormatMonth := Date{
TagKey: "month",
DateFormat: "Jan",
}
err := dateFormatMonth.Init()
require.NoError(t, err)
currentTime := time.Now()
month := currentTime.Format("Jan")
m1 := MustMetric("foo", nil, nil, currentTime)
m2 := MustMetric("bar", nil, nil, currentTime)
m3 := MustMetric("baz", nil, nil, currentTime)
monthApply := dateFormatMonth.Apply(m1, m2, m3)
assert.Equal(t, map[string]string{"month": month}, monthApply[0].Tags(), "should add tag 'month'")
assert.Equal(t, map[string]string{"month": month}, monthApply[1].Tags(), "should add tag 'month'")
assert.Equal(t, map[string]string{"month": month}, monthApply[2].Tags(), "should add tag 'month'")
}
func TestMonthField(t *testing.T) {
dateFormatMonth := Date{
FieldKey: "month",
DateFormat: "Jan",
}
err := dateFormatMonth.Init()
require.NoError(t, err)
currentTime := time.Now()
month := currentTime.Format("Jan")
m1 := MustMetric("foo", nil, nil, currentTime)
m2 := MustMetric("bar", nil, nil, currentTime)
m3 := MustMetric("baz", nil, nil, currentTime)
monthApply := dateFormatMonth.Apply(m1, m2, m3)
assert.Equal(t, map[string]interface{}{"month": month}, monthApply[0].Fields(), "should add field 'month'")
assert.Equal(t, map[string]interface{}{"month": month}, monthApply[1].Fields(), "should add field 'month'")
assert.Equal(t, map[string]interface{}{"month": month}, monthApply[2].Fields(), "should add field 'month'")
}
func TestOldDateTag(t *testing.T) {
dateFormatYear := Date{
TagKey: "year",
DateFormat: "2006",
}
err := dateFormatYear.Init()
require.NoError(t, err)
m7 := MustMetric("foo", nil, nil, time.Date(1993, 05, 27, 0, 0, 0, 0, time.UTC))
customDateApply := dateFormatYear.Apply(m7)
assert.Equal(t, map[string]string{"year": "1993"}, customDateApply[0].Tags(), "should add tag 'year'")
}
func TestFieldUnix(t *testing.T) {
dateFormatUnix := Date{
FieldKey: "unix",
DateFormat: "unix",
}
err := dateFormatUnix.Init()
require.NoError(t, err)
currentTime := time.Now()
unixTime := currentTime.Unix()
m8 := MustMetric("foo", nil, nil, currentTime)
unixApply := dateFormatUnix.Apply(m8)
assert.Equal(t, map[string]interface{}{"unix": unixTime}, unixApply[0].Fields(), "should add unix time in s as field 'unix'")
}
func TestFieldUnixNano(t *testing.T) {
dateFormatUnixNano := Date{
FieldKey: "unix_ns",
DateFormat: "unix_ns",
}
err := dateFormatUnixNano.Init()
require.NoError(t, err)
currentTime := time.Now()
unixNanoTime := currentTime.UnixNano()
m9 := MustMetric("foo", nil, nil, currentTime)
unixNanoApply := dateFormatUnixNano.Apply(m9)
assert.Equal(t, map[string]interface{}{"unix_ns": unixNanoTime}, unixNanoApply[0].Fields(), "should add unix time in ns as field 'unix_ns'")
}
func TestFieldUnixMillis(t *testing.T) {
dateFormatUnixMillis := Date{
FieldKey: "unix_ms",
DateFormat: "unix_ms",
}
err := dateFormatUnixMillis.Init()
require.NoError(t, err)
currentTime := time.Now()
unixMillisTime := currentTime.UnixNano() / 1000000
m10 := MustMetric("foo", nil, nil, currentTime)
unixMillisApply := dateFormatUnixMillis.Apply(m10)
assert.Equal(t, map[string]interface{}{"unix_ms": unixMillisTime}, unixMillisApply[0].Fields(), "should add unix time in ms as field 'unix_ms'")
}
func TestFieldUnixMicros(t *testing.T) {
dateFormatUnixMicros := Date{
FieldKey: "unix_us",
DateFormat: "unix_us",
}
err := dateFormatUnixMicros.Init()
require.NoError(t, err)
currentTime := time.Now()
unixMicrosTime := currentTime.UnixNano() / 1000
m11 := MustMetric("foo", nil, nil, currentTime)
unixMicrosApply := dateFormatUnixMicros.Apply(m11)
assert.Equal(t, map[string]interface{}{"unix_us": unixMicrosTime}, unixMicrosApply[0].Fields(), "should add unix time in us as field 'unix_us'")
}
func TestDateOffset(t *testing.T) {
plugin := &Date{
TagKey: "hour",
DateFormat: "15",
DateOffset: internal.Duration{Duration: 2 * time.Hour},
}
err := plugin.Init()
require.NoError(t, err)
metric := testutil.MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{
"time_idle": 42.0,
},
time.Unix(1578603600, 0),
)
expected := []telegraf.Metric{
testutil.MustMetric(
"cpu",
map[string]string{
"hour": "23",
},
map[string]interface{}{
"time_idle": 42.0,
},
time.Unix(1578603600, 0),
),
}
actual := plugin.Apply(metric)
testutil.RequireMetricsEqual(t, expected, actual)
}