Watcher: Fix wrong logging in reporting attachment parser (elastic/x-pack-elasticsearch#1900)
The logging shows a wrong HTTP response status code from a previous request. In addition the body now also gets logged, as debugging is impossible otherwise. Original commit: elastic/x-pack-elasticsearch@cc998cd587
This commit is contained in:
parent
fd518ea020
commit
fff72256a5
|
@ -136,16 +136,18 @@ public class ReportingAttachmentParser implements EmailAttachmentParser<Reportin
|
||||||
logger.trace("Watch[{}] reporting[{}] pdf is not ready, polling in [{}] again", context.watch().id(), attachment.id(),
|
logger.trace("Watch[{}] reporting[{}] pdf is not ready, polling in [{}] again", context.watch().id(), attachment.id(),
|
||||||
TimeValue.timeValueMillis(sleepMillis));
|
TimeValue.timeValueMillis(sleepMillis));
|
||||||
} else if (response.status() >= 400) {
|
} else if (response.status() >= 400) {
|
||||||
|
String body = response.body() != null ? response.body().utf8ToString() : null;
|
||||||
throw new ElasticsearchException("Watch[{}] reporting[{}] Error when polling pdf from host[{}], port[{}], " +
|
throw new ElasticsearchException("Watch[{}] reporting[{}] Error when polling pdf from host[{}], port[{}], " +
|
||||||
"method[{}], path[{}], status[{}]", context.watch().id(), attachment.id(), request.host(), request.port(),
|
"method[{}], path[{}], status[{}], body[{}]", context.watch().id(), attachment.id(), request.host(),
|
||||||
request.method(), request.path(), reportGenerationResponse.status());
|
request.port(), request.method(), request.path(), response.status(), body);
|
||||||
} else if (response.status() == 200) {
|
} else if (response.status() == 200) {
|
||||||
return new Attachment.Bytes(attachment.id(), BytesReference.toBytes(response.body()),
|
return new Attachment.Bytes(attachment.id(), BytesReference.toBytes(response.body()),
|
||||||
response.contentType(), attachment.inline());
|
response.contentType(), attachment.inline());
|
||||||
} else {
|
} else {
|
||||||
|
String body = response.body() != null ? response.body().utf8ToString() : null;
|
||||||
String message = LoggerMessageFormat.format("", "Watch[{}] reporting[{}] Unexpected status code host[{}], port[{}], " +
|
String message = LoggerMessageFormat.format("", "Watch[{}] reporting[{}] Unexpected status code host[{}], port[{}], " +
|
||||||
"method[{}], path[{}], status[{}]", context.watch().id(), attachment.id(), request.host(), request.port(),
|
"method[{}], path[{}], status[{}], body[{}]", context.watch().id(), attachment.id(), request.host(),
|
||||||
request.method(), request.path(), reportGenerationResponse.status());
|
request.port(), request.method(), request.path(), response.status(), body);
|
||||||
throw new IllegalStateException(message);
|
throw new IllegalStateException(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -240,9 +240,10 @@ public class ReportingAttachmentParserTests extends ESTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testPollingRequestIsError() throws Exception {
|
public void testPollingRequestIsError() throws Exception {
|
||||||
|
boolean hasBody = randomBoolean();
|
||||||
when(httpClient.execute(any(HttpRequest.class)))
|
when(httpClient.execute(any(HttpRequest.class)))
|
||||||
.thenReturn(new HttpResponse(200, "{\"path\":\"whatever\"}"))
|
.thenReturn(new HttpResponse(200, "{\"path\":\"whatever\"}"))
|
||||||
.thenReturn(new HttpResponse(403));
|
.thenReturn(new HttpResponse(403, hasBody ? "no permissions" : null));
|
||||||
|
|
||||||
ReportingAttachment attachment =
|
ReportingAttachment attachment =
|
||||||
new ReportingAttachment("foo", "http://www.example.org/", randomBoolean(), TimeValue.timeValueMillis(1), 10, null, null);
|
new ReportingAttachment("foo", "http://www.example.org/", randomBoolean(), TimeValue.timeValueMillis(1), 10, null, null);
|
||||||
|
@ -250,6 +251,9 @@ public class ReportingAttachmentParserTests extends ESTestCase {
|
||||||
ElasticsearchException e = expectThrows(ElasticsearchException.class,
|
ElasticsearchException e = expectThrows(ElasticsearchException.class,
|
||||||
() -> reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment));
|
() -> reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment));
|
||||||
assertThat(e.getMessage(), containsString("Error when polling pdf"));
|
assertThat(e.getMessage(), containsString("Error when polling pdf"));
|
||||||
|
if (hasBody) {
|
||||||
|
assertThat(e.getMessage(), containsString("body[no permissions]"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testPollingRequestRetryIsExceeded() throws Exception {
|
public void testPollingRequestRetryIsExceeded() throws Exception {
|
||||||
|
|
Loading…
Reference in New Issue