Skip to content

Commit 1a2dbeb

Browse files
authored
Let Prometheus exporter plugin support utf8 characters (#8228)
1 parent b7835d0 commit 1a2dbeb

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

plugins/integrations/prometheus/src/main/java/org/apache/cloudstack/metrics/PrometheusExporterServerImpl.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.io.IOException;
2929
import java.io.OutputStream;
3030
import java.net.InetSocketAddress;
31+
import java.nio.charset.StandardCharsets;
3132
import java.util.Arrays;
3233

3334
public class PrometheusExporterServerImpl extends ManagerBase implements PrometheusExporterServer, Configurable {
@@ -57,11 +58,21 @@ public void handle(final HttpExchange httpExchange) throws IOException {
5758
response = prometheusExporter.getMetrics();
5859
responseCode = 200;
5960
}
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);
6264
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+
}
6576
}
6677
}
6778

0 commit comments

Comments
 (0)