Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
wzy9607 committed Mar 9, 2025
1 parent 2c32bdc commit 3426c02
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
11 changes: 8 additions & 3 deletions extra/redisotel/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,15 @@ func InstrumentMetrics(rdb redis.UniversalClient, opts ...MetricsOption) error {
}
}

func poolStatsAttrs(conf *config) (poolAttrs, idleAttrs, usedAttrs attribute.Set) {
poolAttrs = attribute.NewSet(conf.attrs...)
idleAttrs = attribute.NewSet(append(poolAttrs.ToSlice(), attribute.String("state", "idle"))...)
usedAttrs = attribute.NewSet(append(poolAttrs.ToSlice(), attribute.String("state", "used"))...)
return
}

func reportPoolStats(rdb *redis.Client, conf *config) error {
poolAttrs := attribute.NewSet(conf.attrs...)
idleAttrs := attribute.NewSet(append(poolAttrs.ToSlice(), attribute.String("state", "idle"))...)
usedAttrs := attribute.NewSet(append(poolAttrs.ToSlice(), attribute.String("state", "used"))...)
poolAttrs, idleAttrs, usedAttrs := poolStatsAttrs(conf)

idleMax, err := conf.meter.Int64ObservableUpDownCounter(
"db.client.connections.idle.max",
Expand Down
54 changes: 54 additions & 0 deletions extra/redisotel/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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)
}
})
}
}

0 comments on commit 3426c02

Please sign in to comment.