Merge pull request #7027 from Icinga/feature/es-improve-error-handling

Elasticsearch: Improve error handling/logging
This commit is contained in:
Michael Friedrich 2019-03-19 08:35:16 +01:00 committed by GitHub
commit 9f91ab98b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 7 deletions

View File

@ -512,15 +512,13 @@ void ElasticsearchWriter::SendRequest(const String& body)
return;
}
Log(LogWarning, "ElasticsearchWriter")
<< "Unexpected response code " << resp.StatusCode;
std::ostringstream msgbuf;
msgbuf << "Unexpected response code " << resp.StatusCode << " from URL '" << req.RequestUrl->Format() << "'";
String contentType = resp.Headers->Get("content-type");
if (contentType != "application/json") {
Log(LogWarning, "ElasticsearchWriter")
<< "Unexpected Content-Type: " << contentType;
return;
if (contentType != "application/json" && contentType != "application/json; charset=utf-8") {
msgbuf << "; Unexpected Content-Type: '" << contentType << "'";
}
size_t responseSize = resp.GetBodySize();
@ -528,6 +526,11 @@ void ElasticsearchWriter::SendRequest(const String& body)
resp.ReadBody(buffer.get(), responseSize);
buffer.get()[responseSize] = '\0';
#ifdef I2_DEBUG
msgbuf << "; Response body: '" << buffer.get() << "'";
#endif /* I2_DEBUG */
/* {"statusCode":404,"error":"Not Found","message":"Not Found"} */
Dictionary::Ptr jsonResponse;
try {
jsonResponse = JsonDecode(buffer.get());
@ -540,7 +543,7 @@ void ElasticsearchWriter::SendRequest(const String& body)
String error = jsonResponse->Get("error");
Log(LogCritical, "ElasticsearchWriter")
<< "Elasticsearch error message:\n" << error;
<< "Error: '" << error << "'. " << msgbuf.str();
return;
}