From 7117d0d6bd49a3099d1efb36941096a502c9a4bb Mon Sep 17 00:00:00 2001 From: Juha Hyttinen Date: Thu, 30 Sep 2021 22:44:47 +0300 Subject: [PATCH] Improved metrics - Added counter and gauge vector caching. Warn log if mismatching vector labels for same name. - Support single counter/gauge/countervector/gaugevector manipulating Signed-off-by: Juha Hyttinen Change-Id: I48277a2a11af96d5fe797d75527a9932397dd4e1 --- pkg/xapp/metrics.go | 303 +++++++++++++++++++++++++++++++---------------- pkg/xapp/metrics_test.go | 280 +++++++++++++++++++++++++++++++------------ 2 files changed, 404 insertions(+), 179 deletions(-) diff --git a/pkg/xapp/metrics.go b/pkg/xapp/metrics.go index 40adbcd..4f673d0 100644 --- a/pkg/xapp/metrics.go +++ b/pkg/xapp/metrics.go @@ -35,6 +35,30 @@ type CounterOpts prometheus.Opts type Counter prometheus.Counter type Gauge prometheus.Gauge +type CounterVec struct { + Vec *prometheus.CounterVec + Opts CounterOpts + Labels []string +} + +type GaugeVec struct { + Vec *prometheus.GaugeVec + Opts CounterOpts + Labels []string +} + +func strSliceCompare(a, b []string) bool { + if len(a) != len(b) { + return false + } + for i, v := range a { + if v != b[i] { + return false + } + } + return true +} + //----------------------------------------------------------------------------- // //----------------------------------------------------------------------------- @@ -107,6 +131,16 @@ func (met *MetricGroupsCache) GDec(metric string) { met.gauges[metric].Dec() } +func (met *MetricGroupsCache) CombineCounterGroupsWithPrefix(prefix string, srcs ...map[string]Counter) { + met.Lock() + defer met.Unlock() + for _, src := range srcs { + for k, v := range src { + met.counters[prefix+k] = v + } + } +} + func (met *MetricGroupsCache) CombineCounterGroups(srcs ...map[string]Counter) { met.Lock() defer met.Unlock() @@ -117,6 +151,16 @@ func (met *MetricGroupsCache) CombineCounterGroups(srcs ...map[string]Counter) { } } +func (met *MetricGroupsCache) CombineGaugeGroupsWithPrefix(prefix string, srcs ...map[string]Gauge) { + met.Lock() + defer met.Unlock() + for _, src := range srcs { + for k, v := range src { + met.gauges[prefix+k] = v + } + } +} + func (met *MetricGroupsCache) CombineGaugeGroups(srcs ...map[string]Gauge) { met.Lock() defer met.Unlock() @@ -141,10 +185,14 @@ func NewMetricGroupsCache() *MetricGroupsCache { var globalLock sync.Mutex var cache_allcounters map[string]Counter var cache_allgauges map[string]Gauge +var cache_allcountervects map[string]CounterVec +var cache_allgaugevects map[string]GaugeVec func init() { cache_allcounters = make(map[string]Counter) cache_allgauges = make(map[string]Gauge) + cache_allcountervects = make(map[string]CounterVec) + cache_allgaugevects = make(map[string]GaugeVec) } //----------------------------------------------------------------------------- @@ -188,62 +236,66 @@ func (m *Metrics) getFullName(opts prometheus.Opts, labels []string) string { /* * Handling counters */ -func (m *Metrics) registerCounter(opts CounterOpts) Counter { - Logger.Info("Register new counter with opts: %v", opts) - return promauto.NewCounter(prometheus.CounterOpts(opts)) -} - -func (m *Metrics) RegisterCounterGroup(opts []CounterOpts, subsytem string) (c map[string]Counter) { +func (m *Metrics) RegisterCounter(opts CounterOpts, subsytem string) Counter { globalLock.Lock() defer globalLock.Unlock() - c = make(map[string]Counter) - for _, opt := range opts { - opt.Namespace = m.Namespace - opt.Subsystem = subsytem - - id := m.getFullName(prometheus.Opts(opt), []string{}) - if _, ok := cache_allcounters[id]; !ok { - cache_allcounters[id] = m.registerCounter(opt) - } - - c[opt.Name] = cache_allcounters[id] + opts.Namespace = m.Namespace + opts.Subsystem = subsytem + id := m.getFullName(prometheus.Opts(opts), []string{}) + if _, ok := cache_allcounters[id]; !ok { + Logger.Info("Register new counter with opts: %v", opts) + cache_allcounters[id] = promauto.NewCounter(prometheus.CounterOpts(opts)) } + return cache_allcounters[id] +} - return +func (m *Metrics) RegisterCounterGroup(optsgroup []CounterOpts, subsytem string) map[string]Counter { + c := make(map[string]Counter) + for _, opts := range optsgroup { + c[opts.Name] = m.RegisterCounter(opts, subsytem) + } + return c } /* * Handling gauges */ -func (m *Metrics) registerGauge(opts CounterOpts) Gauge { - Logger.Info("Register new gauge with opts: %v", opts) - return promauto.NewGauge(prometheus.GaugeOpts(opts)) -} - -func (m *Metrics) RegisterGaugeGroup(opts []CounterOpts, subsytem string) (c map[string]Gauge) { +func (m *Metrics) RegisterGauge(opts CounterOpts, subsytem string) Gauge { globalLock.Lock() defer globalLock.Unlock() - c = make(map[string]Gauge) - for _, opt := range opts { - opt.Namespace = m.Namespace - opt.Subsystem = subsytem - - id := m.getFullName(prometheus.Opts(opt), []string{}) - if _, ok := cache_allgauges[id]; !ok { - cache_allgauges[id] = m.registerGauge(opt) - } - - c[opt.Name] = cache_allgauges[id] + opts.Namespace = m.Namespace + opts.Subsystem = subsytem + id := m.getFullName(prometheus.Opts(opts), []string{}) + if _, ok := cache_allgauges[id]; !ok { + Logger.Info("Register new gauge with opts: %v", opts) + cache_allgauges[id] = promauto.NewGauge(prometheus.GaugeOpts(opts)) } + return cache_allgauges[id] +} - return +func (m *Metrics) RegisterGaugeGroup(optsgroup []CounterOpts, subsytem string) map[string]Gauge { + c := make(map[string]Gauge) + for _, opts := range optsgroup { + c[opts.Name] = m.RegisterGauge(opts, subsytem) + } + return c } /* * Handling counter vectors * - * Example: + * Examples: + //--------- + vec := Metric.RegisterCounterVec( + CounterOpts{Name: "counter0", Help: "counter0"}, + []string{"host"}, + "SUBSYSTEM") + + stat:=Metric.GetCounterFromVect([]string{"localhost:8888"},vec) + stat.Inc() + + //--------- vec := Metric.RegisterCounterVecGroup( []CounterOpts{ {Name: "counter1", Help: "counter1"}, @@ -252,60 +304,86 @@ func (m *Metrics) RegisterGaugeGroup(opts []CounterOpts, subsytem string) (c map []string{"host"}, "SUBSYSTEM") - stat:=Metric.GetCounterGroupFromVects([]string{"localhost:8888"}, vec) - + stats:=Metric.GetCounterGroupFromVects([]string{"localhost:8888"}, vec) + stats["counter1"].Inc() */ -type CounterVec struct { - Vec *prometheus.CounterVec - Opts CounterOpts -} -func (m *Metrics) registerCounterVec(opts CounterOpts, labelNames []string) *prometheus.CounterVec { - Logger.Info("Register new counter vector with opts: %v labelNames: %v", opts, labelNames) - return promauto.NewCounterVec(prometheus.CounterOpts(opts), labelNames) +func (m *Metrics) RegisterCounterVec(opts CounterOpts, labelNames []string, subsytem string) CounterVec { + globalLock.Lock() + defer globalLock.Unlock() + opts.Namespace = m.Namespace + opts.Subsystem = subsytem + id := m.getFullName(prometheus.Opts(opts), []string{}) + if _, ok := cache_allcountervects[id]; !ok { + Logger.Info("Register new counter vector with opts: %v labelNames: %v", opts, labelNames) + entry := CounterVec{} + entry.Opts = opts + entry.Labels = labelNames + entry.Vec = promauto.NewCounterVec(prometheus.CounterOpts(entry.Opts), entry.Labels) + cache_allcountervects[id] = entry + } + entry := cache_allcountervects[id] + if strSliceCompare(entry.Labels, labelNames) == false { + Logger.Warn("id:%s cached counter vec labels dont match %v != %v", id, entry.Labels, labelNames) + } + return entry } -func (m *Metrics) RegisterCounterVecGroup(opts []CounterOpts, labelNames []string, subsytem string) (c map[string]CounterVec) { - c = make(map[string]CounterVec) - for _, opt := range opts { - entry := CounterVec{} - entry.Opts = opt - entry.Opts.Namespace = m.Namespace - entry.Opts.Subsystem = subsytem - entry.Vec = m.registerCounterVec(entry.Opts, labelNames) - c[opt.Name] = entry +func (m *Metrics) RegisterCounterVecGroup(optsgroup []CounterOpts, labelNames []string, subsytem string) map[string]CounterVec { + c := make(map[string]CounterVec) + for _, opts := range optsgroup { + c[opts.Name] = m.RegisterCounterVec(opts, labelNames, subsytem) } - return + return c } -func (m *Metrics) GetCounterGroupFromVectsWithPrefix(prefix string, labels []string, vects ...map[string]CounterVec) (c map[string]Counter) { +func (m *Metrics) GetCounterFromVect(labelValues []string, vec CounterVec) (c Counter) { globalLock.Lock() defer globalLock.Unlock() - c = make(map[string]Counter) - for _, vec := range vects { - for name, opt := range vec { - - id := m.getFullName(prometheus.Opts(opt.Opts), labels) - if _, ok := cache_allcounters[id]; !ok { - Logger.Info("Register new counter from vector with opts: %v labels: %v prefix: %s", opt.Opts, labels, prefix) - cache_allcounters[id] = opt.Vec.WithLabelValues(labels...) - } - c[prefix+name] = cache_allcounters[id] + id := m.getFullName(prometheus.Opts(vec.Opts), labelValues) + if _, ok := cache_allcounters[id]; !ok { + Logger.Info("Register new counter from vector with opts: %v labelValues: %v", vec.Opts, labelValues) + cache_allcounters[id] = vec.Vec.WithLabelValues(labelValues...) + } + return cache_allcounters[id] +} + +func (m *Metrics) GetCounterGroupFromVects(labelValues []string, vects ...map[string]CounterVec) map[string]Counter { + c := make(map[string]Counter) + for _, vect := range vects { + for name, vec := range vect { + c[name] = m.GetCounterFromVect(labelValues, vec) } } - return + return c } -func (m *Metrics) GetCounterGroupFromVects(labels []string, vects ...map[string]CounterVec) (c map[string]Counter) { - return m.GetCounterGroupFromVectsWithPrefix("", labels, vects...) +func (m *Metrics) GetCounterGroupFromVectsWithPrefix(prefix string, labelValues []string, vects ...map[string]CounterVec) map[string]Counter { + c := make(map[string]Counter) + for _, vect := range vects { + for name, vec := range vect { + c[prefix+name] = m.GetCounterFromVect(labelValues, vec) + } + } + return c } /* * Handling gauge vectors * - * Example: + * Examples: - vec := Metric.RegisterGaugeVecGroup( + //--------- + vec := Metric.RegisterGaugeVec( + CounterOpts{Name: "gauge0", Help: "gauge0"}, + []string{"host"}, + "SUBSYSTEM") + + stat:=Metric.GetGaugeFromVect([]string{"localhost:8888"},vec) + stat.Inc() + + //--------- + vecgrp := Metric.RegisterGaugeVecGroup( []CounterOpts{ {Name: "gauge1", Help: "gauge1"}, {Name: "gauge2", Help: "gauge2"}, @@ -313,51 +391,66 @@ func (m *Metrics) GetCounterGroupFromVects(labels []string, vects ...map[string] []string{"host"}, "SUBSYSTEM") - stat:=Metric.GetGaugeGroupFromVects([]string{"localhost:8888"},vec) - + stats:=Metric.GetGaugeGroupFromVects([]string{"localhost:8888"},vecgrp) + stats["gauge1"].Inc() */ -type GaugeVec struct { - Vec *prometheus.GaugeVec - Opts CounterOpts -} -func (m *Metrics) registerGaugeVec(opts CounterOpts, labelNames []string) *prometheus.GaugeVec { - Logger.Info("Register new gauge vector with opts: %v labelNames: %v", opts, labelNames) - return promauto.NewGaugeVec(prometheus.GaugeOpts(opts), labelNames) -} - -func (m *Metrics) RegisterGaugeVecGroup(opts []CounterOpts, labelNames []string, subsytem string) (c map[string]GaugeVec) { - c = make(map[string]GaugeVec) - for _, opt := range opts { +func (m *Metrics) RegisterGaugeVec(opt CounterOpts, labelNames []string, subsytem string) GaugeVec { + globalLock.Lock() + defer globalLock.Unlock() + opt.Namespace = m.Namespace + opt.Subsystem = subsytem + id := m.getFullName(prometheus.Opts(opt), []string{}) + if _, ok := cache_allgaugevects[id]; !ok { + Logger.Info("Register new gauge vector with opt: %v labelNames: %v", opt, labelNames) entry := GaugeVec{} entry.Opts = opt - entry.Opts.Namespace = m.Namespace - entry.Opts.Subsystem = subsytem - entry.Vec = m.registerGaugeVec(entry.Opts, labelNames) - c[opt.Name] = entry + entry.Labels = labelNames + entry.Vec = promauto.NewGaugeVec(prometheus.GaugeOpts(entry.Opts), entry.Labels) + cache_allgaugevects[id] = entry + } + entry := cache_allgaugevects[id] + if strSliceCompare(entry.Labels, labelNames) == false { + Logger.Warn("id:%s cached gauge vec labels dont match %v != %v", id, entry.Labels, labelNames) + } + return entry +} +func (m *Metrics) RegisterGaugeVecGroup(opts []CounterOpts, labelNames []string, subsytem string) map[string]GaugeVec { + c := make(map[string]GaugeVec) + for _, opt := range opts { + c[opt.Name] = m.RegisterGaugeVec(opt, labelNames, subsytem) } - return + return c } -func (m *Metrics) GetGaugeGroupFromVectsWithPrefix(prefix string, labels []string, vects ...map[string]GaugeVec) (c map[string]Gauge) { +func (m *Metrics) GetGaugeFromVect(labelValues []string, vec GaugeVec) Gauge { globalLock.Lock() defer globalLock.Unlock() - c = make(map[string]Gauge) - for _, vec := range vects { - for name, opt := range vec { - - id := m.getFullName(prometheus.Opts(opt.Opts), labels) - if _, ok := cache_allgauges[id]; !ok { - Logger.Info("Register new gauge from vector with opts: %v labels: %v prefix: %s", opt.Opts, labels, prefix) - cache_allgauges[id] = opt.Vec.WithLabelValues(labels...) - } - c[prefix+name] = cache_allgauges[id] + id := m.getFullName(prometheus.Opts(vec.Opts), labelValues) + if _, ok := cache_allgauges[id]; !ok { + Logger.Info("Register new gauge from vector with opts: %v labelValues: %v", vec.Opts, labelValues) + cache_allgauges[id] = vec.Vec.WithLabelValues(labelValues...) + } + return cache_allgauges[id] +} + +func (m *Metrics) GetGaugeGroupFromVects(labelValues []string, vects ...map[string]GaugeVec) map[string]Gauge { + c := make(map[string]Gauge) + for _, vect := range vects { + for name, vec := range vect { + c[name] = m.GetGaugeFromVect(labelValues, vec) } } - return + return c } -func (m *Metrics) GetGaugeGroupFromVects(labels []string, vects ...map[string]GaugeVec) (c map[string]Gauge) { - return m.GetGaugeGroupFromVectsWithPrefix("", labels, vects...) +func (m *Metrics) GetGaugeGroupFromVectsWithPrefix(prefix string, labelValues []string, vects ...map[string]GaugeVec) map[string]Gauge { + c := make(map[string]Gauge) + for _, vect := range vects { + for name, vec := range vect { + c[prefix+name] = m.GetGaugeFromVect(labelValues, vec) + } + } + return c } diff --git a/pkg/xapp/metrics_test.go b/pkg/xapp/metrics_test.go index bd7c737..4c98c95 100755 --- a/pkg/xapp/metrics_test.go +++ b/pkg/xapp/metrics_test.go @@ -23,52 +23,135 @@ import ( "testing" ) -var mCVect map[string]CounterVec -var mGVect map[string]GaugeVec -var mGGroup map[string]Gauge +var mCVect CounterVec +var mGVect GaugeVec + +var mCGroupVect map[string]CounterVec +var mGGroupVect map[string]GaugeVec func TestMetricSetup(t *testing.T) { - mCVect = Metric.RegisterCounterVecGroup( + mCVect = Metric.RegisterCounterVec(CounterOpts{Name: "counter1", Help: "counter1"}, []string{"name", "event"}, "SUBSYSTEM0") + + mCGroupVect = Metric.RegisterCounterVecGroup( []CounterOpts{ {Name: "counter1", Help: "counter1"}, }, []string{"name", "event"}, - "SUBSYSTEM") + "SUBSYSTEM1") - mGVect = Metric.RegisterGaugeVecGroup( + mGVect = Metric.RegisterGaugeVec(CounterOpts{Name: "gauge1", Help: "gauge1"}, []string{"name", "event"}, "SUBSYSTEM0") + + mGGroupVect = Metric.RegisterGaugeVecGroup( []CounterOpts{ - {Name: "counter2", Help: "counter2"}, + {Name: "gauge1", Help: "gauge1"}, }, []string{"name", "event"}, - "SUBSYSTEM") + "SUBSYSTEM1") + + tmpCVect := Metric.RegisterCounterVec(CounterOpts{Name: "counter1", Help: "counter1"}, []string{"name", "event"}, "SUBSYSTEM0") + + if tmpCVect.Vec != mCVect.Vec { + t.Errorf("tmpCVect not same than mCVect. cache not working?") + } + + tmpGVect := Metric.RegisterGaugeVec(CounterOpts{Name: "gauge1", Help: "gauge1"}, []string{"name", "event"}, "SUBSYSTEM0") + + if tmpGVect.Vec != mGVect.Vec { + t.Errorf("tmpGVect not same than mGVect. cache not working?") + } + + Metric.RegisterCounterVec(CounterOpts{Name: "counter1", Help: "counter1"}, []string{"name", "eventMismatch"}, "SUBSYSTEM0") + Metric.RegisterGaugeVec(CounterOpts{Name: "gauge1", Help: "gauge1"}, []string{"name", "eventMismatch"}, "SUBSYSTEM0") - mGGroup = Metric.RegisterGaugeGroup( - []CounterOpts{ - {Name: "counter3", Help: "counter3"}, - }, - "SUBSYSTEM2") } func TestMetricCounter(t *testing.T) { + TestCounterOpt := CounterOpts{Name: "CounterBlaah1", Help: "CounterBlaah1"} + ret1 := Metric.RegisterCounter(TestCounterOpt, "TestMetricCounter") + ret1.Inc() + ret2 := Metric.RegisterCounter(TestCounterOpt, "TestMetricCounter") + ret2.Inc() + if ret1 != ret2 { + t.Errorf("ret1 not same than ret2. cache not working?") + } +} + +func TestMetricCounterGroup(t *testing.T) { var TestCounterOpts = []CounterOpts{ - {Name: "Blaah1", Help: "Blaah1"}, - {Name: "Blaah2", Help: "Blaah2"}, - {Name: "Blaah3", Help: "Blaah3"}, - {Name: "Blaah4", Help: "Blaah4"}, + {Name: "CounterBlaah1", Help: "CounterBlaah1"}, + {Name: "CounterBlaah2", Help: "CounterBlaah2"}, + {Name: "CounterBlaah3", Help: "CounterBlaah3"}, + {Name: "CounterBlaah4", Help: "CounterBlaah4"}, } - ret1 := Metric.RegisterCounterGroup(TestCounterOpts, "TestMetricCounter") + ret1 := Metric.RegisterCounterGroup(TestCounterOpts, "TestMetricCounterGroup") if len(ret1) == 0 { t.Errorf("ret1 counter group is empty") } - ret2 := Metric.RegisterCounterGroup(TestCounterOpts, "TestMetricCounter") + ret1["CounterBlaah1"].Inc() + ret1["CounterBlaah2"].Inc() + ret1["CounterBlaah3"].Inc() + ret1["CounterBlaah4"].Inc() + + ret2 := Metric.RegisterCounterGroup(TestCounterOpts, "TestMetricCounterGroup") if len(ret2) == 0 { t.Errorf("ret2 counter group is empty") } + ret2["CounterBlaah1"].Inc() + ret2["CounterBlaah2"].Inc() + ret2["CounterBlaah3"].Inc() + ret2["CounterBlaah4"].Inc() + + if len(ret1) != len(ret2) { + t.Errorf("ret1 len %d differs from ret2 len %d", len(ret1), len(ret2)) + } +} + +func TestMetricGauge(t *testing.T) { + TestGaugeOpts := CounterOpts{Name: "GaugeBlaah1", Help: "GaugeBlaah1"} + ret1 := Metric.RegisterGauge(TestGaugeOpts, "TestMetricGauge") + ret1.Inc() + ret2 := Metric.RegisterGauge(TestGaugeOpts, "TestMetricGauge") + ret2.Inc() + if ret1 != ret2 { + t.Errorf("ret1 not same than ret2. cache not working?") + } +} + +func TestMetricGaugeGroup(t *testing.T) { + var TestGaugeOpts = []CounterOpts{ + {Name: "GaugeBlaah1", Help: "GaugeBlaah1"}, + {Name: "GaugeBlaah2", Help: "GaugeBlaah2"}, + {Name: "GaugeBlaah3", Help: "GaugeBlaah3"}, + {Name: "GaugeBlaah4", Help: "GaugeBlaah4"}, + } + + ret1 := Metric.RegisterGaugeGroup(TestGaugeOpts, "TestMetricGaugeGroup") + + if len(ret1) == 0 { + t.Errorf("ret1 gauge group is empty") + } + + ret1["GaugeBlaah1"].Inc() + ret1["GaugeBlaah2"].Inc() + ret1["GaugeBlaah3"].Inc() + ret1["GaugeBlaah4"].Inc() + + ret2 := Metric.RegisterGaugeGroup(TestGaugeOpts, "TestMetricGaugeGroup") + + if len(ret2) == 0 { + t.Errorf("ret2 gauge group is empty") + } + + ret2["GaugeBlaah1"].Inc() + ret2["GaugeBlaah2"].Inc() + ret2["GaugeBlaah3"].Inc() + ret2["GaugeBlaah4"].Inc() + if len(ret1) != len(ret2) { t.Errorf("ret1 len %d differs from ret2 len %d", len(ret1), len(ret2)) } @@ -77,7 +160,32 @@ func TestMetricCounter(t *testing.T) { func TestMetricCounterVector(t *testing.T) { // // - c_grp1 := Metric.GetCounterGroupFromVects([]string{"name1", "event1"}, mCVect) + c_1_1 := Metric.GetCounterFromVect([]string{"name1", "event1"}, mCVect) + c_1_1.Inc() + c_1_2 := Metric.GetCounterFromVect([]string{"name1", "event1"}, mCVect) + c_1_2.Inc() + if c_1_1 != c_1_2 { + t.Errorf("c_1_1 not same than c_1_2. cache not working?") + } + // + // + c_2_1 := Metric.GetCounterFromVect([]string{"name1", "event2"}, mCVect) + c_2_1.Inc() + c_2_2 := Metric.GetCounterFromVect([]string{"name1", "event2"}, mCVect) + c_2_2.Inc() + if c_2_1 != c_2_2 { + t.Errorf("c_2_1 not same than c_2_2. cache not working?") + } + if c_1_1 == c_2_1 { + t.Errorf("c_1_1 same than c_2_1. what?") + } + +} + +func TestMetricCounterGroupVector(t *testing.T) { + // + // + c_grp1 := Metric.GetCounterGroupFromVects([]string{"name1", "event1"}, mCGroupVect) if _, ok := c_grp1["counter1"]; ok == false { t.Errorf("c_grp1 counter1 not exists") } @@ -85,7 +193,7 @@ func TestMetricCounterVector(t *testing.T) { // // - c_grp2 := Metric.GetCounterGroupFromVects([]string{"name1", "event2"}, mCVect) + c_grp2 := Metric.GetCounterGroupFromVects([]string{"name1", "event2"}, mCGroupVect) if _, ok := c_grp2["counter1"]; ok == false { t.Errorf("c_grp2 counter1 not exists") } @@ -95,25 +203,49 @@ func TestMetricCounterVector(t *testing.T) { 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") + c_1_1 := Metric.GetGaugeFromVect([]string{"name1", "event1"}, mGVect) + c_1_1.Inc() + c_1_2 := Metric.GetGaugeFromVect([]string{"name1", "event1"}, mGVect) + c_1_2.Inc() + if c_1_1 != c_1_2 { + t.Errorf("c_1_1 not same than c_1_2. cache not working?") } - g_grp1["counter2"].Inc() + // + // + c_2_1 := Metric.GetGaugeFromVect([]string{"name1", "event2"}, mGVect) + c_2_1.Inc() + c_2_2 := Metric.GetGaugeFromVect([]string{"name1", "event2"}, mGVect) + c_2_2.Inc() + if c_2_1 != c_2_2 { + t.Errorf("c_2_1 not same than c_2_2. cache not working?") + } + if c_1_1 == c_2_1 { + t.Errorf("c_1_1 same than c_2_1. what?") + } +} +func TestMetricGaugeGroupVector(t *testing.T) { // // - g_grp2 := Metric.GetGaugeGroupFromVects([]string{"name1", "event2"}, mGVect) - if _, ok := g_grp2["counter2"]; ok == false { - t.Errorf("g_grp2 counter2 not exists") + g_grp1 := Metric.GetGaugeGroupFromVects([]string{"name1", "event1"}, mGGroupVect) + if _, ok := g_grp1["gauge1"]; ok == false { + t.Errorf("g_grp1 gauge1 not exists") } - g_grp2["counter2"].Inc() + g_grp1["gauge1"].Inc() + + // + // + g_grp2 := Metric.GetGaugeGroupFromVects([]string{"name1", "event2"}, mGGroupVect) + if _, ok := g_grp2["gauge1"]; ok == false { + t.Errorf("g_grp2 gauge1 not exists") + } + g_grp2["gauge1"].Inc() } -func TestMetricCounterVectorPrefix(t *testing.T) { +func TestMetricCounterGroupVectorPrefix(t *testing.T) { // // - c_grp1 := Metric.GetCounterGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mCVect) + c_grp1 := Metric.GetCounterGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mCGroupVect) if _, ok := c_grp1["event1_counter1"]; ok == false { t.Errorf("c_grp1 event1_counter1 not exists") } @@ -121,7 +253,7 @@ func TestMetricCounterVectorPrefix(t *testing.T) { // // - c_grp2 := Metric.GetCounterGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mCVect) + c_grp2 := Metric.GetCounterGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mCGroupVect) if _, ok := c_grp2["event2_counter1"]; ok == false { t.Errorf("c_grp2 event2_counter1 not exists") } @@ -149,49 +281,49 @@ func TestMetricCounterVectorPrefix(t *testing.T) { m_grp.CGet("event2_counter1") } -func TestMetricGaugeVectorPrefix(t *testing.T) { +func TestMetricGaugeGroupVectorPrefix(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 := Metric.GetGaugeGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mGGroupVect) + if _, ok := g_grp1["event1_gauge1"]; ok == false { + t.Errorf("g_grp1 event1_gauge1 not exists") } - g_grp1["event1_counter2"].Inc() + g_grp1["event1_gauge1"].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 := Metric.GetGaugeGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mGGroupVect) + if _, ok := g_grp2["event2_gauge1"]; ok == false { + t.Errorf("g_grp2 event2_gauge1 not exists") } - g_grp2["event2_counter2"].Inc() + g_grp2["event2_gauge1"].Inc() m_grp := NewMetricGroupsCache() m_grp.CombineGaugeGroups(g_grp1, g_grp2) // // - if m_grp.GIs("event1_counter2") == false { - t.Errorf("m_grp event1_counter2 not exists") + if m_grp.GIs("event1_gauge1") == false { + t.Errorf("m_grp event1_gauge1 not exists") } - m_grp.GInc("event1_counter2") + m_grp.GInc("event1_gauge1") // // - if m_grp.GIs("event2_counter2") == false { - t.Errorf("m_grp event2_counter2 not exists") + if m_grp.GIs("event2_gauge1") == false { + t.Errorf("m_grp event2_gauge1 not exists") } - m_grp.GInc("event2_counter2") + m_grp.GInc("event2_gauge1") - m_grp.GGet("event2_counter2") - m_grp.GDec("event2_counter2") - m_grp.GSet("event2_counter2", 1) + m_grp.GGet("event2_gauge1") + m_grp.GDec("event2_gauge1") + m_grp.GSet("event2_gauge1", 1) } func TestMetricGroupCache(t *testing.T) { // // - c_grp1 := Metric.GetCounterGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mCVect) + c_grp1 := Metric.GetCounterGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mCGroupVect) if _, ok := c_grp1["event1_counter1"]; ok == false { t.Errorf("c_grp1 event1_counter1 not exists") } @@ -199,35 +331,35 @@ func TestMetricGroupCache(t *testing.T) { // // - 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 := Metric.GetCounterGroupFromVects([]string{"name1", "event2"}, mCGroupVect) + if _, ok := c_grp2["counter1"]; ok == false { + t.Errorf("c_grp2 counter1 not exists") } - c_grp2["event2_counter1"].Inc() + c_grp2["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 := Metric.GetGaugeGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mGGroupVect) + if _, ok := g_grp1["event1_gauge1"]; ok == false { + t.Errorf("g_grp1 event1_gauge1 not exists") } - g_grp1["event1_counter2"].Inc() + g_grp1["event1_gauge1"].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 := Metric.GetGaugeGroupFromVects([]string{"name1", "event2"}, mGGroupVect) + if _, ok := g_grp2["gauge1"]; ok == false { + t.Errorf("g_grp2 gauge1 not exists") } - g_grp2["event2_counter2"].Inc() + g_grp2["gauge1"].Inc() // // m_grp := NewMetricGroupsCache() m_grp.CombineCounterGroups(c_grp1) - m_grp.CombineCounterGroups(c_grp2) + m_grp.CombineCounterGroupsWithPrefix("event2_", c_grp2) m_grp.CombineGaugeGroups(g_grp1) - m_grp.CombineGaugeGroups(g_grp2) + m_grp.CombineGaugeGroupsWithPrefix("event2_", g_grp2) if m_grp == nil { t.Errorf("Cache failed") @@ -243,19 +375,19 @@ func TestMetricGroupCache(t *testing.T) { } m_grp.CInc("event2_counter1") - if m_grp.GIs("event1_counter2") == false { - t.Errorf("m_grp.Gauges event1_counter2 not exists") + if m_grp.GIs("event1_gauge1") == false { + t.Errorf("m_grp.Gauges event1_gauge1 not exists") } - m_grp.GInc("event1_counter2") + m_grp.GInc("event1_gauge1") - if m_grp.GIs("event2_counter2") == false { - t.Errorf("m_grp.Gauges event2_counter2 not exists") + if m_grp.GIs("event2_gauge1") == false { + t.Errorf("m_grp.Gauges event2_gauge1 not exists") } - m_grp.GInc("event2_counter2") + m_grp.GInc("event2_gauge1") m_grp.CAdd("event2_counter1", 1) m_grp.CGet("event2_counter1") - m_grp.GGet("event2_counter2") - m_grp.GDec("event2_counter2") - m_grp.GSet("event2_counter2", 1) + m_grp.GGet("event2_gauge1") + m_grp.GDec("event2_gauge1") + m_grp.GSet("event2_gauge1", 1) } -- 2.16.6