2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
23 "github.com/gorilla/mux"
24 "github.com/prometheus/client_golang/prometheus"
25 "github.com/prometheus/client_golang/prometheus/promauto"
26 "github.com/prometheus/client_golang/prometheus/promhttp"
34 type CounterOpts prometheus.Opts
35 type Counter prometheus.Counter
36 type Gauge prometheus.Gauge
38 func NewMetrics(url, namespace string, r *mux.Router) *Metrics {
40 url = "/ric/v1/metrics"
46 Logger.Info("Serving metrics on: url=%s namespace=%s", url, namespace)
48 // Expose 'metrics' endpoint with standard golang metrics used by prometheus
49 r.Handle(url, promhttp.Handler())
51 return &Metrics{Namespace: namespace}
57 func (m *Metrics) registerCounter(opts CounterOpts) Counter {
58 Logger.Info("Register new counter with opts: %v", opts)
59 return promauto.NewCounter(prometheus.CounterOpts(opts))
62 func (m *Metrics) RegisterCounterGroup(opts []CounterOpts, subsytem string) (c map[string]Counter) {
63 c = make(map[string]Counter)
64 for _, opt := range opts {
65 opt.Namespace = m.Namespace
66 opt.Subsystem = subsytem
67 c[opt.Name] = m.registerCounter(opt)
76 func (m *Metrics) registerGauge(opts CounterOpts) Gauge {
77 Logger.Info("Register new gauge with opts: %v", opts)
78 return promauto.NewGauge(prometheus.GaugeOpts(opts))
81 func (m *Metrics) RegisterGaugeGroup(opts []CounterOpts, subsytem string) (c map[string]Gauge) {
82 c = make(map[string]Gauge)
83 for _, opt := range opts {
84 opt.Namespace = m.Namespace
85 opt.Subsystem = subsytem
86 c[opt.Name] = m.registerGauge(opt)
93 * Handling counter vectors
97 vec := Metric.RegisterCounterVecGroup(
99 {Name: "counter1", Help: "counter1"},
100 {Name: "counter2", Help: "counter2"},
105 stat:=Metric.GetCounterGroupFromVects([]string{"localhost:8888"}, vec)
108 type CounterVec struct {
109 Vec *prometheus.CounterVec
113 func (m *Metrics) registerCounterVec(opts CounterOpts, labelNames []string) *prometheus.CounterVec {
114 Logger.Info("Register new counter vector with opts: %v labelNames: %v", opts, labelNames)
116 return promauto.NewCounterVec(prometheus.CounterOpts(opts), labelNames)
119 func (m *Metrics) RegisterCounterVecGroup(opts []CounterOpts, labelNames []string, subsytem string) (c map[string]CounterVec) {
120 c = make(map[string]CounterVec)
121 for _, opt := range opts {
122 entry := CounterVec{}
124 entry.Opts.Namespace = m.Namespace
125 entry.Opts.Subsystem = subsytem
126 entry.Vec = m.registerCounterVec(entry.Opts, labelNames)
132 func (m *Metrics) GetCounterGroupFromVects(labels []string, vects ...map[string]CounterVec) (c map[string]Counter) {
133 c = make(map[string]Counter)
134 for _, vec := range vects {
135 for name, opt := range vec {
136 c[name] = opt.Vec.WithLabelValues(labels...)
137 Logger.Info("Register new counter for vector with opts: %v labels: %v", opt.Opts, labels)
144 * Handling gauge vectors
148 vec := Metric.RegisterGaugeVecGroup(
150 {Name: "gauge1", Help: "gauge1"},
151 {Name: "gauge2", Help: "gauge2"},
156 stat:=Metric.GetGaugeGroupFromVects([]string{"localhost:8888"},vec)
159 type GaugeVec struct {
160 Vec *prometheus.GaugeVec
164 func (m *Metrics) registerGaugeVec(opts CounterOpts, labelNames []string) *prometheus.GaugeVec {
165 Logger.Info("Register new gauge vector with opts: %v labelNames: %v", opts, labelNames)
167 return promauto.NewGaugeVec(prometheus.GaugeOpts(opts), labelNames)
170 func (m *Metrics) RegisterGaugeVecGroup(opts []CounterOpts, labelNames []string, subsytem string) (c map[string]GaugeVec) {
171 c = make(map[string]GaugeVec)
172 for _, opt := range opts {
175 entry.Opts.Namespace = m.Namespace
176 entry.Opts.Subsystem = subsytem
177 entry.Vec = m.registerGaugeVec(entry.Opts, labelNames)
184 func (m *Metrics) GetGaugeGroupFromVects(labels []string, vects ...map[string]GaugeVec) (c map[string]Gauge) {
185 c = make(map[string]Gauge)
186 for _, vec := range vects {
187 for name, opt := range vec {
188 c[name] = opt.Vec.WithLabelValues(labels...)
189 Logger.Info("Register new gauge for vector with opts: %v labels: %v", opt.Opts, labels)
198 func (m *Metrics) CombineCounterGroups(srcs ...map[string]Counter) map[string]Counter {
199 trg := make(map[string]Counter)
200 for _, src := range srcs {
201 for k, v := range src {
208 func (m *Metrics) CombineGaugeGroups(srcs ...map[string]Gauge) map[string]Gauge {
209 trg := make(map[string]Gauge)
210 for _, src := range srcs {
211 for k, v := range src {