Skip to content

Commit 3426c02

Browse files
committed
add test
1 parent 2c32bdc commit 3426c02

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

extra/redisotel/metrics.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,15 @@ func InstrumentMetrics(rdb redis.UniversalClient, opts ...MetricsOption) error {
8282
}
8383
}
8484

85+
func poolStatsAttrs(conf *config) (poolAttrs, idleAttrs, usedAttrs attribute.Set) {
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"))...)
89+
return
90+
}
91+
8592
func reportPoolStats(rdb *redis.Client, conf *config) error {
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"))...)
93+
poolAttrs, idleAttrs, usedAttrs := poolStatsAttrs(conf)
8994

9095
idleMax, err := conf.meter.Int64ObservableUpDownCounter(
9196
"db.client.connections.idle.max",

extra/redisotel/metrics_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package redisotel
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"go.opentelemetry.io/otel/attribute"
8+
)
9+
10+
func Test_poolStatsAttrs(t *testing.T) {
11+
t.Parallel()
12+
type args struct {
13+
conf *config
14+
}
15+
tests := []struct {
16+
name string
17+
args args
18+
wantPoolAttrs attribute.Set
19+
wantIdleAttrs attribute.Set
20+
wantUsedAttrs attribute.Set
21+
}{
22+
{
23+
name: "#3122",
24+
args: func() args {
25+
conf := &config{
26+
attrs: make([]attribute.KeyValue, 0, 4),
27+
}
28+
conf.attrs = append(conf.attrs, attribute.String("foo1", "bar1"), attribute.String("foo2", "bar2"))
29+
conf.attrs = append(conf.attrs, attribute.String("pool.name", "pool1"))
30+
return args{conf: conf}
31+
}(),
32+
wantPoolAttrs: attribute.NewSet(attribute.String("foo1", "bar1"), attribute.String("foo2", "bar2"),
33+
attribute.String("pool.name", "pool1")),
34+
wantIdleAttrs: attribute.NewSet(attribute.String("foo1", "bar1"), attribute.String("foo2", "bar2"),
35+
attribute.String("pool.name", "pool1"), attribute.String("state", "idle")),
36+
wantUsedAttrs: attribute.NewSet(attribute.String("foo1", "bar1"), attribute.String("foo2", "bar2"),
37+
attribute.String("pool.name", "pool1"), attribute.String("state", "used")),
38+
},
39+
}
40+
for _, tt := range tests {
41+
t.Run(tt.name, func(t *testing.T) {
42+
gotPoolAttrs, gotIdleAttrs, gotUsedAttrs := poolStatsAttrs(tt.args.conf)
43+
if !reflect.DeepEqual(gotPoolAttrs, tt.wantPoolAttrs) {
44+
t.Errorf("poolStatsAttrs() gotPoolAttrs = %v, want %v", gotPoolAttrs, tt.wantPoolAttrs)
45+
}
46+
if !reflect.DeepEqual(gotIdleAttrs, tt.wantIdleAttrs) {
47+
t.Errorf("poolStatsAttrs() gotIdleAttrs = %v, want %v", gotIdleAttrs, tt.wantIdleAttrs)
48+
}
49+
if !reflect.DeepEqual(gotUsedAttrs, tt.wantUsedAttrs) {
50+
t.Errorf("poolStatsAttrs() gotUsedAttrs = %v, want %v", gotUsedAttrs, tt.wantUsedAttrs)
51+
}
52+
})
53+
}
54+
}

0 commit comments

Comments
 (0)