Skip to content

Commit 759be4e

Browse files
committed
Log request ID and extended request ID with the requestId logger and request logger
1 parent dbe8b17 commit 759be4e

File tree

6 files changed

+42
-18
lines changed

6 files changed

+42
-18
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"category": "AWS SDK for Java v2",
3+
"contributor": "",
4+
"type": "bugfix",
5+
"description": "Log request ID and extended request ID with the request logger and requestId logger. See [#2876](https://github.com/aws/aws-sdk-java-v2/issues/2876)"
6+
}

core/protocols/aws-json-protocol/src/main/java/software/amazon/awssdk/protocols/json/internal/unmarshall/JsonResponseHandler.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,6 @@ public JsonResponseHandler(JsonProtocolUnmarshaller unmarshaller,
7171
public T handle(SdkHttpFullResponse response, ExecutionAttributes executionAttributes) throws Exception {
7272
SdkStandardLogger.REQUEST_LOGGER.trace(() -> "Parsing service response JSON.");
7373

74-
SdkStandardLogger.REQUEST_ID_LOGGER.debug(() -> X_AMZN_REQUEST_ID_HEADER + " : " +
75-
response.firstMatchingHeader(X_AMZN_REQUEST_ID_HEADER)
76-
.orElse("not available"));
77-
78-
SdkStandardLogger.REQUEST_ID_LOGGER.debug(() -> X_AMZ_ID_2_HEADER + " : " +
79-
response.firstMatchingHeader(X_AMZ_ID_2_HEADER)
80-
.orElse("not available"));
81-
8274
try {
8375
T result = unmarshaller.unmarshall(pojoSupplier.apply(response), response);
8476

core/protocols/aws-xml-protocol/src/main/java/software/amazon/awssdk/protocols/xml/internal/unmarshall/AwsXmlPredicatedResponseHandler.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515

1616
package software.amazon.awssdk.protocols.xml.internal.unmarshall;
1717

18+
import static software.amazon.awssdk.core.SdkStandardLogger.logRequestId;
19+
1820
import java.util.Optional;
1921
import java.util.function.Function;
2022
import org.slf4j.Logger;
2123
import org.slf4j.LoggerFactory;
2224
import software.amazon.awssdk.annotations.SdkInternalApi;
2325
import software.amazon.awssdk.core.Response;
2426
import software.amazon.awssdk.core.SdkPojo;
25-
import software.amazon.awssdk.core.SdkStandardLogger;
2627
import software.amazon.awssdk.core.exception.RetryableException;
2728
import software.amazon.awssdk.core.exception.SdkClientException;
2829
import software.amazon.awssdk.core.exception.SdkException;
@@ -94,6 +95,7 @@ private Response<OutputT> handleResponse(SdkHttpFullResponse httpResponse,
9495

9596
AwsXmlUnmarshallingContext parsedResponse = parseResponse(httpResponse, executionAttributes);
9697
parsedResponse = decorateContextWithError.apply(parsedResponse);
98+
logRequestId(httpResponse);
9799

98100
if (parsedResponse.isResponseSuccess()) {
99101
OutputT response = handleSuccessResponse(parsedResponse);
@@ -128,8 +130,6 @@ private AwsXmlUnmarshallingContext parseResponse(SdkHttpFullResponse httpFullRes
128130
*/
129131
private OutputT handleSuccessResponse(AwsXmlUnmarshallingContext parsedResponse) {
130132
try {
131-
SdkStandardLogger.REQUEST_LOGGER.debug(() -> "Received successful response: "
132-
+ parsedResponse.sdkHttpFullResponse().statusCode());
133133
return successResponseTransformer.apply(parsedResponse);
134134
} catch (RetryableException e) {
135135
throw e;
@@ -154,7 +154,6 @@ private SdkException handleErrorResponse(AwsXmlUnmarshallingContext parsedRespon
154154
try {
155155
SdkException exception = errorResponseTransformer.apply(parsedResponse);
156156
exception.fillInStackTrace();
157-
SdkStandardLogger.REQUEST_LOGGER.debug(() -> "Received error response: " + exception);
158157
return exception;
159158
} catch (Exception e) {
160159
String errorMessage = String.format("Unable to unmarshall error response (%s). " +

core/sdk-core/src/main/java/software/amazon/awssdk/core/SdkStandardLogger.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@
1515

1616
package software.amazon.awssdk.core;
1717

18+
import static software.amazon.awssdk.core.http.HttpResponseHandler.X_AMZN_REQUEST_ID_HEADERS;
19+
import static software.amazon.awssdk.core.http.HttpResponseHandler.X_AMZ_ID_2_HEADER;
20+
21+
import java.util.function.Supplier;
1822
import software.amazon.awssdk.annotations.SdkProtectedApi;
23+
import software.amazon.awssdk.http.SdkHttpResponse;
1924
import software.amazon.awssdk.utils.Logger;
25+
import software.amazon.awssdk.utils.http.SdkHttpUtils;
2026

2127
/**
2228
* A centralized set of loggers that used across the SDK to log particular types of events. SDK users can then specifically enable
@@ -38,4 +44,23 @@ public final class SdkStandardLogger {
3844

3945
private SdkStandardLogger() {
4046
}
47+
48+
/**
49+
* Log the response status code and request ID
50+
*/
51+
public static void logRequestId(SdkHttpResponse response) {
52+
String placeholder = "not available";
53+
String requestId = String.format("Request ID: %s, Extended Request ID: %s",
54+
SdkHttpUtils.firstMatchingHeaderFromCollection(response.headers(),
55+
X_AMZN_REQUEST_ID_HEADERS)
56+
.orElse(placeholder),
57+
response.firstMatchingHeader(X_AMZ_ID_2_HEADER)
58+
.orElse(placeholder));
59+
Supplier<String> logStatement = () -> String.format("Received %s response: %s, %s",
60+
response.isSuccessful() ? "successful" : "failed",
61+
response.statusCode(),
62+
requestId);
63+
REQUEST_ID_LOGGER.debug(logStatement);
64+
REQUEST_LOGGER.debug(logStatement);
65+
}
4166
}

core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/http/CombinedResponseHandler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515

1616
package software.amazon.awssdk.core.internal.http;
1717

18+
import static software.amazon.awssdk.core.SdkStandardLogger.logRequestId;
19+
1820
import java.io.IOException;
1921
import java.util.Optional;
2022
import org.slf4j.Logger;
2123
import org.slf4j.LoggerFactory;
2224
import software.amazon.awssdk.annotations.SdkInternalApi;
2325
import software.amazon.awssdk.core.Response;
24-
import software.amazon.awssdk.core.SdkStandardLogger;
2526
import software.amazon.awssdk.core.exception.RetryableException;
2627
import software.amazon.awssdk.core.exception.SdkClientException;
2728
import software.amazon.awssdk.core.exception.SdkException;
@@ -68,6 +69,8 @@ private Response<OutputT> handleResponse(SdkHttpFullResponse httpResponse,
6869
ExecutionAttributes executionAttributes)
6970
throws IOException, InterruptedException {
7071

72+
logRequestId(httpResponse);
73+
7174
if (httpResponse.isSuccessful()) {
7275
OutputT response = handleSuccessResponse(httpResponse, executionAttributes);
7376
return Response.<OutputT>builder().httpResponse(httpResponse)
@@ -93,7 +96,6 @@ private Response<OutputT> handleResponse(SdkHttpFullResponse httpResponse,
9396
private OutputT handleSuccessResponse(SdkHttpFullResponse httpResponse, ExecutionAttributes executionAttributes)
9497
throws IOException, InterruptedException {
9598
try {
96-
SdkStandardLogger.REQUEST_LOGGER.debug(() -> "Received successful response: " + httpResponse.statusCode());
9799
return successResponseHandler.handle(httpResponse, executionAttributes);
98100
} catch (IOException | InterruptedException | RetryableException e) {
99101
throw e;
@@ -121,7 +123,6 @@ private SdkException handleErrorResponse(SdkHttpFullResponse httpResponse,
121123
try {
122124
SdkException exception = errorResponseHandler.handle(httpResponse, executionAttributes);
123125
exception.fillInStackTrace();
124-
SdkStandardLogger.REQUEST_LOGGER.debug(() -> "Received error response: " + exception);
125126
return exception;
126127
} catch (InterruptedException | IOException e) {
127128
throw e;

core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/http/async/CombinedResponseAsyncHttpResponseHandler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515

1616
package software.amazon.awssdk.core.internal.http.async;
1717

18+
import static software.amazon.awssdk.core.SdkStandardLogger.logRequestId;
19+
1820
import java.nio.ByteBuffer;
1921
import java.util.concurrent.CompletableFuture;
2022
import java.util.concurrent.atomic.AtomicReference;
2123
import org.reactivestreams.Publisher;
2224
import software.amazon.awssdk.annotations.SdkInternalApi;
2325
import software.amazon.awssdk.core.Response;
24-
import software.amazon.awssdk.core.SdkStandardLogger;
2526
import software.amazon.awssdk.core.exception.SdkException;
2627
import software.amazon.awssdk.core.internal.http.TransformingAsyncResponseHandler;
2728
import software.amazon.awssdk.http.SdkHttpFullResponse;
@@ -51,11 +52,11 @@ public CombinedResponseAsyncHttpResponseHandler(
5152
@Override
5253
public void onHeaders(SdkHttpResponse response) {
5354
headersFuture.complete(response);
55+
logRequestId(response);
56+
5457
if (response.isSuccessful()) {
55-
SdkStandardLogger.REQUEST_LOGGER.debug(() -> "Received successful response: " + response.statusCode());
5658
successResponseHandler.onHeaders(response);
5759
} else {
58-
SdkStandardLogger.REQUEST_LOGGER.debug(() -> "Received error response: " + response.statusCode());
5960
errorResponseHandler.onHeaders(response);
6061
}
6162
this.response.set(toFullResponse(response));

0 commit comments

Comments
 (0)