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.CounterOpts
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}
54 func (m *Metrics) RegisterCounter(opts CounterOpts) Counter {
55 Logger.Info("Register new counter with opts: %v", opts)
57 return promauto.NewCounter(prometheus.CounterOpts(opts))
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)
71 func (m *Metrics) RegisterGauge(opts CounterOpts) Gauge {
72 Logger.Info("Register new gauge with opts: %v", opts)
74 return promauto.NewGauge(prometheus.GaugeOpts(opts))
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)