Skip to content

Commit 91a5f2c

Browse files
committed
remove dependency on 'commons-io:commons-io' and refactor
1 parent 34a7171 commit 91a5f2c

File tree

3 files changed

+21
-25
lines changed

3 files changed

+21
-25
lines changed

instrumentation/aws-sdk/aws-sdk-2.2/javaagent/build.gradle.kts

-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ muzzle {
1010
// Used by all SDK services, the only case it isn't is an SDK extension such as a custom HTTP
1111
// client, which is not target of instrumentation anyways.
1212
extraDependency("software.amazon.awssdk:protocol-core")
13-
extraDependency("commons-io:commons-io:2.13.0")
1413

1514
excludeInstrumentationName("aws-sdk-2.2-sqs")
1615
excludeInstrumentationName("aws-sdk-2.2-sns")
@@ -26,7 +25,6 @@ muzzle {
2625
// Used by all SDK services, the only case it isn't is an SDK extension such as a custom HTTP
2726
// client, which is not target of instrumentation anyways.
2827
extraDependency("software.amazon.awssdk:protocol-core")
29-
extraDependency("commons-io:commons-io:2.13.0")
3028

3129
// "fail" asserts that *all* the instrumentation modules fail to load, but the core one is
3230
// actually expected to succeed, so exclude it from checks.
@@ -43,7 +41,6 @@ muzzle {
4341
// Used by all SDK services, the only case it isn't is an SDK extension such as a custom HTTP
4442
// client, which is not target of instrumentation anyways.
4543
extraDependency("software.amazon.awssdk:protocol-core")
46-
extraDependency("commons-io:commons-io:2.13.0")
4744

4845
excludeInstrumentationName("aws-sdk-2.2-sns")
4946

@@ -58,7 +55,6 @@ muzzle {
5855
// Used by all SDK services, the only case it isn't is an SDK extension such as a custom HTTP
5956
// client, which is not target of instrumentation anyways.
6057
extraDependency("software.amazon.awssdk:protocol-core")
61-
extraDependency("commons-io:commons-io:2.13.0")
6258

6359
excludeInstrumentationName("aws-sdk-2.2-sqs")
6460

instrumentation/aws-sdk/aws-sdk-2.2/library/build.gradle.kts

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ plugins {
44

55
dependencies {
66
implementation("io.opentelemetry.contrib:opentelemetry-aws-xray-propagator")
7-
implementation("commons-io:commons-io")
87

98
library("software.amazon.awssdk:aws-core:2.2.0")
109
library("software.amazon.awssdk:sqs:2.2.0")

instrumentation/aws-sdk/aws-sdk-2.2/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v2_2/TracingExecutionInterceptor.java

+21-20
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616
import io.opentelemetry.contrib.awsxray.propagator.AwsXrayPropagator;
1717
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
1818
import io.opentelemetry.semconv.SemanticAttributes;
19-
import java.io.IOException;
19+
import java.io.BufferedReader;
2020
import java.io.InputStream;
21-
import java.nio.charset.StandardCharsets;
21+
import java.io.InputStreamReader;
22+
import java.nio.charset.Charset;
2223
import java.util.List;
2324
import java.util.Optional;
24-
import java.util.logging.Level;
25-
import java.util.logging.Logger;
25+
import java.util.stream.Collectors;
2626
import javax.annotation.Nullable;
27-
import org.apache.commons.io.IOUtils;
2827
import software.amazon.awssdk.auth.signer.AwsSignerExecutionAttribute;
2928
import software.amazon.awssdk.awscore.AwsResponse;
3029
import software.amazon.awssdk.core.ClientType;
@@ -57,7 +56,6 @@ final class TracingExecutionInterceptor implements ExecutionInterceptor {
5756
private final Instrumenter<ExecutionAttributes, SdkHttpResponse> requestInstrumenter;
5857
private final Instrumenter<ExecutionAttributes, SdkHttpResponse> consumerInstrumenter;
5958
private final boolean captureExperimentalSpanAttributes;
60-
private static final Logger logger = Logger.getLogger(PluginImplUtil.class.getName());
6159

6260
static final AttributeKey<String> HTTP_ERROR_MSG =
6361
AttributeKey.stringKey("aws.http.error_message");
@@ -308,10 +306,13 @@ private void onSdkResponse(
308306
@Override
309307
public void afterTransmission(
310308
Context.AfterTransmission context, ExecutionAttributes executionAttributes) {
311-
if (!recordIndividualHttpError) {
312-
return;
309+
if (recordIndividualHttpError) {
310+
extractHttpErrorAsEvent(context, executionAttributes);
313311
}
312+
}
314313

314+
private static void extractHttpErrorAsEvent(
315+
Context.AfterTransmission context, ExecutionAttributes executionAttributes) {
315316
io.opentelemetry.context.Context otelContext = getContext(executionAttributes);
316317
if (otelContext != null) {
317318
Span span = Span.fromContext(otelContext);
@@ -322,18 +323,18 @@ public void afterTransmission(
322323
// we want to record the error message from http response
323324
Optional<InputStream> responseBody = context.responseBody();
324325
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);
337338
}
338339
}
339340
}

0 commit comments

Comments
 (0)