-
Notifications
You must be signed in to change notification settings - Fork 0
/
gauge.go
62 lines (48 loc) · 1.29 KB
/
gauge.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
package promgo
import (
"context"
"github.com/go-redis/redis/v8"
)
// Gauge dashboard
type Gauge interface {
Collector
Inc(context.Context, ConstLabels)
IncBy(context.Context, float64, ConstLabels)
Dec(context.Context, ConstLabels)
DecBy(context.Context, float64, ConstLabels)
Set(context.Context, float64, ConstLabels)
}
// GaugeOptions ...
type GaugeOptions CollectorOptions
// redisGauge ...
type redisGauge struct {
redisCollector
}
// Inc 自增
func (rg redisGauge) Inc(ctx context.Context, cl ConstLabels) {
rg.IncBy(ctx, 1, cl)
}
func (rg redisGauge) IncBy(ctx context.Context, v float64, cl ConstLabels) {
rg.Rdb.HIncrByFloat(ctx, rg.key(), rg.field(cl), v)
}
func (rg redisGauge) Dec(ctx context.Context, cl ConstLabels) {
rg.DecBy(ctx, 1, cl)
}
func (rg redisGauge) DecBy(ctx context.Context, v float64, cl ConstLabels) {
rg.IncBy(ctx, -1*v, cl)
}
func (rg redisGauge) Set(ctx context.Context, v float64, cl ConstLabels) {
rg.Rdb.HSet(ctx, rg.key(), rg.field(cl), v)
}
// NewGauge ...
func NewGauge(rdb redis.Cmdable, opts GaugeOptions) Gauge {
desc := &Desc{
Namespace: opts.Namespace,
Name: opts.Name,
Help: opts.Help,
Type: GaugeValue,
Labels: opts.Labels,
}
rc := redisCollector{Rdb: rdb, Desc: desc}
return redisGauge{redisCollector: rc}
}