1
0

[HUDI-1305] Added an API to shutdown and remove the metrics reporter. (#2132)

This helps in removing reporter once the test has complete. Prevents log pollution from un-necessary metric logs.

- Added an API to shutdown the metrics reporter after tests.
This commit is contained in:
Prashant Wason
2020-10-04 09:30:04 -07:00
committed by GitHub
parent 1f7add9291
commit 6c610b91ef
3 changed files with 30 additions and 9 deletions

View File

@@ -68,5 +68,8 @@ public class ConsoleMetricsReporter extends MetricsReporter {
@Override @Override
public void stop() { public void stop() {
if (consoleReporter != null) {
consoleReporter.stop();
}
} }
} }

View File

@@ -52,18 +52,22 @@ public class Metrics {
reporter.start(); reporter.start();
Runtime.getRuntime().addShutdownHook(new Thread(() -> { Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try { reportAndCloseReporter();
registerHoodieCommonMetrics();
reporter.report();
if (getReporter() != null) {
getReporter().close();
}
} catch (Exception e) {
LOG.warn("Error while closing reporter", e);
}
})); }));
} }
private void reportAndCloseReporter() {
try {
registerHoodieCommonMetrics();
reporter.report();
if (getReporter() != null) {
getReporter().close();
}
} catch (Exception e) {
LOG.warn("Error while closing reporter", e);
}
}
private void registerHoodieCommonMetrics() { private void registerHoodieCommonMetrics() {
registerGauges(Registry.getAllMetrics(true, true), Option.empty()); registerGauges(Registry.getAllMetrics(true, true), Option.empty());
} }
@@ -85,6 +89,14 @@ public class Metrics {
initialized = true; initialized = true;
} }
public static synchronized void shutdown() {
if (!initialized) {
return;
}
metrics.reportAndCloseReporter();
initialized = false;
}
public static void registerGauges(Map<String, Long> metricsMap, Option<String> prefix) { public static void registerGauges(Map<String, Long> metricsMap, Option<String> prefix) {
String metricPrefix = prefix.isPresent() ? prefix.get() + "." : ""; String metricPrefix = prefix.isPresent() ? prefix.get() + "." : "";
metricsMap.forEach((k, v) -> registerGauge(metricPrefix + k, v)); metricsMap.forEach((k, v) -> registerGauge(metricPrefix + k, v));

View File

@@ -19,6 +19,7 @@
package org.apache.hudi.metrics; package org.apache.hudi.metrics;
import org.apache.hudi.config.HoodieWriteConfig; import org.apache.hudi.config.HoodieWriteConfig;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -38,6 +39,11 @@ public class TestHoodieConsoleMetrics {
new HoodieMetrics(config, "raw_table"); new HoodieMetrics(config, "raw_table");
} }
@AfterEach
public void stop() {
Metrics.shutdown();
}
@Test @Test
public void testRegisterGauge() { public void testRegisterGauge() {
registerGauge("metric1", 123L); registerGauge("metric1", 123L);