-
Notifications
You must be signed in to change notification settings - Fork 1
/
metric_test.go
66 lines (62 loc) · 1.97 KB
/
metric_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
package exporters
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestEncodeInfluxLine(t *testing.T) {
cases := []struct {
metric *Metric
out string
}{
{
metric: &Metric{Name: "req", Type: "counter", Time: time.Unix(1667123357, 0),
Labels: map[string]string{"host": "localhost"},
Fields: map[string]float64{"count": 1}},
out: "req,host=localhost count=1 1667123357",
},
{
metric: &Metric{Name: "req", Type: "counter", Time: time.Unix(1667123357, 0),
Labels: map[string]string{"host": "localhost", "region": "us-west-2"},
Fields: map[string]float64{"count": 1, "max": 10}},
out: "req,host=localhost,region=us-west-2 count=1,max=10 1667123357",
},
{
// name with labels
metric: &Metric{Name: "req,method=POST", Type: "counter", Time: time.Unix(1667123357, 0),
Labels: map[string]string{"host": "localhost", "region": "us-west-2"},
Fields: map[string]float64{"count": 1, "max": 10}},
out: "req,method=POST,host=localhost,region=us-west-2 count=1,max=10 1667123357",
},
}
assert := assert.New(t)
for _, tc := range cases {
line := tc.metric.EncodeInfluxLine("s")
assert.Equal(line, tc.out)
}
}
func TestEncodePromLines(t *testing.T) {
cases := []struct {
metric *Metric
out string
}{
{
metric: &Metric{Name: "req", Type: "counter", Time: time.Unix(1667123357, 0),
Labels: map[string]string{"host": "localhost"},
Fields: map[string]float64{"count": 1}},
out: `req_count{host="localhost"} 1 1667123357000`,
},
{
metric: &Metric{Name: "req", Type: "counter", Time: time.Unix(1667123357, 0),
Labels: map[string]string{"host": "localhost", "region": "us-west-2"},
Fields: map[string]float64{"count": 1, "max": 10}},
out: `req_count{host="localhost",region="us-west-2"} 1 1667123357000
req_max{host="localhost",region="us-west-2"} 10 1667123357000`,
},
}
assert := assert.New(t)
for _, tc := range cases {
line := tc.metric.EncodePromLines()
assert.Equal(line, tc.out)
}
}