e31fdb7b07ab55078f78fc0ce938488add8746e7
[ric-plt/xapp-frame.git] / pkg / xapp / metrics.go
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 (
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"
27 )
28
29 type Metrics struct {
30         Namespace string
31 }
32
33 // Alias
34 type CounterOpts prometheus.CounterOpts
35 type Counter prometheus.Counter
36 type Gauge prometheus.Gauge
37
38 func NewMetrics(url, namespace string, r *mux.Router) *Metrics {
39         if url == "" {
40                 url = "/ric/v1/metrics"
41         }
42         if namespace == "" {
43                 namespace = "ricxapp"
44         }
45
46         Logger.Info("Serving metrics on: url=%s namespace=%s", url, namespace)
47
48         // Expose 'metrics' endpoint with standard golang metrics used by prometheus
49         r.Handle(url, promhttp.Handler())
50
51         return &Metrics{Namespace: namespace}
52 }
53
54 func (m *Metrics) RegisterCounter(opts CounterOpts) Counter {
55         Logger.Info("Register new counter with opts: %v", opts)
56
57         return promauto.NewCounter(prometheus.CounterOpts(opts))
58 }
59
60 func (m *Metrics) RegisterCounterGroup(opts []CounterOpts, subsytem string) (c map[string]Counter) {
61         c = make(map[string]Counter)
62         for _, opt := range opts {
63                 opt.Namespace = m.Namespace
64                 opt.Subsystem = subsytem
65                 c[opt.Name] = m.RegisterCounter(opt)
66         }
67
68         return
69 }
70
71 func (m *Metrics) RegisterGauge(opts CounterOpts) Gauge {
72         Logger.Info("Register new gauge with opts: %v", opts)
73
74         return promauto.NewGauge(prometheus.GaugeOpts(opts))
75 }
76
77 func (m *Metrics) RegisterGaugeGroup(opts []CounterOpts, subsytem string) (c map[string]Gauge) {
78         c = make(map[string]Gauge)
79         for _, opt := range opts {
80                 opt.Namespace = m.Namespace
81                 opt.Subsystem = subsytem
82                 c[opt.Name] = m.RegisterGauge(opt)
83         }
84
85         return
86 }