Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metrics printing fixed on Windows #220

Merged
merged 1 commit into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -58,18 +59,18 @@ 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++;
}

/**
* 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() {
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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);
Expand All @@ -88,7 +88,7 @@ void whenMetricsPrinted_eachHasItsOwnLineSeparatedByCarriageReturns() {
}

@Test
void whenMetricsPrintedOnWindows_eachHasItsOwnLineSeparatedByCarriageReturns() throws NoSuchFieldException {
void whenMetricsPrintedOnWindows_eachHasItsOwnLineSeparatedByLinuxLineSeparators() throws NoSuchFieldException {
simulateWindows();

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

private List<String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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;
Expand Down