1
0
mirror of https://github.com/redis/go-redis.git synced 2025-09-07 07:47:24 +03:00
Files
go-redis/extra/redisotel/metrics_test.go
Cattī Crūdēlēs 4767d9dfaf fix(redisotel): fix buggy append in reportPoolStats (#3122)
The current append twice to `conf.attrs` approach in `reportPoolStats` may result in unexpected idleAttrs,
due to `append` [can mutate](https://github.com/golang/go/issues/29115#issuecomment-444669036) 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/metric@v1.22.0#WithAttributes>.

Co-authored-by: Nedyalko Dyakov <1547186+ndyakov@users.noreply.github.com>
2025-08-05 15:15:34 +03:00

55 lines
1.8 KiB
Go

package redisotel
import (
"reflect"
"testing"
"go.opentelemetry.io/otel/attribute"
)
func Test_poolStatsAttrs(t *testing.T) {
t.Parallel()
type args struct {
conf *config
}
tests := []struct {
name string
args args
wantPoolAttrs attribute.Set
wantIdleAttrs attribute.Set
wantUsedAttrs attribute.Set
}{
{
name: "#3122",
args: func() args {
conf := &config{
attrs: make([]attribute.KeyValue, 0, 4),
}
conf.attrs = append(conf.attrs, attribute.String("foo1", "bar1"), attribute.String("foo2", "bar2"))
conf.attrs = append(conf.attrs, attribute.String("pool.name", "pool1"))
return args{conf: conf}
}(),
wantPoolAttrs: attribute.NewSet(attribute.String("foo1", "bar1"), attribute.String("foo2", "bar2"),
attribute.String("pool.name", "pool1")),
wantIdleAttrs: attribute.NewSet(attribute.String("foo1", "bar1"), attribute.String("foo2", "bar2"),
attribute.String("pool.name", "pool1"), attribute.String("state", "idle")),
wantUsedAttrs: attribute.NewSet(attribute.String("foo1", "bar1"), attribute.String("foo2", "bar2"),
attribute.String("pool.name", "pool1"), attribute.String("state", "used")),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotPoolAttrs, gotIdleAttrs, gotUsedAttrs := poolStatsAttrs(tt.args.conf)
if !reflect.DeepEqual(gotPoolAttrs, tt.wantPoolAttrs) {
t.Errorf("poolStatsAttrs() gotPoolAttrs = %v, want %v", gotPoolAttrs, tt.wantPoolAttrs)
}
if !reflect.DeepEqual(gotIdleAttrs, tt.wantIdleAttrs) {
t.Errorf("poolStatsAttrs() gotIdleAttrs = %v, want %v", gotIdleAttrs, tt.wantIdleAttrs)
}
if !reflect.DeepEqual(gotUsedAttrs, tt.wantUsedAttrs) {
t.Errorf("poolStatsAttrs() gotUsedAttrs = %v, want %v", gotUsedAttrs, tt.wantUsedAttrs)
}
})
}
}