-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.go
73 lines (61 loc) · 1.68 KB
/
repl.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
package exporter
import (
"context"
"mobserver/internal/metric"
"mobserver/internal/model"
"mobserver/internal/mongoutils"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/mongo"
)
type replicationStatusCollector struct {
ctx context.Context
base *baseCollector
isMongos bool
}
func newReplicationStatusCollector(client *mongo.Client, logger *logrus.Logger, isMongos bool) prometheus.Collector {
return &replicationStatusCollector{
ctx: context.Background(),
base: newBaseCollector(client, logger),
isMongos: isMongos,
}
}
func (c *replicationStatusCollector) Describe(ch chan<- *prometheus.Desc) {
c.base.Describe(ch, c.collect)
}
func (c *replicationStatusCollector) Collect(ch chan<- prometheus.Metric) {
c.base.Collect(ch)
}
func (c *replicationStatusCollector) collect(ch chan<- prometheus.Metric) {
if c.isMongos {
// mongos
for _, mt := range metric.NewRoleMetrics(model.MongoReplRoleType(-1)) {
ch <- mt
}
return
}
replStatus, err := mongoutils.GetReplStatus(c.ctx, c.base.client)
if err != nil {
c.base.logger.Errorf("Failed to get replication status: %v", err)
return
}
for _, mt := range metric.NewRoleMetrics(replStatus.MyState) {
ch <- mt
}
replConfig, err := mongoutils.GetReplConfig(c.ctx, c.base.client)
if err != nil {
c.base.logger.Errorf("Failed to get replication config: %v", err)
return
}
if replStatus == nil || replConfig == nil {
return
}
rsMt := metric.NewReplStatus(replStatus, replConfig)
if rsMt == nil {
c.base.logger.Errorf("Failed to find self member in replication status")
return
}
for _, mt := range rsMt.ToPromMetrics() {
ch <- mt
}
}