Skip to content

Log request ID and extended request ID with the requestId logger and … #2929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/next-release/bugfix-AWSSDKforJavav2-0d4ca4a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"category": "AWS SDK for Java v2",
"contributor": "",
"type": "bugfix",
"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)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,6 @@ public JsonResponseHandler(JsonProtocolUnmarshaller unmarshaller,
public T handle(SdkHttpFullResponse response, ExecutionAttributes executionAttributes) throws Exception {
SdkStandardLogger.REQUEST_LOGGER.trace(() -> "Parsing service response JSON.");

SdkStandardLogger.REQUEST_ID_LOGGER.debug(() -> X_AMZN_REQUEST_ID_HEADER + " : " +
response.firstMatchingHeader(X_AMZN_REQUEST_ID_HEADER)
.orElse("not available"));

SdkStandardLogger.REQUEST_ID_LOGGER.debug(() -> X_AMZ_ID_2_HEADER + " : " +
response.firstMatchingHeader(X_AMZ_ID_2_HEADER)
.orElse("not available"));

try {
T result = unmarshaller.unmarshall(pojoSupplier.apply(response), response);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@

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

import static software.amazon.awssdk.core.SdkStandardLogger.logRequestId;

import java.util.Optional;
import java.util.function.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.core.Response;
import software.amazon.awssdk.core.SdkPojo;
import software.amazon.awssdk.core.SdkStandardLogger;
import software.amazon.awssdk.core.exception.RetryableException;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.exception.SdkException;
Expand Down Expand Up @@ -94,6 +95,7 @@ private Response<OutputT> handleResponse(SdkHttpFullResponse httpResponse,

AwsXmlUnmarshallingContext parsedResponse = parseResponse(httpResponse, executionAttributes);
parsedResponse = decorateContextWithError.apply(parsedResponse);
logRequestId(httpResponse);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not handled by the combined response handler below?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it seems AwsXmlPredicatedResponseHandler is used by s3, and combined response handler is used by all other services. (I just learned it yesterday)


if (parsedResponse.isResponseSuccess()) {
OutputT response = handleSuccessResponse(parsedResponse);
Expand Down Expand Up @@ -128,8 +130,6 @@ private AwsXmlUnmarshallingContext parseResponse(SdkHttpFullResponse httpFullRes
*/
private OutputT handleSuccessResponse(AwsXmlUnmarshallingContext parsedResponse) {
try {
SdkStandardLogger.REQUEST_LOGGER.debug(() -> "Received successful response: "
+ parsedResponse.sdkHttpFullResponse().statusCode());
return successResponseTransformer.apply(parsedResponse);
} catch (RetryableException e) {
throw e;
Expand All @@ -154,7 +154,6 @@ private SdkException handleErrorResponse(AwsXmlUnmarshallingContext parsedRespon
try {
SdkException exception = errorResponseTransformer.apply(parsedResponse);
exception.fillInStackTrace();
SdkStandardLogger.REQUEST_LOGGER.debug(() -> "Received error response: " + exception);
return exception;
} catch (Exception e) {
String errorMessage = String.format("Unable to unmarshall error response (%s). " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@

package software.amazon.awssdk.core;

import static software.amazon.awssdk.core.http.HttpResponseHandler.X_AMZN_REQUEST_ID_HEADERS;
import static software.amazon.awssdk.core.http.HttpResponseHandler.X_AMZ_ID_2_HEADER;

import java.util.function.Supplier;
import software.amazon.awssdk.annotations.SdkProtectedApi;
import software.amazon.awssdk.http.SdkHttpResponse;
import software.amazon.awssdk.utils.Logger;
import software.amazon.awssdk.utils.http.SdkHttpUtils;

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

private SdkStandardLogger() {
}

/**
* Log the response status code and request ID
*/
public static void logRequestId(SdkHttpResponse response) {
String placeholder = "not available";
String requestId = String.format("Request ID: %s, Extended Request ID: %s",
SdkHttpUtils.firstMatchingHeaderFromCollection(response.headers(),
X_AMZN_REQUEST_ID_HEADERS)
.orElse(placeholder),
response.firstMatchingHeader(X_AMZ_ID_2_HEADER)
.orElse(placeholder));
Supplier<String> logStatement = () -> String.format("Received %s response: %s, %s",
response.isSuccessful() ? "successful" : "failed",
response.statusCode(),
requestId);
REQUEST_ID_LOGGER.debug(logStatement);
REQUEST_LOGGER.debug(logStatement);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@

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

import static software.amazon.awssdk.core.SdkStandardLogger.logRequestId;

import java.io.IOException;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.core.Response;
import software.amazon.awssdk.core.SdkStandardLogger;
import software.amazon.awssdk.core.exception.RetryableException;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.exception.SdkException;
Expand Down Expand Up @@ -68,6 +69,8 @@ private Response<OutputT> handleResponse(SdkHttpFullResponse httpResponse,
ExecutionAttributes executionAttributes)
throws IOException, InterruptedException {

logRequestId(httpResponse);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any specific reason to have picked this response handler to do the logging?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where we log the response status code currently, so I just included the request ID here


if (httpResponse.isSuccessful()) {
OutputT response = handleSuccessResponse(httpResponse, executionAttributes);
return Response.<OutputT>builder().httpResponse(httpResponse)
Expand All @@ -93,7 +96,6 @@ private Response<OutputT> handleResponse(SdkHttpFullResponse httpResponse,
private OutputT handleSuccessResponse(SdkHttpFullResponse httpResponse, ExecutionAttributes executionAttributes)
throws IOException, InterruptedException {
try {
SdkStandardLogger.REQUEST_LOGGER.debug(() -> "Received successful response: " + httpResponse.statusCode());
return successResponseHandler.handle(httpResponse, executionAttributes);
} catch (IOException | InterruptedException | RetryableException e) {
throw e;
Expand Down Expand Up @@ -121,7 +123,6 @@ private SdkException handleErrorResponse(SdkHttpFullResponse httpResponse,
try {
SdkException exception = errorResponseHandler.handle(httpResponse, executionAttributes);
exception.fillInStackTrace();
SdkStandardLogger.REQUEST_LOGGER.debug(() -> "Received error response: " + exception);
return exception;
} catch (InterruptedException | IOException e) {
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@

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

import static software.amazon.awssdk.core.SdkStandardLogger.logRequestId;

import java.nio.ByteBuffer;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
import org.reactivestreams.Publisher;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.core.Response;
import software.amazon.awssdk.core.SdkStandardLogger;
import software.amazon.awssdk.core.exception.SdkException;
import software.amazon.awssdk.core.internal.http.TransformingAsyncResponseHandler;
import software.amazon.awssdk.http.SdkHttpFullResponse;
Expand Down Expand Up @@ -51,11 +52,11 @@ public CombinedResponseAsyncHttpResponseHandler(
@Override
public void onHeaders(SdkHttpResponse response) {
headersFuture.complete(response);
logRequestId(response);

if (response.isSuccessful()) {
SdkStandardLogger.REQUEST_LOGGER.debug(() -> "Received successful response: " + response.statusCode());
successResponseHandler.onHeaders(response);
} else {
SdkStandardLogger.REQUEST_LOGGER.debug(() -> "Received error response: " + response.statusCode());
errorResponseHandler.onHeaders(response);
}
this.response.set(toFullResponse(response));
Expand Down