Skip to content

Commit 4e564af

Browse files
authored
feat: more test (#43)
1 parent 491b3cd commit 4e564af

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Diff for: hook_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,37 @@ func Test_clientHook_ProcessHook(t *testing.T) { //nolint:maintidx //table drive
446446
attribute.String("status", "ok"),
447447
})
448448
},
449+
}, {
450+
name: "DisableMetrics option",
451+
fields: fields{
452+
rdsOpt: &redis.Options{Addr: "10.1.1.1:6379", DB: 3},
453+
opts: []Option{DisableMetrics()},
454+
},
455+
args: args{
456+
hook: func(ctx context.Context, cmd redis.Cmder) error { return nil },
457+
cmd: redis.NewCmd(context.Background(), "set", "key", "value"),
458+
},
459+
checkSpan: func(t *testing.T, span sdktrace.ReadOnlySpan) {
460+
t.Helper()
461+
assert.Equal(t, "set", span.Name())
462+
assert.Equal(t, sdktrace.Status{Code: codes.Unset}, span.Status())
463+
t.Logf("attrs: %v", span.Attributes())
464+
465+
wantAttrs := []attribute.KeyValue{
466+
semconv.DBSystemNameRedis,
467+
semconv.DBNamespace("3"),
468+
semconv.DBOperationName("set"),
469+
semconv.ServerAddress("10.1.1.1"),
470+
semconv.ServerPort(6379),
471+
}
472+
assert.Subset(t, span.Attributes(), wantAttrs)
473+
474+
wantNotExistAttrs := []attribute.Key{semconv.DBResponseStatusCodeKey, semconv.DBQueryTextKey}
475+
attrs := attrMap(span.Attributes())
476+
for _, key := range wantNotExistAttrs {
477+
assert.NotContains(t, attrs, key)
478+
}
479+
},
449480
},
450481
}
451482
for _, tt := range tests {
@@ -468,6 +499,10 @@ func Test_clientHook_ProcessHook(t *testing.T) { //nolint:maintidx //table drive
468499

469500
rm := metricdata.ResourceMetrics{}
470501
require.NoError(t, mr.Collect(context.Background(), &rm))
502+
if tt.checkMetrics == nil { // DisableMetrics
503+
require.Empty(t, rm.ScopeMetrics)
504+
return
505+
}
471506
require.Len(t, rm.ScopeMetrics, 1)
472507
tt.checkMetrics(t, rm.ScopeMetrics[0])
473508
})

0 commit comments

Comments
 (0)