From: Juha Hyttinen Date: Thu, 7 May 2020 09:42:26 +0000 (+0300) Subject: Support to prometheus CounterVec and GaugeVec. X-Git-Tag: v0.4.10~2 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=f619d03fbe1a5f4711f56bc973139a2094044066;p=ric-plt%2Fxapp-frame.git Support to prometheus CounterVec and GaugeVec. Change-Id: I053340aa148ab10499dfab7ec4f83aa94a811e31 Signed-off-by: Juha Hyttinen --- diff --git a/.dockerignore b/.dockerignore index 8705d90..b8d7351 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,2 +1,2 @@ -build/Dockerfile +ci/Dockerfile diff --git a/ci/Dockerfile b/ci/Dockerfile index dd5f626..ba1e07f 100755 --- a/ci/Dockerfile +++ b/ci/Dockerfile @@ -17,7 +17,7 @@ # #---------------------------------------------------------- -FROM nexus3.o-ran-sc.org:10004/bldr-ubuntu18-c-go:5-u18.04-nng as xapp-base +FROM nexus3.o-ran-sc.org:10004/o-ran-sc/bldr-ubuntu18-c-go:8-u18.04 as xapp-base RUN apt-get update -y \ &&apt-get install -y \ apt-utils \ @@ -35,10 +35,10 @@ RUN apt-get update -y \ RUN curl -s https://packagecloud.io/install/repositories/o-ran-sc/master/script.deb.sh | bash # RMR -ARG RMRVERSION=3.7.2 +ARG RMRVERSION=4.0.2 #RUN apt-get install -y rmr=${RMRVERSION} rmr-dev=${RMRVERSION} -RUN wget --content-disposition https://packagecloud.io/o-ran-sc/staging/packages/debian/stretch/rmr_${RMRVERSION}_amd64.deb/download.deb && dpkg -i rmr_${RMRVERSION}_amd64.deb -RUN wget --content-disposition https://packagecloud.io/o-ran-sc/staging/packages/debian/stretch/rmr-dev_${RMRVERSION}_amd64.deb/download.deb && dpkg -i rmr-dev_${RMRVERSION}_amd64.deb +RUN wget --content-disposition https://packagecloud.io/o-ran-sc/release/packages/debian/stretch/rmr_${RMRVERSION}_amd64.deb/download.deb && dpkg -i rmr_${RMRVERSION}_amd64.deb +RUN wget --content-disposition https://packagecloud.io/o-ran-sc/release/packages/debian/stretch/rmr-dev_${RMRVERSION}_amd64.deb/download.deb && dpkg -i rmr-dev_${RMRVERSION}_amd64.deb RUN rm -f rmr_${RMRVERSION}_amd64.deb rmr-dev_${RMRVERSION}_amd64.deb # diff --git a/pkg/xapp/metrics.go b/pkg/xapp/metrics.go index e31fdb7..d2d801c 100644 --- a/pkg/xapp/metrics.go +++ b/pkg/xapp/metrics.go @@ -31,7 +31,7 @@ type Metrics struct { } // Alias -type CounterOpts prometheus.CounterOpts +type CounterOpts prometheus.Opts type Counter prometheus.Counter type Gauge prometheus.Gauge @@ -51,9 +51,11 @@ func NewMetrics(url, namespace string, r *mux.Router) *Metrics { return &Metrics{Namespace: namespace} } -func (m *Metrics) RegisterCounter(opts CounterOpts) Counter { +/* + * Handling counters + */ +func (m *Metrics) registerCounter(opts CounterOpts) Counter { Logger.Info("Register new counter with opts: %v", opts) - return promauto.NewCounter(prometheus.CounterOpts(opts)) } @@ -62,15 +64,17 @@ func (m *Metrics) RegisterCounterGroup(opts []CounterOpts, subsytem string) (c m for _, opt := range opts { opt.Namespace = m.Namespace opt.Subsystem = subsytem - c[opt.Name] = m.RegisterCounter(opt) + c[opt.Name] = m.registerCounter(opt) } return } -func (m *Metrics) RegisterGauge(opts CounterOpts) Gauge { +/* + * Handling gauges + */ +func (m *Metrics) registerGauge(opts CounterOpts) Gauge { Logger.Info("Register new gauge with opts: %v", opts) - return promauto.NewGauge(prometheus.GaugeOpts(opts)) } @@ -79,8 +83,134 @@ func (m *Metrics) RegisterGaugeGroup(opts []CounterOpts, subsytem string) (c map for _, opt := range opts { opt.Namespace = m.Namespace opt.Subsystem = subsytem - c[opt.Name] = m.RegisterGauge(opt) + c[opt.Name] = m.registerGauge(opt) + } + + 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) +} + +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 + } + return +} + +func (m *Metrics) GetCounterGroupFromVects(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...) + Logger.Info("Register new counter for vector with opts: %v labels: %v", opt.Opts, labels) + } + } + return +} + +/* + * 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) +} + +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 + } + return +} +func (m *Metrics) GetGaugeGroupFromVects(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...) + Logger.Info("Register new gauge for vector with opts: %v labels: %v", opt.Opts, labels) + } + } return } + +/* + * + */ +func (m *Metrics) CombineCounterGroups(srcs ...map[string]Counter) map[string]Counter { + trg := make(map[string]Counter) + for _, src := range srcs { + for k, v := range src { + trg[k] = v + } + } + return trg +} + +func (m *Metrics) CombineGaugeGroups(srcs ...map[string]Gauge) map[string]Gauge { + trg := make(map[string]Gauge) + for _, src := range srcs { + for k, v := range src { + trg[k] = v + } + } + return trg +} diff --git a/pkg/xapp/xapp_test.go b/pkg/xapp/xapp_test.go index 9696e6e..37cca2a 100755 --- a/pkg/xapp/xapp_test.go +++ b/pkg/xapp/xapp_test.go @@ -114,7 +114,7 @@ func TestMessagesReceivedSuccessfully(t *testing.T) { } // Allow time to process the messages - time.Sleep(time.Duration(2) * time.Second) + time.Sleep(time.Duration(5) * time.Second) waitForSdl := viper.GetBool("db.waitForSdl") stats := getMetrics(t) @@ -159,7 +159,7 @@ func TestMessagesReceivedSuccessfullyUsingWh(t *testing.T) { } // Allow time to process the messages - time.Sleep(time.Duration(2) * time.Second) + time.Sleep(time.Duration(5) * time.Second) waitForSdl := viper.GetBool("db.waitForSdl") stats := getMetrics(t)