11package io .prometheus .client .exporter ;
22
33import java .io .BufferedWriter ;
4+ import java .io .ByteArrayOutputStream ;
45import java .io .IOException ;
6+ import java .io .InputStream ;
57import java .io .OutputStreamWriter ;
68import java .net .HttpURLConnection ;
79import 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