X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=pkg%2Fxapp%2Fmetrics.go;h=a90aaedd70e15c2f7b75a6bae983ae837e679c97;hb=2a05b784df856f77c8c4c0a64ddc4200f14adf13;hp=e31fdb7b07ab55078f78fc0ce938488add8746e7;hpb=2e78e42c5896b61b77ab3a97e45704f6749161b2;p=ric-plt%2Fxapp-frame.git diff --git a/pkg/xapp/metrics.go b/pkg/xapp/metrics.go old mode 100755 new mode 100644 index e31fdb7..a90aaed --- a/pkg/xapp/metrics.go +++ b/pkg/xapp/metrics.go @@ -20,21 +20,188 @@ package xapp import ( + "fmt" "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" ) -type Metrics struct { - Namespace string -} - +//----------------------------------------------------------------------------- // Alias -type CounterOpts prometheus.CounterOpts +//----------------------------------------------------------------------------- +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 +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- + +type MetricGroupsCache struct { + sync.RWMutex //This is for map locking + counters map[string]Counter + gauges map[string]Gauge +} + +func (met *MetricGroupsCache) CIs(metric string) bool { + met.RLock() + defer met.RUnlock() + _, ok := met.counters[metric] + return ok +} + +func (met *MetricGroupsCache) CGet(metric string) Counter { + met.RLock() + defer met.RUnlock() + return met.counters[metric] +} + +func (met *MetricGroupsCache) CInc(metric string) { + met.RLock() + defer met.RUnlock() + met.counters[metric].Inc() +} + +func (met *MetricGroupsCache) CAdd(metric string, val float64) { + met.RLock() + defer met.RUnlock() + met.counters[metric].Add(val) +} + +func (met *MetricGroupsCache) GIs(metric string) bool { + met.RLock() + defer met.RUnlock() + _, ok := met.gauges[metric] + return ok +} + +func (met *MetricGroupsCache) GGet(metric string) Gauge { + met.RLock() + defer met.RUnlock() + return met.gauges[metric] +} + +func (met *MetricGroupsCache) GSet(metric string, val float64) { + met.RLock() + defer met.RUnlock() + met.gauges[metric].Set(val) +} + +func (met *MetricGroupsCache) GAdd(metric string, val float64) { + met.RLock() + defer met.RUnlock() + met.gauges[metric].Add(val) +} + +func (met *MetricGroupsCache) GInc(metric string) { + met.RLock() + defer met.RUnlock() + met.gauges[metric].Inc() +} + +func (met *MetricGroupsCache) GDec(metric string) { + met.RLock() + defer met.RUnlock() + 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() + for _, src := range srcs { + for k, v := range src { + met.counters[k] = v + } + } +} + +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() + for _, src := range srcs { + for k, v := range src { + met.gauges[k] = v + } + } +} + +func NewMetricGroupsCache() *MetricGroupsCache { + entry := &MetricGroupsCache{} + entry.counters = make(map[string]Counter) + entry.gauges = make(map[string]Gauge) + return entry +} + +//----------------------------------------------------------------------------- +// All counters/gauges registered via Metrics instances: +// Counter names are build from: namespace, subsystem, metric and possible labels +//----------------------------------------------------------------------------- +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) +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +type Metrics struct { + Namespace string +} + func NewMetrics(url, namespace string, r *mux.Router) *Metrics { if url == "" { url = "/ric/v1/metrics" @@ -51,36 +218,338 @@ func NewMetrics(url, namespace string, r *mux.Router) *Metrics { return &Metrics{Namespace: namespace} } -func (m *Metrics) RegisterCounter(opts CounterOpts) Counter { - Logger.Info("Register new counter with opts: %v", opts) +/* + * Helpers + */ +func (m *Metrics) getFullName(opts prometheus.Opts, labels []string) string { + labelname := "" + for _, lbl := range labels { + if len(labelname) == 0 { + labelname += lbl + } else { + labelname += "_" + lbl + } + } + return fmt.Sprintf("%s_%s_%s_%s", opts.Namespace, opts.Subsystem, opts.Name, labelname) +} - return promauto.NewCounter(prometheus.CounterOpts(opts)) +// +// +// +func (m *Metrics) RegisterCounter(opts CounterOpts, subsytem string) Counter { + globalLock.Lock() + defer globalLock.Unlock() + 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] } -func (m *Metrics) RegisterCounterGroup(opts []CounterOpts, subsytem string) (c map[string]Counter) { - c = make(map[string]Counter) +// +// +// +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) RegisterLabeledCounter(opts CounterOpts, labelNames []string, labelValues []string, subsytem string) Counter { + globalLock.Lock() + defer globalLock.Unlock() + opts.Namespace = m.Namespace + opts.Subsystem = subsytem + vecid := m.getFullName(prometheus.Opts(opts), []string{}) + if _, ok := cache_allcountervects[vecid]; !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[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) + } + + valid := m.getFullName(prometheus.Opts(entry.Opts), labelValues) + if _, ok := cache_allcounters[valid]; !ok { + Logger.Info("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) 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 c +} + +// +// +// +func (m *Metrics) RegisterGauge(opts CounterOpts, subsytem string) Gauge { + globalLock.Lock() + defer globalLock.Unlock() + 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] +} + +// +// +// +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 +} + +// +// +// +func (m *Metrics) RegisterLabeledGauge(opt CounterOpts, labelNames []string, labelValues []string, subsytem string) Gauge { + globalLock.Lock() + defer globalLock.Unlock() + opt.Namespace = m.Namespace + opt.Subsystem = subsytem + vecid := m.getFullName(prometheus.Opts(opt), []string{}) + if _, ok := cache_allgaugevects[vecid]; !ok { + Logger.Info("Register new gauge vector with opt: %v labelNames: %v", opt, labelNames) + entry := GaugeVec{} + entry.Opts = opt + 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) + } + valid := m.getFullName(prometheus.Opts(entry.Opts), labelValues) + if _, ok := cache_allgauges[valid]; !ok { + Logger.Info("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) RegisterLabeledGaugeGroup(opts []CounterOpts, labelNames []string, labelValues []string, subsytem string) map[string]Gauge { + c := make(map[string]Gauge) for _, opt := range opts { - opt.Namespace = m.Namespace - opt.Subsystem = subsytem - c[opt.Name] = m.RegisterCounter(opt) + c[opt.Name] = m.RegisterLabeledGauge(opt, labelNames, labelValues, subsytem) } + return c +} + +/* + * Handling counter vectors + * + * 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"}, + {Name: "counter2", Help: "counter2"}, + }, + []string{"host"}, + "SUBSYSTEM") + + stats:=Metric.GetCounterGroupFromVects([]string{"localhost:8888"}, vec) + stats["counter1"].Inc() +*/ - return +// Deprecated: Use RegisterLabeledCounter +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 +} + +// Deprecated: Use RegisterLabeledCounterGroup +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 c +} + +// Deprecated: Use RegisterLabeledCounter +func (m *Metrics) GetCounterFromVect(labelValues []string, vec CounterVec) (c Counter) { + globalLock.Lock() + defer globalLock.Unlock() + 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) RegisterGauge(opts CounterOpts) Gauge { - Logger.Info("Register new gauge with opts: %v", opts) +// Deprecated: Use RegisterLabeledCounterGroup +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 c +} + +// Deprecated: Use RegisterLabeledCounterGroup +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 + * + * Examples: + + //--------- + 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"}, + }, + []string{"host"}, + "SUBSYSTEM") + + stats:=Metric.GetGaugeGroupFromVects([]string{"localhost:8888"},vecgrp) + stats["gauge1"].Inc() +*/ - return promauto.NewGauge(prometheus.GaugeOpts(opts)) +// Deprecated: Use RegisterLabeledGauge +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.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) RegisterGaugeGroup(opts []CounterOpts, subsytem string) (c map[string]Gauge) { - c = make(map[string]Gauge) +// Deprecated: Use RegisterLabeledGaugeGroup +func (m *Metrics) RegisterGaugeVecGroup(opts []CounterOpts, labelNames []string, subsytem string) map[string]GaugeVec { + c := make(map[string]GaugeVec) for _, opt := range opts { - opt.Namespace = m.Namespace - opt.Subsystem = subsytem - c[opt.Name] = m.RegisterGauge(opt) + c[opt.Name] = m.RegisterGaugeVec(opt, labelNames, subsytem) } + return c +} - return +// Deprecated: Use RegisterLabeledGauge +func (m *Metrics) GetGaugeFromVect(labelValues []string, vec GaugeVec) Gauge { + globalLock.Lock() + defer globalLock.Unlock() + 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] +} + +// Deprecated: Use RegisterLabeledGaugeGroup +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 c +} + +// Deprecated: Use RegisterLabeledGaugeGroup +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 }