Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 1 | /* |
| 2 | ================================================================================== |
| 3 | Copyright (c) 2019 AT&T Intellectual Property. |
| 4 | Copyright (c) 2019 Nokia |
| 5 | |
| 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 |
| 9 | |
| 10 | http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | |
| 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 | ================================================================================== |
| 18 | */ |
| 19 | |
| 20 | package xapp |
| 21 | |
| 22 | import ( |
Juha Hyttinen | 5bfa60a | 2020-10-30 10:31:39 +0200 | [diff] [blame^] | 23 | "fmt" |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 24 | "github.com/gorilla/mux" |
| 25 | "github.com/prometheus/client_golang/prometheus" |
| 26 | "github.com/prometheus/client_golang/prometheus/promauto" |
| 27 | "github.com/prometheus/client_golang/prometheus/promhttp" |
Juha Hyttinen | 6492438 | 2020-10-08 14:06:57 +0300 | [diff] [blame] | 28 | "sync" |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 29 | ) |
| 30 | |
Juha Hyttinen | 90f6dd4 | 2020-05-08 12:17:05 +0300 | [diff] [blame] | 31 | //----------------------------------------------------------------------------- |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 32 | // Alias |
Juha Hyttinen | 90f6dd4 | 2020-05-08 12:17:05 +0300 | [diff] [blame] | 33 | //----------------------------------------------------------------------------- |
Juha Hyttinen | f619d03 | 2020-05-07 12:42:26 +0300 | [diff] [blame] | 34 | type CounterOpts prometheus.Opts |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 35 | type Counter prometheus.Counter |
| 36 | type Gauge prometheus.Gauge |
| 37 | |
Juha Hyttinen | 90f6dd4 | 2020-05-08 12:17:05 +0300 | [diff] [blame] | 38 | //----------------------------------------------------------------------------- |
| 39 | // |
| 40 | //----------------------------------------------------------------------------- |
| 41 | |
| 42 | type MetricGroupsCache struct { |
Juha Hyttinen | 5bfa60a | 2020-10-30 10:31:39 +0200 | [diff] [blame^] | 43 | sync.RWMutex //This is for map locking |
| 44 | counters map[string]Counter |
| 45 | gauges map[string]Gauge |
| 46 | } |
| 47 | |
| 48 | func (met *MetricGroupsCache) CIs(metric string) bool { |
| 49 | met.RLock() |
| 50 | defer met.RUnlock() |
| 51 | _, ok := met.counters[metric] |
| 52 | return ok |
Juha Hyttinen | 90f6dd4 | 2020-05-08 12:17:05 +0300 | [diff] [blame] | 53 | } |
| 54 | |
Juha Hyttinen | 622bae3 | 2020-08-10 08:20:22 +0300 | [diff] [blame] | 55 | func (met *MetricGroupsCache) CInc(metric string) { |
Juha Hyttinen | 5bfa60a | 2020-10-30 10:31:39 +0200 | [diff] [blame^] | 56 | met.RLock() |
| 57 | defer met.RUnlock() |
| 58 | met.counters[metric].Inc() |
Juha Hyttinen | 622bae3 | 2020-08-10 08:20:22 +0300 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | func (met *MetricGroupsCache) CAdd(metric string, val float64) { |
Juha Hyttinen | 5bfa60a | 2020-10-30 10:31:39 +0200 | [diff] [blame^] | 62 | met.RLock() |
| 63 | defer met.RUnlock() |
| 64 | met.counters[metric].Add(val) |
| 65 | } |
| 66 | |
| 67 | func (met *MetricGroupsCache) GIs(metric string) bool { |
| 68 | met.RLock() |
| 69 | defer met.RUnlock() |
| 70 | _, ok := met.gauges[metric] |
| 71 | return ok |
Juha Hyttinen | 622bae3 | 2020-08-10 08:20:22 +0300 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | func (met *MetricGroupsCache) GSet(metric string, val float64) { |
Juha Hyttinen | 5bfa60a | 2020-10-30 10:31:39 +0200 | [diff] [blame^] | 75 | met.RLock() |
| 76 | defer met.RUnlock() |
| 77 | met.gauges[metric].Set(val) |
| 78 | } |
| 79 | |
| 80 | func (met *MetricGroupsCache) GInc(metric string) { |
| 81 | met.RLock() |
| 82 | defer met.RUnlock() |
| 83 | met.gauges[metric].Inc() |
| 84 | } |
| 85 | |
| 86 | func (met *MetricGroupsCache) GDec(metric string) { |
| 87 | met.RLock() |
| 88 | defer met.RUnlock() |
| 89 | met.gauges[metric].Dec() |
| 90 | } |
| 91 | |
| 92 | func (met *MetricGroupsCache) CombineCounterGroups(srcs ...map[string]Counter) { |
| 93 | met.Lock() |
| 94 | defer met.Unlock() |
| 95 | for _, src := range srcs { |
| 96 | for k, v := range src { |
| 97 | met.counters[k] = v |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | func (met *MetricGroupsCache) CombineGaugeGroups(srcs ...map[string]Gauge) { |
| 103 | met.Lock() |
| 104 | defer met.Unlock() |
| 105 | for _, src := range srcs { |
| 106 | for k, v := range src { |
| 107 | met.gauges[k] = v |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func NewMetricGroupsCache() *MetricGroupsCache { |
| 113 | entry := &MetricGroupsCache{} |
| 114 | entry.counters = make(map[string]Counter) |
| 115 | entry.gauges = make(map[string]Gauge) |
| 116 | return entry |
| 117 | } |
| 118 | |
| 119 | //----------------------------------------------------------------------------- |
| 120 | // All counters/gauges registered via Metrics instances: |
| 121 | // Counter names are build from: namespace, subsystem, metric and possible labels |
| 122 | //----------------------------------------------------------------------------- |
| 123 | var globalLock sync.Mutex |
| 124 | var cache_allcounters map[string]Counter |
| 125 | var cache_allgauges map[string]Gauge |
| 126 | |
| 127 | func init() { |
| 128 | cache_allcounters = make(map[string]Counter) |
| 129 | cache_allgauges = make(map[string]Gauge) |
Juha Hyttinen | 622bae3 | 2020-08-10 08:20:22 +0300 | [diff] [blame] | 130 | } |
| 131 | |
Juha Hyttinen | 90f6dd4 | 2020-05-08 12:17:05 +0300 | [diff] [blame] | 132 | //----------------------------------------------------------------------------- |
| 133 | // |
| 134 | //----------------------------------------------------------------------------- |
| 135 | type Metrics struct { |
Juha Hyttinen | 5bfa60a | 2020-10-30 10:31:39 +0200 | [diff] [blame^] | 136 | Namespace string |
Juha Hyttinen | 90f6dd4 | 2020-05-08 12:17:05 +0300 | [diff] [blame] | 137 | } |
| 138 | |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 139 | func NewMetrics(url, namespace string, r *mux.Router) *Metrics { |
| 140 | if url == "" { |
| 141 | url = "/ric/v1/metrics" |
| 142 | } |
| 143 | if namespace == "" { |
| 144 | namespace = "ricxapp" |
| 145 | } |
| 146 | |
| 147 | Logger.Info("Serving metrics on: url=%s namespace=%s", url, namespace) |
| 148 | |
| 149 | // Expose 'metrics' endpoint with standard golang metrics used by prometheus |
| 150 | r.Handle(url, promhttp.Handler()) |
| 151 | |
Juha Hyttinen | 5bfa60a | 2020-10-30 10:31:39 +0200 | [diff] [blame^] | 152 | return &Metrics{Namespace: namespace} |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Helpers |
| 157 | */ |
| 158 | func (m *Metrics) getFullName(opts prometheus.Opts, labels []string) string { |
| 159 | labelname := "" |
| 160 | for _, lbl := range labels { |
| 161 | if len(labelname) == 0 { |
| 162 | labelname += lbl |
| 163 | } else { |
| 164 | labelname += "_" + lbl |
| 165 | } |
| 166 | } |
| 167 | return fmt.Sprintf("%s_%s_%s_%s", opts.Namespace, opts.Subsystem, opts.Name, labelname) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 168 | } |
| 169 | |
Juha Hyttinen | f619d03 | 2020-05-07 12:42:26 +0300 | [diff] [blame] | 170 | /* |
| 171 | * Handling counters |
| 172 | */ |
| 173 | func (m *Metrics) registerCounter(opts CounterOpts) Counter { |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 174 | Logger.Info("Register new counter with opts: %v", opts) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 175 | return promauto.NewCounter(prometheus.CounterOpts(opts)) |
| 176 | } |
| 177 | |
| 178 | func (m *Metrics) RegisterCounterGroup(opts []CounterOpts, subsytem string) (c map[string]Counter) { |
Juha Hyttinen | 5bfa60a | 2020-10-30 10:31:39 +0200 | [diff] [blame^] | 179 | globalLock.Lock() |
| 180 | defer globalLock.Unlock() |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 181 | c = make(map[string]Counter) |
| 182 | for _, opt := range opts { |
| 183 | opt.Namespace = m.Namespace |
| 184 | opt.Subsystem = subsytem |
Juha Hyttinen | 5bfa60a | 2020-10-30 10:31:39 +0200 | [diff] [blame^] | 185 | |
| 186 | id := m.getFullName(prometheus.Opts(opt), []string{}) |
| 187 | if _, ok := cache_allcounters[id]; !ok { |
| 188 | cache_allcounters[id] = m.registerCounter(opt) |
| 189 | } |
| 190 | |
| 191 | c[opt.Name] = cache_allcounters[id] |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | return |
| 195 | } |
| 196 | |
Juha Hyttinen | f619d03 | 2020-05-07 12:42:26 +0300 | [diff] [blame] | 197 | /* |
| 198 | * Handling gauges |
| 199 | */ |
| 200 | func (m *Metrics) registerGauge(opts CounterOpts) Gauge { |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 201 | Logger.Info("Register new gauge with opts: %v", opts) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 202 | return promauto.NewGauge(prometheus.GaugeOpts(opts)) |
| 203 | } |
| 204 | |
| 205 | func (m *Metrics) RegisterGaugeGroup(opts []CounterOpts, subsytem string) (c map[string]Gauge) { |
Juha Hyttinen | 5bfa60a | 2020-10-30 10:31:39 +0200 | [diff] [blame^] | 206 | globalLock.Lock() |
| 207 | defer globalLock.Unlock() |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 208 | c = make(map[string]Gauge) |
| 209 | for _, opt := range opts { |
| 210 | opt.Namespace = m.Namespace |
| 211 | opt.Subsystem = subsytem |
Juha Hyttinen | 5bfa60a | 2020-10-30 10:31:39 +0200 | [diff] [blame^] | 212 | |
| 213 | id := m.getFullName(prometheus.Opts(opt), []string{}) |
| 214 | if _, ok := cache_allgauges[id]; !ok { |
| 215 | cache_allgauges[id] = m.registerGauge(opt) |
| 216 | } |
| 217 | |
| 218 | c[opt.Name] = cache_allgauges[id] |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | return |
| 222 | } |
Juha Hyttinen | f619d03 | 2020-05-07 12:42:26 +0300 | [diff] [blame] | 223 | |
| 224 | /* |
| 225 | * Handling counter vectors |
| 226 | * |
| 227 | * Example: |
| 228 | |
| 229 | vec := Metric.RegisterCounterVecGroup( |
| 230 | []CounterOpts{ |
| 231 | {Name: "counter1", Help: "counter1"}, |
| 232 | {Name: "counter2", Help: "counter2"}, |
| 233 | }, |
| 234 | []string{"host"}, |
| 235 | "SUBSYSTEM") |
| 236 | |
| 237 | stat:=Metric.GetCounterGroupFromVects([]string{"localhost:8888"}, vec) |
| 238 | |
| 239 | */ |
| 240 | type CounterVec struct { |
| 241 | Vec *prometheus.CounterVec |
| 242 | Opts CounterOpts |
| 243 | } |
| 244 | |
| 245 | func (m *Metrics) registerCounterVec(opts CounterOpts, labelNames []string) *prometheus.CounterVec { |
| 246 | Logger.Info("Register new counter vector with opts: %v labelNames: %v", opts, labelNames) |
Juha Hyttinen | f619d03 | 2020-05-07 12:42:26 +0300 | [diff] [blame] | 247 | return promauto.NewCounterVec(prometheus.CounterOpts(opts), labelNames) |
| 248 | } |
| 249 | |
| 250 | func (m *Metrics) RegisterCounterVecGroup(opts []CounterOpts, labelNames []string, subsytem string) (c map[string]CounterVec) { |
| 251 | c = make(map[string]CounterVec) |
| 252 | for _, opt := range opts { |
| 253 | entry := CounterVec{} |
| 254 | entry.Opts = opt |
| 255 | entry.Opts.Namespace = m.Namespace |
| 256 | entry.Opts.Subsystem = subsytem |
| 257 | entry.Vec = m.registerCounterVec(entry.Opts, labelNames) |
| 258 | c[opt.Name] = entry |
| 259 | } |
| 260 | return |
| 261 | } |
| 262 | |
Juha Hyttinen | 90f6dd4 | 2020-05-08 12:17:05 +0300 | [diff] [blame] | 263 | func (m *Metrics) GetCounterGroupFromVectsWithPrefix(prefix string, labels []string, vects ...map[string]CounterVec) (c map[string]Counter) { |
Juha Hyttinen | 5bfa60a | 2020-10-30 10:31:39 +0200 | [diff] [blame^] | 264 | globalLock.Lock() |
| 265 | defer globalLock.Unlock() |
Juha Hyttinen | f619d03 | 2020-05-07 12:42:26 +0300 | [diff] [blame] | 266 | c = make(map[string]Counter) |
| 267 | for _, vec := range vects { |
| 268 | for name, opt := range vec { |
Juha Hyttinen | 5bfa60a | 2020-10-30 10:31:39 +0200 | [diff] [blame^] | 269 | |
| 270 | id := m.getFullName(prometheus.Opts(opt.Opts), labels) |
| 271 | if _, ok := cache_allcounters[id]; !ok { |
| 272 | Logger.Info("Register new counter from vector with opts: %v labels: %v prefix: %s", opt.Opts, labels, prefix) |
| 273 | cache_allcounters[id] = opt.Vec.WithLabelValues(labels...) |
| 274 | } |
| 275 | c[prefix+name] = cache_allcounters[id] |
Juha Hyttinen | f619d03 | 2020-05-07 12:42:26 +0300 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | return |
| 279 | } |
| 280 | |
Juha Hyttinen | 90f6dd4 | 2020-05-08 12:17:05 +0300 | [diff] [blame] | 281 | func (m *Metrics) GetCounterGroupFromVects(labels []string, vects ...map[string]CounterVec) (c map[string]Counter) { |
| 282 | return m.GetCounterGroupFromVectsWithPrefix("", labels, vects...) |
| 283 | } |
| 284 | |
Juha Hyttinen | f619d03 | 2020-05-07 12:42:26 +0300 | [diff] [blame] | 285 | /* |
| 286 | * Handling gauge vectors |
| 287 | * |
| 288 | * Example: |
| 289 | |
| 290 | vec := Metric.RegisterGaugeVecGroup( |
| 291 | []CounterOpts{ |
| 292 | {Name: "gauge1", Help: "gauge1"}, |
| 293 | {Name: "gauge2", Help: "gauge2"}, |
| 294 | }, |
| 295 | []string{"host"}, |
| 296 | "SUBSYSTEM") |
| 297 | |
| 298 | stat:=Metric.GetGaugeGroupFromVects([]string{"localhost:8888"},vec) |
| 299 | |
| 300 | */ |
| 301 | type GaugeVec struct { |
| 302 | Vec *prometheus.GaugeVec |
| 303 | Opts CounterOpts |
| 304 | } |
| 305 | |
| 306 | func (m *Metrics) registerGaugeVec(opts CounterOpts, labelNames []string) *prometheus.GaugeVec { |
| 307 | Logger.Info("Register new gauge vector with opts: %v labelNames: %v", opts, labelNames) |
Juha Hyttinen | f619d03 | 2020-05-07 12:42:26 +0300 | [diff] [blame] | 308 | return promauto.NewGaugeVec(prometheus.GaugeOpts(opts), labelNames) |
| 309 | } |
| 310 | |
| 311 | func (m *Metrics) RegisterGaugeVecGroup(opts []CounterOpts, labelNames []string, subsytem string) (c map[string]GaugeVec) { |
| 312 | c = make(map[string]GaugeVec) |
| 313 | for _, opt := range opts { |
| 314 | entry := GaugeVec{} |
| 315 | entry.Opts = opt |
| 316 | entry.Opts.Namespace = m.Namespace |
| 317 | entry.Opts.Subsystem = subsytem |
| 318 | entry.Vec = m.registerGaugeVec(entry.Opts, labelNames) |
| 319 | c[opt.Name] = entry |
| 320 | |
| 321 | } |
| 322 | return |
| 323 | } |
| 324 | |
Juha Hyttinen | 90f6dd4 | 2020-05-08 12:17:05 +0300 | [diff] [blame] | 325 | func (m *Metrics) GetGaugeGroupFromVectsWithPrefix(prefix string, labels []string, vects ...map[string]GaugeVec) (c map[string]Gauge) { |
Juha Hyttinen | 5bfa60a | 2020-10-30 10:31:39 +0200 | [diff] [blame^] | 326 | globalLock.Lock() |
| 327 | defer globalLock.Unlock() |
Juha Hyttinen | f619d03 | 2020-05-07 12:42:26 +0300 | [diff] [blame] | 328 | c = make(map[string]Gauge) |
| 329 | for _, vec := range vects { |
| 330 | for name, opt := range vec { |
Juha Hyttinen | 5bfa60a | 2020-10-30 10:31:39 +0200 | [diff] [blame^] | 331 | |
| 332 | id := m.getFullName(prometheus.Opts(opt.Opts), labels) |
| 333 | if _, ok := cache_allgauges[id]; !ok { |
| 334 | Logger.Info("Register new gauge from vector with opts: %v labels: %v prefix: %s", opt.Opts, labels, prefix) |
| 335 | cache_allgauges[id] = opt.Vec.WithLabelValues(labels...) |
| 336 | } |
| 337 | c[prefix+name] = cache_allgauges[id] |
Juha Hyttinen | f619d03 | 2020-05-07 12:42:26 +0300 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | return |
| 341 | } |
| 342 | |
Juha Hyttinen | 90f6dd4 | 2020-05-08 12:17:05 +0300 | [diff] [blame] | 343 | func (m *Metrics) GetGaugeGroupFromVects(labels []string, vects ...map[string]GaugeVec) (c map[string]Gauge) { |
| 344 | return m.GetGaugeGroupFromVectsWithPrefix("", labels, vects...) |
Juha Hyttinen | 90f6dd4 | 2020-05-08 12:17:05 +0300 | [diff] [blame] | 345 | } |