Skip to content

Commit 01e1e9e

Browse files
authored
Specify the charset of HTTPServer response (#564)
* Update to specify the charset of response In some Operational Systems (Like IBM Z/OS and IBM Z/Unix) the JRE runs with a charset different of UTF-8, those default charsets produce wrong behavior and truncated responses. To avoid this kind of situations I put the specifc charset in the output writer. This create a consistent response between different platforms. Signed-off-by: João Paulo Binda Delboni <[email protected]> * Specify the charset of Graphite Request To prevent problems with encoding in some Operational Systems we set the charset of Graphite Charset. Signed-off-by: João Paulo Binda Delboni <[email protected]>
1 parent 570f8bb commit 01e1e9e

File tree

2 files changed

+5
-2
lines changed
  • simpleclient_graphite_bridge/src/main/java/io/prometheus/client/bridge
  • simpleclient_httpserver/src/main/java/io/prometheus/client/exporter

2 files changed

+5
-2
lines changed

simpleclient_graphite_bridge/src/main/java/io/prometheus/client/bridge/Graphite.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
import java.io.BufferedWriter;
77
import java.io.IOException;
8+
import java.io.OutputStreamWriter;
89
import java.io.PrintWriter;
910
import java.net.Socket;
11+
import java.nio.charset.Charset;
1012
import java.util.Collections;
1113
import java.util.logging.Level;
1214
import java.util.logging.Logger;
@@ -50,7 +52,7 @@ public Graphite(String host, int port) {
5052
*/
5153
public void push(CollectorRegistry registry) throws IOException {
5254
Socket s = new Socket(host, port);
53-
BufferedWriter writer = new BufferedWriter(new PrintWriter(s.getOutputStream()));
55+
BufferedWriter writer = new BufferedWriter(new PrintWriter(new OutputStreamWriter(s.getOutputStream(), Charset.forName("UTF-8"))));
5456
Matcher m = INVALID_GRAPHITE_CHARS.matcher("");
5557
long now = System.currentTimeMillis() / 1000;
5658
for (Collector.MetricFamilySamples metricFamilySamples: Collections.list(registry.metricFamilySamples())) {

simpleclient_httpserver/src/main/java/io/prometheus/client/exporter/HTTPServer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.net.HttpURLConnection;
1010
import java.net.InetSocketAddress;
1111
import java.net.URLDecoder;
12+
import java.nio.charset.Charset;
1213
import java.util.List;
1314
import java.util.Set;
1415
import java.util.HashSet;
@@ -61,7 +62,7 @@ public void handle(HttpExchange t) throws IOException {
6162
String contextPath = t.getHttpContext().getPath();
6263
ByteArrayOutputStream response = this.response.get();
6364
response.reset();
64-
OutputStreamWriter osw = new OutputStreamWriter(response);
65+
OutputStreamWriter osw = new OutputStreamWriter(response, Charset.forName("UTF-8"));
6566
if ("/-/healthy".equals(contextPath)) {
6667
osw.write(HEALTHY_RESPONSE);
6768
} else {

0 commit comments

Comments
 (0)