xapp frame rmr wrapper buffer handling fix
[ric-plt/xapp-frame.git] / pkg / xapp / metrics.go
index f85c488..8e13f7c 100644 (file)
@@ -21,13 +21,29 @@ package xapp
 
 import (
        "fmt"
+       "sync"
+
        "github.com/gorilla/mux"
        "github.com/prometheus/client_golang/prometheus"
        "github.com/prometheus/client_golang/prometheus/promauto"
        "github.com/prometheus/client_golang/prometheus/promhttp"
-       "sync"
 )
 
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+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
+}
+
 //-----------------------------------------------------------------------------
 // Alias
 //-----------------------------------------------------------------------------
@@ -35,6 +51,44 @@ 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
+}
+
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+type MetricGroupsCacheCounterRegisterer interface {
+       RegisterCounter(CounterOpts) Counter
+}
+
+type MetricGroupsCacheCounterRegistererFunc func(CounterOpts) Counter
+
+func (fn MetricGroupsCacheCounterRegistererFunc) RegisterCounter(copts CounterOpts) Counter {
+       return fn(copts)
+}
+
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+type MetricGroupsCacheGaugeRegisterer interface {
+       RegisterGauge(CounterOpts) Gauge
+}
+
+type MetricGroupsCacheGaugeRegistererFunc func(CounterOpts) Gauge
+
+func (fn MetricGroupsCacheGaugeRegistererFunc) RegisterGauge(copts CounterOpts) Gauge {
+       return fn(copts)
+}
+
 //-----------------------------------------------------------------------------
 //
 //-----------------------------------------------------------------------------
@@ -43,88 +97,179 @@ type MetricGroupsCache struct {
        sync.RWMutex //This is for map locking
        counters     map[string]Counter
        gauges       map[string]Gauge
+       regcnt       MetricGroupsCacheCounterRegisterer
+       reggau       MetricGroupsCacheGaugeRegisterer
+}
+
+func (met *MetricGroupsCache) Registerer(regcnt MetricGroupsCacheCounterRegisterer, reggau MetricGroupsCacheGaugeRegisterer) {
+       met.regcnt = regcnt
+       met.reggau = reggau
+}
+
+func (met *MetricGroupsCache) cReg(metric string) Counter {
+       if met.regcnt != nil {
+               cntr := met.regcnt.RegisterCounter(CounterOpts{Name: metric, Help: "Amount of " + metric + "(auto)"})
+               met.counters[metric] = cntr
+               return cntr
+       }
+       return nil
+}
+func (met *MetricGroupsCache) gReg(metric string) Gauge {
+       if met.reggau != nil {
+               gaug := met.reggau.RegisterGauge(CounterOpts{Name: metric, Help: "Amount of " + metric + "(auto)"})
+               met.gauges[metric] = gaug
+               return gaug
+       }
+       return nil
 }
 
 func (met *MetricGroupsCache) CIs(metric string) bool {
-       met.RLock()
-       defer met.RUnlock()
+       met.Lock()
+       defer met.Unlock()
        _, ok := met.counters[metric]
        return ok
 }
 
 func (met *MetricGroupsCache) CGet(metric string) Counter {
-       met.RLock()
-       defer met.RUnlock()
-       return met.counters[metric]
+       met.Lock()
+       defer met.Unlock()
+       cntr, ok := met.counters[metric]
+       if !ok {
+               cntr = met.cReg(metric)
+       }
+       return cntr
 }
 
 func (met *MetricGroupsCache) CInc(metric string) {
-       met.RLock()
-       defer met.RUnlock()
-       met.counters[metric].Inc()
+       met.Lock()
+       defer met.Unlock()
+       cntr, ok := met.counters[metric]
+       if !ok {
+               cntr = met.cReg(metric)
+       }
+       cntr.Inc()
 }
 
 func (met *MetricGroupsCache) CAdd(metric string, val float64) {
-       met.RLock()
-       defer met.RUnlock()
-       met.counters[metric].Add(val)
+       met.Lock()
+       defer met.Unlock()
+       cntr, ok := met.counters[metric]
+       if !ok {
+               cntr = met.cReg(metric)
+       }
+       cntr.Add(val)
 }
 
 func (met *MetricGroupsCache) GIs(metric string) bool {
-       met.RLock()
-       defer met.RUnlock()
+       met.Lock()
+       defer met.Unlock()
        _, ok := met.gauges[metric]
        return ok
 }
 
 func (met *MetricGroupsCache) GGet(metric string) Gauge {
-       met.RLock()
-       defer met.RUnlock()
-       return met.gauges[metric]
+       met.Lock()
+       defer met.Unlock()
+       gaug, ok := met.gauges[metric]
+       if !ok {
+               gaug = met.gReg(metric)
+       }
+       return gaug
 }
 
 func (met *MetricGroupsCache) GSet(metric string, val float64) {
-       met.RLock()
-       defer met.RUnlock()
-       met.gauges[metric].Set(val)
+       met.Lock()
+       defer met.Unlock()
+       gaug, ok := met.gauges[metric]
+       if !ok {
+               gaug = met.gReg(metric)
+       }
+       gaug.Set(val)
 }
 
-func (met *MetricGroupsCache) GInc(metric string) {
-       met.RLock()
-       defer met.RUnlock()
-       met.gauges[metric].Inc()
+func (met *MetricGroupsCache) GAdd(metric string, val float64) {
+       met.Lock()
+       defer met.Unlock()
+       gaug, ok := met.gauges[metric]
+       if !ok {
+               gaug = met.gReg(metric)
+       }
+       gaug.Add(val)
 }
 
-func (met *MetricGroupsCache) GDec(metric string) {
-       met.RLock()
-       defer met.RUnlock()
-       met.gauges[metric].Dec()
+func (met *MetricGroupsCache) GInc(metric string) {
+       met.Lock()
+       defer met.Unlock()
+       gaug, ok := met.gauges[metric]
+       if !ok {
+               gaug = met.gReg(metric)
+       }
+       gaug.Inc()
 }
 
-func (met *MetricGroupsCache) CombineCounterGroups(srcs ...map[string]Counter) {
+func (met *MetricGroupsCache) GDec(metric string) {
        met.Lock()
        defer met.Unlock()
+       gaug, ok := met.gauges[metric]
+       if !ok {
+               gaug = met.gReg(metric)
+       }
+       gaug.Dec()
+}
+
+func (met *MetricGroupsCache) combineCounterGroupsWithPrefix(prefix string, srcs ...map[string]Counter) {
        for _, src := range srcs {
                for k, v := range src {
-                       met.counters[k] = v
+                       met.counters[prefix+k] = v
                }
        }
 }
 
-func (met *MetricGroupsCache) CombineGaugeGroups(srcs ...map[string]Gauge) {
+func (met *MetricGroupsCache) CombineCounterGroupsWithPrefix(prefix string, srcs ...map[string]Counter) {
        met.Lock()
        defer met.Unlock()
+       met.combineCounterGroupsWithPrefix(prefix, srcs...)
+}
+
+func (met *MetricGroupsCache) CombineCounterGroups(srcs ...map[string]Counter) {
+       met.Lock()
+       defer met.Unlock()
+       met.combineCounterGroupsWithPrefix("", srcs...)
+}
+
+func (met *MetricGroupsCache) combineGaugeGroupsWithPrefix(prefix string, srcs ...map[string]Gauge) {
        for _, src := range srcs {
                for k, v := range src {
-                       met.gauges[k] = v
+                       met.gauges[prefix+k] = v
                }
        }
 }
 
+func (met *MetricGroupsCache) CombineGaugeGroupsWithPrefix(prefix string, srcs ...map[string]Gauge) {
+       met.Lock()
+       defer met.Unlock()
+       met.combineGaugeGroupsWithPrefix(prefix, srcs...)
+}
+
+func (met *MetricGroupsCache) CombineGaugeGroups(srcs ...map[string]Gauge) {
+       met.Lock()
+       defer met.Unlock()
+       met.combineGaugeGroupsWithPrefix("", srcs...)
+}
+
 func NewMetricGroupsCache() *MetricGroupsCache {
        entry := &MetricGroupsCache{}
        entry.counters = make(map[string]Counter)
        entry.gauges = make(map[string]Gauge)
+       entry.regcnt = nil
+       entry.reggau = nil
+       return entry
+}
+
+func NewMetricGroupsCacheWithRegisterers(regcnt MetricGroupsCacheCounterRegisterer, reggau MetricGroupsCacheGaugeRegisterer) *MetricGroupsCache {
+       entry := NewMetricGroupsCache()
+       entry.regcnt = regcnt
+       entry.reggau = reggau
        return entry
 }
 
@@ -135,10 +280,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)
 }
 
 //-----------------------------------------------------------------------------
@@ -152,10 +301,6 @@ func NewMetrics(url, namespace string, r *mux.Router) *Metrics {
        if url == "" {
                url = "/ric/v1/metrics"
        }
-       if namespace == "" {
-               namespace = "ricxapp"
-       }
-
        Logger.Info("Serving metrics on: url=%s namespace=%s", url, namespace)
 
        // Expose 'metrics' endpoint with standard golang metrics used by prometheus
@@ -179,179 +324,154 @@ func (m *Metrics) getFullName(opts prometheus.Opts, labels []string) string {
        return fmt.Sprintf("%s_%s_%s_%s", opts.Namespace, opts.Subsystem, opts.Name, labelname)
 }
 
-/*
- * 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_allcountervects[id]; ok {
+               Logger.Warn("Register new counter with opts: %v, name conflicts existing counter vector", opts)
+               return nil
        }
-
-       return
+       if _, ok := cache_allcounters[id]; !ok {
+               Logger.Debug("Register new counter with opts: %v", opts)
+               cache_allcounters[id] = promauto.NewCounter(prometheus.CounterOpts(opts))
+       }
+       return cache_allcounters[id]
 }
 
-/*
- * 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) 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
 }
 
-func (m *Metrics) RegisterGaugeGroup(opts []CounterOpts, subsytem string) (c map[string]Gauge) {
+//
+//
+//
+func (m *Metrics) RegisterLabeledCounter(opts CounterOpts, labelNames []string, labelValues []string, subsytem string) Counter {
        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
+       vecid := m.getFullName(prometheus.Opts(opts), []string{})
+       if _, ok := cache_allcounters[vecid]; ok {
+               Logger.Warn("Register new counter vector with opts: %v labelNames: %v, name conflicts existing counter", opts, labelNames)
+               return nil
        }
-
-       return
-}
-
-/*
- * Handling counter vectors
- *
- * Example:
-
-       vec := Metric.RegisterCounterVecGroup(
-               []CounterOpts{
-                       {Name: "counter1", Help: "counter1"},
-                       {Name: "counter2", Help: "counter2"},
-               },
-               []string{"host"},
-               "SUBSYSTEM")
-
-       stat:=Metric.GetCounterGroupFromVects([]string{"localhost:8888"}, vec)
-
-*/
-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)
+       if _, ok := cache_allcountervects[vecid]; !ok {
+               Logger.Debug("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[vecid] = entry
+       }
+       entry := cache_allcountervects[vecid]
+       if strSliceCompare(entry.Labels, labelNames) == false {
+               Logger.Warn("id:%s cached counter vec labels dont match %v != %v", vecid, entry.Labels, labelNames)
+               return nil
+       }
+       valid := m.getFullName(prometheus.Opts(entry.Opts), labelValues)
+       if _, ok := cache_allcounters[valid]; !ok {
+               Logger.Debug("Register new counter from vector with opts: %v labelValues: %v", entry.Opts, labelValues)
+               cache_allcounters[valid] = entry.Vec.WithLabelValues(labelValues...)
+       }
+       return cache_allcounters[valid]
 }
 
-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) RegisterLabeledCounterGroup(optsgroup []CounterOpts, labelNames []string, labelValues []string, subsytem string) map[string]Counter {
+       c := make(map[string]Counter)
+       for _, opts := range optsgroup {
+               c[opts.Name] = m.RegisterLabeledCounter(opts, labelNames, labelValues, subsytem)
        }
-       return
+       return c
 }
 
-func (m *Metrics) GetCounterGroupFromVectsWithPrefix(prefix string, labels []string, vects ...map[string]CounterVec) (c map[string]Counter) {
+//
+//
+//
+func (m *Metrics) RegisterGauge(opts CounterOpts, subsytem string) Gauge {
        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]
-               }
+       opts.Namespace = m.Namespace
+       opts.Subsystem = subsytem
+       id := m.getFullName(prometheus.Opts(opts), []string{})
+       if _, ok := cache_allgaugevects[id]; ok {
+               Logger.Warn("Register new gauge with opts: %v, name conflicts existing gauge vector", opts)
+               return nil
        }
-       return
-}
-
-func (m *Metrics) GetCounterGroupFromVects(labels []string, vects ...map[string]CounterVec) (c map[string]Counter) {
-       return m.GetCounterGroupFromVectsWithPrefix("", labels, vects...)
-}
-
-/*
- * Handling gauge vectors
- *
- * Example:
-
-       vec := Metric.RegisterGaugeVecGroup(
-               []CounterOpts{
-                       {Name: "gauge1", Help: "gauge1"},
-                       {Name: "gauge2", Help: "gauge2"},
-               },
-               []string{"host"},
-               "SUBSYSTEM")
-
-       stat:=Metric.GetGaugeGroupFromVects([]string{"localhost:8888"},vec)
-
-*/
-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)
+       if _, ok := cache_allgauges[id]; !ok {
+               Logger.Debug("Register new gauge with opts: %v", opts)
+               cache_allgauges[id] = promauto.NewGauge(prometheus.GaugeOpts(opts))
+       }
+       return cache_allgauges[id]
 }
 
-func (m *Metrics) RegisterGaugeVecGroup(opts []CounterOpts, labelNames []string, subsytem string) (c map[string]GaugeVec) {
-       c = make(map[string]GaugeVec)
-       for _, opt := range opts {
-               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
-
+//
+//
+//
+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
+       return c
 }
 
-func (m *Metrics) GetGaugeGroupFromVectsWithPrefix(prefix string, labels []string, vects ...map[string]GaugeVec) (c map[string]Gauge) {
+//
+//
+//
+func (m *Metrics) RegisterLabeledGauge(opts CounterOpts, labelNames []string, labelValues []string, subsytem string) 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]
-               }
+       opts.Namespace = m.Namespace
+       opts.Subsystem = subsytem
+       vecid := m.getFullName(prometheus.Opts(opts), []string{})
+       if _, ok := cache_allgauges[vecid]; ok {
+               Logger.Warn("Register new gauge vector with opts: %v labelNames: %v, name conflicts existing counter", opts, labelNames)
+               return nil
+       }
+       if _, ok := cache_allgaugevects[vecid]; !ok {
+               Logger.Debug("Register new gauge vector with opts: %v labelNames: %v", opts, labelNames)
+               entry := GaugeVec{}
+               entry.Opts = opts
+               entry.Labels = labelNames
+               entry.Vec = promauto.NewGaugeVec(prometheus.GaugeOpts(entry.Opts), entry.Labels)
+               cache_allgaugevects[vecid] = entry
+       }
+       entry := cache_allgaugevects[vecid]
+       if strSliceCompare(entry.Labels, labelNames) == false {
+               Logger.Warn("id:%s cached gauge vec labels dont match %v != %v", vecid, entry.Labels, labelNames)
+               return nil
        }
-       return
+       valid := m.getFullName(prometheus.Opts(entry.Opts), labelValues)
+       if _, ok := cache_allgauges[valid]; !ok {
+               Logger.Debug("Register new gauge from vector with opts: %v labelValues: %v", entry.Opts, labelValues)
+               cache_allgauges[valid] = entry.Vec.WithLabelValues(labelValues...)
+       }
+       return cache_allgauges[valid]
 }
 
-func (m *Metrics) GetGaugeGroupFromVects(labels []string, vects ...map[string]GaugeVec) (c map[string]Gauge) {
-       return m.GetGaugeGroupFromVectsWithPrefix("", labels, vects...)
+//
+//
+//
+func (m *Metrics) RegisterLabeledGaugeGroup(optsgroup []CounterOpts, labelNames []string, labelValues []string, subsytem string) map[string]Gauge {
+       c := make(map[string]Gauge)
+       for _, opts := range optsgroup {
+               c[opts.Name] = m.RegisterLabeledGauge(opts, labelNames, labelValues, subsytem)
+       }
+       return c
 }