github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/internal/metrics/promtheus.go (about)

     1  package metrics
     2  
     3  import (
     4  	"github.com/prometheus/client_golang/prometheus"
     5  )
     6  
     7  /*
     8     Creation Time: 2019 - Dec - 06
     9     Created by:  (ehsan)
    10     Maintainers:
    11        1.  Ehsan N. Moosa (E2)
    12     Auditor: Ehsan N. Moosa (E2)
    13     Copyright Ronak Software Group 2018
    14  */
    15  
    16  type Prometheus struct {
    17  	ns             string
    18  	labels         map[string]string
    19  	counters       map[string]prometheus.Counter
    20  	counterVectors map[string]*prometheus.CounterVec
    21  	gauges         map[string]prometheus.Gauge
    22  	gaugeVectors   map[string]*prometheus.GaugeVec
    23  	histograms     map[string]prometheus.Histogram
    24  }
    25  
    26  func NewPrometheus(ns string, constLabels map[string]string) *Prometheus {
    27  	m := &Prometheus{
    28  		ns:             ns,
    29  		counters:       make(map[string]prometheus.Counter),
    30  		counterVectors: make(map[string]*prometheus.CounterVec),
    31  		gauges:         make(map[string]prometheus.Gauge),
    32  		gaugeVectors:   make(map[string]*prometheus.GaugeVec),
    33  		histograms:     make(map[string]prometheus.Histogram),
    34  		labels:         constLabels,
    35  	}
    36  
    37  	return m
    38  }
    39  
    40  func (p *Prometheus) SetConstLabels(m map[string]string) {
    41  	appendMap(&p.labels, &m)
    42  }
    43  
    44  func (p *Prometheus) Counter(name string) prometheus.Counter {
    45  	return p.counters[name]
    46  }
    47  
    48  func (p *Prometheus) CounterVec(name string) *prometheus.CounterVec {
    49  	return p.counterVectors[name]
    50  }
    51  
    52  func (p *Prometheus) CounterVecWithLabelValues(name string, labelValues ...string) prometheus.Counter {
    53  	return p.counterVectors[name].WithLabelValues(labelValues...)
    54  }
    55  
    56  func (p *Prometheus) CounterVecWithLabels(name string, labels prometheus.Labels) prometheus.Counter {
    57  	return p.counterVectors[name].With(labels)
    58  }
    59  
    60  func (p *Prometheus) RegisterCounter(name, help string, labels map[string]string) {
    61  	appendMap(&labels, &p.labels)
    62  	p.counters[name] = prometheus.NewCounter(prometheus.CounterOpts{
    63  		Namespace:   p.ns,
    64  		Name:        name,
    65  		Help:        help,
    66  		ConstLabels: labels,
    67  	})
    68  }
    69  
    70  func (p *Prometheus) RegisterCounterVec(name, help string, constLabels map[string]string, varLabels []string) {
    71  	appendMap(&constLabels, &p.labels)
    72  	p.counterVectors[name] = prometheus.NewCounterVec(prometheus.CounterOpts{
    73  		Namespace:   p.ns,
    74  		Name:        name,
    75  		Help:        help,
    76  		ConstLabels: constLabels,
    77  	}, varLabels)
    78  }
    79  
    80  func (p *Prometheus) Gauge(name string) prometheus.Gauge {
    81  	return p.gauges[name]
    82  }
    83  
    84  func (p *Prometheus) GaugeVec(name string) *prometheus.GaugeVec {
    85  	return p.gaugeVectors[name]
    86  }
    87  
    88  func (p *Prometheus) RegisterGauge(name, help string, labels map[string]string) {
    89  	appendMap(&labels, &p.labels)
    90  	p.gauges[name] = prometheus.NewGauge(prometheus.GaugeOpts{
    91  		Namespace:   p.ns,
    92  		Name:        name,
    93  		Help:        help,
    94  		ConstLabels: labels,
    95  	})
    96  }
    97  
    98  func (p *Prometheus) RegisterGaugeVec(name, help string, constLabels map[string]string, varLabels []string) {
    99  	appendMap(&constLabels, &p.labels)
   100  	p.gaugeVectors[name] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
   101  		Namespace:   p.ns,
   102  		Name:        name,
   103  		Help:        help,
   104  		ConstLabels: constLabels,
   105  	}, varLabels)
   106  }
   107  
   108  func (p *Prometheus) Histogram(name string) prometheus.Histogram {
   109  	return p.histograms[name]
   110  }
   111  
   112  func (p *Prometheus) RegisterHistogram(name, help string, buckets []float64, labels map[string]string) {
   113  	appendMap(&labels, &p.labels)
   114  	p.histograms[name] = prometheus.NewHistogram(prometheus.HistogramOpts{
   115  		Namespace:   p.ns,
   116  		Name:        name,
   117  		Help:        help,
   118  		ConstLabels: labels,
   119  		Buckets:     buckets,
   120  	})
   121  }
   122  
   123  func appendMap(m1, m2 *map[string]string) {
   124  	if *m1 == nil {
   125  		*m1 = make(map[string]string)
   126  	}
   127  	for k, v := range *m2 {
   128  		(*m1)[k] = v
   129  	}
   130  }