This is an automated email from the ASF dual-hosted git repository. mattyb149 pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/nifi.git
The following commit(s) were added to refs/heads/main by this push: new b7a48f766a NIFI-10666 PrometheusReportingTask needs to use UTF-8 (not jvm default charset) for /metrics endpoint b7a48f766a is described below commit b7a48f766a35e594e5d8c5085938687cb900b59c Author: Jim Steinebrey <jrsteineb...@gmail.com> AuthorDate: Wed Jun 19 09:59:52 2024 -0400 NIFI-10666 PrometheusReportingTask needs to use UTF-8 (not jvm default charset) for /metrics endpoint Signed-off-by: Matt Burgess <mattyb...@apache.org> This closes #8988 --- .../nifi/reporting/prometheus/PrometheusServer.java | 17 +++++++++-------- .../reporting/prometheus/TestPrometheusRecordSink.java | 10 +++++----- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/nifi-extension-bundles/nifi-prometheus-bundle/nifi-prometheus-reporting-task/src/main/java/org/apache/nifi/reporting/prometheus/PrometheusServer.java b/nifi-extension-bundles/nifi-prometheus-bundle/nifi-prometheus-reporting-task/src/main/java/org/apache/nifi/reporting/prometheus/PrometheusServer.java index 2f89da2d70..8ce4da09b5 100644 --- a/nifi-extension-bundles/nifi-prometheus-bundle/nifi-prometheus-reporting-task/src/main/java/org/apache/nifi/reporting/prometheus/PrometheusServer.java +++ b/nifi-extension-bundles/nifi-prometheus-bundle/nifi-prometheus-reporting-task/src/main/java/org/apache/nifi/reporting/prometheus/PrometheusServer.java @@ -42,6 +42,7 @@ import java.io.IOException; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.InetSocketAddress; +import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.List; import java.util.function.Function; @@ -58,24 +59,24 @@ public class PrometheusServer { @Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws IOException { - if (logger.isDebugEnabled()) { - logger.debug("PrometheusServer Do get called"); - } + logger.debug("PrometheusServer doGet() called"); ServletOutputStream response = resp.getOutputStream(); - OutputStreamWriter osw = new OutputStreamWriter(response); + OutputStreamWriter osw = new OutputStreamWriter(response, StandardCharsets.UTF_8); for (Function<ReportingContext, CollectorRegistry> mc : metricsCollectors) { CollectorRegistry collectorRegistry = mc.apply(getReportingContext()); TextFormat.write004(osw, collectorRegistry.metricFamilySamples()); } + // These must be set BEFORE osw.flush() because osw.flush() commits resp which blocks any set calls. + resp.setHeader("Content-Type", TextFormat.CONTENT_TYPE_004); + resp.setStatus(HttpURLConnection.HTTP_OK); + osw.flush(); osw.close(); response.flush(); response.close(); - resp.setHeader("Content-Type", TextFormat.CONTENT_TYPE_004); - resp.setStatus(HttpURLConnection.HTTP_OK); resp.flushBuffer(); } } @@ -91,7 +92,7 @@ public class PrometheusServer { this.server.start(); } catch (Exception e) { // If Jetty couldn't start, stop it explicitly to avoid dangling threads - logger.debug("PrometheusServer: Couldn't start Jetty server, stopping manually"); + logger.debug("PrometheusServer: Couldn't start Jetty server, stopping manually", e); this.server.stop(); throw e; } @@ -118,7 +119,7 @@ public class PrometheusServer { this.server.start(); } catch (Exception e) { // If Jetty couldn't start, stop it explicitly to avoid dangling threads - logger.debug("PrometheusServer: Couldn't start Jetty server, stopping manually"); + logger.debug("PrometheusServer: Couldn't start Jetty server, stopping manually", e); this.server.stop(); throw e; } diff --git a/nifi-extension-bundles/nifi-prometheus-bundle/nifi-prometheus-reporting-task/src/test/java/org/apache/nifi/reporting/prometheus/TestPrometheusRecordSink.java b/nifi-extension-bundles/nifi-prometheus-bundle/nifi-prometheus-reporting-task/src/test/java/org/apache/nifi/reporting/prometheus/TestPrometheusRecordSink.java index b501db2297..0608911caa 100644 --- a/nifi-extension-bundles/nifi-prometheus-bundle/nifi-prometheus-reporting-task/src/test/java/org/apache/nifi/reporting/prometheus/TestPrometheusRecordSink.java +++ b/nifi-extension-bundles/nifi-prometheus-bundle/nifi-prometheus-reporting-task/src/test/java/org/apache/nifi/reporting/prometheus/TestPrometheusRecordSink.java @@ -87,7 +87,7 @@ public class TestPrometheusRecordSink { Map<String, Object> row2 = new LinkedHashMap<>(); row2.put("field1", 6); row2.put("field2", BigDecimal.valueOf(0.1234567890123456789D)); - row2.put("field3", "World!"); + row2.put("field3", "WorldÄËÖÜ!"); RecordSet recordSet = new ListRecordSet(recordSchema, Arrays.asList( new MapRecord(recordSchema, row1), @@ -103,10 +103,10 @@ public class TestPrometheusRecordSink { final String content = getMetrics(); - assertTrue(content.contains("field1{field3=\"Hello\",} 15.0\nfield1{field3=\"World!\",} 6.0\n") - || content.contains("field1{field3=\"World!\",} 6.0\nfield1{field3=\"Hello\",} 15.0\n")); - assertTrue(content.contains("field2{field3=\"Hello\",} 12.34567\nfield2{field3=\"World!\",} 0.12345678901234568\n") - || content.contains("field2{field3=\"World!\",} 0.12345678901234568\nfield2{field3=\"Hello\",} 12.34567\n")); + assertTrue(content.contains("field1{field3=\"Hello\",} 15.0\nfield1{field3=\"WorldÄËÖÜ!\",} 6.0\n") + || content.contains("field1{field3=\"WorldÄËÖÜ!\",} 6.0\nfield1{field3=\"Hello\",} 15.0\n")); + assertTrue(content.contains("field2{field3=\"Hello\",} 12.34567\nfield2{field3=\"WorldÄËÖÜ!\",} 0.12345678901234568\n") + || content.contains("field2{field3=\"WorldÄËÖÜ!\",} 0.12345678901234568\nfield2{field3=\"Hello\",} 12.34567\n")); try { sink.onStopped();