Skip to content

Commit 907fdcb

Browse files
authored
Reimplement platform-independent metrics printing (#220)
1 parent d8c9a0e commit 907fdcb

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

wls-exporter-core/src/main/java/com/oracle/wls/exporter/MetricsStream.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
package com.oracle.wls.exporter;
55

6-
import javax.management.MBeanServerConnection;
76
import java.io.IOException;
87
import java.io.OutputStream;
98
import java.io.PrintStream;
109
import java.lang.management.ManagementFactory;
1110
import java.util.Locale;
11+
import javax.management.MBeanServerConnection;
1212

1313
import com.sun.management.OperatingSystemMXBean;
1414

@@ -19,6 +19,7 @@
1919
* @author Russell Gold
2020
*/
2121
class MetricsStream extends PrintStream {
22+
private static final String PROMETHEUS_LINE_SEPARATOR = "\n"; // This is not dependent on the platform running the exporter.
2223
private static final double NANOSEC_PER_SECONDS = 1000000000;
2324

2425
private final PerformanceProbe performanceProbe;
@@ -58,18 +59,18 @@ class MetricsStream extends PrintStream {
5859
* @param value the metric value
5960
*/
6061
void printMetric(String name, Object value) {
61-
print(name + " " + value + System.lineSeparator());
62+
print(name + " " + value + PROMETHEUS_LINE_SEPARATOR);
6263
scrapeCount++;
6364
}
6465

6566
/**
6667
* Prints the summary performance metrics
6768
*/
6869
void printPlatformMetrics() {
69-
printf( "%s %d%n", getCountName(), scrapeCount);
70-
printf(Locale.US, "%s %.2f%n", getDurationName(), toSeconds(getElapsedTime()));
71-
printf(Locale.US, "%s %.2f%n", getCpuUsageName(), toSeconds(getCpuUsed()));
72-
printf("%s %d%n", getExporterVersionName(), 1);
70+
printMetric(getCountName(), scrapeCount);
71+
printMetric(getDurationName(), toSecondsString(getElapsedTime()));
72+
printMetric(getCpuUsageName(), toSecondsString(getCpuUsed()));
73+
printMetric(getExporterVersionName(), 1);
7374
}
7475

7576
private String getDurationName() {
@@ -107,8 +108,8 @@ private long getElapsedTime() {
107108
return performanceProbe.getCurrentTime() - startTime;
108109
}
109110

110-
private double toSeconds(long nanoSeconds) {
111-
return nanoSeconds / NANOSEC_PER_SECONDS;
111+
private String toSecondsString(long nanoSeconds) {
112+
return String.format(Locale.US, "%.2f", nanoSeconds / NANOSEC_PER_SECONDS);
112113
}
113114

114115
private long getCpuUsed() {

wls-exporter-core/src/test/java/com/oracle/wls/exporter/MetricsStreamTest.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.meterware.simplestub.Memento;
1919
import com.meterware.simplestub.StaticStubSupport;
2020
import com.meterware.simplestub.SystemPropertySupport;
21-
import com.oracle.wls.exporter.matchers.PrometheusMetricsMatcher;
2221
import com.oracle.wls.exporter.webapp.HttpServletRequestStub;
2322
import com.oracle.wls.exporter.webapp.ServletUtils;
2423
import org.junit.jupiter.api.AfterEach;
@@ -35,6 +34,7 @@
3534
* @author Russell Gold
3635
*/
3736
class MetricsStreamTest {
37+
private static final String Linux_LINE_SEPARATOR = "\n";
3838
private static final long NANOSEC_PER_SECONDS = 1000000000;
3939
private static final String LINE_SEPARATOR = "line.separator";
4040
private static final String WINDOWS_LINE_SEPARATOR = "\r\n";
@@ -79,7 +79,7 @@ private String getPrintedMetrics() {
7979
}
8080

8181
@Test
82-
void whenMetricsPrinted_eachHasItsOwnLineSeparatedByCarriageReturns() {
82+
void whenMetricsPrinted_eachHasItsOwnLineSeparatedByLinuxLineSeparators() {
8383
metrics.printMetric("a", 12);
8484
metrics.printMetric("b", 120);
8585
metrics.printMetric("c", 0);
@@ -88,7 +88,7 @@ void whenMetricsPrinted_eachHasItsOwnLineSeparatedByCarriageReturns() {
8888
}
8989

9090
@Test
91-
void whenMetricsPrintedOnWindows_eachHasItsOwnLineSeparatedByCarriageReturns() throws NoSuchFieldException {
91+
void whenMetricsPrintedOnWindows_eachHasItsOwnLineSeparatedByLinuxLineSeparators() throws NoSuchFieldException {
9292
simulateWindows();
9393

9494
metrics.printMetric("a", 12);
@@ -106,11 +106,15 @@ private void simulateWindows() throws NoSuchFieldException {
106106

107107
private List<String> getPrintedMetricValues() {
108108
return Arrays.stream(getPrintedMetrics()
109-
.split(System.lineSeparator()))
110-
.map(PrometheusMetricsMatcher::getMetricValue)
109+
.split(Linux_LINE_SEPARATOR))
110+
.map(this::getMetricValue)
111111
.collect(Collectors.toList());
112112
}
113113

114+
private String getMetricValue(String metricLine) {
115+
return metricLine.substring(metricLine.lastIndexOf(' ') + 1);
116+
}
117+
114118
@Test
115119
void afterMetricsScraped_reportScrapedCount() {
116120
metrics.printMetric("a", 12);

wls-exporter-core/src/test/java/com/oracle/wls/exporter/matchers/PrometheusMetricsMatcher.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@ public static PrometheusMetricsMatcher followsPrometheusRules() {
2020
return new PrometheusMetricsMatcher();
2121
}
2222

23-
/**
24-
* Given a line with a described metric, returns the actual metric string.
25-
* @param metric a line containing a qualified name and a Prometheus metric
26-
*/
27-
public static String getMetricValue(String metric) {
28-
return metric.substring(metric.lastIndexOf(' ')).trim();
29-
}
30-
3123
@Override
3224
protected boolean matchesSafely(String metricsList, Description description) {
3325
String[] metrics = Arrays.stream(metricsList.split("\n"))
@@ -69,6 +61,10 @@ private boolean hasNonNumericValue(String metric) {
6961
}
7062
}
7163

64+
private static String getMetricValue(String metric) {
65+
return metric.substring(metric.lastIndexOf(' ')).trim();
66+
}
67+
7268
private boolean metricsInOrder(Description description, String[] metrics) {
7369
String outOfOrderMetric = getOutOfOrderMetric(metrics);
7470
if (outOfOrderMetric == null) return true;

0 commit comments

Comments
 (0)