-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
87 lines (72 loc) · 1.71 KB
/
collector.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
package promgo
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/go-redis/redis/v8"
)
// Collector ...
type Collector interface {
// Collect 收集指标值
Collect(ch chan<- *MetricErr)
// Describe 指标描述
Describe() *Desc
}
// CollectorOptions 参数管理
type CollectorOptions struct {
Namespace string
Name string
Help string
Labels []string
}
const (
// CollectorPrefix ...
CollectorPrefix = `prometheus`
// FieldSeperator 域分隔符
FieldSeperator = `__`
)
type redisCollector struct {
Collector
Rdb redis.Cmdable
Desc *Desc
}
func (rc redisCollector) Describe() *Desc {
return rc.Desc
}
func (rc redisCollector) Collect(ch chan<- *MetricErr) {
values, err := rc.Rdb.HGetAll(context.Background(), rc.key()).Result()
if err != nil {
ch <- NewMetricErr(nil, err)
return
}
for field, value := range values {
v, _ := strconv.ParseFloat(value, 64)
constlabels := rc.constLabels(field)
metric := NewMetric(rc.Desc, v, constlabels)
ch <- NewMetricErr(metric, nil)
}
}
// 在缓存中存储的key值
func (rc redisCollector) key() string {
return fmt.Sprintf(`%s:%s:%s`, CollectorPrefix, rc.Desc.GetType(), rc.Desc.ID())
}
// Hash类型中的field值
func (rc redisCollector) field(constLabels ConstLabels) string {
vv := make([]string, 0, len(rc.Desc.Labels))
for _, l := range rc.Desc.Labels {
if v, ok := constLabels[l]; ok {
vv = append(vv, v)
}
}
return strings.Join(vv, FieldSeperator)
}
// 解析Hash类型中的field值,并与指标label进行映射
func (rc redisCollector) constLabels(field string) ConstLabels {
vv := strings.Split(field, FieldSeperator)
cl := make(ConstLabels)
for i, l := range rc.Desc.Labels {
cl[l] = vv[i]
}
return cl
}