Skip to content

Commit ba5fb28

Browse files
authored
fix: use addr as ServerAddress when SplitHostPort failed (#33)
* fix: use addr as ServerAddress when SplitHostPort failed * . * test
1 parent 9b991e1 commit ba5fb28

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

attributes.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strconv"
99
"strings"
1010

11+
"go.opentelemetry.io/otel"
1112
"go.opentelemetry.io/otel/attribute"
1213
semconv "go.opentelemetry.io/otel/semconv/v1.30.0"
1314

@@ -42,12 +43,14 @@ func commonPoolAttrs(conf *config, opt *redis.Options) attribute.Set {
4243
func serverAttributes(addr string) []attribute.KeyValue {
4344
host, portString, err := net.SplitHostPort(addr)
4445
if err != nil {
45-
return []attribute.KeyValue{semconv.ServerAddress(host)}
46+
otel.Handle(err)
47+
return []attribute.KeyValue{semconv.ServerAddress(addr)}
4648
}
4749

4850
// Parse the port string to an integer
4951
port, err := strconv.Atoi(portString)
5052
if err != nil {
53+
otel.Handle(err)
5154
return []attribute.KeyValue{semconv.ServerAddress(host)}
5255
}
5356

attributes_test.go

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)