2
2
// under the Apache License Version 2.0.
3
3
// This product includes software developed at Datadog (https://www.datadoghq.com/).
4
4
// Copyright 2016 Datadog, Inc.
5
- package valkey_test
5
+ package valkey
6
6
7
7
import (
8
8
"context"
@@ -14,7 +14,7 @@ import (
14
14
"github.com/stretchr/testify/assert"
15
15
"github.com/stretchr/testify/require"
16
16
"github.com/valkey-io/valkey-go"
17
- valkeytrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/valkey-go "
17
+ "gopkg.in/DataDog/dd-trace-go.v1/ddtrace "
18
18
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
19
19
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/mocktracer"
20
20
)
@@ -35,11 +35,89 @@ func TestMain(m *testing.M) {
35
35
os .Exit (m .Run ())
36
36
}
37
37
38
+ func TestPeerTags (t * testing.T ) {
39
+ tests := []struct {
40
+ initAddress string
41
+ expectedTags map [string ]interface {}
42
+ }{
43
+ {
44
+ initAddress : "127.0.0.1:6379" ,
45
+ expectedTags : map [string ]interface {}{
46
+ ext .PeerService : "valkey" ,
47
+ ext .PeerHostIPV4 : "127.0.0.1" ,
48
+ ext .PeerPort : 6379 ,
49
+ },
50
+ },
51
+ {
52
+ initAddress : "[::1]:6379" ,
53
+ expectedTags : map [string ]interface {}{
54
+ ext .PeerService : "valkey" ,
55
+ ext .PeerHostIPV6 : "::1" ,
56
+ ext .PeerPort : 6379 ,
57
+ },
58
+ },
59
+ {
60
+ initAddress : "[2001:db8::2]:6379" ,
61
+ expectedTags : map [string ]interface {}{
62
+ ext .PeerService : "valkey" ,
63
+ ext .PeerHostIPV6 : "2001:db8::2" ,
64
+ ext .PeerPort : 6379 ,
65
+ },
66
+ },
67
+ {
68
+ initAddress : "[2001:db8::2%lo]:6379" ,
69
+ expectedTags : map [string ]interface {}{
70
+ ext .PeerService : "valkey" ,
71
+ ext .PeerHostname : "2001:db8::2%lo" ,
72
+ ext .PeerPort : 6379 ,
73
+ },
74
+ },
75
+ {
76
+ initAddress : "::1:7777" ,
77
+ expectedTags : map [string ]interface {}{
78
+ ext .PeerService : "valkey" ,
79
+ ext .PeerHostname : "" ,
80
+ ext .PeerPort : 0 ,
81
+ },
82
+ },
83
+ {
84
+ initAddress : ":::7777" ,
85
+ expectedTags : map [string ]interface {}{
86
+ ext .PeerService : "valkey" ,
87
+ ext .PeerHostname : "" ,
88
+ ext .PeerPort : 0 ,
89
+ },
90
+ },
91
+ {
92
+ initAddress : "localhost:7777" ,
93
+ expectedTags : map [string ]interface {}{
94
+ ext .PeerService : "valkey" ,
95
+ ext .PeerHostname : "localhost" ,
96
+ ext .PeerPort : 7777 ,
97
+ },
98
+ },
99
+ }
100
+ for _ , tt := range tests {
101
+ t .Run (tt .initAddress , func (t * testing.T ) {
102
+ host , port := splitHostPort (tt .initAddress )
103
+ client := coreClient {
104
+ host : host ,
105
+ port : port ,
106
+ }
107
+ var startSpanConfig ddtrace.StartSpanConfig
108
+ for _ , tag := range client .peerTags () {
109
+ tag (& startSpanConfig )
110
+ }
111
+ require .Equal (t , tt .expectedTags , startSpanConfig .Tags )
112
+ })
113
+ }
114
+ }
115
+
38
116
func TestNewClient (t * testing.T ) {
39
117
tests := []struct {
40
118
name string
41
119
valkeyClientOptions valkey.ClientOption
42
- valkeytraceClientOptions []valkeytrace. ClientOption
120
+ valkeytraceClientOptions []ClientOption
43
121
valkeytraceClientEnvVars map [string ]string
44
122
createSpans func (* testing.T , context.Context , valkey.Client )
45
123
assertNewClientError func (* testing.T , error )
@@ -74,19 +152,15 @@ func TestNewClient(t *testing.T) {
74
152
Username : valkeyUsername ,
75
153
Password : valkeyPassword ,
76
154
},
77
- valkeytraceClientOptions : []valkeytrace.ClientOption {
78
- valkeytrace .WithServiceName ("my-valkey-client" ),
79
- valkeytrace .WithAnalytics (true ),
80
- valkeytrace .WithSkipRawCommand (true ),
155
+ valkeytraceClientOptions : []ClientOption {
156
+ WithAnalytics (true ),
157
+ WithSkipRawCommand (true ),
81
158
},
82
159
createSpans : func (t * testing.T , ctx context.Context , client valkey.Client ) {
83
160
assert .NoError (t , client .Do (ctx , client .B ().Set ().Key ("test_key" ).Value ("test_value" ).Build ()).Error ())
84
161
},
85
162
assertSpans : []func (t * testing.T , span mocktracer.Span ){
86
163
func (t * testing.T , span mocktracer.Span ) {
87
- assert .Equal (t , "my-valkey-client" , span .Tag (ext .ServiceName ))
88
- assert .Equal (t , "127.0.0.1" , span .Tag (ext .TargetHost ))
89
- assert .Equal (t , valkeyPort , span .Tag (ext .TargetPort ))
90
164
assert .Equal (t , "SET" , span .Tag (ext .DBStatement ))
91
165
assert .Equal (t , "SET" , span .Tag (ext .ResourceName ))
92
166
assert .Greater (t , span .Tag ("db.stmt_size" ), 0 )
@@ -119,9 +193,6 @@ func TestNewClient(t *testing.T) {
119
193
},
120
194
assertSpans : []func (t * testing.T , span mocktracer.Span ){
121
195
func (t * testing.T , span mocktracer.Span ) {
122
- assert .Equal (t , "valkey.client" , span .Tag (ext .ServiceName ))
123
- assert .Equal (t , "127.0.0.1" , span .Tag (ext .TargetHost ))
124
- assert .Equal (t , valkeyPort , span .Tag (ext .TargetPort ))
125
196
assert .Equal (t , "SET\n test_key\n test_value\n GET\n test_key" , span .Tag (ext .DBStatement ))
126
197
assert .Equal (t , "SET\n test_key\n test_value\n GET\n test_key" , span .Tag (ext .ResourceName ))
127
198
assert .Greater (t , span .Tag ("db.stmt_size" ), 0 )
@@ -155,9 +226,6 @@ func TestNewClient(t *testing.T) {
155
226
},
156
227
assertSpans : []func (t * testing.T , span mocktracer.Span ){
157
228
func (t * testing.T , span mocktracer.Span ) {
158
- assert .Equal (t , "valkey.client" , span .Tag (ext .ServiceName ))
159
- assert .Equal (t , "127.0.0.1" , span .Tag (ext .TargetHost ))
160
- assert .Equal (t , valkeyPort , span .Tag (ext .TargetPort ))
161
229
assert .Greater (t , span .Tag ("db.stmt_size" ), 0 )
162
230
assert .Equal (t , "HMGET\n mk\n 1\n 2" , span .Tag (ext .DBStatement ))
163
231
assert .Equal (t , "HMGET\n mk\n 1\n 2" , span .Tag (ext .ResourceName ))
@@ -175,9 +243,6 @@ func TestNewClient(t *testing.T) {
175
243
assert .Nil (t , span .Tag (ext .Error ))
176
244
},
177
245
func (t * testing.T , span mocktracer.Span ) {
178
- assert .Equal (t , "valkey.client" , span .Tag (ext .ServiceName ))
179
- assert .Equal (t , "127.0.0.1" , span .Tag (ext .TargetHost ))
180
- assert .Equal (t , valkeyPort , span .Tag (ext .TargetPort ))
181
246
assert .Greater (t , span .Tag ("db.stmt_size" ), 0 )
182
247
assert .Equal (t , "HMGET\n mk\n 1\n 2" , span .Tag (ext .DBStatement ))
183
248
assert .Equal (t , "HMGET\n mk\n 1\n 2" , span .Tag (ext .ResourceName ))
@@ -204,7 +269,6 @@ func TestNewClient(t *testing.T) {
204
269
Password : valkeyPassword ,
205
270
},
206
271
valkeytraceClientEnvVars : map [string ]string {
207
- "DD_TRACE_VALKEY_SERVICE_NAME" : "my-valkey-client" ,
208
272
"DD_TRACE_VALKEY_ANALYTICS_ENABLED" : "true" ,
209
273
"DD_TRACE_VALKEY_SKIP_RAW_COMMAND" : "true" ,
210
274
},
@@ -214,9 +278,6 @@ func TestNewClient(t *testing.T) {
214
278
},
215
279
assertSpans : []func (t * testing.T , span mocktracer.Span ){
216
280
func (t * testing.T , span mocktracer.Span ) {
217
- assert .Equal (t , "my-valkey-client" , span .Tag (ext .ServiceName ))
218
- assert .Equal (t , "127.0.0.1" , span .Tag (ext .TargetHost ))
219
- assert .Equal (t , valkeyPort , span .Tag (ext .TargetPort ))
220
281
assert .Equal (t , "GET" , span .Tag (ext .DBStatement ))
221
282
assert .Equal (t , "GET" , span .Tag (ext .ResourceName ))
222
283
assert .Greater (t , span .Tag ("db.stmt_size" ), 0 )
@@ -252,9 +313,6 @@ func TestNewClient(t *testing.T) {
252
313
},
253
314
assertSpans : []func (t * testing.T , span mocktracer.Span ){
254
315
func (t * testing.T , span mocktracer.Span ) {
255
- assert .Equal (t , "valkey.client" , span .Tag (ext .ServiceName ))
256
- assert .Equal (t , "127.0.0.1" , span .Tag (ext .TargetHost ))
257
- assert .Equal (t , valkeyPort , span .Tag (ext .TargetPort ))
258
316
assert .Greater (t , span .Tag ("db.stmt_size" ), 0 )
259
317
assert .Equal (t , "SUBSCRIBE\n test_channel" , span .Tag (ext .DBStatement ))
260
318
assert .Equal (t , "SUBSCRIBE\n test_channel" , span .Tag (ext .ResourceName ))
@@ -282,7 +340,7 @@ func TestNewClient(t *testing.T) {
282
340
for k , v := range tt .valkeytraceClientEnvVars {
283
341
t .Setenv (k , v )
284
342
}
285
- client , err := valkeytrace . NewClient (tt .valkeyClientOptions , tt .valkeytraceClientOptions ... )
343
+ client , err := NewClient (tt .valkeyClientOptions , tt .valkeytraceClientOptions ... )
286
344
if tt .assertNewClientError == nil {
287
345
require .NoErrorf (t , err , tt .name )
288
346
} else {
@@ -295,6 +353,12 @@ func TestNewClient(t *testing.T) {
295
353
for i , span := range spans {
296
354
tt .assertSpans [i ](t , span )
297
355
// Following assertions are common to all spans
356
+ assert .Equal (t , "" , span .Tag (ext .ServiceName )) // Do not overwrite service name as per DD_APM_PEER_TAGS_AGGREGATION in agent
357
+ assert .Equal (t , "valkey" , span .Tag (ext .PeerService ))
358
+ assert .Equal (t , "127.0.0.1" , span .Tag (ext .PeerHostIPV4 ))
359
+ assert .Equal (t , "127.0.0.1" , span .Tag (ext .TargetHost ))
360
+ assert .Equal (t , valkeyPort , span .Tag (ext .PeerPort ))
361
+ assert .Equal (t , valkeyPort , span .Tag (ext .TargetPort ))
298
362
assert .NotNil (t , span )
299
363
assert .True (t , span .Tag (ext .ValkeyClientCommandWithPassword ).(bool ))
300
364
assert .Equal (t , tt .valkeyClientOptions .Username , span .Tag (ext .DBUser ))
@@ -304,6 +368,7 @@ func TestNewClient(t *testing.T) {
304
368
assert .Equal (t , "valkey-go/valkey" , span .Tag (ext .Component ))
305
369
assert .Equal (t , "valkey" , span .Tag (ext .DBType ))
306
370
assert .Equal (t , "valkey" , span .Tag (ext .DBSystem ))
371
+ assert .Equal (t , "valkey" , span .Tag (ext .DBInstance ))
307
372
}
308
373
})
309
374
}
0 commit comments