forked from aerospike/aerospike-prometheus-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher_sets_test.go
219 lines (161 loc) · 6.4 KB
/
watcher_sets_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/assert"
)
func TestSets_PassOneKeys(t *testing.T) {
watcher := new(SetWatcher)
// Check passoneKeys
passOneKeys := watcher.passOneKeys()
assert.Nil(t, passOneKeys)
}
func TestSets_PassTwoKeys(t *testing.T) {
watcher := new(SetWatcher)
// simulate, as if we are sending requestInfo to AS and get the NodeStats, these are coming from mock-data-generator
pass2Keys := make(map[string]string)
outputs := watcher.passTwoKeys(pass2Keys)
fmt.Println("TestNodeStats_PassTwoKeys: outputs: ", outputs)
assert.Equal(t, outputs, []string{"sets"})
}
func TestSets_RefreshDefault(t *testing.T) {
os.Setenv(TESTCASE_MODE, TESTCASE_MODE_TRUE)
fmt.Println("initializing config ... TestSets_RefreshDefault")
// Initialize and validate config
config = new(Config)
initConfig(DEFAULT_APE_TOML, config)
config.validateAndUpdate()
// run the test-case logic
sets_runTestCase(t)
os.Setenv(TESTCASE_MODE, TESTCASE_MODE_FALSE)
}
func TestSets_Allowlist(t *testing.T) {
os.Setenv(TESTCASE_MODE, TESTCASE_MODE_TRUE)
fmt.Println("initializing config ... TestSets_Allowlist")
// Initialize and validate config
config = new(Config)
initConfig(NS_ALLOWLIST_APE_TOML, config)
config.validateAndUpdate()
// run the test-case logic
sets_runTestCase(t)
os.Setenv(TESTCASE_MODE, TESTCASE_MODE_FALSE)
}
func TestSets_RefreshWithLabelsConfig(t *testing.T) {
os.Setenv(TESTCASE_MODE, TESTCASE_MODE_TRUE)
fmt.Println("initializing config ... TestSets_RefreshWithLabelsConfig")
// Initialize and validate config
config = new(Config)
initConfig(LABELS_APE_TOML, config)
config.validateAndUpdate()
watcher := new(SetWatcher)
gaugeStatHandler = new(GaugeStats)
initGaugeStats(METRICS_CONFIG_FILE, gaugeStatHandler)
rawMetrics := getRawMetrics()
lObserver := &Observer{}
ch := make(chan prometheus.Metric, 1000)
setsInfoKeys := []string{}
watcher.passTwoKeys(rawMetrics)
err := watcher.refresh(lObserver, setsInfoKeys, rawMetrics, ch)
if err != nil {
fmt.Println("watcher_latency_test : Unable to refresh Latencies")
} else {
domore := 1
for domore == 1 {
select {
case nsMetric := <-ch:
description := nsMetric.Desc().String()
var protobuffer dto.Metric
err := nsMetric.Write(&protobuffer)
if err != nil {
fmt.Println(" unable to get metric ", description, " data into protobuf ", err)
}
metricLabel := fmt.Sprintf("%s", protobuffer.Label)
// Description: Desc{fqName: "aerospike_latencies_read_ms_bucket", help: "read ms bucket", constLabels: {}, variableLabels: [cluster_name service ns le]}
// Label: [name:"cluster_name" value:"null" name:"le" value:"+Inf" name:"ns" value:"test" name:"service" value:"172.17.0.3:3000" ]
for eachConfigMetricLabel := range config.AeroProm.MetricLabels {
modifiedConfigMetricLabels := strings.ReplaceAll(eachConfigMetricLabel, "=", ":")
assert.Contains(t, metricLabel, modifiedConfigMetricLabels)
}
case <-time.After(1 * time.Second):
domore = 0
} // end select
}
} // end else-refresh-failure
}
/**
* complete logic to call watcher, generate-mock data and asset is part of this function
*/
func sets_runTestCase(t *testing.T) {
watcher := new(SetWatcher)
gaugeStatHandler = new(GaugeStats)
initGaugeStats(METRICS_CONFIG_FILE, gaugeStatHandler)
rawMetrics := getRawMetrics()
lObserver := &Observer{}
ch := make(chan prometheus.Metric, 1000)
setsInfoKeys := []string{}
watcher.passTwoKeys(rawMetrics)
err := watcher.refresh(lObserver, setsInfoKeys, rawMetrics, ch)
if err != nil {
fmt.Println("watcher_sets_test : Unable to refresh set stats")
} else {
domore := 1
// map of string ==> map["namespace/metric-name"]["<Label>"]
// both used to assert the return values from actual code against calculated values
lOutputValues := map[string]string{}
lOutputLabels := map[string]string{}
arrNamespaceSets := map[string]string{}
for domore == 1 {
select {
case nsMetric := <-ch:
description := nsMetric.Desc().String()
var protobuffer dto.Metric
err := nsMetric.Write(&protobuffer)
if err != nil {
fmt.Println(" unable to get metric ", description, " data into protobuf ", err)
}
metricValue := ""
metricLabel := fmt.Sprintf("%s", protobuffer.Label)
if protobuffer.Gauge != nil {
metricValue = fmt.Sprintf("%.0f", *protobuffer.Gauge.Value)
} else if protobuffer.Counter != nil {
metricValue = fmt.Sprintf("%.0f", *protobuffer.Counter.Value)
}
// Description: Desc{fqName: "aerospike_sets_truncate_lut", help: "truncate lut", constLabels: {}, variableLabels: [cluster_name service ns set]}
// Label: [name:"cluster_name" value:"null" name:"ns" value:"bar" name:"service" value:"172.17.0.3:3000" name:"set" value:"west_region" ]
metricNameFromDesc := extractMetricNameFromDesc(description)
namespaceFromLabel := extractLabelNameValueFromFullLabel(metricLabel, "ns")
setFromLabel := extractLabelNameValueFromFullLabel(metricLabel, "set")
// key will be like namespace/set/<metric_name>, this we use this check during assertion
keyName := makeKeyname(setFromLabel, metricNameFromDesc, true)
keyName = makeKeyname(namespaceFromLabel, keyName, true)
// appends to the sets array
namespaceSetKey := makeKeyname(namespaceFromLabel, setFromLabel, true)
arrNamespaceSets[namespaceSetKey] = namespaceSetKey
lOutputValues[keyName] = metricValue
lOutputLabels[keyName] = metricLabel
case <-time.After(1 * time.Second):
domore = 0
} // end select
}
// we have only 1 service in our mock-data, however loop thru service array
for _, namespaceWithSetName := range arrNamespaceSets {
lExpectedMetricNamedValues, lExpectedMetricLabels := createSetsWatcherExpectedOutputs(namespaceWithSetName)
for key := range lOutputValues {
expectedValues := lExpectedMetricNamedValues[key]
expectedLabels := lExpectedMetricLabels[key]
outputMetricValues := lOutputValues[key]
outpuMetricLabels := lOutputLabels[key]
// assert - only if the value belongs to the namespace/set we read expected values and processing
if strings.HasPrefix(key, namespaceWithSetName) {
assert.Contains(t, expectedValues, outputMetricValues)
assert.Contains(t, expectedLabels, outpuMetricLabels)
}
}
}
} // end else-refresh-failure
}