66package io .opentelemetry .instrumentation .api .semconv .http ;
77
88import static io .opentelemetry .sdk .testing .assertj .OpenTelemetryAssertions .assertThat ;
9+ import static io .opentelemetry .semconv .ErrorAttributes .ERROR_TYPE ;
10+ import static io .opentelemetry .semconv .HttpAttributes .HTTP_REQUEST_METHOD ;
11+ import static io .opentelemetry .semconv .HttpAttributes .HTTP_REQUEST_METHOD_ORIGINAL ;
12+ import static io .opentelemetry .semconv .HttpAttributes .HTTP_REQUEST_RESEND_COUNT ;
13+ import static io .opentelemetry .semconv .HttpAttributes .HTTP_RESPONSE_STATUS_CODE ;
14+ import static io .opentelemetry .semconv .NetworkAttributes .NETWORK_PEER_ADDRESS ;
15+ import static io .opentelemetry .semconv .NetworkAttributes .NETWORK_PEER_PORT ;
16+ import static io .opentelemetry .semconv .NetworkAttributes .NETWORK_PROTOCOL_NAME ;
17+ import static io .opentelemetry .semconv .NetworkAttributes .NETWORK_PROTOCOL_VERSION ;
18+ import static io .opentelemetry .semconv .ServerAttributes .SERVER_ADDRESS ;
19+ import static io .opentelemetry .semconv .ServerAttributes .SERVER_PORT ;
20+ import static io .opentelemetry .semconv .UrlAttributes .URL_FULL ;
921import static java .util .Arrays .asList ;
1022import static java .util .Collections .emptyList ;
1123import static java .util .Collections .emptyMap ;
1830import io .opentelemetry .context .Context ;
1931import io .opentelemetry .instrumentation .api .instrumenter .AttributesExtractor ;
2032import io .opentelemetry .instrumentation .api .internal .HttpConstants ;
21- import io .opentelemetry .semconv .ErrorAttributes ;
22- import io .opentelemetry .semconv .HttpAttributes ;
23- import io .opentelemetry .semconv .NetworkAttributes ;
24- import io .opentelemetry .semconv .ServerAttributes ;
25- import io .opentelemetry .semconv .UrlAttributes ;
2633import java .net .ConnectException ;
2734import java .util .HashMap ;
2835import java .util .HashSet ;
@@ -171,26 +178,26 @@ void normal() {
171178 extractor .onStart (startAttributes , Context .root (), request );
172179 assertThat (startAttributes .build ())
173180 .containsOnly (
174- entry (HttpAttributes . HTTP_REQUEST_METHOD , "POST" ),
175- entry (UrlAttributes . URL_FULL , "http://github.com" ),
181+ entry (HTTP_REQUEST_METHOD , "POST" ),
182+ entry (URL_FULL , "http://github.com" ),
176183 entry (
177184 AttributeKey .stringArrayKey ("http.request.header.custom-request-header" ),
178185 asList ("123" , "456" )),
179- entry (ServerAttributes . SERVER_ADDRESS , "github.com" ),
180- entry (ServerAttributes . SERVER_PORT , 80L ),
181- entry (HttpAttributes . HTTP_REQUEST_RESEND_COUNT , 2L ));
186+ entry (SERVER_ADDRESS , "github.com" ),
187+ entry (SERVER_PORT , 80L ),
188+ entry (HTTP_REQUEST_RESEND_COUNT , 2L ));
182189
183190 AttributesBuilder endAttributes = Attributes .builder ();
184191 extractor .onEnd (endAttributes , Context .root (), request , response , null );
185192 assertThat (endAttributes .build ())
186193 .containsOnly (
187- entry (HttpAttributes . HTTP_RESPONSE_STATUS_CODE , 202L ),
194+ entry (HTTP_RESPONSE_STATUS_CODE , 202L ),
188195 entry (
189196 AttributeKey .stringArrayKey ("http.response.header.custom-response-header" ),
190197 asList ("654" , "321" )),
191- entry (NetworkAttributes . NETWORK_PROTOCOL_VERSION , "1.1" ),
192- entry (NetworkAttributes . NETWORK_PEER_ADDRESS , "4.3.2.1" ),
193- entry (NetworkAttributes . NETWORK_PEER_PORT , 456L ));
198+ entry (NETWORK_PROTOCOL_VERSION , "1.1" ),
199+ entry (NETWORK_PEER_ADDRESS , "4.3.2.1" ),
200+ entry (NETWORK_PEER_PORT , 456L ));
194201 }
195202
196203 @ ParameterizedTest
@@ -207,8 +214,8 @@ void shouldExtractKnownMethods(String requestMethod) {
207214 extractor .onEnd (attributes , Context .root (), request , emptyMap (), null );
208215
209216 assertThat (attributes .build ())
210- .containsEntry (HttpAttributes . HTTP_REQUEST_METHOD , requestMethod )
211- .doesNotContainKey (HttpAttributes . HTTP_REQUEST_METHOD_ORIGINAL );
217+ .containsEntry (HTTP_REQUEST_METHOD , requestMethod )
218+ .doesNotContainKey (HTTP_REQUEST_METHOD_ORIGINAL );
212219 }
213220
214221 @ ParameterizedTest
@@ -225,8 +232,8 @@ void shouldTreatMethodsAsCaseSensitive(String requestMethod) {
225232 extractor .onEnd (attributes , Context .root (), request , emptyMap (), null );
226233
227234 assertThat (attributes .build ())
228- .containsEntry (HttpAttributes . HTTP_REQUEST_METHOD , HttpConstants ._OTHER )
229- .containsEntry (HttpAttributes . HTTP_REQUEST_METHOD_ORIGINAL , requestMethod );
235+ .containsEntry (HTTP_REQUEST_METHOD , HttpConstants ._OTHER )
236+ .containsEntry (HTTP_REQUEST_METHOD_ORIGINAL , requestMethod );
230237 }
231238
232239 @ ParameterizedTest
@@ -243,8 +250,8 @@ void shouldUseOtherForUnknownMethods(String requestMethod) {
243250 extractor .onEnd (attributes , Context .root (), request , emptyMap (), null );
244251
245252 assertThat (attributes .build ())
246- .containsEntry (HttpAttributes . HTTP_REQUEST_METHOD , HttpConstants ._OTHER )
247- .containsEntry (HttpAttributes . HTTP_REQUEST_METHOD_ORIGINAL , requestMethod );
253+ .containsEntry (HTTP_REQUEST_METHOD , HttpConstants ._OTHER )
254+ .containsEntry (HTTP_REQUEST_METHOD_ORIGINAL , requestMethod );
248255 }
249256
250257 @ ParameterizedTest
@@ -263,8 +270,8 @@ void shouldExtractKnownMethods_override(String requestMethod) {
263270 extractor .onEnd (attributes , Context .root (), request , emptyMap (), null );
264271
265272 assertThat (attributes .build ())
266- .containsEntry (HttpAttributes . HTTP_REQUEST_METHOD , requestMethod )
267- .doesNotContainKey (HttpAttributes . HTTP_REQUEST_METHOD_ORIGINAL );
273+ .containsEntry (HTTP_REQUEST_METHOD , requestMethod )
274+ .doesNotContainKey (HTTP_REQUEST_METHOD_ORIGINAL );
268275 }
269276
270277 @ ParameterizedTest
@@ -283,8 +290,8 @@ void shouldUseOtherForUnknownMethods_override(String requestMethod) {
283290 extractor .onEnd (attributes , Context .root (), request , emptyMap (), null );
284291
285292 assertThat (attributes .build ())
286- .containsEntry (HttpAttributes . HTTP_REQUEST_METHOD , HttpConstants ._OTHER )
287- .containsEntry (HttpAttributes . HTTP_REQUEST_METHOD_ORIGINAL , requestMethod );
293+ .containsEntry (HTTP_REQUEST_METHOD , HttpConstants ._OTHER )
294+ .containsEntry (HTTP_REQUEST_METHOD_ORIGINAL , requestMethod );
288295 }
289296
290297 @ Test
@@ -300,8 +307,8 @@ void shouldExtractErrorType_httpStatusCode() {
300307 extractor .onEnd (attributes , Context .root (), emptyMap (), response , null );
301308
302309 assertThat (attributes .build ())
303- .containsEntry (HttpAttributes . HTTP_RESPONSE_STATUS_CODE , 400 )
304- .containsEntry (ErrorAttributes . ERROR_TYPE , "400" );
310+ .containsEntry (HTTP_RESPONSE_STATUS_CODE , 400 )
311+ .containsEntry (ERROR_TYPE , "400" );
305312 }
306313
307314 @ Test
@@ -317,7 +324,7 @@ void shouldExtractErrorType_getter() {
317324 extractor .onStart (attributes , Context .root (), emptyMap ());
318325 extractor .onEnd (attributes , Context .root (), request , emptyMap (), null );
319326
320- assertThat (attributes .build ()).containsEntry (ErrorAttributes . ERROR_TYPE , "custom error type" );
327+ assertThat (attributes .build ()).containsEntry (ERROR_TYPE , "custom error type" );
321328 }
322329
323330 @ Test
@@ -329,8 +336,7 @@ void shouldExtractErrorType_exceptionClassName() {
329336 extractor .onStart (attributes , Context .root (), emptyMap ());
330337 extractor .onEnd (attributes , Context .root (), emptyMap (), emptyMap (), new ConnectException ());
331338
332- assertThat (attributes .build ())
333- .containsEntry (ErrorAttributes .ERROR_TYPE , "java.net.ConnectException" );
339+ assertThat (attributes .build ()).containsEntry (ERROR_TYPE , "java.net.ConnectException" );
334340 }
335341
336342 @ Test
@@ -342,7 +348,7 @@ void shouldExtractErrorType_other() {
342348 extractor .onStart (attributes , Context .root (), emptyMap ());
343349 extractor .onEnd (attributes , Context .root (), emptyMap (), emptyMap (), null );
344350
345- assertThat (attributes .build ()).containsEntry (ErrorAttributes . ERROR_TYPE , HttpConstants ._OTHER );
351+ assertThat (attributes .build ()).containsEntry (ERROR_TYPE , HttpConstants ._OTHER );
346352 }
347353
348354 @ Test
@@ -359,14 +365,11 @@ void shouldExtractServerAddressAndPortFromHostHeader() {
359365 AttributesBuilder startAttributes = Attributes .builder ();
360366 extractor .onStart (startAttributes , Context .root (), request );
361367 assertThat (startAttributes .build ())
362- .containsOnly (
363- entry (ServerAttributes .SERVER_ADDRESS , "github.com" ),
364- entry (ServerAttributes .SERVER_PORT , 123L ));
368+ .containsOnly (entry (SERVER_ADDRESS , "github.com" ), entry (SERVER_PORT , 123L ));
365369
366370 AttributesBuilder endAttributes = Attributes .builder ();
367371 extractor .onEnd (endAttributes , Context .root (), request , response , null );
368- assertThat (endAttributes .build ())
369- .containsOnly (entry (HttpAttributes .HTTP_RESPONSE_STATUS_CODE , 200L ));
372+ assertThat (endAttributes .build ()).containsOnly (entry (HTTP_RESPONSE_STATUS_CODE , 200L ));
370373 }
371374
372375 @ Test
@@ -386,17 +389,15 @@ void shouldExtractPeerAddressEvenIfItDuplicatesServerAddress() {
386389 AttributesBuilder startAttributes = Attributes .builder ();
387390 extractor .onStart (startAttributes , Context .root (), request );
388391 assertThat (startAttributes .build ())
389- .containsOnly (
390- entry (ServerAttributes .SERVER_ADDRESS , "1.2.3.4" ),
391- entry (ServerAttributes .SERVER_PORT , 123L ));
392+ .containsOnly (entry (SERVER_ADDRESS , "1.2.3.4" ), entry (SERVER_PORT , 123L ));
392393
393394 AttributesBuilder endAttributes = Attributes .builder ();
394395 extractor .onEnd (endAttributes , Context .root (), request , response , null );
395396 assertThat (endAttributes .build ())
396397 .containsOnly (
397- entry (HttpAttributes . HTTP_RESPONSE_STATUS_CODE , 200L ),
398- entry (NetworkAttributes . NETWORK_PEER_ADDRESS , "1.2.3.4" ),
399- entry (NetworkAttributes . NETWORK_PEER_PORT , 456L ));
398+ entry (HTTP_RESPONSE_STATUS_CODE , 200L ),
399+ entry (NETWORK_PEER_ADDRESS , "1.2.3.4" ),
400+ entry (NETWORK_PEER_PORT , 456L ));
400401 }
401402
402403 @ Test
@@ -419,8 +420,8 @@ void shouldExtractProtocolNameDifferentFromHttp() {
419420 extractor .onEnd (endAttributes , Context .root (), request , response , null );
420421 assertThat (endAttributes .build ())
421422 .containsOnly (
422- entry (HttpAttributes . HTTP_RESPONSE_STATUS_CODE , 200L ),
423- entry (NetworkAttributes . NETWORK_PROTOCOL_NAME , "spdy" ),
424- entry (NetworkAttributes . NETWORK_PROTOCOL_VERSION , "3.1" ));
423+ entry (HTTP_RESPONSE_STATUS_CODE , 200L ),
424+ entry (NETWORK_PROTOCOL_NAME , "spdy" ),
425+ entry (NETWORK_PROTOCOL_VERSION , "3.1" ));
425426 }
426427}
0 commit comments