@@ -8,9 +8,26 @@ import (
8
8
"github.com/influxdata/telegraf/testutil"
9
9
)
10
10
11
+ func NewTestStatsd () * Statsd {
12
+ s := Statsd {}
13
+
14
+ // Make data structures
15
+ s .done = make (chan struct {})
16
+ s .in = make (chan []byte , s .AllowedPendingMessages )
17
+ s .gauges = make (map [string ]cachedgauge )
18
+ s .counters = make (map [string ]cachedcounter )
19
+ s .sets = make (map [string ]cachedset )
20
+ s .timings = make (map [string ]cachedtimings )
21
+
22
+ s .MetricSeparator = "_"
23
+ s .UDPPacketSize = UDP_PACKET_SIZE
24
+
25
+ return & s
26
+ }
27
+
11
28
// Invalid lines should return an error
12
29
func TestParse_InvalidLines (t * testing.T ) {
13
- s := NewStatsd ()
30
+ s := NewTestStatsd ()
14
31
invalid_lines := []string {
15
32
"i.dont.have.a.pipe:45g" ,
16
33
"i.dont.have.a.colon45|c" ,
@@ -34,7 +51,7 @@ func TestParse_InvalidLines(t *testing.T) {
34
51
35
52
// Invalid sample rates should be ignored and not applied
36
53
func TestParse_InvalidSampleRate (t * testing.T ) {
37
- s := NewStatsd ()
54
+ s := NewTestStatsd ()
38
55
invalid_lines := []string {
39
56
"invalid.sample.rate:45|c|0.1" ,
40
57
"invalid.sample.rate.2:45|c|@foo" ,
@@ -84,9 +101,9 @@ func TestParse_InvalidSampleRate(t *testing.T) {
84
101
}
85
102
}
86
103
87
- // Names should be parsed like . -> _ and - -> __
104
+ // Names should be parsed like . -> _
88
105
func TestParse_DefaultNameParsing (t * testing.T ) {
89
- s := NewStatsd ()
106
+ s := NewTestStatsd ()
90
107
valid_lines := []string {
91
108
"valid:1|c" ,
92
109
"valid.foo-bar:11|c" ,
@@ -108,7 +125,7 @@ func TestParse_DefaultNameParsing(t *testing.T) {
108
125
1 ,
109
126
},
110
127
{
111
- "valid_foo__bar " ,
128
+ "valid_foo-bar " ,
112
129
11 ,
113
130
},
114
131
}
@@ -123,7 +140,7 @@ func TestParse_DefaultNameParsing(t *testing.T) {
123
140
124
141
// Test that template name transformation works
125
142
func TestParse_Template (t * testing.T ) {
126
- s := NewStatsd ()
143
+ s := NewTestStatsd ()
127
144
s .Templates = []string {
128
145
"measurement.measurement.host.service" ,
129
146
}
@@ -165,7 +182,7 @@ func TestParse_Template(t *testing.T) {
165
182
166
183
// Test that template filters properly
167
184
func TestParse_TemplateFilter (t * testing.T ) {
168
- s := NewStatsd ()
185
+ s := NewTestStatsd ()
169
186
s .Templates = []string {
170
187
"cpu.idle.* measurement.measurement.host" ,
171
188
}
@@ -207,7 +224,7 @@ func TestParse_TemplateFilter(t *testing.T) {
207
224
208
225
// Test that most specific template is chosen
209
226
func TestParse_TemplateSpecificity (t * testing.T ) {
210
- s := NewStatsd ()
227
+ s := NewTestStatsd ()
211
228
s .Templates = []string {
212
229
"cpu.* measurement.foo.host" ,
213
230
"cpu.idle.* measurement.measurement.host" ,
@@ -245,7 +262,7 @@ func TestParse_TemplateSpecificity(t *testing.T) {
245
262
246
263
// Test that most specific template is chosen
247
264
func TestParse_TemplateFields (t * testing.T ) {
248
- s := NewStatsd ()
265
+ s := NewTestStatsd ()
249
266
s .Templates = []string {
250
267
"* measurement.measurement.field" ,
251
268
}
@@ -359,7 +376,7 @@ func TestParse_Fields(t *testing.T) {
359
376
360
377
// Test that tags within the bucket are parsed correctly
361
378
func TestParse_Tags (t * testing.T ) {
362
- s := NewStatsd ()
379
+ s := NewTestStatsd ()
363
380
364
381
tests := []struct {
365
382
bucket string
@@ -412,7 +429,7 @@ func TestParse_Tags(t *testing.T) {
412
429
413
430
// Test that DataDog tags are parsed
414
431
func TestParse_DataDogTags (t * testing.T ) {
415
- s := NewStatsd ()
432
+ s := NewTestStatsd ()
416
433
s .ParseDataDogTags = true
417
434
418
435
lines := []string {
@@ -490,7 +507,7 @@ func tagsForItem(m interface{}) map[string]string {
490
507
491
508
// Test that statsd buckets are parsed to measurement names properly
492
509
func TestParseName (t * testing.T ) {
493
- s := NewStatsd ()
510
+ s := NewTestStatsd ()
494
511
495
512
tests := []struct {
496
513
in_name string
@@ -506,7 +523,7 @@ func TestParseName(t *testing.T) {
506
523
},
507
524
{
508
525
"foo.bar-baz" ,
509
- "foo_bar__baz " ,
526
+ "foo_bar-baz " ,
510
527
},
511
528
}
512
529
@@ -517,8 +534,8 @@ func TestParseName(t *testing.T) {
517
534
}
518
535
}
519
536
520
- // Test with ConvertNames = false
521
- s .ConvertNames = false
537
+ // Test with separator == "."
538
+ s .MetricSeparator = "."
522
539
523
540
tests = []struct {
524
541
in_name string
@@ -549,7 +566,7 @@ func TestParseName(t *testing.T) {
549
566
// Test that measurements with the same name, but different tags, are treated
550
567
// as different outputs
551
568
func TestParse_MeasurementsWithSameName (t * testing.T ) {
552
- s := NewStatsd ()
569
+ s := NewTestStatsd ()
553
570
554
571
// Test that counters work
555
572
valid_lines := []string {
@@ -607,8 +624,8 @@ func TestParse_MeasurementsWithMultipleValues(t *testing.T) {
607
624
"valid.multiple.mixed:1|c:1|ms:2|s:1|g" ,
608
625
}
609
626
610
- s_single := NewStatsd ()
611
- s_multiple := NewStatsd ()
627
+ s_single := NewTestStatsd ()
628
+ s_multiple := NewTestStatsd ()
612
629
613
630
for _ , line := range single_lines {
614
631
err := s_single .parseStatsdLine (line )
@@ -701,7 +718,7 @@ func TestParse_MeasurementsWithMultipleValues(t *testing.T) {
701
718
702
719
// Valid lines should be parsed and their values should be cached
703
720
func TestParse_ValidLines (t * testing.T ) {
704
- s := NewStatsd ()
721
+ s := NewTestStatsd ()
705
722
valid_lines := []string {
706
723
"valid:45|c" ,
707
724
"valid:45|s" ,
@@ -720,7 +737,7 @@ func TestParse_ValidLines(t *testing.T) {
720
737
721
738
// Tests low-level functionality of gauges
722
739
func TestParse_Gauges (t * testing.T ) {
723
- s := NewStatsd ()
740
+ s := NewTestStatsd ()
724
741
725
742
// Test that gauge +- values work
726
743
valid_lines := []string {
@@ -786,7 +803,7 @@ func TestParse_Gauges(t *testing.T) {
786
803
787
804
// Tests low-level functionality of sets
788
805
func TestParse_Sets (t * testing.T ) {
789
- s := NewStatsd ()
806
+ s := NewTestStatsd ()
790
807
791
808
// Test that sets work
792
809
valid_lines := []string {
@@ -834,7 +851,7 @@ func TestParse_Sets(t *testing.T) {
834
851
835
852
// Tests low-level functionality of counters
836
853
func TestParse_Counters (t * testing.T ) {
837
- s := NewStatsd ()
854
+ s := NewTestStatsd ()
838
855
839
856
// Test that counters work
840
857
valid_lines := []string {
@@ -888,7 +905,7 @@ func TestParse_Counters(t *testing.T) {
888
905
889
906
// Tests low-level functionality of timings
890
907
func TestParse_Timings (t * testing.T ) {
891
- s := NewStatsd ()
908
+ s := NewTestStatsd ()
892
909
s .Percentiles = []int {90 }
893
910
acc := & testutil.Accumulator {}
894
911
@@ -925,7 +942,7 @@ func TestParse_Timings(t *testing.T) {
925
942
// Tests low-level functionality of timings when multiple fields is enabled
926
943
// and a measurement template has been defined which can parse field names
927
944
func TestParse_Timings_MultipleFieldsWithTemplate (t * testing.T ) {
928
- s := NewStatsd ()
945
+ s := NewTestStatsd ()
929
946
s .Templates = []string {"measurement.field" }
930
947
s .Percentiles = []int {90 }
931
948
acc := & testutil.Accumulator {}
@@ -974,7 +991,7 @@ func TestParse_Timings_MultipleFieldsWithTemplate(t *testing.T) {
974
991
// but a measurement template hasn't been defined so we can't parse field names
975
992
// In this case the behaviour should be the same as normal behaviour
976
993
func TestParse_Timings_MultipleFieldsWithoutTemplate (t * testing.T ) {
977
- s := NewStatsd ()
994
+ s := NewTestStatsd ()
978
995
s .Templates = []string {}
979
996
s .Percentiles = []int {90 }
980
997
acc := & testutil.Accumulator {}
@@ -1022,7 +1039,7 @@ func TestParse_Timings_MultipleFieldsWithoutTemplate(t *testing.T) {
1022
1039
}
1023
1040
1024
1041
func TestParse_Timings_Delete (t * testing.T ) {
1025
- s := NewStatsd ()
1042
+ s := NewTestStatsd ()
1026
1043
s .DeleteTimings = true
1027
1044
fakeacc := & testutil.Accumulator {}
1028
1045
var err error
@@ -1046,7 +1063,7 @@ func TestParse_Timings_Delete(t *testing.T) {
1046
1063
1047
1064
// Tests the delete_gauges option
1048
1065
func TestParse_Gauges_Delete (t * testing.T ) {
1049
- s := NewStatsd ()
1066
+ s := NewTestStatsd ()
1050
1067
s .DeleteGauges = true
1051
1068
fakeacc := & testutil.Accumulator {}
1052
1069
var err error
@@ -1072,7 +1089,7 @@ func TestParse_Gauges_Delete(t *testing.T) {
1072
1089
1073
1090
// Tests the delete_sets option
1074
1091
func TestParse_Sets_Delete (t * testing.T ) {
1075
- s := NewStatsd ()
1092
+ s := NewTestStatsd ()
1076
1093
s .DeleteSets = true
1077
1094
fakeacc := & testutil.Accumulator {}
1078
1095
var err error
@@ -1098,7 +1115,7 @@ func TestParse_Sets_Delete(t *testing.T) {
1098
1115
1099
1116
// Tests the delete_counters option
1100
1117
func TestParse_Counters_Delete (t * testing.T ) {
1101
- s := NewStatsd ()
1118
+ s := NewTestStatsd ()
1102
1119
s .DeleteCounters = true
1103
1120
fakeacc := & testutil.Accumulator {}
1104
1121
var err error
0 commit comments