forked from soundcloud/rsyslog_exporter
-
Notifications
You must be signed in to change notification settings - Fork 9
/
dyn_stat_test.go
128 lines (114 loc) · 3.79 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
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2024 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
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)
}
}
}