forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
newrelic_test.go
180 lines (172 loc) · 4.63 KB
/
newrelic_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
package newrelic
import (
"math"
"testing"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/testutil"
"github.com/newrelic/newrelic-telemetry-sdk-go/telemetry"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestBasic(t *testing.T) {
nr := &NewRelic{
MetricPrefix: "Test",
InsightsKey: "12345",
Timeout: internal.Duration{Duration: time.Second * 5},
}
if testing.Short() {
t.Skip("skipping test in short mode.")
}
err := nr.Connect()
require.NoError(t, err)
err = nr.Write(testutil.MockMetrics())
assert.Contains(t, err.Error(), "unable to harvest metrics")
}
func TestNewRelic_Write(t *testing.T) {
type args struct {
metrics []telegraf.Metric
}
tests := []struct {
name string
metrics []telegraf.Metric
auditMessage string
wantErr bool
}{
{
name: "Test: Basic mock metric write",
metrics: testutil.MockMetrics(),
wantErr: false,
auditMessage: `"metrics":[{"name":"test1.value","type":"gauge","value":1,"timestamp":1257894000000,"attributes":{"tag1":"value1"}}]`,
},
{
name: "Test: Test string ",
metrics: []telegraf.Metric{
testutil.TestMetric("value1", "test_String"),
},
wantErr: false,
auditMessage: "",
},
{
name: "Test: Test int64 ",
metrics: []telegraf.Metric{
testutil.TestMetric(int64(15), "test_int64"),
},
wantErr: false,
auditMessage: `"metrics":[{"name":"test_int64.value","type":"gauge","value":15,"timestamp":1257894000000,"attributes":{"tag1":"value1"}}]`,
},
{
name: "Test: Test uint64 ",
metrics: []telegraf.Metric{
testutil.TestMetric(uint64(20), "test_uint64"),
},
wantErr: false,
auditMessage: `"metrics":[{"name":"test_uint64.value","type":"gauge","value":20,"timestamp":1257894000000,"attributes":{"tag1":"value1"}}]`,
},
{
name: "Test: Test bool true ",
metrics: []telegraf.Metric{
testutil.TestMetric(bool(true), "test_bool_true"),
},
wantErr: false,
auditMessage: `"metrics":[{"name":"test_bool_true.value","type":"gauge","value":1,"timestamp":1257894000000,"attributes":{"tag1":"value1"}}]`,
},
{
name: "Test: Test bool false ",
metrics: []telegraf.Metric{
testutil.TestMetric(bool(false), "test_bool_false"),
},
wantErr: false,
auditMessage: `"metrics":[{"name":"test_bool_false.value","type":"gauge","value":0,"timestamp":1257894000000,"attributes":{"tag1":"value1"}}]`,
},
{
name: "Test: Test max float64 ",
metrics: []telegraf.Metric{
testutil.TestMetric(math.MaxFloat64, "test_maxfloat64"),
},
wantErr: false,
auditMessage: `"metrics":[{"name":"test_maxfloat64.value","type":"gauge","value":1.7976931348623157e+308,"timestamp":1257894000000,"attributes":{"tag1":"value1"}}]`,
},
{
name: "Test: Test NAN ",
metrics: []telegraf.Metric{
testutil.TestMetric(math.NaN, "test_NaN"),
},
wantErr: false,
auditMessage: ``,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var auditLog map[string]interface{}
nr := &NewRelic{}
nr.harvestor, _ = telemetry.NewHarvester(
telemetry.ConfigHarvestPeriod(0),
func(cfg *telemetry.Config) {
cfg.APIKey = "dummyTestKey"
cfg.HarvestPeriod = 0
cfg.HarvestTimeout = 0
cfg.AuditLogger = func(e map[string]interface{}) {
auditLog = e
}
})
err := nr.Write(tt.metrics)
assert.NoError(t, err)
if auditLog["data"] != nil {
assert.Contains(t, auditLog["data"], tt.auditMessage)
} else {
assert.Contains(t, "", tt.auditMessage)
}
if (err != nil) != tt.wantErr {
t.Errorf("NewRelic.Write() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestNewRelic_Connect(t *testing.T) {
tests := []struct {
name string
newrelic *NewRelic
wantErr bool
}{
{
name: "Test: No Insights key",
newrelic: &NewRelic{
MetricPrefix: "prefix",
},
wantErr: true,
},
{
name: "Test: Insights key",
newrelic: &NewRelic{
InsightsKey: "12312133",
MetricPrefix: "prefix",
},
wantErr: false,
},
{
name: "Test: Only Insights key",
newrelic: &NewRelic{
InsightsKey: "12312133",
},
wantErr: false,
},
{
name: "Test: Insights key and Timeout",
newrelic: &NewRelic{
InsightsKey: "12312133",
Timeout: internal.Duration{Duration: time.Second * 5},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
nr := tt.newrelic
if err := nr.Connect(); (err != nil) != tt.wantErr {
t.Errorf("NewRelic.Connect() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}