5
5
6
6
package io .opentelemetry .instrumentation .ratpack .client ;
7
7
8
+ import static io .opentelemetry .semconv .ServerAttributes .SERVER_ADDRESS ;
9
+ import static io .opentelemetry .semconv .ServerAttributes .SERVER_PORT ;
10
+
8
11
import io .netty .channel .ConnectTimeoutException ;
9
12
import io .netty .handler .timeout .ReadTimeoutException ;
10
13
import io .opentelemetry .api .common .AttributeKey ;
11
14
import io .opentelemetry .instrumentation .testing .junit .http .AbstractHttpClientTest ;
12
15
import io .opentelemetry .instrumentation .testing .junit .http .HttpClientResult ;
13
16
import io .opentelemetry .instrumentation .testing .junit .http .HttpClientTestOptions ;
17
+ import io .opentelemetry .semconv .NetworkAttributes ;
14
18
import java .net .URI ;
15
19
import java .time .Duration ;
16
20
import java .util .Collections ;
21
+ import java .util .HashSet ;
17
22
import java .util .Map ;
18
23
import java .util .Set ;
19
24
import org .junit .jupiter .api .AfterAll ;
29
34
30
35
public abstract class AbstractRatpackHttpClientTest extends AbstractHttpClientTest <Void > {
31
36
32
- private final ExecHarness exec = ExecHarness .harness ();
37
+ protected final ExecHarness exec = ExecHarness .harness ();
33
38
34
- private HttpClient client ;
35
- private HttpClient singleConnectionClient ;
39
+ protected HttpClient client ;
40
+ protected HttpClient singleConnectionClient ;
36
41
37
42
@ BeforeAll
38
- void setUpClient () throws Exception {
43
+ protected void setUpClient () throws Exception {
39
44
exec .run (
40
45
unused -> {
41
46
client = buildHttpClient ();
@@ -66,7 +71,7 @@ public Void buildRequest(String method, URI uri, Map<String, String> headers) {
66
71
@ Override
67
72
public int sendRequest (Void request , String method , URI uri , Map <String , String > headers )
68
73
throws Exception {
69
- return exec .yield (unused -> internalSendRequest (client , method , uri , headers ))
74
+ return exec .yield (execution -> internalSendRequest (client , method , uri , headers ))
70
75
.getValueOrThrow ();
71
76
}
72
77
@@ -78,13 +83,17 @@ public final void sendRequestWithCallback(
78
83
Map <String , String > headers ,
79
84
HttpClientResult httpClientResult )
80
85
throws Exception {
81
- exec .execute (
82
- Operation .of (
83
- () ->
84
- internalSendRequest (client , method , uri , headers )
85
- .result (
86
- result ->
87
- httpClientResult .complete (result ::getValue , result .getThrowable ()))));
86
+ exec .yield (
87
+ (e ) ->
88
+ Operation .of (
89
+ () ->
90
+ internalSendRequest (client , method , uri , headers )
91
+ .result (
92
+ result ->
93
+ httpClientResult .complete (
94
+ result ::getValue , result .getThrowable ())))
95
+ .promise ())
96
+ .getValueOrThrow ();
88
97
}
89
98
90
99
// overridden in RatpackForkedHttpClientTest
@@ -118,32 +127,13 @@ protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
118
127
.getValueOrThrow ();
119
128
});
120
129
121
- optionsBuilder .setExpectedClientSpanNameMapper (
122
- (uri , method ) -> {
123
- switch (uri .toString ()) {
124
- case "http://localhost:61/" : // unopened port
125
- case "https://192.0.2.1/" : // non routable address
126
- return "CONNECT" ;
127
- default :
128
- return HttpClientTestOptions .DEFAULT_EXPECTED_CLIENT_SPAN_NAME_MAPPER .apply (
129
- uri , method );
130
- }
131
- });
130
+ if (useNettyClientAttributes ()) {
131
+ optionsBuilder .setExpectedClientSpanNameMapper (
132
+ AbstractRatpackHttpClientTest ::nettyExpectedClientSpanNameMapper );
133
+ }
132
134
133
135
optionsBuilder .setClientSpanErrorMapper (
134
- (uri , exception ) -> {
135
- if (uri .toString ().equals ("https://192.0.2.1/" )) {
136
- return new ConnectTimeoutException (
137
- "connection timed out"
138
- + (Boolean .getBoolean ("testLatestDeps" ) ? " after 2000 ms" : "" )
139
- + ": /192.0.2.1:443" );
140
- } else if (OS .WINDOWS .isCurrentOs () && uri .toString ().equals ("http://localhost:61/" )) {
141
- return new ConnectTimeoutException ("connection timed out: localhost/127.0.0.1:61" );
142
- } else if (uri .getPath ().equals ("/read-timeout" )) {
143
- return ReadTimeoutException .INSTANCE ;
144
- }
145
- return exception ;
146
- });
136
+ AbstractRatpackHttpClientTest ::nettyClientSpanErrorMapper );
147
137
148
138
optionsBuilder .setHttpAttributes (this ::computeHttpAttributes );
149
139
@@ -160,7 +150,45 @@ protected Set<AttributeKey<?>> computeHttpAttributes(URI uri) {
160
150
case "https://192.0.2.1/" : // non routable address
161
151
return Collections .emptySet ();
162
152
default :
163
- return HttpClientTestOptions .DEFAULT_HTTP_ATTRIBUTES ;
153
+ HashSet <AttributeKey <?>> attributes =
154
+ new HashSet <>(HttpClientTestOptions .DEFAULT_HTTP_ATTRIBUTES );
155
+ if (useNettyClientAttributes ()) {
156
+ // underlying netty instrumentation does not provide these
157
+ attributes .remove (SERVER_ADDRESS );
158
+ attributes .remove (SERVER_PORT );
159
+ } else {
160
+ // ratpack client instrumentation does not provide this
161
+ attributes .remove (NetworkAttributes .NETWORK_PROTOCOL_VERSION );
162
+ }
163
+ return attributes ;
164
+ }
165
+ }
166
+
167
+ protected boolean useNettyClientAttributes () {
168
+ return true ;
169
+ }
170
+
171
+ private static Throwable nettyClientSpanErrorMapper (URI uri , Throwable exception ) {
172
+ if (uri .toString ().equals ("https://192.0.2.1/" )) {
173
+ return new ConnectTimeoutException (
174
+ "connection timed out"
175
+ + (Boolean .getBoolean ("testLatestDeps" ) ? " after 2000 ms" : "" )
176
+ + ": /192.0.2.1:443" );
177
+ } else if (OS .WINDOWS .isCurrentOs () && uri .toString ().equals ("http://localhost:61/" )) {
178
+ return new ConnectTimeoutException ("connection timed out: localhost/127.0.0.1:61" );
179
+ } else if (uri .getPath ().equals ("/read-timeout" )) {
180
+ return ReadTimeoutException .INSTANCE ;
181
+ }
182
+ return exception ;
183
+ }
184
+
185
+ private static String nettyExpectedClientSpanNameMapper (URI uri , String method ) {
186
+ switch (uri .toString ()) {
187
+ case "http://localhost:61/" : // unopened port
188
+ case "https://192.0.2.1/" : // non routable address
189
+ return "CONNECT" ;
190
+ default :
191
+ return HttpClientTestOptions .DEFAULT_EXPECTED_CLIENT_SPAN_NAME_MAPPER .apply (uri , method );
164
192
}
165
193
}
166
194
}
0 commit comments