-
Notifications
You must be signed in to change notification settings - Fork 6
/
prometheus.go
167 lines (143 loc) · 3.63 KB
/
prometheus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package prometheus
import (
"log"
"net/http"
"sync"
"github.com/dipdup-net/go-lib/config"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// Service -
type Service struct {
counters map[string]*prometheus.CounterVec
histograms map[string]*prometheus.HistogramVec
gauge map[string]*prometheus.GaugeVec
server *http.Server
wg sync.WaitGroup
}
// NewService -
func NewService(cfg *config.Prometheus) *Service {
var s Service
s.counters = make(map[string]*prometheus.CounterVec)
s.histograms = make(map[string]*prometheus.HistogramVec)
s.gauge = make(map[string]*prometheus.GaugeVec)
if cfg != nil && cfg.URL != "" {
s.server = &http.Server{Addr: cfg.URL}
http.Handle("/metrics", promhttp.Handler())
}
return &s
}
// Start -
func (service *Service) Start() {
if service.server == nil {
return
}
service.wg.Add(1)
go func() {
defer service.wg.Done()
if err := service.server.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalf("ListenAndServe(): %v", err)
}
}()
}
// Close -
func (service *Service) Close() error {
if service.server != nil {
if err := service.server.Close(); err != nil {
return err
}
}
service.wg.Wait()
return nil
}
// RegisterCounter -
func (service *Service) RegisterCounter(name, help string, labels ...string) {
vec := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: name,
Help: help,
}, labels)
service.counters[name] = vec
prometheus.MustRegister(vec)
}
// Counter -
func (service *Service) Counter(name string) *prometheus.CounterVec {
counter, ok := service.counters[name]
if ok {
return counter
}
return nil
}
// IncrementCounter -
func (service *Service) IncrementCounter(name string, labels map[string]string) {
counter, ok := service.counters[name]
if ok {
counter.With(labels).Inc()
}
}
// RegisterGoBuildMetrics -
func (service *Service) RegisterGoBuildMetrics() {
prometheus.MustRegister(collectors.NewBuildInfoCollector())
}
// RegisterHistogram -
func (service *Service) RegisterHistogram(name, help string, labels ...string) {
vec := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: name,
Help: help,
}, labels)
service.histograms[name] = vec
prometheus.MustRegister(vec)
}
// Histogram -
func (service *Service) Histogram(name string) *prometheus.HistogramVec {
histogram, ok := service.histograms[name]
if ok {
return histogram
}
return nil
}
// AddHistogramValue -
func (service *Service) AddHistogramValue(name string, labels map[string]string, observe float64) {
histogram, ok := service.histograms[name]
if ok {
histogram.With(labels).Observe(observe)
}
}
// RegisterGauge -
func (service *Service) RegisterGauge(name, help string, labels ...string) {
vec := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: name,
Help: help,
}, labels)
service.gauge[name] = vec
prometheus.MustRegister(vec)
}
// Gauge -
func (service *Service) Gauge(name string) *prometheus.GaugeVec {
gauge, ok := service.gauge[name]
if ok {
return gauge
}
return nil
}
// SetGaugeValue -
func (service *Service) SetGaugeValue(name string, labels map[string]string, observe float64) {
gauge, ok := service.gauge[name]
if ok {
gauge.With(labels).Set(observe)
}
}
// IncGaugeValue -
func (service *Service) IncGaugeValue(name string, labels map[string]string) {
gauge, ok := service.gauge[name]
if ok {
gauge.With(labels).Inc()
}
}
// DecGaugeValue -
func (service *Service) DecGaugeValue(name string, labels map[string]string) {
gauge, ok := service.gauge[name]
if ok {
gauge.With(labels).Dec()
}
}