]> granicus.if.org Git - icinga2/commitdiff
Elasticsearch: Improve error handling/logging 7027/head
authorMichael Friedrich <michael.friedrich@icinga.com>
Mon, 18 Mar 2019 15:41:01 +0000 (16:41 +0100)
committerMichael Friedrich <michael.friedrich@icinga.com>
Mon, 18 Mar 2019 15:41:01 +0000 (16:41 +0100)
- Missing content-type
- Wrong content-type shouldn't hide the error message
- Error message should include more details (request URL, etc.)

lib/perfdata/elasticsearchwriter.cpp

index e66a790769d79fce7301419ba624d24f7f999c07..f1a84a5af517c9c2ca34d140529c8a55478cf1d3 100644 (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;
        }