-
Notifications
You must be signed in to change notification settings - Fork 2
/
prometheus.go
195 lines (169 loc) · 4.85 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
"runtime"
"time"
)
type gcStatsCollector struct {
pauseTotalDesc *prometheus.Desc
numGCDesc *prometheus.Desc
}
func newGCStatsCollector() *gcStatsCollector {
return &gcStatsCollector{
pauseTotalDesc: prometheus.NewDesc(
"allas_gc_pause_seconds_total",
"how much total time has been spent in garbage collector pauses",
nil,
nil,
),
numGCDesc: prometheus.NewDesc(
"allas_gc_pauses_total",
"how many times the garbage collector has run",
nil,
nil,
),
}
}
func (c *gcStatsCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.pauseTotalDesc
ch <- c.numGCDesc
}
func (c *gcStatsCollector) Collect(ch chan<- prometheus.Metric) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
ch <- prometheus.MustNewConstMetric(c.pauseTotalDesc, prometheus.CounterValue, float64(m.PauseTotalNs) / 1000000000.0)
ch <- prometheus.MustNewConstMetric(c.numGCDesc, prometheus.CounterValue, float64(m.NumGC))
}
type PrometheusConfig struct {
Enabled bool
Listen ListenConfig
registry *prometheus.Registry
startupTimeDesc *prometheus.Desc
startupTimeMetric prometheus.Metric
gcStatsCollector *gcStatsCollector
}
func (c *PrometheusConfig) RegisterMetricsCollector(coll prometheus.Collector) error {
if c.registry == nil {
panic("registry not initialized")
}
return c.registry.Register(coll)
}
type elogWrapper struct {
}
func (w elogWrapper) Println(v ...interface{}) {
elog.Warningf("Prometheus handler error: %s", fmt.Sprintln(v...))
}
var MetricClientConnections prometheus.Gauge
var MetricNotificationsReceived prometheus.Counter
var MetricNotificationsDispatched prometheus.Counter
var MetricListensExecuted prometheus.Counter
var MetricUnlistensExecuted prometheus.Counter
var MetricSlowClientsTerminated prometheus.Counter
func (cfg *PrometheusConfig) InitializeMetrics(r *prometheus.Registry) error {
var err error
cfg.startupTimeDesc = prometheus.NewDesc(
"allas_start_time",
"when this instance of allas was started",
nil,
nil,
)
cfg.startupTimeMetric = prometheus.MustNewConstMetric(cfg.startupTimeDesc, prometheus.GaugeValue, float64(time.Now().Unix()))
err = r.Register(cfg)
if err != nil {
return err
}
MetricClientConnections = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "allas",
Name: "client_connections",
Help: "the number of clients currently connected to allas",
})
err = r.Register(MetricClientConnections)
if err != nil {
return err
}
MetricNotificationsReceived = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "allas",
Name: "notifications_received_total",
Help: "how many notifications have been received from the PostgreSQL server so far",
})
err = r.Register(MetricNotificationsReceived)
if err != nil {
return err
}
MetricNotificationsDispatched = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "allas",
Name: "notifications_dispatched_total",
Help: "how many notifications have been dispatched so far",
})
err = r.Register(MetricNotificationsDispatched)
if err != nil {
return err
}
MetricListensExecuted = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "allas",
Name: "listens_executed_total",
Help: "how many LISTEN queries have been executed so far",
})
err = r.Register(MetricListensExecuted)
if err != nil {
return err
}
MetricUnlistensExecuted = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "allas",
Name: "unlistens_executed_total",
Help: "how many UNLISTEN queries have been executed so far",
})
err = r.Register(MetricUnlistensExecuted)
if err != nil {
return err
}
MetricSlowClientsTerminated = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "allas",
Name: "slow_clients_terminated_total",
Help: "how many clients have been terminated because they could not keep up",
})
err = r.Register(MetricSlowClientsTerminated)
if err != nil {
return err
}
cfg.gcStatsCollector = newGCStatsCollector()
err = r.Register(cfg.gcStatsCollector)
if err != nil {
return err
}
return nil
}
func (cfg *PrometheusConfig) Describe(ch chan<- *prometheus.Desc) {
ch <- cfg.startupTimeDesc
}
func (cfg *PrometheusConfig) Collect(ch chan<- prometheus.Metric) {
ch <- cfg.startupTimeMetric
}
func (cfg *PrometheusConfig) Setup() error {
elogWrapper := elogWrapper{}
registry := prometheus.NewPedanticRegistry()
err := cfg.InitializeMetrics(registry)
if err != nil {
return err
}
cfg.registry = registry
metricsHandler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{
ErrorLog: elogWrapper,
})
muxer := http.NewServeMux()
muxer.Handle("/metrics", metricsHandler)
s := &http.Server{
Handler: muxer,
}
l, err := cfg.Listen.Listen()
if err != nil {
return err
}
go func() {
elog.Fatalf("Prometheus HTTP endpoint failed: %s", s.Serve(l))
}()
return nil
}