-
Notifications
You must be signed in to change notification settings - Fork 2
/
sentinel_metrics.go
252 lines (211 loc) · 7.92 KB
/
sentinel_metrics.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
package metrics
import (
"context"
"fmt"
"time"
"github.com/3scale-ops/saas-operator/pkg/reconcilers/threads"
"github.com/3scale-ops/saas-operator/pkg/redis/client"
redis "github.com/3scale-ops/saas-operator/pkg/redis/server"
"github.com/3scale-ops/saas-operator/pkg/redis/sharded"
"github.com/go-logr/logr"
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)
var (
linkPendingCommands = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "link_pending_commands",
Namespace: "saas_redis_sentinel",
Help: `"sentinel master <name> link-pending-commands"`,
},
[]string{"sentinel", "shard", "redis_server", "role"},
)
lastOkPingReply = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "last_ok_ping_reply",
Namespace: "saas_redis_sentinel",
Help: `"sentinel master <name> last-ok-ping-reply"`,
},
[]string{"sentinel", "shard", "redis_server", "role"},
)
roleReportedTime = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "role_reported_time",
Namespace: "saas_redis_sentinel",
Help: `"sentinel master <name> role-reported-time"`,
},
[]string{"sentinel", "shard", "redis_server", "role"},
)
numOtherSentinels = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "num_other_sentinels",
Namespace: "saas_redis_sentinel",
Help: `"sentinel master <name> num-other-sentinels"`,
},
[]string{"sentinel", "shard", "redis_server", "role"},
)
masterLinkDownTime = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "master_link_down_time",
Namespace: "saas_redis_sentinel",
Help: `"sentinel slaves master-link-down-time"`,
},
[]string{"sentinel", "shard", "redis_server", "role"},
)
slaveReplOffset = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "slave_repl_offset",
Namespace: "saas_redis_sentinel",
Help: `"sentinel slaves slave-repl-offset"`,
},
[]string{"sentinel", "shard", "redis_server", "role"},
)
)
func init() {
// Register custom metrics with the global prometheus registry
metrics.Registry.MustRegister(
linkPendingCommands, lastOkPingReply, roleReportedTime,
numOtherSentinels, masterLinkDownTime, slaveReplOffset,
)
}
// SentinelEventWatcher implements RunnableThread
var _ threads.RunnableThread = &SentinelMetricsGatherer{}
// SentinelMetricsGatherer is used to export sentinel metrics, obtained
// thrugh several admin commands, as prometheus metrics
type SentinelMetricsGatherer struct {
refreshInterval time.Duration
sentinelURI string
sentinel *sharded.SentinelServer
started bool
cancel context.CancelFunc
}
func NewSentinelMetricsGatherer(sentinelURI string, refreshInterval time.Duration, pool *redis.ServerPool) (*SentinelMetricsGatherer, error) {
sentinel, err := sharded.NewSentinelServerFromPool(sentinelURI, nil, pool)
if err != nil {
return nil, err
}
return &SentinelMetricsGatherer{
refreshInterval: refreshInterval,
sentinelURI: sentinelURI,
sentinel: sentinel,
}, nil
}
func (fw *SentinelMetricsGatherer) GetID() string {
return fw.sentinelURI
}
// IsStarted returns whether the metrics gatherer is running or not
func (smg *SentinelMetricsGatherer) IsStarted() bool {
return smg.started
}
func (smg *SentinelMetricsGatherer) CanBeDeleted() bool {
return true
}
// SetChannel is required for SentinelMetricsGatherer to implement the RunnableThread
// interface, but it actually does nothing with the channel.
func (fw *SentinelMetricsGatherer) SetChannel(ch chan event.GenericEvent) {}
// Start starts metrics gatherer for sentinel
func (smg *SentinelMetricsGatherer) Start(parentCtx context.Context, l logr.Logger) error {
log := l.WithValues("sentinel", smg.sentinelURI)
if smg.started {
log.Info("the metrics gatherer is already running")
return nil
}
go func() {
var ctx context.Context
ctx, smg.cancel = context.WithCancel(parentCtx)
ticker := time.NewTicker(smg.refreshInterval)
log.Info("sentinel metrics gatherer running")
for {
select {
case <-ticker.C:
if err := smg.gatherMetrics(ctx); err != nil {
log.Error(err, "error gathering sentinel metrics")
}
case <-ctx.Done():
log.Info("shutting down sentinel metrics gatherer")
smg.started = false
return
}
}
}()
smg.started = true
return nil
}
// Stop stops metrics gatherering for sentinel
func (smg *SentinelMetricsGatherer) Stop() {
// stop gathering metrics
smg.cancel()
// Reset all gauge metrics so the values related to
// this exporter are deleted from the collection
linkPendingCommands.Reset()
lastOkPingReply.Reset()
roleReportedTime.Reset()
numOtherSentinels.Reset()
masterLinkDownTime.Reset()
slaveReplOffset.Reset()
}
func (smg *SentinelMetricsGatherer) gatherMetrics(ctx context.Context) error {
mresult, err := smg.sentinel.SentinelMasters(ctx)
if err != nil {
return err
}
for _, master := range mresult {
linkPendingCommands.With(prometheus.Labels{"sentinel": smg.sentinelURI, "shard": master.Name,
"redis_server": fmt.Sprintf("%s:%d", master.IP, master.Port), "role": master.RoleReported,
}).Set(float64(master.LinkPendingCommands))
lastOkPingReply.With(prometheus.Labels{"sentinel": smg.sentinelURI, "shard": master.Name,
"redis_server": fmt.Sprintf("%s:%d", master.IP, master.Port), "role": master.RoleReported,
}).Set(float64(master.LastOkPingReply))
roleReportedTime.With(prometheus.Labels{"sentinel": smg.sentinelURI, "shard": master.Name,
"redis_server": fmt.Sprintf("%s:%d", master.IP, master.Port), "role": master.RoleReported,
}).Set(float64(master.RoleReportedTime))
numOtherSentinels.With(prometheus.Labels{"sentinel": smg.sentinelURI, "shard": master.Name,
"redis_server": fmt.Sprintf("%s:%d", master.IP, master.Port), "role": master.RoleReported,
}).Set(float64(master.NumOtherSentinels))
sresult, err := smg.sentinel.SentinelSlaves(ctx, master.Name)
if err != nil {
return err
}
// Cleanup any vector that corresponds to the same server but with a
// different role to avoid stale metrics after a role switch
cleanupMetrics(prometheus.Labels{
"sentinel": smg.sentinelURI,
"shard": master.Name,
"redis_server": fmt.Sprintf("%s:%d", master.IP, master.Port),
"role": string(client.Slave),
})
for _, slave := range sresult {
linkPendingCommands.With(prometheus.Labels{"sentinel": smg.sentinelURI, "shard": master.Name,
"redis_server": fmt.Sprintf("%s:%d", slave.IP, slave.Port), "role": slave.RoleReported,
}).Set(float64(slave.LinkPendingCommands))
lastOkPingReply.With(prometheus.Labels{"sentinel": smg.sentinelURI, "shard": master.Name,
"redis_server": fmt.Sprintf("%s:%d", slave.IP, slave.Port), "role": slave.RoleReported,
}).Set(float64(slave.LastOkPingReply))
roleReportedTime.With(prometheus.Labels{"sentinel": smg.sentinelURI, "shard": master.Name,
"redis_server": fmt.Sprintf("%s:%d", slave.IP, slave.Port), "role": slave.RoleReported,
}).Set(float64(slave.RoleReportedTime))
masterLinkDownTime.With(prometheus.Labels{"sentinel": smg.sentinelURI, "shard": master.Name,
"redis_server": fmt.Sprintf("%s:%d", slave.IP, slave.Port), "role": slave.RoleReported,
}).Set(float64(slave.MasterLinkDownTime))
slaveReplOffset.With(prometheus.Labels{"sentinel": smg.sentinelURI, "shard": master.Name,
"redis_server": fmt.Sprintf("%s:%d", slave.IP, slave.Port), "role": slave.RoleReported,
}).Set(float64(slave.SlaveReplOffset))
cleanupMetrics(prometheus.Labels{
"sentinel": smg.sentinelURI,
"shard": master.Name,
"redis_server": fmt.Sprintf("%s:%d", slave.IP, slave.Port),
"role": string(client.Master),
})
}
}
return nil
}
func cleanupMetrics(labels prometheus.Labels) {
linkPendingCommands.Delete(labels)
lastOkPingReply.Delete(labels)
roleReportedTime.Delete(labels)
numOtherSentinels.Delete(labels)
masterLinkDownTime.Delete(labels)
slaveReplOffset.Delete(labels)
}