/
monitoring.go
256 lines (218 loc) · 6.21 KB
/
monitoring.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package uos
import (
"fmt"
"net/http"
"net/http/pprof"
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func setupMonitoring() {
if Config.Monitoring.PortPPROF > 0 {
go func() {
Log.Info("starting PPROF web interface")
pprofMux := http.NewServeMux()
pprofMux.HandleFunc("/debug/pprof/", pprof.Index)
pprofMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
pprofMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
pprofMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
pprofMux.HandleFunc("/debug/pprof/trace", pprof.Trace)
err := http.ListenAndServe(fmt.Sprintf(":%d", Config.Monitoring.PortPPROF), pprofMux)
if err != nil {
Log.ErrorObj("profiling web interface stopped", err)
}
}()
}
if Config.Monitoring.PortMetrics > 0 {
// initialize metrics registry
Metrics = newMetricsRegistry()
go func() {
Log.Info("starting metrics server")
metricsMux := http.NewServeMux()
metricsMux.Handle("/metrics", promhttp.Handler())
err := http.ListenAndServe(fmt.Sprintf(":%d", Config.Monitoring.PortMetrics), metricsMux)
if err != nil {
Log.ErrorObj("profiling web interface stopped", err)
}
}()
}
}
type metricsRegistry struct {
// used to insert NEW metrics - the prometheus.* objects are already thread-safe
mutex sync.Mutex
metrics map[string]int
counter map[int]prometheus.Counter
gauge map[int]prometheus.Gauge
}
// metric IDs - will be assigned during registration
var (
mStartupTime int
mRequestCount int
mRequestDuration int
mRequestActive int
mRequestFailed int
mRequestSlow int
mLogMessage int
mLogMessageWarning int
mLogMessageError int
mLogMessagePanic int
)
func newMetricsRegistry() *metricsRegistry {
registry := metricsRegistry{
metrics: map[string]int{},
counter: map[int]prometheus.Counter{},
gauge: map[int]prometheus.Gauge{},
}
// register standard metrics
mStartupTime = registry.RegisterGauge(
"app_start_timestamp_seconds",
"Timestamp of application start.",
)
registry.GaugeCurrentTime(mStartupTime)
mRequestCount = registry.RegisterCounter(
"app_http_requests_count",
"Total number of received HTTP requests.",
)
mRequestDuration = registry.RegisterCounter(
"app_http_requests_duration_ms",
"Total time spent request processing (status code < 500).",
)
mRequestFailed = registry.RegisterCounter(
"app_http_requests_failed_count",
"Total number of failed HTTP requests (status code >= 500).",
)
mRequestSlow = registry.RegisterCounter(
"app_http_requests_slow_count",
"Total number of slow HTTP requests (duration >= 2s).",
)
mRequestActive = registry.RegisterGauge(
"app_http_requests_active_count",
"Current number of active HTTP request.",
)
mLogMessage = registry.RegisterCounter(
"app_log_messages_count",
"Number of recorded log messages (level INFO or higher).",
)
mLogMessageWarning = registry.RegisterCounter(
"app_log_messages_warning_count",
"Number of recorded WARNING log messages.",
)
mLogMessageError = registry.RegisterCounter(
"app_log_messages_error_count",
"Number of recorded ERROR log messages.",
)
mLogMessagePanic = registry.RegisterCounter(
"app_log_messages_panic_count",
"Number of recorded PANIC/FATAL log messages.",
)
return ®istry
}
// Metrics allows to publish application metrics
var Metrics *metricsRegistry
func (m *metricsRegistry) assignMetricID(name string) int {
m.mutex.Lock()
defer m.mutex.Unlock()
if _, ok := m.metrics[name]; !ok {
// assign new ID
m.metrics[name] = len(m.metrics) + 1
return m.metrics[name]
}
// metric name already defined
return 0
}
// RegisterCounter creates a new counter with the given name. Does nothing if the name is already registered.
// Prepends 'app_' and appends '_count' to the name. Returns metric ID - used for actual metric operation.
func (m *metricsRegistry) RegisterCounter(name, help string) int {
metricID := m.assignMetricID(name)
if metricID == 0 {
// already registered
return 0
}
// create
m.counter[metricID] = promauto.NewCounter(prometheus.CounterOpts{
Name: name,
Help: help,
})
return metricID
}
// CounterInc increases the counter value.
func (m *metricsRegistry) CounterInc(id int) {
if m == nil {
return
}
if c, ok := m.counter[id]; ok {
c.Inc()
}
}
// CounterIncValue increases the counter value by the specified value. Does nothin if value <= 0.
func (m *metricsRegistry) CounterIncValue(id int, value int64) {
if m == nil || value <= 0 {
return
}
if c, ok := m.counter[id]; ok {
c.Inc()
}
}
// CounterIncCondition increases the counter value if the specified condition is 'true'.
func (m *metricsRegistry) CounterIncCondition(id int, condition bool) {
if m == nil || !condition {
return
}
if c, ok := m.counter[id]; ok {
c.Inc()
}
}
// CounterIncValueCondition increases the counter value by the specified value if the condition is 'true'.
// Does nothin if value <= 0.
func (m *metricsRegistry) CounterIncValueCondition(id int, value int64, condition bool) {
if m == nil || value <= 0 || !condition {
return
}
if c, ok := m.counter[id]; ok {
c.Add(float64(value))
}
}
// RegisterGauge creates a new gauge with the given name. Does nothing if the name is already registered.
// Prepends 'app_' to the name. The name should end with the unit of the value (e.g. '_seconds').
// Returns metric ID - used for actual metric operation.
func (m *metricsRegistry) RegisterGauge(name, help string) int {
metricID := m.assignMetricID(name)
if metricID == 0 {
// already registered
return 0
}
// create
m.gauge[metricID] = promauto.NewGauge(prometheus.GaugeOpts{
Name: name,
Help: help,
})
return metricID
}
// GaugeCurrentTime sets the specified gauge to the current time.
func (m *metricsRegistry) GaugeCurrentTime(id int) {
if m == nil {
return
}
if g, ok := m.gauge[id]; ok {
g.SetToCurrentTime()
}
}
// GaugeInc increases the gauge value.
func (m *metricsRegistry) GaugeInc(id int) {
if m == nil {
return
}
if g, ok := m.gauge[id]; ok {
g.Inc()
}
}
// GaugeDec decreases the gauge value.
func (m *metricsRegistry) GaugeDec(id int) {
if m == nil {
return
}
if g, ok := m.gauge[id]; ok {
g.Dec()
}
}