2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
26 func TestMetricCounter(t *testing.T) {
27 TestCounterOpt := CounterOpts{Name: "CounterBlaah1", Help: "CounterBlaah1"}
28 ret1 := Metric.RegisterCounter(TestCounterOpt, "TestMetricCounter")
30 ret2 := Metric.RegisterCounter(TestCounterOpt, "TestMetricCounter")
33 t.Errorf("ret1 not same than ret2. cache not working?")
37 func TestMetricCounterGroup(t *testing.T) {
38 var TestCounterOpts = []CounterOpts{
39 {Name: "CounterBlaah1", Help: "CounterBlaah1"},
40 {Name: "CounterBlaah2", Help: "CounterBlaah2"},
41 {Name: "CounterBlaah3", Help: "CounterBlaah3"},
42 {Name: "CounterBlaah4", Help: "CounterBlaah4"},
45 ret1 := Metric.RegisterCounterGroup(TestCounterOpts, "TestMetricCounterGroup")
48 t.Errorf("ret1 counter group is empty")
51 ret1["CounterBlaah1"].Inc()
52 ret1["CounterBlaah2"].Inc()
53 ret1["CounterBlaah3"].Inc()
54 ret1["CounterBlaah4"].Inc()
56 ret2 := Metric.RegisterCounterGroup(TestCounterOpts, "TestMetricCounterGroup")
59 t.Errorf("ret2 counter group is empty")
62 ret2["CounterBlaah1"].Inc()
63 ret2["CounterBlaah2"].Inc()
64 ret2["CounterBlaah3"].Inc()
65 ret2["CounterBlaah4"].Inc()
67 if len(ret1) != len(ret2) {
68 t.Errorf("ret1 len %d differs from ret2 len %d", len(ret1), len(ret2))
72 func TestMetricLabeledCounter(t *testing.T) {
75 c_1_1 := Metric.RegisterLabeledCounter(
76 CounterOpts{Name: "counter1", Help: "counter1"},
77 []string{"name", "event"},
78 []string{"name1", "event1"},
81 c_1_2 := Metric.RegisterLabeledCounter(
82 CounterOpts{Name: "counter1", Help: "counter1"},
83 []string{"name", "event"},
84 []string{"name1", "event1"},
90 t.Errorf("c_1_1 not same than c_1_2. cache not working?")
95 c_2_1 := Metric.RegisterLabeledCounter(
96 CounterOpts{Name: "counter1", Help: "counter1"},
97 []string{"name", "event"},
98 []string{"name1", "event2"},
101 c_2_2 := Metric.RegisterLabeledCounter(
102 CounterOpts{Name: "counter1", Help: "counter1"},
103 []string{"name", "event"},
104 []string{"name1", "event2"},
110 t.Errorf("c_2_1 not same than c_2_2. cache not working?")
114 t.Errorf("c_1_1 same than c_2_1. what?")
117 t.Errorf("c_1_2 same than c_2_2. what?")
122 func TestMetricLabeledCounterMismatch(t *testing.T) {
123 Metric.RegisterLabeledCounter(
124 CounterOpts{Name: "counter1", Help: "counter1"},
125 []string{"name", "event"},
126 []string{"name1", "event1"},
129 Metric.RegisterLabeledCounter(
130 CounterOpts{Name: "counter1", Help: "counter1"},
131 []string{"name", "eventmismatch"},
132 []string{"name1", "event1"},
136 func TestMetricLabeledCounterGroup(t *testing.T) {
139 c_grp1 := Metric.RegisterLabeledCounterGroup(
140 []CounterOpts{{Name: "counter1", Help: "counter1"}},
141 []string{"name", "event"},
142 []string{"name1", "event1"},
145 if _, ok := c_grp1["counter1"]; ok == false {
146 t.Errorf("c_grp1 counter1 not exists")
148 c_grp1["counter1"].Inc()
152 c_grp2 := Metric.RegisterLabeledCounterGroup(
153 []CounterOpts{{Name: "counter1", Help: "counter1"}},
154 []string{"name", "event"},
155 []string{"name1", "event2"},
158 if _, ok := c_grp2["counter1"]; ok == false {
159 t.Errorf("c_grp2 counter1 not exists")
161 c_grp2["counter1"].Inc()
164 func TestMetricGauge(t *testing.T) {
165 TestGaugeOpts := CounterOpts{Name: "GaugeBlaah1", Help: "GaugeBlaah1"}
166 ret1 := Metric.RegisterGauge(TestGaugeOpts, "TestMetricGauge")
168 ret2 := Metric.RegisterGauge(TestGaugeOpts, "TestMetricGauge")
171 t.Errorf("ret1 not same than ret2. cache not working?")
175 func TestMetricGaugeGroup(t *testing.T) {
176 var TestGaugeOpts = []CounterOpts{
177 {Name: "GaugeBlaah1", Help: "GaugeBlaah1"},
178 {Name: "GaugeBlaah2", Help: "GaugeBlaah2"},
179 {Name: "GaugeBlaah3", Help: "GaugeBlaah3"},
180 {Name: "GaugeBlaah4", Help: "GaugeBlaah4"},
183 ret1 := Metric.RegisterGaugeGroup(TestGaugeOpts, "TestMetricGaugeGroup")
186 t.Errorf("ret1 gauge group is empty")
189 ret1["GaugeBlaah1"].Inc()
190 ret1["GaugeBlaah2"].Inc()
191 ret1["GaugeBlaah3"].Inc()
192 ret1["GaugeBlaah4"].Inc()
194 ret2 := Metric.RegisterGaugeGroup(TestGaugeOpts, "TestMetricGaugeGroup")
197 t.Errorf("ret2 gauge group is empty")
200 ret2["GaugeBlaah1"].Inc()
201 ret2["GaugeBlaah2"].Inc()
202 ret2["GaugeBlaah3"].Inc()
203 ret2["GaugeBlaah4"].Inc()
205 if len(ret1) != len(ret2) {
206 t.Errorf("ret1 len %d differs from ret2 len %d", len(ret1), len(ret2))
210 func TestMetricLabeledGauge(t *testing.T) {
213 c_1_1 := Metric.RegisterLabeledGauge(
214 CounterOpts{Name: "gauge1", Help: "gauge1"},
215 []string{"name", "event"},
216 []string{"name1", "event1"},
219 c_1_2 := Metric.RegisterLabeledGauge(
220 CounterOpts{Name: "gauge1", Help: "gauge1"},
221 []string{"name", "event"},
222 []string{"name1", "event1"},
228 t.Errorf("c_1_1 not same than c_1_2. cache not working?")
233 c_2_1 := Metric.RegisterLabeledGauge(
234 CounterOpts{Name: "gauge1", Help: "gauge1"},
235 []string{"name", "event"},
236 []string{"name1", "event2"},
239 c_2_2 := Metric.RegisterLabeledGauge(
240 CounterOpts{Name: "gauge1", Help: "gauge1"},
241 []string{"name", "event"},
242 []string{"name1", "event2"},
248 t.Errorf("c_2_1 not same than c_2_2. cache not working?")
252 t.Errorf("c_1_1 same than c_2_1. what?")
255 t.Errorf("c_1_2 same than c_2_2. what?")
260 func TestMetricLabeledGaugeMismatch(t *testing.T) {
261 Metric.RegisterLabeledGauge(
262 CounterOpts{Name: "gauge1", Help: "gauge1"},
263 []string{"name", "event"},
264 []string{"name1", "event1"},
267 Metric.RegisterLabeledGauge(
268 CounterOpts{Name: "gauge1", Help: "gauge1"},
269 []string{"name", "eventmismatch"},
270 []string{"name1", "event1"},
274 func TestMetricLabeledGaugeGroup(t *testing.T) {
277 g_grp1 := Metric.RegisterLabeledGaugeGroup(
278 []CounterOpts{{Name: "gauge1", Help: "gauge1"}},
279 []string{"name", "event"},
280 []string{"name1", "event1"},
283 if _, ok := g_grp1["gauge1"]; ok == false {
284 t.Errorf("g_grp1 gauge1 not exists")
286 g_grp1["gauge1"].Inc()
290 g_grp2 := Metric.RegisterLabeledGaugeGroup(
291 []CounterOpts{{Name: "gauge1", Help: "gauge1"}},
292 []string{"name", "event"},
293 []string{"name1", "event2"},
296 if _, ok := g_grp2["gauge1"]; ok == false {
297 t.Errorf("g_grp2 gauge1 not exists")
299 g_grp2["gauge1"].Inc()
302 func TestMetricGroupCache(t *testing.T) {
305 c_grp1 := Metric.RegisterLabeledCounterGroup(
306 []CounterOpts{{Name: "counter1", Help: "counter1"}},
307 []string{"name", "event"},
308 []string{"name1", "event1"},
310 if _, ok := c_grp1["counter1"]; ok == false {
311 t.Errorf("c_grp1 counter1 not exists")
313 c_grp1["counter1"].Inc()
317 c_grp2 := Metric.RegisterLabeledCounterGroup(
318 []CounterOpts{{Name: "counter1", Help: "counter1"}},
319 []string{"name", "event"},
320 []string{"name1", "event2"},
322 if _, ok := c_grp2["counter1"]; ok == false {
323 t.Errorf("c_grp2 counter1 not exists")
325 c_grp2["counter1"].Inc()
329 g_grp1 := Metric.RegisterLabeledGaugeGroup(
330 []CounterOpts{{Name: "gauge1", Help: "gauge1"}},
331 []string{"name", "event"},
332 []string{"name1", "event1"},
334 if _, ok := g_grp1["gauge1"]; ok == false {
335 t.Errorf("g_grp1 gauge1 not exists")
337 g_grp1["gauge1"].Inc()
341 g_grp2 := Metric.RegisterLabeledGaugeGroup(
342 []CounterOpts{{Name: "gauge1", Help: "gauge1"}},
343 []string{"name", "event"},
344 []string{"name1", "event2"},
346 if _, ok := g_grp2["gauge1"]; ok == false {
347 t.Errorf("g_grp2 gauge1 not exists")
349 g_grp2["gauge1"].Inc()
353 m_grp := NewMetricGroupsCache()
354 m_grp.CombineCounterGroupsWithPrefix("event1_", c_grp1)
355 m_grp.CombineCounterGroupsWithPrefix("event2_", c_grp2)
356 m_grp.CombineGaugeGroupsWithPrefix("event1_", g_grp1)
357 m_grp.CombineGaugeGroupsWithPrefix("event2_", g_grp2)
360 t.Errorf("Cache failed")
363 if m_grp.CIs("event1_counter1") == false {
364 t.Errorf("m_grp.Counters event1_counter1 not exists")
366 m_grp.CInc("event1_counter1")
368 if m_grp.CIs("event2_counter1") == false {
369 t.Errorf("m_grp.Counters event2_counter1 not exists")
371 m_grp.CInc("event2_counter1")
373 if m_grp.GIs("event1_gauge1") == false {
374 t.Errorf("m_grp.Gauges event1_gauge1 not exists")
376 m_grp.GInc("event1_gauge1")
378 if m_grp.GIs("event2_gauge1") == false {
379 t.Errorf("m_grp.Gauges event2_gauge1 not exists")
381 m_grp.GInc("event2_gauge1")
383 m_grp.CAdd("event2_counter1", 1)
384 m_grp.CGet("event2_counter1")
385 m_grp.GGet("event2_gauge1")
386 m_grp.GDec("event2_gauge1")
387 m_grp.GSet("event2_gauge1", 1)
391 // VECTORS ARE OLD WAY
392 // *Labeled* will do all work under the hood
395 var mCVect CounterVec
398 var mCGroupVect map[string]CounterVec
399 var mGGroupVect map[string]GaugeVec
401 func TestMetricSetup(t *testing.T) {
402 mCVect = Metric.RegisterCounterVec(CounterOpts{Name: "counter1", Help: "counter1"}, []string{"name", "event"}, "SUBSYSTEM0")
404 mCGroupVect = Metric.RegisterCounterVecGroup(
406 {Name: "counter1", Help: "counter1"},
408 []string{"name", "event"},
411 mGVect = Metric.RegisterGaugeVec(CounterOpts{Name: "gauge1", Help: "gauge1"}, []string{"name", "event"}, "SUBSYSTEM0")
413 mGGroupVect = Metric.RegisterGaugeVecGroup(
415 {Name: "gauge1", Help: "gauge1"},
417 []string{"name", "event"},
420 tmpCVect := Metric.RegisterCounterVec(CounterOpts{Name: "counter1", Help: "counter1"}, []string{"name", "event"}, "SUBSYSTEM0")
422 if tmpCVect.Vec != mCVect.Vec {
423 t.Errorf("tmpCVect not same than mCVect. cache not working?")
426 tmpGVect := Metric.RegisterGaugeVec(CounterOpts{Name: "gauge1", Help: "gauge1"}, []string{"name", "event"}, "SUBSYSTEM0")
428 if tmpGVect.Vec != mGVect.Vec {
429 t.Errorf("tmpGVect not same than mGVect. cache not working?")
432 Metric.RegisterCounterVec(CounterOpts{Name: "counter1", Help: "counter1"}, []string{"name", "eventMismatch"}, "SUBSYSTEM0")
433 Metric.RegisterGaugeVec(CounterOpts{Name: "gauge1", Help: "gauge1"}, []string{"name", "eventMismatch"}, "SUBSYSTEM0")
437 func TestMetricCounterVector(t *testing.T) {
440 c_1_1 := Metric.GetCounterFromVect([]string{"name1", "event1"}, mCVect)
442 c_1_2 := Metric.GetCounterFromVect([]string{"name1", "event1"}, mCVect)
445 t.Errorf("c_1_1 not same than c_1_2. cache not working?")
449 c_2_1 := Metric.GetCounterFromVect([]string{"name1", "event2"}, mCVect)
451 c_2_2 := Metric.GetCounterFromVect([]string{"name1", "event2"}, mCVect)
454 t.Errorf("c_2_1 not same than c_2_2. cache not working?")
457 t.Errorf("c_1_1 same than c_2_1. what?")
462 func TestMetricCounterGroupVector(t *testing.T) {
465 c_grp1 := Metric.GetCounterGroupFromVects([]string{"name1", "event1"}, mCGroupVect)
466 if _, ok := c_grp1["counter1"]; ok == false {
467 t.Errorf("c_grp1 counter1 not exists")
469 c_grp1["counter1"].Inc()
473 c_grp2 := Metric.GetCounterGroupFromVects([]string{"name1", "event2"}, mCGroupVect)
474 if _, ok := c_grp2["counter1"]; ok == false {
475 t.Errorf("c_grp2 counter1 not exists")
477 c_grp2["counter1"].Inc()
480 func TestMetricGaugeVector(t *testing.T) {
483 c_1_1 := Metric.GetGaugeFromVect([]string{"name1", "event1"}, mGVect)
485 c_1_2 := Metric.GetGaugeFromVect([]string{"name1", "event1"}, mGVect)
488 t.Errorf("c_1_1 not same than c_1_2. cache not working?")
492 c_2_1 := Metric.GetGaugeFromVect([]string{"name1", "event2"}, mGVect)
494 c_2_2 := Metric.GetGaugeFromVect([]string{"name1", "event2"}, mGVect)
497 t.Errorf("c_2_1 not same than c_2_2. cache not working?")
500 t.Errorf("c_1_1 same than c_2_1. what?")
504 func TestMetricGaugeGroupVector(t *testing.T) {
507 g_grp1 := Metric.GetGaugeGroupFromVects([]string{"name1", "event1"}, mGGroupVect)
508 if _, ok := g_grp1["gauge1"]; ok == false {
509 t.Errorf("g_grp1 gauge1 not exists")
511 g_grp1["gauge1"].Inc()
515 g_grp2 := Metric.GetGaugeGroupFromVects([]string{"name1", "event2"}, mGGroupVect)
516 if _, ok := g_grp2["gauge1"]; ok == false {
517 t.Errorf("g_grp2 gauge1 not exists")
519 g_grp2["gauge1"].Inc()
522 func TestMetricCounterGroupVectorPrefix(t *testing.T) {
525 c_grp1 := Metric.GetCounterGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mCGroupVect)
526 if _, ok := c_grp1["event1_counter1"]; ok == false {
527 t.Errorf("c_grp1 event1_counter1 not exists")
529 c_grp1["event1_counter1"].Inc()
533 c_grp2 := Metric.GetCounterGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mCGroupVect)
534 if _, ok := c_grp2["event2_counter1"]; ok == false {
535 t.Errorf("c_grp2 event2_counter1 not exists")
537 c_grp2["event2_counter1"].Inc()
541 m_grp := NewMetricGroupsCache()
542 m_grp.CombineCounterGroups(c_grp1, c_grp2)
546 if m_grp.CIs("event1_counter1") == false {
547 t.Errorf("m_grp event1_counter1 not exists")
549 m_grp.CInc("event1_counter1")
553 if m_grp.CIs("event2_counter1") == false {
554 t.Errorf("m_grp event2_counter1 not exists")
557 m_grp.CAdd("event2_counter1", 1)
558 m_grp.CGet("event2_counter1")
561 func TestMetricGaugeGroupVectorPrefix(t *testing.T) {
564 g_grp1 := Metric.GetGaugeGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mGGroupVect)
565 if _, ok := g_grp1["event1_gauge1"]; ok == false {
566 t.Errorf("g_grp1 event1_gauge1 not exists")
568 g_grp1["event1_gauge1"].Inc()
572 g_grp2 := Metric.GetGaugeGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mGGroupVect)
573 if _, ok := g_grp2["event2_gauge1"]; ok == false {
574 t.Errorf("g_grp2 event2_gauge1 not exists")
576 g_grp2["event2_gauge1"].Inc()
578 m_grp := NewMetricGroupsCache()
579 m_grp.CombineGaugeGroups(g_grp1, g_grp2)
583 if m_grp.GIs("event1_gauge1") == false {
584 t.Errorf("m_grp event1_gauge1 not exists")
586 m_grp.GInc("event1_gauge1")
590 if m_grp.GIs("event2_gauge1") == false {
591 t.Errorf("m_grp event2_gauge1 not exists")
593 m_grp.GInc("event2_gauge1")
595 m_grp.GGet("event2_gauge1")
596 m_grp.GDec("event2_gauge1")
597 m_grp.GSet("event2_gauge1", 1)
600 func TestMetricGroupCacheWithVect(t *testing.T) {
603 c_grp1 := Metric.GetCounterGroupFromVects([]string{"name1", "event1"}, mCGroupVect)
604 if _, ok := c_grp1["counter1"]; ok == false {
605 t.Errorf("c_grp1 counter1 not exists")
607 c_grp1["counter1"].Inc()
611 c_grp2 := Metric.GetCounterGroupFromVects([]string{"name1", "event2"}, mCGroupVect)
612 if _, ok := c_grp2["counter1"]; ok == false {
613 t.Errorf("c_grp2 counter1 not exists")
615 c_grp2["counter1"].Inc()
619 g_grp1 := Metric.GetGaugeGroupFromVects([]string{"name1", "event1"}, mGGroupVect)
620 if _, ok := g_grp1["gauge1"]; ok == false {
621 t.Errorf("g_grp1 gauge1 not exists")
623 g_grp1["gauge1"].Inc()
627 g_grp2 := Metric.GetGaugeGroupFromVects([]string{"name1", "event2"}, mGGroupVect)
628 if _, ok := g_grp2["gauge1"]; ok == false {
629 t.Errorf("g_grp2 gauge1 not exists")
631 g_grp2["gauge1"].Inc()
635 m_grp := NewMetricGroupsCache()
636 m_grp.CombineCounterGroupsWithPrefix("event1_", c_grp1)
637 m_grp.CombineCounterGroupsWithPrefix("event2_", c_grp2)
638 m_grp.CombineGaugeGroupsWithPrefix("event1_", g_grp1)
639 m_grp.CombineGaugeGroupsWithPrefix("event2_", g_grp2)
642 t.Errorf("Cache failed")
645 if m_grp.CIs("event1_counter1") == false {
646 t.Errorf("m_grp.Counters event1_counter1 not exists")
648 m_grp.CInc("event1_counter1")
650 if m_grp.CIs("event2_counter1") == false {
651 t.Errorf("m_grp.Counters event2_counter1 not exists")
653 m_grp.CInc("event2_counter1")
655 if m_grp.GIs("event1_gauge1") == false {
656 t.Errorf("m_grp.Gauges event1_gauge1 not exists")
658 m_grp.GInc("event1_gauge1")
660 if m_grp.GIs("event2_gauge1") == false {
661 t.Errorf("m_grp.Gauges event2_gauge1 not exists")
663 m_grp.GInc("event2_gauge1")
665 m_grp.CAdd("event2_counter1", 1)
666 m_grp.CGet("event2_counter1")
667 m_grp.GGet("event2_gauge1")
668 m_grp.GDec("event2_gauge1")
669 m_grp.GSet("event2_gauge1", 1)