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
public void stop() {
if (consoleReporter != null) {
consoleReporter.stop();
}
}
}

View File

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

View File

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