Skip to content

Commit bbff4dd

Browse files
committed
chore: fix metrics in redisotel
1 parent 58e87e0 commit bbff4dd

File tree

6 files changed

+160
-32
lines changed

6 files changed

+160
-32
lines changed

example/otel/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ replace github.com/go-redis/redis/extra/rediscmd/v9 => ../../extra/rediscmd
1111
require (
1212
github.com/go-redis/redis/extra/redisotel/v9 v9.0.0-rc.1
1313
github.com/go-redis/redis/v9 v9.0.0-rc.1
14-
github.com/uptrace/uptrace-go v1.11.4
14+
github.com/uptrace/uptrace-go v1.11.6
1515
go.opentelemetry.io/otel v1.11.1
1616
)

example/otel/go.sum

Lines changed: 111 additions & 13 deletions
Large diffs are not rendered by default.

extra/redisotel/go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ replace github.com/go-redis/redis/extra/rediscmd/v9 => ../rediscmd
99
require (
1010
github.com/go-redis/redis/extra/rediscmd/v9 v9.0.0-rc.1
1111
github.com/go-redis/redis/v9 v9.0.0-rc.1
12-
go.opentelemetry.io/otel v1.10.0
13-
go.opentelemetry.io/otel/metric v0.32.1
12+
go.opentelemetry.io/otel v1.11.1
13+
go.opentelemetry.io/otel/metric v0.33.0
1414
go.opentelemetry.io/otel/sdk v1.9.0
15-
go.opentelemetry.io/otel/trace v1.10.0
15+
go.opentelemetry.io/otel/trace v1.11.1
1616
)

extra/redisotel/go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
3333
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
3434
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
3535
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
36+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
3637
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
3738
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
3839
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
@@ -69,13 +70,19 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t
6970
go.opentelemetry.io/otel v1.9.0/go.mod h1:np4EoPGzoPs3O67xUVNoPPcmSvsfOxNlNA4F4AC+0Eo=
7071
go.opentelemetry.io/otel v1.10.0 h1:Y7DTJMR6zs1xkS/upamJYk0SxxN4C9AqRd77jmZnyY4=
7172
go.opentelemetry.io/otel v1.10.0/go.mod h1:NbvWjCthWHKBEUMpf0/v8ZRZlni86PpGFEMA9pnQSnQ=
73+
go.opentelemetry.io/otel v1.11.1 h1:4WLLAmcfkmDk2ukNXJyq3/kiz/3UzCaYq6PskJsaou4=
74+
go.opentelemetry.io/otel v1.11.1/go.mod h1:1nNhXBbWSD0nsL38H6btgnFN2k4i0sNLHNNMZMSbUGE=
7275
go.opentelemetry.io/otel/metric v0.32.1 h1:ftff5LSBCIDwL0UkhBuDg8j9NNxx2IusvJ18q9h6RC4=
7376
go.opentelemetry.io/otel/metric v0.32.1/go.mod h1:iLPP7FaKMAD5BIxJ2VX7f2KTuz//0QK2hEUyti5psqQ=
77+
go.opentelemetry.io/otel/metric v0.33.0 h1:xQAyl7uGEYvrLAiV/09iTJlp1pZnQ9Wl793qbVvED1E=
78+
go.opentelemetry.io/otel/metric v0.33.0/go.mod h1:QlTYc+EnYNq/M2mNk1qDDMRLpqCOj2f/r5c7Fd5FYaI=
7479
go.opentelemetry.io/otel/sdk v1.9.0 h1:LNXp1vrr83fNXTHgU8eO89mhzxb/bbWAsHG6fNf3qWo=
7580
go.opentelemetry.io/otel/sdk v1.9.0/go.mod h1:AEZc8nt5bd2F7BC24J5R0mrjYnpEgYHyTcM/vrSple4=
7681
go.opentelemetry.io/otel/trace v1.9.0/go.mod h1:2737Q0MuG8q1uILYm2YYVkAyLtOofiTNGg6VODnOiPo=
7782
go.opentelemetry.io/otel/trace v1.10.0 h1:npQMbR8o7mum8uF95yFbOEJffhs1sbCOfDh8zAJiH5E=
7883
go.opentelemetry.io/otel/trace v1.10.0/go.mod h1:Sij3YYczqAdz+EhmGhE6TpTxUO5/F/AzrK+kxfGqySM=
84+
go.opentelemetry.io/otel/trace v1.11.1 h1:ofxdnzsNrGBYXbP7t7zpUK281+go5rF7dvdIZXF8gdQ=
85+
go.opentelemetry.io/otel/trace v1.11.1/go.mod h1:f/Q9G7vzk5u91PhbmKbg1Qn0rzH1LJ4vbPHFGkTPtOk=
7986
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
8087
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
8188
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=

extra/redisotel/metrics.go

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"go.opentelemetry.io/otel/attribute"
1212
"go.opentelemetry.io/otel/metric"
1313
"go.opentelemetry.io/otel/metric/instrument"
14-
"go.opentelemetry.io/otel/metric/instrument/syncint64"
14+
"go.opentelemetry.io/otel/metric/instrument/syncfloat64"
1515
)
1616

1717
// InstrumentMetrics starts reporting OpenTelemetry Metrics.
@@ -64,6 +64,12 @@ func InstrumentMetrics(rdb redis.UniversalClient, opts ...MetricsOption) error {
6464
return nil
6565
case *redis.Ring:
6666
rdb.OnNewNode(func(rdb *redis.Client) {
67+
if conf.poolName == "" {
68+
opt := rdb.Options()
69+
conf.poolName = opt.Addr
70+
}
71+
conf.attrs = append(conf.attrs, attribute.String("pool.name", conf.poolName))
72+
6773
if err := reportPoolStats(rdb, conf); err != nil {
6874
otel.Handle(err)
6975
}
@@ -134,9 +140,9 @@ func reportPoolStats(rdb *redis.Client, conf *config) error {
134140
func(ctx context.Context) {
135141
stats := rdb.PoolStats()
136142

137-
idleMax.Observe(ctx, int64(redisConf.MinIdleConns))
138-
idleMin.Observe(ctx, int64(redisConf.MaxIdleConns))
139-
connsMax.Observe(ctx, int64(redisConf.PoolSize))
143+
idleMax.Observe(ctx, int64(redisConf.MaxIdleConns), labels...)
144+
idleMin.Observe(ctx, int64(redisConf.MinIdleConns), labels...)
145+
connsMax.Observe(ctx, int64(redisConf.PoolSize), labels...)
140146

141147
usage.Observe(ctx, int64(stats.IdleConns), idleAttrs...)
142148
usage.Observe(ctx, int64(stats.TotalConns-stats.IdleConns), usedAttrs...)
@@ -147,7 +153,7 @@ func reportPoolStats(rdb *redis.Client, conf *config) error {
147153
}
148154

149155
func addMetricsHook(rdb *redis.Client, conf *config) error {
150-
createTime, err := conf.meter.SyncInt64().Histogram(
156+
createTime, err := conf.meter.SyncFloat64().Histogram(
151157
"db.client.connections.create_time",
152158
instrument.WithDescription("The time it took to create a new connection."),
153159
instrument.WithUnit("ms"),
@@ -156,7 +162,7 @@ func addMetricsHook(rdb *redis.Client, conf *config) error {
156162
return err
157163
}
158164

159-
useTime, err := conf.meter.SyncInt64().Histogram(
165+
useTime, err := conf.meter.SyncFloat64().Histogram(
160166
"db.client.connections.use_time",
161167
instrument.WithDescription("The time between borrowing a connection and returning it to the pool."),
162168
instrument.WithUnit("ms"),
@@ -168,13 +174,15 @@ func addMetricsHook(rdb *redis.Client, conf *config) error {
168174
rdb.AddHook(&metricsHook{
169175
createTime: createTime,
170176
useTime: useTime,
177+
attrs: conf.attrs,
171178
})
172179
return nil
173180
}
174181

175182
type metricsHook struct {
176-
createTime syncint64.Histogram
177-
useTime syncint64.Histogram
183+
createTime syncfloat64.Histogram
184+
useTime syncfloat64.Histogram
185+
attrs []attribute.KeyValue
178186
}
179187

180188
var _ redis.Hook = (*metricsHook)(nil)
@@ -185,7 +193,7 @@ func (mh *metricsHook) DialHook(hook redis.DialHook) redis.DialHook {
185193

186194
conn, err := hook(ctx, network, addr)
187195

188-
mh.createTime.Record(ctx, time.Since(start).Milliseconds())
196+
mh.createTime.Record(ctx, milliseconds(time.Since(start)), mh.attrs...)
189197
return conn, err
190198
}
191199
}
@@ -196,8 +204,14 @@ func (mh *metricsHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
196204

197205
err := hook(ctx, cmd)
198206

199-
dur := time.Since(start).Milliseconds()
200-
mh.useTime.Record(ctx, dur, attribute.String("type", "command"), statusAttr(err))
207+
dur := time.Since(start)
208+
209+
attrs := make([]attribute.KeyValue, 0, len(mh.attrs)+2)
210+
attrs = append(attrs, mh.attrs...)
211+
attrs = append(attrs, attribute.String("type", "command"))
212+
attrs = append(attrs, statusAttr(err))
213+
214+
mh.useTime.Record(ctx, milliseconds(dur), attrs...)
201215

202216
return err
203217
}
@@ -211,13 +225,23 @@ func (mh *metricsHook) ProcessPipelineHook(
211225

212226
err := hook(ctx, cmds)
213227

214-
dur := time.Since(start).Milliseconds()
215-
mh.useTime.Record(ctx, dur, attribute.String("type", "pipeline"), statusAttr(err))
228+
dur := time.Since(start)
229+
230+
attrs := make([]attribute.KeyValue, 0, len(mh.attrs)+2)
231+
attrs = append(attrs, mh.attrs...)
232+
attrs = append(attrs, attribute.String("type", "pipeline"))
233+
attrs = append(attrs, statusAttr(err))
234+
235+
mh.useTime.Record(ctx, milliseconds(dur), attrs...)
216236

217237
return err
218238
}
219239
}
220240

241+
func milliseconds(d time.Duration) float64 {
242+
return float64(d) / float64(time.Millisecond)
243+
}
244+
221245
func statusAttr(err error) attribute.KeyValue {
222246
if err != nil {
223247
return attribute.String("status", "error")

internal/pool/pool.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,14 +430,13 @@ func (p *ConnPool) IdleLen() int {
430430
}
431431

432432
func (p *ConnPool) Stats() *Stats {
433-
idleLen := p.IdleLen()
434433
return &Stats{
435434
Hits: atomic.LoadUint32(&p.stats.Hits),
436435
Misses: atomic.LoadUint32(&p.stats.Misses),
437436
Timeouts: atomic.LoadUint32(&p.stats.Timeouts),
438437

439438
TotalConns: uint32(p.Len()),
440-
IdleConns: uint32(idleLen),
439+
IdleConns: uint32(p.IdleLen()),
441440
StaleConns: atomic.LoadUint32(&p.stats.StaleConns),
442441
}
443442
}

0 commit comments

Comments
 (0)