6
6
package io .opentelemetry .instrumentation .api .incubator .semconv .messaging ;
7
7
8
8
import static org .junit .jupiter .api .Assertions .assertEquals ;
9
+ import static org .mockito .Mockito .lenient ;
9
10
import static org .mockito .Mockito .when ;
10
11
11
12
import io .opentelemetry .instrumentation .api .instrumenter .SpanNameExtractor ;
13
+ import io .opentelemetry .instrumentation .api .internal .SemconvStability ;
14
+ import io .opentelemetry .instrumentation .api .semconv .network .ServerAttributesGetter ;
12
15
import java .util .stream .Stream ;
13
16
import org .junit .jupiter .api .extension .ExtendWith ;
14
17
import org .junit .jupiter .params .ParameterizedTest ;
21
24
class MessagingSpanNameExtractorTest {
22
25
23
26
@ Mock MessagingAttributesGetter <Message , Void > getter ;
27
+ @ Mock ServerAttributesGetter <Message > serverAttributesGetter ;
24
28
25
29
@ ParameterizedTest
26
30
@ MethodSource ("spanNameParams" )
27
31
void shouldExtractSpanName (
28
32
boolean isTemporaryQueue ,
33
+ boolean isAnonymous ,
29
34
String destinationName ,
35
+ String destinationTemplate ,
30
36
MessageOperation operation ,
31
37
String expectedSpanName ) {
32
38
// given
33
39
Message message = new Message ();
34
40
41
+ when (getter .getDestinationTemplate (message )).thenReturn (destinationTemplate );
42
+ if (isAnonymous ) {
43
+ lenient ().when (getter .isAnonymousDestination (message )).thenReturn (true );
44
+ }
45
+
35
46
if (isTemporaryQueue ) {
36
- when (getter .isTemporaryDestination (message )).thenReturn (true );
47
+ lenient (). when (getter .isTemporaryDestination (message )).thenReturn (true );
37
48
} else {
38
- when (getter .getDestination (message )).thenReturn (destinationName );
49
+ lenient (). when (getter .getDestination (message )).thenReturn (destinationName );
39
50
}
51
+ when (getter .getOperationName (message )).thenReturn (operation .operationType ());
52
+
53
+ lenient ().when (serverAttributesGetter .getServerPort (message )).thenReturn (1234 );
54
+ lenient ().when (serverAttributesGetter .getServerAddress (message )).thenReturn ("127.0.0.1" );
40
55
41
56
SpanNameExtractor <Message > underTest =
42
- MessagingSpanNameExtractor .create (getter , operation , null );
57
+ MessagingSpanNameExtractor .create (getter , operation , serverAttributesGetter );
43
58
44
59
// when
45
60
String spanName = underTest .extract (message );
@@ -50,10 +65,41 @@ void shouldExtractSpanName(
50
65
51
66
@ SuppressWarnings ("deprecation" ) // using deprecated semconv
52
67
static Stream <Arguments > spanNameParams () {
68
+ if (SemconvStability .emitStableMessagingSemconv ()) {
69
+ return Stream .of (
70
+ Arguments .of (
71
+ false ,
72
+ false ,
73
+ "destination/1" ,
74
+ "destination/{}" ,
75
+ MessageOperation .PUBLISH ,
76
+ "publish destination/{}" ),
77
+ Arguments .of (
78
+ false ,
79
+ false ,
80
+ "destination/1" ,
81
+ null ,
82
+ MessageOperation .PUBLISH ,
83
+ "publish destination/1" ),
84
+ Arguments .of (true , false , null , "temp" , MessageOperation .PROCESS , "process temp" ),
85
+ Arguments .of (true , false , null , null , MessageOperation .PROCESS , "process (temporary)" ),
86
+ Arguments .of (false , true , null , "anon" , MessageOperation .PROCESS , "process anon" ),
87
+ Arguments .of (false , true , null , null , MessageOperation .PROCESS , "process (anonymous)" ),
88
+ Arguments .of (true , true , null , null , MessageOperation .PROCESS , "process (temporary)" ),
89
+ Arguments .of (
90
+ false , false , null , null , MessageOperation .RECEIVE , "receive 127.0.0.1:1234" ));
91
+ }
53
92
return Stream .of (
54
- Arguments .of (false , "destination" , MessageOperation .PUBLISH , "destination publish" ),
55
- Arguments .of (true , null , MessageOperation .PROCESS , "(temporary) process" ),
56
- Arguments .of (false , null , MessageOperation .RECEIVE , "unknown receive" ));
93
+ Arguments .of (
94
+ false ,
95
+ false ,
96
+ "destination" ,
97
+ "not used" ,
98
+ MessageOperation .PUBLISH ,
99
+ "destination publish" ),
100
+ Arguments .of (
101
+ true , false , null , "not used" , MessageOperation .PROCESS , "(temporary) process" ),
102
+ Arguments .of (false , false , null , "not used" , MessageOperation .RECEIVE , "unknown receive" ));
57
103
}
58
104
59
105
static class Message {}
0 commit comments