|
28 | 28 | import java.io.IOException; |
29 | 29 | import java.io.OutputStream; |
30 | 30 | import java.net.InetSocketAddress; |
| 31 | +import java.nio.charset.StandardCharsets; |
31 | 32 | import java.util.Arrays; |
32 | 33 |
|
33 | 34 | public class PrometheusExporterServerImpl extends ManagerBase implements PrometheusExporterServer, Configurable { |
@@ -57,11 +58,21 @@ public void handle(final HttpExchange httpExchange) throws IOException { |
57 | 58 | response = prometheusExporter.getMetrics(); |
58 | 59 | responseCode = 200; |
59 | 60 | } |
60 | | - httpExchange.getResponseHeaders().set("content-type", "text/plain"); |
61 | | - httpExchange.sendResponseHeaders(responseCode, response.length()); |
| 61 | + byte[] bytesToOutput = response.getBytes(StandardCharsets.UTF_8); |
| 62 | + httpExchange.getResponseHeaders().set("content-type", "text/plain; charset=UTF-8"); |
| 63 | + httpExchange.sendResponseHeaders(responseCode, bytesToOutput.length); |
62 | 64 | final OutputStream os = httpExchange.getResponseBody(); |
63 | | - os.write(response.getBytes()); |
64 | | - os.close(); |
| 65 | + try { |
| 66 | + os.write(bytesToOutput); |
| 67 | + } catch (IOException e) { |
| 68 | + LOG.error(String.format("could not export Prometheus data due to %s", e.getLocalizedMessage())); |
| 69 | + if (LOG.isDebugEnabled()) { |
| 70 | + LOG.debug("Error during Prometheus export: ", e); |
| 71 | + } |
| 72 | + os.write("The system could not export Prometheus due to an internal error. Contact your operator to learn about the reason.".getBytes()); |
| 73 | + } finally { |
| 74 | + os.close(); |
| 75 | + } |
65 | 76 | } |
66 | 77 | } |
67 | 78 |
|
|
0 commit comments