"github.com/prometheus/client_golang/prometheus/promhttp"
)
-type Metrics struct {
- Namespace string
-}
-
+//-----------------------------------------------------------------------------
// Alias
+//-----------------------------------------------------------------------------
type CounterOpts prometheus.Opts
type Counter prometheus.Counter
type Gauge prometheus.Gauge
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+
+type MetricGroupsCache struct {
+ Counters map[string]Counter
+ Gauges map[string]Gauge
+}
+
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+type Metrics struct {
+ Namespace string
+ MetricGroupsCacheMap map[string]*MetricGroupsCache
+}
+
func NewMetrics(url, namespace string, r *mux.Router) *Metrics {
if url == "" {
url = "/ric/v1/metrics"
// Expose 'metrics' endpoint with standard golang metrics used by prometheus
r.Handle(url, promhttp.Handler())
- return &Metrics{Namespace: namespace}
+ return &Metrics{Namespace: namespace, MetricGroupsCacheMap: make(map[string]*MetricGroupsCache)}
}
/*
return
}
-func (m *Metrics) GetCounterGroupFromVects(labels []string, vects ...map[string]CounterVec) (c map[string]Counter) {
+func (m *Metrics) GetCounterGroupFromVectsWithPrefix(prefix string, labels []string, vects ...map[string]CounterVec) (c map[string]Counter) {
c = make(map[string]Counter)
for _, vec := range vects {
for name, opt := range vec {
- c[name] = opt.Vec.WithLabelValues(labels...)
+ c[prefix+name] = opt.Vec.WithLabelValues(labels...)
Logger.Info("Register new counter for vector with opts: %v labels: %v", opt.Opts, labels)
}
}
return
}
+func (m *Metrics) GetCounterGroupFromVects(labels []string, vects ...map[string]CounterVec) (c map[string]Counter) {
+ return m.GetCounterGroupFromVectsWithPrefix("", labels, vects...)
+}
+
/*
* Handling gauge vectors
*
return
}
-func (m *Metrics) GetGaugeGroupFromVects(labels []string, vects ...map[string]GaugeVec) (c map[string]Gauge) {
+func (m *Metrics) GetGaugeGroupFromVectsWithPrefix(prefix string, labels []string, vects ...map[string]GaugeVec) (c map[string]Gauge) {
c = make(map[string]Gauge)
for _, vec := range vects {
for name, opt := range vec {
- c[name] = opt.Vec.WithLabelValues(labels...)
+ c[prefix+name] = opt.Vec.WithLabelValues(labels...)
Logger.Info("Register new gauge for vector with opts: %v labels: %v", opt.Opts, labels)
}
}
return
}
+func (m *Metrics) GetGaugeGroupFromVects(labels []string, vects ...map[string]GaugeVec) (c map[string]Gauge) {
+ return m.GetGaugeGroupFromVectsWithPrefix("", labels, vects...)
+
+}
+
/*
*
*/
}
return trg
}
+
+/*
+ *
+ */
+func (m *Metrics) GroupCacheGet(id string) *MetricGroupsCache {
+ entry, ok := m.MetricGroupsCacheMap[id]
+ if ok == false {
+ return nil
+ }
+ return entry
+}
+
+func (m *Metrics) GroupCacheAddCounters(id string, vals map[string]Counter) {
+ entry, ok := m.MetricGroupsCacheMap[id]
+ if ok == false {
+ entry = &MetricGroupsCache{}
+ m.MetricGroupsCacheMap[id] = entry
+ }
+ m.MetricGroupsCacheMap[id].Counters = m.CombineCounterGroups(m.MetricGroupsCacheMap[id].Counters, vals)
+}
+
+func (m *Metrics) GroupCacheAddGauges(id string, vals map[string]Gauge) {
+ entry, ok := m.MetricGroupsCacheMap[id]
+ if ok == false {
+ entry = &MetricGroupsCache{}
+ m.MetricGroupsCacheMap[id] = entry
+ }
+ m.MetricGroupsCacheMap[id].Gauges = m.CombineGaugeGroups(m.MetricGroupsCacheMap[id].Gauges, vals)
+}
--- /dev/null
+/*
+==================================================================================
+ Copyright (c) 2019 AT&T Intellectual Property.
+ Copyright (c) 2019 Nokia
+
+ 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 xapp
+
+import (
+ "testing"
+)
+
+var mCVect map[string]CounterVec
+var mGVect map[string]GaugeVec
+
+func TestMetricSetup(t *testing.T) {
+ mCVect = Metric.RegisterCounterVecGroup(
+ []CounterOpts{
+ {Name: "counter1", Help: "counter1"},
+ },
+ []string{"name", "event"},
+ "SUBSYSTEM")
+
+ mGVect = Metric.RegisterGaugeVecGroup(
+ []CounterOpts{
+ {Name: "counter2", Help: "counter2"},
+ },
+ []string{"name", "event"},
+ "SUBSYSTEM")
+}
+
+func TestMetricCounterVector(t *testing.T) {
+ //
+ //
+ c_grp1 := Metric.GetCounterGroupFromVects([]string{"name1", "event1"}, mCVect)
+ if _, ok := c_grp1["counter1"]; ok == false {
+ t.Errorf("c_grp1 counter1 not exists")
+ }
+ c_grp1["counter1"].Inc()
+
+ //
+ //
+ c_grp2 := Metric.GetCounterGroupFromVects([]string{"name1", "event2"}, mCVect)
+ if _, ok := c_grp2["counter1"]; ok == false {
+ t.Errorf("c_grp2 counter1 not exists")
+ }
+ c_grp2["counter1"].Inc()
+}
+
+func TestMetricGaugeVector(t *testing.T) {
+ //
+ //
+ g_grp1 := Metric.GetGaugeGroupFromVects([]string{"name1", "event1"}, mGVect)
+ if _, ok := g_grp1["counter2"]; ok == false {
+ t.Errorf("g_grp1 counter2 not exists")
+ }
+ g_grp1["counter2"].Inc()
+
+ //
+ //
+ g_grp2 := Metric.GetGaugeGroupFromVects([]string{"name1", "event2"}, mGVect)
+ if _, ok := g_grp2["counter2"]; ok == false {
+ t.Errorf("g_grp2 counter2 not exists")
+ }
+ g_grp2["counter2"].Inc()
+}
+
+func TestMetricCounterVectorPrefix(t *testing.T) {
+ //
+ //
+ c_grp1 := Metric.GetCounterGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mCVect)
+ if _, ok := c_grp1["event1_counter1"]; ok == false {
+ t.Errorf("c_grp1 event1_counter1 not exists")
+ }
+ c_grp1["event1_counter1"].Inc()
+
+ //
+ //
+ c_grp2 := Metric.GetCounterGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mCVect)
+ if _, ok := c_grp2["event2_counter1"]; ok == false {
+ t.Errorf("c_grp2 event2_counter1 not exists")
+ }
+ c_grp2["event2_counter1"].Inc()
+
+ //
+ //
+ c_grp := Metric.CombineCounterGroups(c_grp1, c_grp2)
+
+ //
+ //
+ if _, ok := c_grp["event1_counter1"]; ok == false {
+ t.Errorf("c_grp event1_counter1 not exists")
+ }
+ c_grp["event1_counter1"].Inc()
+
+ //
+ //
+ if _, ok := c_grp["event2_counter1"]; ok == false {
+ t.Errorf("c_grp event2_counter1 not exists")
+ }
+ c_grp["event2_counter1"].Inc()
+}
+
+func TestMetricGaugeVectorPrefix(t *testing.T) {
+ //
+ //
+ g_grp1 := Metric.GetGaugeGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mGVect)
+ if _, ok := g_grp1["event1_counter2"]; ok == false {
+ t.Errorf("g_grp1 event1_counter2 not exists")
+ }
+ g_grp1["event1_counter2"].Inc()
+
+ //
+ //
+ g_grp2 := Metric.GetGaugeGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mGVect)
+ if _, ok := g_grp2["event2_counter2"]; ok == false {
+ t.Errorf("g_grp2 event2_counter2 not exists")
+ }
+ g_grp2["event2_counter2"].Inc()
+
+ //
+ //
+ g_grp := Metric.CombineGaugeGroups(g_grp1, g_grp2)
+
+ //
+ //
+ if _, ok := g_grp["event1_counter2"]; ok == false {
+ t.Errorf("g_grp event1_counter2 not exists")
+ }
+ g_grp["event1_counter2"].Inc()
+
+ //
+ //
+ if _, ok := g_grp["event2_counter2"]; ok == false {
+ t.Errorf("g_grp event2_counter2 not exists")
+ }
+ g_grp["event2_counter2"].Inc()
+}
+
+func TestMetricGroupCache(t *testing.T) {
+ //
+ //
+ c_grp1 := Metric.GetCounterGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mCVect)
+ if _, ok := c_grp1["event1_counter1"]; ok == false {
+ t.Errorf("c_grp1 event1_counter1 not exists")
+ }
+ c_grp1["event1_counter1"].Inc()
+
+ //
+ //
+ c_grp2 := Metric.GetCounterGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mCVect)
+ if _, ok := c_grp2["event2_counter1"]; ok == false {
+ t.Errorf("c_grp2 event2_counter1 not exists")
+ }
+ c_grp2["event2_counter1"].Inc()
+
+ //
+ //
+ g_grp1 := Metric.GetGaugeGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mGVect)
+ if _, ok := g_grp1["event1_counter2"]; ok == false {
+ t.Errorf("g_grp1 event1_counter2 not exists")
+ }
+ g_grp1["event1_counter2"].Inc()
+
+ //
+ //
+ g_grp2 := Metric.GetGaugeGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mGVect)
+ if _, ok := g_grp2["event2_counter2"]; ok == false {
+ t.Errorf("g_grp2 event2_counter2 not exists")
+ }
+ g_grp2["event2_counter2"].Inc()
+
+ //
+ //
+ cacheid := "CACHEID"
+ entry := Metric.GroupCacheGet(cacheid)
+ if entry == nil {
+ Metric.GroupCacheAddCounters(cacheid, c_grp1)
+ Metric.GroupCacheAddCounters(cacheid, c_grp2)
+ Metric.GroupCacheAddGauges(cacheid, g_grp1)
+ Metric.GroupCacheAddGauges(cacheid, g_grp2)
+ entry = Metric.GroupCacheGet(cacheid)
+ }
+
+ if entry == nil {
+ t.Errorf("Cache failed")
+ }
+
+ if _, ok := entry.Counters["event1_counter1"]; ok == false {
+ t.Errorf("entry.Counters event1_counter1 not exists")
+ }
+ entry.Counters["event1_counter1"].Inc()
+
+ if _, ok := entry.Counters["event2_counter1"]; ok == false {
+ t.Errorf("entry.Counters event2_counter1 not exists")
+ }
+ entry.Counters["event2_counter1"].Inc()
+
+ if _, ok := entry.Gauges["event1_counter2"]; ok == false {
+ t.Errorf("entry.Gauges event1_counter2 not exists")
+ }
+ entry.Gauges["event1_counter2"].Inc()
+
+ if _, ok := entry.Gauges["event2_counter2"]; ok == false {
+ t.Errorf("entry.Gauges event2_counter2 not exists")
+ }
+ entry.Gauges["event2_counter2"].Inc()
+
+}