-
Notifications
You must be signed in to change notification settings - Fork 214
/
metrics_test.go
160 lines (150 loc) · 3.02 KB
/
metrics_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
package sentry
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestSanitizeKey(t *testing.T) {
tests := []struct {
name string
key string
want string
}{
{
name: "allowed characters",
key: "test.metric-1",
want: "test.metric-1",
},
{
name: "forbidden characters",
key: "@test.me^tri'@c-1{}[]",
want: "_test.me_tri_c-1_",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if diff := cmp.Diff(sanitizeKey(test.key), test.want); diff != "" {
t.Errorf("Context mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestSanitizeValue(t *testing.T) {
tests := []struct {
name string
value string
want string
}{
{
name: "allowed characters",
value: "test.metric-1",
want: "test.metric-1",
},
{
name: "forbidden characters",
value: "@test.me^tri'+@c-1{}[]",
want: "@test.metri@c-1{}[]",
},
{
name: "allow empty character",
value: "@route /test",
want: "@route /test",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if diff := cmp.Diff(sanitizeValue(test.value), test.want); diff != "" {
t.Errorf("Context mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestSerializeTags(t *testing.T) {
tests := []struct {
name string
metric abstractMetric
want string
}{
{
name: "normal tags",
metric: abstractMetric{
tags: map[string]string{"tag1": "val1", "tag2": "val2"},
},
want: "tag1:val1,tag2:val2",
},
{
name: "empty tags",
metric: abstractMetric{
tags: map[string]string{},
},
want: "",
},
{
name: "un-sanitized tags",
metric: abstractMetric{
tags: map[string]string{"@env": "pro+d", "vers^^ion": `\release@`},
},
want: "_env:prod,vers_ion:release@",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if diff := cmp.Diff(test.metric.SerializeTags(), test.want); diff != "" {
t.Errorf("Context mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestSerializeValue(t *testing.T) {
tests := []struct {
name string
metric Metric
want string
}{
{
name: "distribution metric",
metric: DistributionMetric{
values: []float64{2.0, 4.0, 3.0, 6.0},
},
want: ":2:4:3:6",
},
{
name: "gauge metric",
metric: GaugeMetric{
last: 1.0,
min: 1.0,
max: 1.0,
sum: 1.0,
count: 1.0,
},
want: ":1:1:1:1:1",
},
{
name: "set metric with strings",
metric: SetMetric[string]{
values: map[string]void{"Hello": member, "World": member},
},
want: ":4157704578:4223024711",
},
{
name: "set metric with integers",
metric: SetMetric[int]{
values: map[int]void{1: member, 2: member},
},
want: ":1:2",
},
{
name: "counter metric",
metric: CounterMetric{
value: 2.0,
},
want: ":2",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if diff := cmp.Diff(test.metric.SerializeValue(), test.want); diff != "" {
t.Errorf("Context mismatch (-want +got):\n%s", diff)
}
})
}
}