-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance.go
59 lines (49 loc) · 1.27 KB
/
instance.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
package exporter
import (
"context"
"mobserver/internal/metric"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
type instanceCollector struct {
ctx context.Context
base *baseCollector
}
func newInstanceCollector(client *mongo.Client, logger *logrus.Logger) prometheus.Collector {
return &instanceCollector{
ctx: context.Background(),
base: newBaseCollector(client, logger),
}
}
func (c *instanceCollector) Describe(ch chan<- *prometheus.Desc) {
c.base.Describe(ch, c.collect)
}
func (c *instanceCollector) Collect(ch chan<- prometheus.Metric) {
c.base.Collect(ch)
}
func (c *instanceCollector) collect(ch chan<- prometheus.Metric) {
doc := struct {
Version string `bson:"version"`
}{}
cmd := bson.D{{Key: "buildinfo", Value: 1}}
err := c.base.client.Database("admin").RunCommand(context.TODO(), cmd).Decode(&doc)
if err != nil {
return
}
v := strings.Split(doc.Version, ".")
majorVersion, err := strconv.ParseFloat(v[0]+"."+v[1], 64)
if err != nil {
logrus.Errorf("Failed to parse major version: %v", err)
return
}
ist := &metric.Instance{
Version: majorVersion,
}
for _, m := range ist.ToPromMetrics() {
ch <- m
}
}