4
4
"context"
5
5
"errors"
6
6
"fmt"
7
+ "slices"
7
8
"strings"
8
9
"testing"
9
10
"time"
@@ -338,12 +339,7 @@ func TestWithDBStatement(t *testing.T) {
338
339
}
339
340
}
340
341
341
- func TestWithClientSimple (t * testing.T ) {
342
- client , err := rueidis .NewClient (rueidis.ClientOption {InitAddress : []string {"127.0.0.1:6379" }})
343
- if err != nil {
344
- t .Fatal (err )
345
- }
346
-
342
+ func TestNewClientSimple (t * testing.T ) {
347
343
exp := tracetest .NewInMemoryExporter ()
348
344
tracerProvider := trace .NewTracerProvider (trace .WithSyncer (exp ))
349
345
@@ -352,39 +348,43 @@ func TestWithClientSimple(t *testing.T) {
352
348
353
349
dbStmtFunc := func (cmdTokens []string ) string { return strings .Join (cmdTokens , " " ) }
354
350
355
- client = WithClient (
356
- client ,
351
+ client , err := NewClient (
352
+ rueidis. ClientOption { InitAddress : [] string { "127.0.0.1:6379" }} ,
357
353
TraceAttrs (attribute .String ("any" , "label" )),
358
354
MetricAttrs (attribute .String ("any" , "label" )),
359
355
WithTracerProvider (tracerProvider ),
360
356
WithMeterProvider (meterProvider ),
361
357
WithDBStatement (dbStmtFunc ),
362
358
WithOperationMetricAttr (),
363
359
)
360
+ if err != nil {
361
+ t .Fatal (err )
362
+ }
364
363
defer client .Close ()
365
364
366
365
cmd := client .B ().Set ().Key ("key" ).Value ("val" ).Build ()
367
366
client .Do (context .Background (), cmd )
368
367
369
368
// Validate trace
370
369
spans := exp .GetSpans ().Snapshots ()
371
- if len (spans ) != 1 {
372
- t .Fatalf ("expected 1 span , got %d" , len (spans ))
370
+ if len (spans ) < 2 {
371
+ t .Fatalf ("expected at least 2 spans , got %d" , len (spans ))
373
372
}
374
- span := spans [0 ]
375
- if span .Name () != "SET" {
376
- t .Fatalf ("unexpected span name: got %s, expected %s" , span .Name (), "Set" )
377
- }
378
- var found bool
379
- for _ , attr := range span .Attributes () {
380
- if string (attr .Key ) == "any" && attr .Value .AsString () == "label" {
381
- found = true
382
- break
383
- }
373
+
374
+ commandSpanIdx := slices .IndexFunc (spans , func (span trace.ReadOnlySpan ) bool { return span .Name () == "SET" })
375
+ if commandSpanIdx == - 1 {
376
+ t .Fatal ("could not find SET span" )
384
377
}
385
- if ! found {
386
- t .Fatalf ("expected attribute 'any: label' not found in span attributes" )
378
+ commandSpan := spans [commandSpanIdx ]
379
+ validateSpanHasAttribute (t , commandSpan , "any" , "label" )
380
+
381
+ dialSpanIdx := slices .IndexFunc (spans , func (span trace.ReadOnlySpan ) bool { return span .Name () == "redis.dial" })
382
+ if dialSpanIdx == - 1 {
383
+ t .Fatal ("could not find dial span" )
387
384
}
385
+ dialSpan := spans [dialSpanIdx ]
386
+ validateSpanHasAttribute (t , dialSpan , "server.address" , "127.0.0.1:6379" )
387
+ validateSpanHasAttribute (t , dialSpan , "any" , "label" )
388
388
389
389
metrics := metricdata.ResourceMetrics {}
390
390
if err := mxp .Collect (context .Background (), & metrics ); err != nil {
@@ -395,6 +395,53 @@ func TestWithClientSimple(t *testing.T) {
395
395
validateMetricHasAttributes (t , metrics , "rueidis_command_duration_seconds" , "operation" )
396
396
}
397
397
398
+ func TestNewClientErrorSpan (t * testing.T ) {
399
+ exp := tracetest .NewInMemoryExporter ()
400
+ tracerProvider := trace .NewTracerProvider (trace .WithSyncer (exp ))
401
+
402
+ mxp := metric .NewManualReader ()
403
+ meterProvider := metric .NewMeterProvider (metric .WithReader (mxp ))
404
+
405
+ _ , err := NewClient (
406
+ rueidis.ClientOption {InitAddress : []string {"256.256.256.256:6379" }},
407
+ TraceAttrs (attribute .String ("any" , "label" )),
408
+ MetricAttrs (attribute .String ("any" , "label" )),
409
+ WithTracerProvider (tracerProvider ),
410
+ WithMeterProvider (meterProvider ),
411
+ WithOperationMetricAttr (),
412
+ )
413
+ if err == nil {
414
+ t .Fatal ("expected error" )
415
+ }
416
+
417
+ spans := exp .GetSpans ().Snapshots ()
418
+ if len (spans ) != 1 {
419
+ t .Fatalf ("expected 1 span, got %d" , len (spans ))
420
+ }
421
+ span := spans [0 ]
422
+ if span .Name () != "redis.dial" {
423
+ t .Fatalf ("expected span name 'redis.dial', got %s" , span .Name ())
424
+ }
425
+ validateSpanHasAttribute (t , span , "any" , "label" )
426
+ events := span .Events ()
427
+ if len (events ) != 1 {
428
+ t .Fatalf ("expected 1 event, got %d" , len (events ))
429
+ }
430
+ event := events [0 ]
431
+ if event .Name != "exception" {
432
+ t .Fatalf ("expected event name 'exception', got %s" , event .Name )
433
+ }
434
+ }
435
+
436
+ func validateSpanHasAttribute (t * testing.T , span trace.ReadOnlySpan , key , value string ) {
437
+ t .Helper ()
438
+ if ! slices .ContainsFunc (span .Attributes (), func (attr attribute.KeyValue ) bool {
439
+ return string (attr .Key ) == key && attr .Value .AsString () == value
440
+ }) {
441
+ t .Fatalf ("expected attribute '%s: %s' not found in span attributes" , key , value )
442
+ }
443
+ }
444
+
398
445
func validateMetrics (t * testing.T , metrics metricdata.ResourceMetrics , name string , value int64 ) {
399
446
t .Helper ()
400
447
0 commit comments