Skip to content

Commit 1950cff

Browse files
ibalashovbrian-brazil
authored andcommitted
Put response body in pushgateway exception (prometheus#83) (prometheus#423)
Signed-off-by: Ivan Balashov <ibalashov@gmail.com>
1 parent 9537b44 commit 1950cff

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

  • simpleclient_pushgateway/src/main/java/io/prometheus/client/exporter

simpleclient_pushgateway/src/main/java/io/prometheus/client/exporter/PushGateway.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package io.prometheus.client.exporter;
22

33
import java.io.BufferedWriter;
4+
import java.io.ByteArrayOutputStream;
45
import java.io.IOException;
6+
import java.io.InputStream;
57
import java.io.OutputStreamWriter;
68
import java.net.HttpURLConnection;
79
import java.net.InetAddress;
@@ -301,7 +303,15 @@ void doRequest(CollectorRegistry registry, String job, Map<String, String> group
301303

302304
int response = connection.getResponseCode();
303305
if (response != HttpURLConnection.HTTP_ACCEPTED) {
304-
throw new IOException("Response code from " + url + " was " + response);
306+
String errorMessage;
307+
InputStream errorStream = connection.getErrorStream();
308+
if(response >= 400 && errorStream != null) {
309+
String errBody = readFromStream(errorStream);
310+
errorMessage = "Response code from " + url + " was " + response + ", response body: " + errBody;
311+
} else {
312+
errorMessage = "Response code from " + url + " was " + response;
313+
}
314+
throw new IOException(errorMessage);
305315
}
306316
} finally {
307317
connection.disconnect();
@@ -320,4 +330,13 @@ public static Map<String, String> instanceIPGroupingKey() throws UnknownHostExce
320330
return groupingKey;
321331
}
322332

333+
private static String readFromStream(InputStream is) throws IOException {
334+
ByteArrayOutputStream result = new ByteArrayOutputStream();
335+
byte[] buffer = new byte[1024];
336+
int length;
337+
while ((length = is.read(buffer)) != -1) {
338+
result.write(buffer, 0, length);
339+
}
340+
return result.toString("UTF-8");
341+
}
323342
}

0 commit comments

Comments
 (0)