-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool_stats.go
83 lines (73 loc) · 2.47 KB
/
pool_stats.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
package redisotel
import (
"context"
"fmt"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
semconv "go.opentelemetry.io/otel/semconv/v1.27.0"
"github.com/redis/go-redis/v9"
)
// InstrumentPoolStatsMetrics starts reporting OpenTelemetry Metrics for the connection pool.
func InstrumentPoolStatsMetrics(rdb redis.UniversalClient, opts ...Option) error {
conf := newConfig(opts...)
switch rdb := rdb.(type) {
case *redis.Client:
if err := reportPoolStats(rdb, conf); err != nil {
return err
}
return nil
case *redis.ClusterClient:
rdb.OnNewNode(func(rdb *redis.Client) {
if err := reportPoolStats(rdb, conf); err != nil {
otel.Handle(err)
}
})
return nil
case *redis.Ring:
rdb.OnNewNode(func(rdb *redis.Client) {
if err := reportPoolStats(rdb, conf); err != nil {
otel.Handle(err)
}
})
return nil
default:
return fmt.Errorf("goredisotel: %T not supported", rdb)
}
}
// reportPoolStats reports connection pool stats.
// todo connPendingRequests and connWaitTime aren't reported.
func reportPoolStats(rdb *redis.Client, conf *config) error {
poolAttrs := commonPoolAttrs(conf, rdb.Options())
idleAttrs := attribute.NewSet(append(poolAttrs.ToSlice(), semconv.DBClientConnectionStateIdle)...)
usedAttrs := attribute.NewSet(append(poolAttrs.ToSlice(), semconv.DBClientConnectionStateUsed)...)
instruments, err := newPoolStatsInstruments(conf.meter)
if err != nil {
return err
}
redisConf := rdb.Options()
_, err = conf.meter.RegisterCallback(
func(ctx context.Context, observer metric.Observer) error {
stats := rdb.PoolStats()
observer.ObserveInt64(instruments.connCount, int64(stats.IdleConns),
metric.WithAttributeSet(idleAttrs))
observer.ObserveInt64(instruments.connCount, int64(stats.TotalConns-stats.IdleConns),
metric.WithAttributeSet(usedAttrs))
observer.ObserveInt64(instruments.connIdleMax, int64(redisConf.MaxIdleConns),
metric.WithAttributeSet(poolAttrs))
observer.ObserveInt64(instruments.connIdleMin, int64(redisConf.MinIdleConns),
metric.WithAttributeSet(poolAttrs))
observer.ObserveInt64(instruments.connMax, int64(redisConf.PoolSize),
metric.WithAttributeSet(poolAttrs))
observer.ObserveInt64(instruments.connTimeouts, int64(stats.Timeouts),
metric.WithAttributeSet(poolAttrs))
return nil
},
instruments.connCount,
instruments.connIdleMax,
instruments.connIdleMin,
instruments.connMax,
instruments.connTimeouts,
)
return err
}