diff --git a/wls-exporter-core/src/main/java/com/oracle/wls/exporter/MetricsStream.java b/wls-exporter-core/src/main/java/com/oracle/wls/exporter/MetricsStream.java index e868fafa..be01e0d4 100644 --- a/wls-exporter-core/src/main/java/com/oracle/wls/exporter/MetricsStream.java +++ b/wls-exporter-core/src/main/java/com/oracle/wls/exporter/MetricsStream.java @@ -3,12 +3,12 @@ package com.oracle.wls.exporter; -import javax.management.MBeanServerConnection; import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; import java.lang.management.ManagementFactory; import java.util.Locale; +import javax.management.MBeanServerConnection; import com.sun.management.OperatingSystemMXBean; @@ -19,6 +19,7 @@ * @author Russell Gold */ class MetricsStream extends PrintStream { + private static final String PROMETHEUS_LINE_SEPARATOR = "\n"; // This is not dependent on the platform running the exporter. private static final double NANOSEC_PER_SECONDS = 1000000000; private final PerformanceProbe performanceProbe; @@ -58,7 +59,7 @@ class MetricsStream extends PrintStream { * @param value the metric value */ void printMetric(String name, Object value) { - print(name + " " + value + System.lineSeparator()); + print(name + " " + value + PROMETHEUS_LINE_SEPARATOR); scrapeCount++; } @@ -66,10 +67,10 @@ void printMetric(String name, Object value) { * Prints the summary performance metrics */ void printPlatformMetrics() { - printf( "%s %d%n", getCountName(), scrapeCount); - printf(Locale.US, "%s %.2f%n", getDurationName(), toSeconds(getElapsedTime())); - printf(Locale.US, "%s %.2f%n", getCpuUsageName(), toSeconds(getCpuUsed())); - printf("%s %d%n", getExporterVersionName(), 1); + printMetric(getCountName(), scrapeCount); + printMetric(getDurationName(), toSecondsString(getElapsedTime())); + printMetric(getCpuUsageName(), toSecondsString(getCpuUsed())); + printMetric(getExporterVersionName(), 1); } private String getDurationName() { @@ -107,8 +108,8 @@ private long getElapsedTime() { return performanceProbe.getCurrentTime() - startTime; } - private double toSeconds(long nanoSeconds) { - return nanoSeconds / NANOSEC_PER_SECONDS; + private String toSecondsString(long nanoSeconds) { + return String.format(Locale.US, "%.2f", nanoSeconds / NANOSEC_PER_SECONDS); } private long getCpuUsed() { diff --git a/wls-exporter-core/src/test/java/com/oracle/wls/exporter/MetricsStreamTest.java b/wls-exporter-core/src/test/java/com/oracle/wls/exporter/MetricsStreamTest.java index 177a8bc3..91cf6178 100644 --- a/wls-exporter-core/src/test/java/com/oracle/wls/exporter/MetricsStreamTest.java +++ b/wls-exporter-core/src/test/java/com/oracle/wls/exporter/MetricsStreamTest.java @@ -18,7 +18,6 @@ import com.meterware.simplestub.Memento; import com.meterware.simplestub.StaticStubSupport; import com.meterware.simplestub.SystemPropertySupport; -import com.oracle.wls.exporter.matchers.PrometheusMetricsMatcher; import com.oracle.wls.exporter.webapp.HttpServletRequestStub; import com.oracle.wls.exporter.webapp.ServletUtils; import org.junit.jupiter.api.AfterEach; @@ -35,6 +34,7 @@ * @author Russell Gold */ class MetricsStreamTest { + private static final String Linux_LINE_SEPARATOR = "\n"; private static final long NANOSEC_PER_SECONDS = 1000000000; private static final String LINE_SEPARATOR = "line.separator"; private static final String WINDOWS_LINE_SEPARATOR = "\r\n"; @@ -79,7 +79,7 @@ private String getPrintedMetrics() { } @Test - void whenMetricsPrinted_eachHasItsOwnLineSeparatedByCarriageReturns() { + void whenMetricsPrinted_eachHasItsOwnLineSeparatedByLinuxLineSeparators() { metrics.printMetric("a", 12); metrics.printMetric("b", 120); metrics.printMetric("c", 0); @@ -88,7 +88,7 @@ void whenMetricsPrinted_eachHasItsOwnLineSeparatedByCarriageReturns() { } @Test - void whenMetricsPrintedOnWindows_eachHasItsOwnLineSeparatedByCarriageReturns() throws NoSuchFieldException { + void whenMetricsPrintedOnWindows_eachHasItsOwnLineSeparatedByLinuxLineSeparators() throws NoSuchFieldException { simulateWindows(); metrics.printMetric("a", 12); @@ -106,11 +106,15 @@ private void simulateWindows() throws NoSuchFieldException { private List getPrintedMetricValues() { return Arrays.stream(getPrintedMetrics() - .split(System.lineSeparator())) - .map(PrometheusMetricsMatcher::getMetricValue) + .split(Linux_LINE_SEPARATOR)) + .map(this::getMetricValue) .collect(Collectors.toList()); } + private String getMetricValue(String metricLine) { + return metricLine.substring(metricLine.lastIndexOf(' ') + 1); + } + @Test void afterMetricsScraped_reportScrapedCount() { metrics.printMetric("a", 12); diff --git a/wls-exporter-core/src/test/java/com/oracle/wls/exporter/matchers/PrometheusMetricsMatcher.java b/wls-exporter-core/src/test/java/com/oracle/wls/exporter/matchers/PrometheusMetricsMatcher.java index 41805481..5a7fafe5 100644 --- a/wls-exporter-core/src/test/java/com/oracle/wls/exporter/matchers/PrometheusMetricsMatcher.java +++ b/wls-exporter-core/src/test/java/com/oracle/wls/exporter/matchers/PrometheusMetricsMatcher.java @@ -20,14 +20,6 @@ public static PrometheusMetricsMatcher followsPrometheusRules() { return new PrometheusMetricsMatcher(); } - /** - * Given a line with a described metric, returns the actual metric string. - * @param metric a line containing a qualified name and a Prometheus metric - */ - public static String getMetricValue(String metric) { - return metric.substring(metric.lastIndexOf(' ')).trim(); - } - @Override protected boolean matchesSafely(String metricsList, Description description) { String[] metrics = Arrays.stream(metricsList.split("\n")) @@ -69,6 +61,10 @@ private boolean hasNonNumericValue(String metric) { } } + private static String getMetricValue(String metric) { + return metric.substring(metric.lastIndexOf(' ')).trim(); + } + private boolean metricsInOrder(Description description, String[] metrics) { String outOfOrderMetric = getOutOfOrderMetric(metrics); if (outOfOrderMetric == null) return true;