Skip to content

Commit 4bdae6e

Browse files
authored
Log response payload for better troubleshooting (#320)
1 parent c2bc996 commit 4bdae6e

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/main/java/software/amazon/cloudformation/AbstractWrapper.java

+24-2
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public void processRequest(final InputStream inputStream, final OutputStream out
231231
} finally {
232232
// A response will be output on all paths, though CloudFormation will
233233
// not block on invoking the handlers, but rather listen for callbacks
234-
writeResponse(outputStream, handlerResponse);
234+
writeResponse(outputStream, handlerResponse, request);
235235
publishExceptionCodeAndCountMetrics(request == null ? null : request.getAction(), handlerResponse.getErrorCode());
236236
}
237237
}
@@ -372,7 +372,9 @@ public void processRequest(final InputStream inputStream, final OutputStream out
372372

373373
}
374374

375-
protected void writeResponse(final OutputStream outputStream, final ProgressEvent<ResourceT, CallbackT> response)
375+
protected void writeResponse(final OutputStream outputStream,
376+
final ProgressEvent<ResourceT, CallbackT> response,
377+
final HandlerRequest<ResourceT, CallbackT> request)
376378
throws IOException {
377379
if (response.getResourceModel() != null) {
378380
// strip write only properties on final results, we will need the intact model
@@ -383,10 +385,21 @@ protected void writeResponse(final OutputStream outputStream, final ProgressEven
383385
}
384386

385387
String output = this.serializer.serialize(response);
388+
if (response.getStatus() != OperationStatus.IN_PROGRESS) {
389+
// if status is not IN_PROGRESS, it means the response has been sanitized
390+
final String logicalResourceId = getLogicalResourceId(request);
391+
final String stackId = getStackId(request);
392+
this.log(String.format("StackId=%s LogicalResourceId=%s Response=%s", stackId, logicalResourceId, output));
393+
}
386394
outputStream.write(output.getBytes(StandardCharsets.UTF_8));
387395
outputStream.flush();
388396
}
389397

398+
protected void writeResponse(final OutputStream outputStream, final ProgressEvent<ResourceT, CallbackT> response)
399+
throws IOException {
400+
this.writeResponse(outputStream, response, null);
401+
}
402+
390403
protected ResourceT sanitizeModel(final ResourceT model) throws IOException {
391404
// strip write only properties on final results, we will need the intact model
392405
// while provisioning
@@ -570,6 +583,15 @@ protected String getStackId(final HandlerRequest<ResourceT, CallbackT> request)
570583
return null;
571584
}
572585

586+
@VisibleForTesting
587+
protected String getLogicalResourceId(final HandlerRequest<ResourceT, CallbackT> request) {
588+
if (request != null && request.getRequestData() != null) {
589+
return request.getRequestData().getLogicalResourceId();
590+
}
591+
592+
return null;
593+
}
594+
573595
private void replaceInMap(final Map<String, String> targetMap, final Map<String, String> sourceMap) {
574596
if (targetMap == null) {
575597
return;

src/test/java/software/amazon/cloudformation/WrapperTest.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void invokeHandler_nullResponse_returnsFailure(final String requestDataPa
160160
}
161161

162162
verify(providerEventsLogger).refreshClient();
163-
verify(providerEventsLogger, times(2)).publishLogEvent(any());
163+
verify(providerEventsLogger, times(3)).publishLogEvent(any());
164164
verifyNoMoreInteractions(providerEventsLogger);
165165

166166
// verify output response
@@ -1029,4 +1029,14 @@ public void getStackId_setAndGetStackId() {
10291029
assertThat(stackId).isNotNull();
10301030
assertThat(stackId).isEqualTo("AWSStackId");
10311031
}
1032+
1033+
@Test
1034+
public void getLogicalResourceId_setAndGetLogicalResourceId() {
1035+
final HandlerRequest<TestModel, TestContext> request = new HandlerRequest<>();
1036+
final RequestData<TestModel> requestData = new RequestData<>();
1037+
requestData.setLogicalResourceId("MyResource");
1038+
request.setRequestData(requestData);
1039+
1040+
assertThat(wrapper.getLogicalResourceId(request)).isEqualTo("MyResource");
1041+
}
10321042
}

0 commit comments

Comments
 (0)