16
16
import io .opentelemetry .contrib .awsxray .propagator .AwsXrayPropagator ;
17
17
import io .opentelemetry .instrumentation .api .instrumenter .Instrumenter ;
18
18
import io .opentelemetry .semconv .SemanticAttributes ;
19
- import java .io .IOException ;
19
+ import java .io .BufferedReader ;
20
20
import java .io .InputStream ;
21
- import java .nio .charset .StandardCharsets ;
21
+ import java .io .InputStreamReader ;
22
+ import java .nio .charset .Charset ;
22
23
import java .util .List ;
23
24
import java .util .Optional ;
24
- import java .util .logging .Level ;
25
- import java .util .logging .Logger ;
25
+ import java .util .stream .Collectors ;
26
26
import javax .annotation .Nullable ;
27
- import org .apache .commons .io .IOUtils ;
28
27
import software .amazon .awssdk .auth .signer .AwsSignerExecutionAttribute ;
29
28
import software .amazon .awssdk .awscore .AwsResponse ;
30
29
import software .amazon .awssdk .core .ClientType ;
@@ -57,7 +56,6 @@ final class TracingExecutionInterceptor implements ExecutionInterceptor {
57
56
private final Instrumenter <ExecutionAttributes , SdkHttpResponse > requestInstrumenter ;
58
57
private final Instrumenter <ExecutionAttributes , SdkHttpResponse > consumerInstrumenter ;
59
58
private final boolean captureExperimentalSpanAttributes ;
60
- private static final Logger logger = Logger .getLogger (PluginImplUtil .class .getName ());
61
59
62
60
static final AttributeKey <String > HTTP_ERROR_MSG =
63
61
AttributeKey .stringKey ("aws.http.error_message" );
@@ -308,10 +306,13 @@ private void onSdkResponse(
308
306
@ Override
309
307
public void afterTransmission (
310
308
Context .AfterTransmission context , ExecutionAttributes executionAttributes ) {
311
- if (! recordIndividualHttpError ) {
312
- return ;
309
+ if (recordIndividualHttpError ) {
310
+ extractHttpErrorAsEvent ( context , executionAttributes ) ;
313
311
}
312
+ }
314
313
314
+ private static void extractHttpErrorAsEvent (
315
+ Context .AfterTransmission context , ExecutionAttributes executionAttributes ) {
315
316
io .opentelemetry .context .Context otelContext = getContext (executionAttributes );
316
317
if (otelContext != null ) {
317
318
Span span = Span .fromContext (otelContext );
@@ -322,18 +323,18 @@ public void afterTransmission(
322
323
// we want to record the error message from http response
323
324
Optional <InputStream > responseBody = context .responseBody ();
324
325
if (responseBody .isPresent ()) {
325
- try {
326
- String errorMsg = IOUtils . toString ( responseBody . get (), StandardCharsets . UTF_8 );
327
- Attributes attributes =
328
- Attributes . of (
329
- SemanticAttributes . HTTP_RESPONSE_STATUS_CODE ,
330
- Long . valueOf ( errorCode ),
331
- HTTP_ERROR_MSG ,
332
- errorMsg );
333
- span . addEvent ( HTTP_FAILURE_EVENT , attributes );
334
- } catch ( IOException ex ) {
335
- logger . log ( Level . FINE , "Failed to read the response" , ex );
336
- }
326
+ String errorMsg =
327
+ new BufferedReader (
328
+ new InputStreamReader ( responseBody . get (), Charset . defaultCharset ()))
329
+ . lines ()
330
+ . collect ( Collectors . joining ( " \n " ));
331
+ Attributes attributes =
332
+ Attributes . of (
333
+ SemanticAttributes . HTTP_RESPONSE_STATUS_CODE ,
334
+ Long . valueOf ( errorCode ),
335
+ HTTP_ERROR_MSG ,
336
+ errorMsg );
337
+ span . addEvent ( HTTP_FAILURE_EVENT , attributes );
337
338
}
338
339
}
339
340
}
0 commit comments