|
| 1 | +package redisotel |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "go.opentelemetry.io/otel/attribute" |
| 8 | + semconv "go.opentelemetry.io/otel/semconv/v1.30.0" |
| 9 | + |
| 10 | + "github.com/redis/go-redis/v9" |
| 11 | +) |
| 12 | + |
| 13 | +func Test_commonOperationAttrs(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + type args struct { |
| 16 | + conf *config |
| 17 | + opt *redis.Options |
| 18 | + } |
| 19 | + tests := []struct { |
| 20 | + name string |
| 21 | + args args |
| 22 | + want attribute.Set |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "addr parsed", |
| 26 | + args: args{ |
| 27 | + conf: &config{ |
| 28 | + attrs: []attribute.KeyValue{attribute.Key("foo").String("bar")}, |
| 29 | + }, |
| 30 | + opt: &redis.Options{ |
| 31 | + Addr: "10.1.1.1:6379", |
| 32 | + DB: 2, |
| 33 | + }, |
| 34 | + }, |
| 35 | + want: attribute.NewSet( |
| 36 | + attribute.Key("foo").String("bar"), |
| 37 | + semconv.DBSystemNameRedis, |
| 38 | + semconv.DBNamespace("2"), |
| 39 | + semconv.ServerAddress("10.1.1.1"), |
| 40 | + semconv.ServerPort(6379), |
| 41 | + ), |
| 42 | + }, { |
| 43 | + name: "cannot parse addr", |
| 44 | + args: args{ |
| 45 | + conf: &config{ |
| 46 | + attrs: []attribute.KeyValue{attribute.Key("foo").String("bar")}, |
| 47 | + }, |
| 48 | + opt: &redis.Options{ |
| 49 | + Addr: "FailoverClient", |
| 50 | + DB: 2, |
| 51 | + }, |
| 52 | + }, |
| 53 | + want: attribute.NewSet( |
| 54 | + attribute.Key("foo").String("bar"), |
| 55 | + semconv.DBSystemNameRedis, |
| 56 | + semconv.DBNamespace("2"), |
| 57 | + semconv.ServerAddress("FailoverClient"), |
| 58 | + ), |
| 59 | + }, { |
| 60 | + name: "cannot parse port", |
| 61 | + args: args{ |
| 62 | + conf: &config{ |
| 63 | + attrs: []attribute.KeyValue{attribute.Key("foo").String("bar")}, |
| 64 | + }, |
| 65 | + opt: &redis.Options{ |
| 66 | + Addr: "10.1.1.1:a", |
| 67 | + DB: 2, |
| 68 | + }, |
| 69 | + }, |
| 70 | + want: attribute.NewSet( |
| 71 | + attribute.Key("foo").String("bar"), |
| 72 | + semconv.DBSystemNameRedis, |
| 73 | + semconv.DBNamespace("2"), |
| 74 | + semconv.ServerAddress("10.1.1.1"), |
| 75 | + ), |
| 76 | + }, |
| 77 | + } |
| 78 | + for _, tt := range tests { |
| 79 | + t.Run(tt.name, func(t *testing.T) { |
| 80 | + t.Parallel() |
| 81 | + assert.Equalf(t, tt.want, commonOperationAttrs(tt.args.conf, tt.args.opt), "commonOperationAttrs(%v, %v)", |
| 82 | + tt.args.conf, tt.args.opt) |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
0 commit comments