Fixing metrics
[ric-plt/xapp-frame.git] / pkg / xapp / metrics_test.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
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
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
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 ==================================================================================
18 */
19
20 package xapp
21
22 import (
23         "testing"
24 )
25
26 var mCVect map[string]CounterVec
27 var mGVect map[string]GaugeVec
28
29 func TestMetricSetup(t *testing.T) {
30         mCVect = Metric.RegisterCounterVecGroup(
31                 []CounterOpts{
32                         {Name: "counter1", Help: "counter1"},
33                 },
34                 []string{"name", "event"},
35                 "SUBSYSTEM")
36
37         mGVect = Metric.RegisterGaugeVecGroup(
38                 []CounterOpts{
39                         {Name: "counter2", Help: "counter2"},
40                 },
41                 []string{"name", "event"},
42                 "SUBSYSTEM")
43
44 }
45
46 func TestMetricCounter(t *testing.T) {
47         var TestCounterOpts = []CounterOpts{
48                 {Name: "Blaah1", Help: "Blaah1"},
49                 {Name: "Blaah2", Help: "Blaah2"},
50                 {Name: "Blaah3", Help: "Blaah3"},
51                 {Name: "Blaah4", Help: "Blaah4"},
52         }
53
54         ret1 := Metric.RegisterCounterGroup(TestCounterOpts, "TestMetricCounter")
55
56         if len(ret1) == 0 {
57                 t.Errorf("ret1 counter group is empty")
58         }
59
60         ret2 := Metric.RegisterCounterGroup(TestCounterOpts, "TestMetricCounter")
61
62         if len(ret2) == 0 {
63                 t.Errorf("ret2 counter group is empty")
64         }
65
66         if len(ret1) != len(ret2) {
67                 t.Errorf("ret1 len %d differs from ret2 len %d", len(ret1), len(ret2))
68         }
69 }
70
71 func TestMetricCounterVector(t *testing.T) {
72         //
73         //
74         c_grp1 := Metric.GetCounterGroupFromVects([]string{"name1", "event1"}, mCVect)
75         if _, ok := c_grp1["counter1"]; ok == false {
76                 t.Errorf("c_grp1 counter1 not exists")
77         }
78         c_grp1["counter1"].Inc()
79
80         //
81         //
82         c_grp2 := Metric.GetCounterGroupFromVects([]string{"name1", "event2"}, mCVect)
83         if _, ok := c_grp2["counter1"]; ok == false {
84                 t.Errorf("c_grp2 counter1 not exists")
85         }
86         c_grp2["counter1"].Inc()
87 }
88
89 func TestMetricGaugeVector(t *testing.T) {
90         //
91         //
92         g_grp1 := Metric.GetGaugeGroupFromVects([]string{"name1", "event1"}, mGVect)
93         if _, ok := g_grp1["counter2"]; ok == false {
94                 t.Errorf("g_grp1 counter2 not exists")
95         }
96         g_grp1["counter2"].Inc()
97
98         //
99         //
100         g_grp2 := Metric.GetGaugeGroupFromVects([]string{"name1", "event2"}, mGVect)
101         if _, ok := g_grp2["counter2"]; ok == false {
102                 t.Errorf("g_grp2 counter2 not exists")
103         }
104         g_grp2["counter2"].Inc()
105 }
106
107 func TestMetricCounterVectorPrefix(t *testing.T) {
108         //
109         //
110         c_grp1 := Metric.GetCounterGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mCVect)
111         if _, ok := c_grp1["event1_counter1"]; ok == false {
112                 t.Errorf("c_grp1 event1_counter1 not exists")
113         }
114         c_grp1["event1_counter1"].Inc()
115
116         //
117         //
118         c_grp2 := Metric.GetCounterGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mCVect)
119         if _, ok := c_grp2["event2_counter1"]; ok == false {
120                 t.Errorf("c_grp2 event2_counter1 not exists")
121         }
122         c_grp2["event2_counter1"].Inc()
123
124         //
125         //
126         m_grp := NewMetricGroupsCache()
127         m_grp.CombineCounterGroups(c_grp1, c_grp2)
128
129         //
130         //
131         if m_grp.CIs("event1_counter1") == false {
132                 t.Errorf("m_grp event1_counter1 not exists")
133         }
134         m_grp.CInc("event1_counter1")
135
136         //
137         //
138         if m_grp.CIs("event2_counter1") == false {
139                 t.Errorf("m_grp event2_counter1 not exists")
140         }
141         m_grp.CInc("event2_counter1")
142 }
143
144 func TestMetricGaugeVectorPrefix(t *testing.T) {
145         //
146         //
147         g_grp1 := Metric.GetGaugeGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mGVect)
148         if _, ok := g_grp1["event1_counter2"]; ok == false {
149                 t.Errorf("g_grp1 event1_counter2 not exists")
150         }
151         g_grp1["event1_counter2"].Inc()
152
153         //
154         //
155         g_grp2 := Metric.GetGaugeGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mGVect)
156         if _, ok := g_grp2["event2_counter2"]; ok == false {
157                 t.Errorf("g_grp2 event2_counter2 not exists")
158         }
159         g_grp2["event2_counter2"].Inc()
160
161         m_grp := NewMetricGroupsCache()
162         m_grp.CombineGaugeGroups(g_grp1, g_grp2)
163
164         //
165         //
166         if m_grp.GIs("event1_counter2") == false {
167                 t.Errorf("m_grp event1_counter2 not exists")
168         }
169         m_grp.GInc("event1_counter2")
170
171         //
172         //
173         if m_grp.GIs("event2_counter2") == false {
174                 t.Errorf("m_grp event2_counter2 not exists")
175         }
176         m_grp.GInc("event2_counter2")
177 }
178
179 func TestMetricGroupCache(t *testing.T) {
180         //
181         //
182         c_grp1 := Metric.GetCounterGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mCVect)
183         if _, ok := c_grp1["event1_counter1"]; ok == false {
184                 t.Errorf("c_grp1 event1_counter1 not exists")
185         }
186         c_grp1["event1_counter1"].Inc()
187
188         //
189         //
190         c_grp2 := Metric.GetCounterGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mCVect)
191         if _, ok := c_grp2["event2_counter1"]; ok == false {
192                 t.Errorf("c_grp2 event2_counter1 not exists")
193         }
194         c_grp2["event2_counter1"].Inc()
195
196         //
197         //
198         g_grp1 := Metric.GetGaugeGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mGVect)
199         if _, ok := g_grp1["event1_counter2"]; ok == false {
200                 t.Errorf("g_grp1 event1_counter2 not exists")
201         }
202         g_grp1["event1_counter2"].Inc()
203
204         //
205         //
206         g_grp2 := Metric.GetGaugeGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mGVect)
207         if _, ok := g_grp2["event2_counter2"]; ok == false {
208                 t.Errorf("g_grp2 event2_counter2 not exists")
209         }
210         g_grp2["event2_counter2"].Inc()
211
212         //
213         //
214         m_grp := NewMetricGroupsCache()
215         m_grp.CombineCounterGroups(c_grp1)
216         m_grp.CombineCounterGroups(c_grp2)
217         m_grp.CombineGaugeGroups(g_grp1)
218         m_grp.CombineGaugeGroups(g_grp2)
219
220         if m_grp == nil {
221                 t.Errorf("Cache failed")
222         }
223
224         if m_grp.CIs("event1_counter1") == false {
225                 t.Errorf("m_grp.Counters event1_counter1 not exists")
226         }
227         m_grp.CInc("event1_counter1")
228
229         if m_grp.CIs("event2_counter1") == false {
230                 t.Errorf("m_grp.Counters event2_counter1 not exists")
231         }
232         m_grp.CInc("event2_counter1")
233
234         if m_grp.GIs("event1_counter2") == false {
235                 t.Errorf("m_grp.Gauges event1_counter2 not exists")
236         }
237         m_grp.GInc("event1_counter2")
238
239         if m_grp.GIs("event2_counter2") == false {
240                 t.Errorf("m_grp.Gauges event2_counter2 not exists")
241         }
242         m_grp.GInc("event2_counter2")
243
244 }