-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics_redis.go
172 lines (154 loc) · 4.31 KB
/
metrics_redis.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
package metrics
import (
"errors"
"fmt"
"github.com/go-autumn/lib-metrics/pure"
"github.com/go-autumn/lib-metrics/util"
"github.com/go-redis/redis"
"gopkg.in/robfig/cron.v2"
"strconv"
"strings"
"sync"
)
const (
DefaultMetricRedisKey = "a:p:rm:%s:%s:%s"
DefaultMetricsValueKey = "m_value"
)
type AutumnRedisMetric struct {
Name string
Type pure.MetricType
subsystem string
localIp string
redisKey string
metricValueRedisKey string
Labels []string
locker *sync.Mutex
client *redis.Client
cronEntryID cron.EntryID
}
var _ pure.AutumnMetric = (*AutumnRedisMetric)(nil)
func (ac *AutumnMetricsCollector) NewAutumnRedisMetric(client *redis.Client, name string, labels ...string) *AutumnRedisMetric {
metric := &AutumnRedisMetric{
client: client,
locker: new(sync.Mutex),
Name: name,
Type: pure.GaugeVec,
Labels: labels,
subsystem: ac.subsystem,
metricValueRedisKey: DefaultMetricsValueKey,
}
ips := util.IntranetIP()
metric.localIp = ips[0]
metric.redisKey = fmt.Sprintf(DefaultMetricRedisKey, ac.subsystem, ips[0], name)
return metric
}
func (arm *AutumnRedisMetric) ResetMetricRedisKey(redisKey string) *AutumnRedisMetric {
arm.redisKey = redisKey
return arm
}
func (arm *AutumnRedisMetric) ResetMetricValueRedisKey(valueKey string) *AutumnRedisMetric {
arm.metricValueRedisKey = valueKey
return arm
}
func (arm *AutumnRedisMetric) GetRedisKey() string {
return arm.redisKey
}
func (arm *AutumnRedisMetric) GetMetricValueKey() string {
return arm.metricValueRedisKey
}
func (arm *AutumnRedisMetric) GetName() string {
return arm.Name
}
func (arm *AutumnRedisMetric) GetValue(label ...string) float64 {
key := arm.metricValueRedisKey
if len(label) > 0 {
key = label[0]
}
f, _ := arm.client.HGet(arm.redisKey, key).Float64()
return f
}
func (arm *AutumnRedisMetric) GetLabels() []string {
return arm.Labels
}
func (arm *AutumnRedisMetric) GetLabelValues() []*pure.LabelValue {
allLabels := arm.client.HGetAll(arm.redisKey).Val()
delete(allLabels, arm.metricValueRedisKey)
r := make([]*pure.LabelValue, 0, len(allLabels))
for k, vStr := range allLabels {
v, _ := strconv.ParseFloat(vStr, 64)
lvs := strings.Split(k, ",")
r = append(r, &pure.LabelValue{
Value: v,
LabelsValue: lvs,
})
}
return r
}
func (arm *AutumnRedisMetric) Set(value float64, labelValues ...string) error {
if len(arm.Labels) != len(labelValues) {
return errors.New("labels key no match label values")
}
if len(arm.Labels) > 0 {
if err := arm.client.HSet(arm.redisKey, strings.Join(labelValues, ","), value).Err(); err != nil {
return err
}
return nil
}
if err := arm.client.HSet(arm.redisKey, arm.metricValueRedisKey, value).Err(); err != nil {
return err
}
return nil
}
func (arm *AutumnRedisMetric) Incr(labelValues ...string) error {
if len(arm.Labels) != len(labelValues) {
return errors.New("labels key no match label values")
}
if len(arm.Labels) > 0 {
if err := arm.client.HIncrByFloat(arm.redisKey, strings.Join(labelValues, ","), 1).Err(); err != nil {
return err
}
return nil
}
if err := arm.client.HIncrByFloat(arm.redisKey, arm.metricValueRedisKey, 1).Err(); err != nil {
return err
}
return nil
}
func (arm *AutumnRedisMetric) Add(value float64, labelValues ...string) error {
if len(arm.Labels) != len(labelValues) {
return errors.New("labels key no match label values")
}
if len(arm.Labels) > 0 {
if err := arm.client.HIncrByFloat(arm.redisKey, strings.Join(labelValues, ","), value).Err(); err != nil {
return err
}
return nil
}
if err := arm.client.HIncrByFloat(arm.redisKey, arm.metricValueRedisKey, value).Err(); err != nil {
return err
}
return nil
}
func (arm *AutumnRedisMetric) ResetByCron(spec string) error {
arm.locker.Lock()
defer arm.locker.Unlock()
if arm.cronEntryID > 0 {
DefaultCron().Remove(arm.cronEntryID)
}
id, err := DefaultCron().AddFunc(spec, func() {
labels := arm.GetLabels()
if labels != nil && len(labels) > 0 {
labelValues := arm.GetLabelValues()
for _, lvs := range labelValues {
arm.Set(0, strings.Join(lvs.LabelsValue, ","))
}
return
}
arm.Set(0)
})
if err != nil {
return err
}
arm.cronEntryID = id
return nil
}