forked from prometheus-community/rsyslog_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdyn_stat_test.go
115 lines (102 loc) · 3.21 KB
/
dyn_stat_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
package main
import (
"reflect"
"testing"
)
func TestGetDynStat(t *testing.T) {
log := []byte(`{ "name": "global", "origin": "dynstats", "values": { "msg_per_host.ops_overflow": 1, "msg_per_host.new_metric_add": 3, "msg_per_host.no_metric": 0, "msg_per_host.metrics_purged": 0, "msg_per_host.ops_ignored": 0 } }`)
values := map[string]int64{
"msg_per_host.ops_overflow": 1,
"msg_per_host.new_metric_add": 3,
"msg_per_host.no_metric": 0,
"msg_per_host.metrics_purged": 0,
"msg_per_host.ops_ignored": 0,
}
if want, got := rsyslogDynStat, getStatType(log); want != got {
t.Errorf("detected pstat type should be %d but is %d", want, got)
}
pstat, err := newDynStatFromJSON(log)
if err != nil {
t.Fatalf("expected parsing dynamic stat not to fail, got: %v", err)
}
if want, got := "global", pstat.Name; want != got {
t.Errorf("invalid name, want '%s', got '%s'", want, got)
}
if want, got := values, pstat.Values; !reflect.DeepEqual(want, got) {
t.Errorf("unexpected values, want: %+v got: %+v", want, got)
}
}
func TestDynStatToPoints(t *testing.T) {
log := []byte(`{ "name": "global", "origin": "dynstats", "values": { "msg_per_host.ops_overflow": 1, "msg_per_host.new_metric_add": 3, "msg_per_host.no_metric": 0, "msg_per_host.metrics_purged": 0, "msg_per_host.ops_ignored": 0 } }`)
wants := map[string]point{
"msg_per_host.ops_overflow": point{
Name: "dynstat_global",
Type: counter,
Value: 1,
Description: "dynamic statistic bucket global",
LabelName: "counter",
LabelValue: "msg_per_host.ops_overflow",
},
"msg_per_host.new_metric_add": point{
Name: "dynstat_global",
Type: counter,
Value: 3,
Description: "dynamic statistic bucket global",
LabelName: "counter",
LabelValue: "msg_per_host.new_metric_add",
},
"msg_per_host.no_metric": point{
Name: "dynstat_global",
Type: counter,
Value: 0,
Description: "dynamic statistic bucket global",
LabelName: "counter",
LabelValue: "msg_per_host.no_metric",
},
"msg_per_host.metrics_purged": point{
Name: "dynstat_global",
Type: counter,
Value: 0,
Description: "dynamic statistic bucket global",
LabelName: "counter",
LabelValue: "msg_per_host.metrics_purged",
},
"msg_per_host.ops_ignored": point{
Name: "dynstat_global",
Type: counter,
Value: 0,
Description: "dynamic statistic bucket global",
LabelName: "counter",
LabelValue: "msg_per_host.ops_ignored",
},
}
seen := map[string]bool{}
for name := range wants {
seen[name] = false
}
pstat, err := newDynStatFromJSON(log)
if err != nil {
t.Fatalf("expected parsing dyn stat not to fail, got: %v", err)
}
points := pstat.toPoints()
for _, got := range points {
key := got.LabelValue
want, ok := wants[key]
if !ok {
t.Errorf("unexpected point, got: %+v", got)
continue
}
if !reflect.DeepEqual(want, *got) {
t.Errorf("expected point to be %+v, got %+v", want, got)
}
if seen[key] {
t.Errorf("point seen multiple times: %+v", got)
}
seen[key] = true
}
for name, ok := range seen {
if !ok {
t.Errorf("expected to see point with key %s, but did not", name)
}
}
}