diff --git a/iotdb-core/metrics/core/src/main/java/org/apache/iotdb/metrics/core/type/IoTDBTimer.java b/iotdb-core/metrics/core/src/main/java/org/apache/iotdb/metrics/core/type/IoTDBTimer.java index adba7f80e715..19def110f4b4 100644 --- a/iotdb-core/metrics/core/src/main/java/org/apache/iotdb/metrics/core/type/IoTDBTimer.java +++ b/iotdb-core/metrics/core/src/main/java/org/apache/iotdb/metrics/core/type/IoTDBTimer.java @@ -26,7 +26,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Objects; +import java.util.Optional; import java.util.concurrent.TimeUnit; public class IoTDBTimer extends AbstractMetricMBean implements Timer, IoTDBTimerMBean { @@ -56,62 +56,36 @@ public HistogramSnapshot takeSnapshot() { @Override public double getSum() { - HistogramSnapshot snapshot = this.takeSnapshot(); - if (Objects.isNull(snapshot)) { - LOGGER.warn("Snapshot is null, can't get sum, return 0.0 instead."); - return 0.0; - } - return snapshot.getSum(); + return Optional.ofNullable(takeSnapshot()).map(HistogramSnapshot::getSum).orElse(0.0); } @Override public double getMax() { - HistogramSnapshot snapshot = this.takeSnapshot(); - if (Objects.isNull(snapshot)) { - LOGGER.warn("Snapshot is null, can't get max, return 0.0 instead."); - return 0.0; - } - return snapshot.getMax(); + return Optional.ofNullable(takeSnapshot()).map(HistogramSnapshot::getMax).orElse(0.0); } @Override public double getMean() { - HistogramSnapshot snapshot = this.takeSnapshot(); - if (Objects.isNull(snapshot)) { - LOGGER.warn("Snapshot is null, can't get mean, return 0.0 instead."); - return 0.0; - } - return snapshot.getMean(); + return Optional.ofNullable(takeSnapshot()).map(HistogramSnapshot::getMean).orElse(0.0); } @Override public int getSize() { - HistogramSnapshot snapshot = this.takeSnapshot(); - if (Objects.isNull(snapshot)) { - LOGGER.warn("Snapshot is null, can't get size, return 0 instead."); - return 0; - } - return snapshot.size(); + return Optional.ofNullable(takeSnapshot()).map(HistogramSnapshot::size).orElse(0); } @Override public double get50thPercentile() { - HistogramSnapshot snapshot = this.takeSnapshot(); - if (Objects.isNull(snapshot)) { - LOGGER.warn("Snapshot is null, can't get 50th percentile, return 0.0 instead."); - return 0; - } - return snapshot.getValue(0.5); + return Optional.ofNullable(takeSnapshot()) + .map(histogramSnapshot -> histogramSnapshot.getValue(0.5)) + .orElse(0.0); } @Override public double get99thPercentile() { - HistogramSnapshot snapshot = this.takeSnapshot(); - if (Objects.isNull(snapshot)) { - LOGGER.warn("Snapshot is null, can't get 99th percentile, return 0.0 instead."); - return 0; - } - return snapshot.getValue(0.99); + return Optional.ofNullable(takeSnapshot()) + .map(histogramSnapshot -> histogramSnapshot.getValue(0.99)) + .orElse(0.0); } @Override diff --git a/iotdb-core/metrics/interface/src/main/java/org/apache/iotdb/metrics/reporter/prometheus/PrometheusReporter.java b/iotdb-core/metrics/interface/src/main/java/org/apache/iotdb/metrics/reporter/prometheus/PrometheusReporter.java index c5fffe30c9a2..08f1e7becd77 100644 --- a/iotdb-core/metrics/interface/src/main/java/org/apache/iotdb/metrics/reporter/prometheus/PrometheusReporter.java +++ b/iotdb-core/metrics/interface/src/main/java/org/apache/iotdb/metrics/reporter/prometheus/PrometheusReporter.java @@ -146,7 +146,8 @@ private String scrape() { Timer timer = (Timer) metric; HistogramSnapshot snapshot = timer.takeSnapshot(); if (Objects.isNull(snapshot)) { - LOGGER.warn("Detected an error when taking snapshot, will discard this metric"); + LOGGER.warn( + "Detected an error when taking metric timer snapshot, will discard this metric"); continue; } name += "_seconds";