Skip to content

Commit 4ff1439

Browse files
committed
fix(redisotel): fix buggy append in reportPoolStats
The current append twice to `conf.attrs` approach in `reportPoolStats` may result in unexpected idleAttrs, due to `append` [can mutate](golang/go#29115 (comment)) the underlying array of the original slice, as demonstrated at <https://go.dev/play/p/jwRMofH91eQ?v=goprev>. Also, I replaced `metric.WithAttributes` in `reportPoolStats` with `metric.WithAttributeSet`, since `WithAttributes` is just `WithAttributeSet` with some extra works that are not needed here, see <https://pkg.go.dev/go.opentelemetry.io/otel/[email protected]#WithAttributes>.
1 parent 233f97a commit 4ff1439

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

extra/redisotel/metrics.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ func InstrumentMetrics(rdb redis.UniversalClient, opts ...MetricsOption) error {
8383
}
8484

8585
func reportPoolStats(rdb *redis.Client, conf *config) error {
86-
labels := conf.attrs
87-
idleAttrs := append(labels, attribute.String("state", "idle"))
88-
usedAttrs := append(labels, attribute.String("state", "used"))
86+
poolAttrs := attribute.NewSet(conf.attrs...)
87+
idleAttrs := attribute.NewSet(append(poolAttrs.ToSlice(), attribute.String("state", "idle"))...)
88+
usedAttrs := attribute.NewSet(append(poolAttrs.ToSlice(), attribute.String("state", "used"))...)
8989

9090
idleMax, err := conf.meter.Int64ObservableUpDownCounter(
9191
"db.client.connections.idle.max",
@@ -132,14 +132,14 @@ func reportPoolStats(rdb *redis.Client, conf *config) error {
132132
func(ctx context.Context, o metric.Observer) error {
133133
stats := rdb.PoolStats()
134134

135-
o.ObserveInt64(idleMax, int64(redisConf.MaxIdleConns), metric.WithAttributes(labels...))
136-
o.ObserveInt64(idleMin, int64(redisConf.MinIdleConns), metric.WithAttributes(labels...))
137-
o.ObserveInt64(connsMax, int64(redisConf.PoolSize), metric.WithAttributes(labels...))
135+
o.ObserveInt64(idleMax, int64(redisConf.MaxIdleConns), metric.WithAttributeSet(poolAttrs))
136+
o.ObserveInt64(idleMin, int64(redisConf.MinIdleConns), metric.WithAttributeSet(poolAttrs))
137+
o.ObserveInt64(connsMax, int64(redisConf.PoolSize), metric.WithAttributeSet(poolAttrs))
138138

139-
o.ObserveInt64(usage, int64(stats.IdleConns), metric.WithAttributes(idleAttrs...))
140-
o.ObserveInt64(usage, int64(stats.TotalConns-stats.IdleConns), metric.WithAttributes(usedAttrs...))
139+
o.ObserveInt64(usage, int64(stats.IdleConns), metric.WithAttributeSet(idleAttrs))
140+
o.ObserveInt64(usage, int64(stats.TotalConns-stats.IdleConns), metric.WithAttributeSet(usedAttrs))
141141

142-
o.ObserveInt64(timeouts, int64(stats.Timeouts), metric.WithAttributes(labels...))
142+
o.ObserveInt64(timeouts, int64(stats.Timeouts), metric.WithAttributeSet(poolAttrs))
143143
return nil
144144
},
145145
idleMax,

0 commit comments

Comments
 (0)