@@ -18,6 +18,7 @@ import (
18
18
"context"
19
19
"io"
20
20
"net"
21
+ "strconv"
21
22
"sync"
22
23
"testing"
23
24
@@ -74,7 +75,7 @@ func TestStatsHandler(t *testing.T) {
74
75
doCalls (client )
75
76
76
77
t .Run ("ClientSpans" , func (t * testing.T ) {
77
- checkClientSpans (t , clientSR .Ended ())
78
+ checkClientSpans (t , clientSR .Ended (), listener . Addr (). String () )
78
79
})
79
80
80
81
t .Run ("ClientMetrics" , func (t * testing.T ) {
@@ -90,9 +91,14 @@ func TestStatsHandler(t *testing.T) {
90
91
})
91
92
}
92
93
93
- func checkClientSpans (t * testing.T , spans []trace.ReadOnlySpan ) {
94
+ func checkClientSpans (t * testing.T , spans []trace.ReadOnlySpan , addr string ) {
94
95
require .Len (t , spans , 5 )
95
96
97
+ host , p , err := net .SplitHostPort (addr )
98
+ require .NoError (t , err )
99
+ port , err := strconv .Atoi (p )
100
+ require .NoError (t , err )
101
+
96
102
emptySpan := spans [0 ]
97
103
assert .False (t , emptySpan .EndTime ().IsZero ())
98
104
assert .Equal (t , "grpc.testing.TestService/EmptyCall" , emptySpan .Name ())
@@ -121,6 +127,8 @@ func checkClientSpans(t *testing.T, spans []trace.ReadOnlySpan) {
121
127
semconv .RPCServiceKey .String ("grpc.testing.TestService" ),
122
128
otelgrpc .RPCSystemGRPC ,
123
129
otelgrpc .GRPCStatusCodeKey .Int64 (int64 (codes .OK )),
130
+ semconv .NetSockPeerAddr (host ),
131
+ semconv .NetSockPeerPort (port ),
124
132
}, emptySpan .Attributes ())
125
133
126
134
largeSpan := spans [1 ]
@@ -151,6 +159,8 @@ func checkClientSpans(t *testing.T, spans []trace.ReadOnlySpan) {
151
159
semconv .RPCServiceKey .String ("grpc.testing.TestService" ),
152
160
otelgrpc .RPCSystemGRPC ,
153
161
otelgrpc .GRPCStatusCodeKey .Int64 (int64 (codes .OK )),
162
+ semconv .NetSockPeerAddr (host ),
163
+ semconv .NetSockPeerPort (port ),
154
164
}, largeSpan .Attributes ())
155
165
156
166
streamInput := spans [2 ]
@@ -209,6 +219,8 @@ func checkClientSpans(t *testing.T, spans []trace.ReadOnlySpan) {
209
219
semconv .RPCServiceKey .String ("grpc.testing.TestService" ),
210
220
otelgrpc .RPCSystemGRPC ,
211
221
otelgrpc .GRPCStatusCodeKey .Int64 (int64 (codes .OK )),
222
+ semconv .NetSockPeerAddr (host ),
223
+ semconv .NetSockPeerPort (port ),
212
224
}, streamInput .Attributes ())
213
225
214
226
streamOutput := spans [3 ]
@@ -266,6 +278,8 @@ func checkClientSpans(t *testing.T, spans []trace.ReadOnlySpan) {
266
278
semconv .RPCServiceKey .String ("grpc.testing.TestService" ),
267
279
otelgrpc .RPCSystemGRPC ,
268
280
otelgrpc .GRPCStatusCodeKey .Int64 (int64 (codes .OK )),
281
+ semconv .NetSockPeerAddr (host ),
282
+ semconv .NetSockPeerPort (port ),
269
283
}, streamOutput .Attributes ())
270
284
271
285
pingPong := spans [4 ]
@@ -350,6 +364,8 @@ func checkClientSpans(t *testing.T, spans []trace.ReadOnlySpan) {
350
364
semconv .RPCServiceKey .String ("grpc.testing.TestService" ),
351
365
otelgrpc .RPCSystemGRPC ,
352
366
otelgrpc .GRPCStatusCodeKey .Int64 (int64 (codes .OK )),
367
+ semconv .NetSockPeerAddr (host ),
368
+ semconv .NetSockPeerPort (port ),
353
369
}, pingPong .Attributes ())
354
370
}
355
371
@@ -379,11 +395,15 @@ func checkServerSpans(t *testing.T, spans []trace.ReadOnlySpan) {
379
395
},
380
396
},
381
397
}, emptySpan .Events ())
398
+ port , ok := findAttribute (emptySpan .Attributes (), semconv .NetSockPeerPortKey )
399
+ assert .True (t , ok )
382
400
assert .ElementsMatch (t , []attribute.KeyValue {
383
401
semconv .RPCMethodKey .String ("EmptyCall" ),
384
402
semconv .RPCServiceKey .String ("grpc.testing.TestService" ),
385
403
otelgrpc .RPCSystemGRPC ,
386
404
otelgrpc .GRPCStatusCodeKey .Int64 (int64 (codes .OK )),
405
+ semconv .NetSockPeerAddr ("127.0.0.1" ),
406
+ port ,
387
407
}, emptySpan .Attributes ())
388
408
389
409
largeSpan := spans [1 ]
@@ -409,11 +429,15 @@ func checkServerSpans(t *testing.T, spans []trace.ReadOnlySpan) {
409
429
},
410
430
},
411
431
}, largeSpan .Events ())
432
+ port , ok = findAttribute (largeSpan .Attributes (), semconv .NetSockPeerPortKey )
433
+ assert .True (t , ok )
412
434
assert .ElementsMatch (t , []attribute.KeyValue {
413
435
semconv .RPCMethodKey .String ("UnaryCall" ),
414
436
semconv .RPCServiceKey .String ("grpc.testing.TestService" ),
415
437
otelgrpc .RPCSystemGRPC ,
416
438
otelgrpc .GRPCStatusCodeKey .Int64 (int64 (codes .OK )),
439
+ semconv .NetSockPeerAddr ("127.0.0.1" ),
440
+ port ,
417
441
}, largeSpan .Attributes ())
418
442
419
443
streamInput := spans [2 ]
@@ -467,11 +491,15 @@ func checkServerSpans(t *testing.T, spans []trace.ReadOnlySpan) {
467
491
},
468
492
// client does not record an event for the server response.
469
493
}, streamInput .Events ())
494
+ port , ok = findAttribute (streamInput .Attributes (), semconv .NetSockPeerPortKey )
495
+ assert .True (t , ok )
470
496
assert .ElementsMatch (t , []attribute.KeyValue {
471
497
semconv .RPCMethodKey .String ("StreamingInputCall" ),
472
498
semconv .RPCServiceKey .String ("grpc.testing.TestService" ),
473
499
otelgrpc .RPCSystemGRPC ,
474
500
otelgrpc .GRPCStatusCodeKey .Int64 (int64 (codes .OK )),
501
+ semconv .NetSockPeerAddr ("127.0.0.1" ),
502
+ port ,
475
503
}, streamInput .Attributes ())
476
504
477
505
streamOutput := spans [3 ]
@@ -524,11 +552,15 @@ func checkServerSpans(t *testing.T, spans []trace.ReadOnlySpan) {
524
552
},
525
553
},
526
554
}, streamOutput .Events ())
555
+ port , ok = findAttribute (streamOutput .Attributes (), semconv .NetSockPeerPortKey )
556
+ assert .True (t , ok )
527
557
assert .ElementsMatch (t , []attribute.KeyValue {
528
558
semconv .RPCMethodKey .String ("StreamingOutputCall" ),
529
559
semconv .RPCServiceKey .String ("grpc.testing.TestService" ),
530
560
otelgrpc .RPCSystemGRPC ,
531
561
otelgrpc .GRPCStatusCodeKey .Int64 (int64 (codes .OK )),
562
+ semconv .NetSockPeerAddr ("127.0.0.1" ),
563
+ port ,
532
564
}, streamOutput .Attributes ())
533
565
534
566
pingPong := spans [4 ]
@@ -608,11 +640,15 @@ func checkServerSpans(t *testing.T, spans []trace.ReadOnlySpan) {
608
640
},
609
641
},
610
642
}, pingPong .Events ())
643
+ port , ok = findAttribute (pingPong .Attributes (), semconv .NetSockPeerPortKey )
644
+ assert .True (t , ok )
611
645
assert .ElementsMatch (t , []attribute.KeyValue {
612
646
semconv .RPCMethodKey .String ("FullDuplexCall" ),
613
647
semconv .RPCServiceKey .String ("grpc.testing.TestService" ),
614
648
otelgrpc .RPCSystemGRPC ,
615
649
otelgrpc .GRPCStatusCodeKey .Int64 (int64 (codes .OK )),
650
+ semconv .NetSockPeerAddr ("127.0.0.1" ),
651
+ port ,
616
652
}, pingPong .Attributes ())
617
653
}
618
654
0 commit comments