6
6
package io .opentelemetry .instrumentation .api .semconv .http ;
7
7
8
8
import 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 ;
9
21
import static java .util .Arrays .asList ;
10
22
import static java .util .Collections .emptyList ;
11
23
import static java .util .Collections .emptyMap ;
18
30
import io .opentelemetry .context .Context ;
19
31
import io .opentelemetry .instrumentation .api .instrumenter .AttributesExtractor ;
20
32
import 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 ;
26
33
import java .net .ConnectException ;
27
34
import java .util .HashMap ;
28
35
import java .util .HashSet ;
@@ -171,26 +178,26 @@ void normal() {
171
178
extractor .onStart (startAttributes , Context .root (), request );
172
179
assertThat (startAttributes .build ())
173
180
.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" ),
176
183
entry (
177
184
AttributeKey .stringArrayKey ("http.request.header.custom-request-header" ),
178
185
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 ));
182
189
183
190
AttributesBuilder endAttributes = Attributes .builder ();
184
191
extractor .onEnd (endAttributes , Context .root (), request , response , null );
185
192
assertThat (endAttributes .build ())
186
193
.containsOnly (
187
- entry (HttpAttributes . HTTP_RESPONSE_STATUS_CODE , 202L ),
194
+ entry (HTTP_RESPONSE_STATUS_CODE , 202L ),
188
195
entry (
189
196
AttributeKey .stringArrayKey ("http.response.header.custom-response-header" ),
190
197
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 ));
194
201
}
195
202
196
203
@ ParameterizedTest
@@ -207,8 +214,8 @@ void shouldExtractKnownMethods(String requestMethod) {
207
214
extractor .onEnd (attributes , Context .root (), request , emptyMap (), null );
208
215
209
216
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 );
212
219
}
213
220
214
221
@ ParameterizedTest
@@ -225,8 +232,8 @@ void shouldTreatMethodsAsCaseSensitive(String requestMethod) {
225
232
extractor .onEnd (attributes , Context .root (), request , emptyMap (), null );
226
233
227
234
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 );
230
237
}
231
238
232
239
@ ParameterizedTest
@@ -243,8 +250,8 @@ void shouldUseOtherForUnknownMethods(String requestMethod) {
243
250
extractor .onEnd (attributes , Context .root (), request , emptyMap (), null );
244
251
245
252
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 );
248
255
}
249
256
250
257
@ ParameterizedTest
@@ -263,8 +270,8 @@ void shouldExtractKnownMethods_override(String requestMethod) {
263
270
extractor .onEnd (attributes , Context .root (), request , emptyMap (), null );
264
271
265
272
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 );
268
275
}
269
276
270
277
@ ParameterizedTest
@@ -283,8 +290,8 @@ void shouldUseOtherForUnknownMethods_override(String requestMethod) {
283
290
extractor .onEnd (attributes , Context .root (), request , emptyMap (), null );
284
291
285
292
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 );
288
295
}
289
296
290
297
@ Test
@@ -300,8 +307,8 @@ void shouldExtractErrorType_httpStatusCode() {
300
307
extractor .onEnd (attributes , Context .root (), emptyMap (), response , null );
301
308
302
309
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" );
305
312
}
306
313
307
314
@ Test
@@ -317,7 +324,7 @@ void shouldExtractErrorType_getter() {
317
324
extractor .onStart (attributes , Context .root (), emptyMap ());
318
325
extractor .onEnd (attributes , Context .root (), request , emptyMap (), null );
319
326
320
- assertThat (attributes .build ()).containsEntry (ErrorAttributes . ERROR_TYPE , "custom error type" );
327
+ assertThat (attributes .build ()).containsEntry (ERROR_TYPE , "custom error type" );
321
328
}
322
329
323
330
@ Test
@@ -329,8 +336,7 @@ void shouldExtractErrorType_exceptionClassName() {
329
336
extractor .onStart (attributes , Context .root (), emptyMap ());
330
337
extractor .onEnd (attributes , Context .root (), emptyMap (), emptyMap (), new ConnectException ());
331
338
332
- assertThat (attributes .build ())
333
- .containsEntry (ErrorAttributes .ERROR_TYPE , "java.net.ConnectException" );
339
+ assertThat (attributes .build ()).containsEntry (ERROR_TYPE , "java.net.ConnectException" );
334
340
}
335
341
336
342
@ Test
@@ -342,7 +348,7 @@ void shouldExtractErrorType_other() {
342
348
extractor .onStart (attributes , Context .root (), emptyMap ());
343
349
extractor .onEnd (attributes , Context .root (), emptyMap (), emptyMap (), null );
344
350
345
- assertThat (attributes .build ()).containsEntry (ErrorAttributes . ERROR_TYPE , HttpConstants ._OTHER );
351
+ assertThat (attributes .build ()).containsEntry (ERROR_TYPE , HttpConstants ._OTHER );
346
352
}
347
353
348
354
@ Test
@@ -359,14 +365,11 @@ void shouldExtractServerAddressAndPortFromHostHeader() {
359
365
AttributesBuilder startAttributes = Attributes .builder ();
360
366
extractor .onStart (startAttributes , Context .root (), request );
361
367
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 ));
365
369
366
370
AttributesBuilder endAttributes = Attributes .builder ();
367
371
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 ));
370
373
}
371
374
372
375
@ Test
@@ -386,17 +389,15 @@ void shouldExtractPeerAddressEvenIfItDuplicatesServerAddress() {
386
389
AttributesBuilder startAttributes = Attributes .builder ();
387
390
extractor .onStart (startAttributes , Context .root (), request );
388
391
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 ));
392
393
393
394
AttributesBuilder endAttributes = Attributes .builder ();
394
395
extractor .onEnd (endAttributes , Context .root (), request , response , null );
395
396
assertThat (endAttributes .build ())
396
397
.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 ));
400
401
}
401
402
402
403
@ Test
@@ -419,8 +420,8 @@ void shouldExtractProtocolNameDifferentFromHttp() {
419
420
extractor .onEnd (endAttributes , Context .root (), request , response , null );
420
421
assertThat (endAttributes .build ())
421
422
.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" ));
425
426
}
426
427
}
0 commit comments