You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ro...@apache.org on 2021/11/22 15:25:13 UTC
[iotdb] branch master updated: [IOTDB-2016][metric] Fix ClassCastException when create new metric types (#4425)
This is an automated email from the ASF dual-hosted git repository.
rong pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/master by this push:
new ab32867 [IOTDB-2016][metric] Fix ClassCastException when create new metric types (#4425)
ab32867 is described below
commit ab32867780157d160ef63ff77a4a80803b484aed
Author: xinzhongtianxia <45...@qq.com>
AuthorDate: Mon Nov 22 23:24:44 2021 +0800
[IOTDB-2016][metric] Fix ClassCastException when create new metric types (#4425)
Co-authored-by: 还真 <hu...@alibaba-inc.com>
---
.../dropwizard/DropwizardMetricManager.java | 134 +++++++++--------
.../dropwizard/type/DropwizardHistogram.java | 5 -
.../dropwizard/DropwizardMetricManagerTest.java | 15 +-
.../org/apache/iotdb/metrics/MetricManager.java | 6 +-
.../iotdb/metrics/impl/DoNothingHistogram.java | 4 -
.../org/apache/iotdb/metrics/type/Histogram.java | 3 -
.../micrometer/MicrometerMetricManager.java | 162 +++++++++++++--------
.../micrometer/type/MicrometerHistogram.java | 5 -
.../micrometer/MicrometerMetricManagerTest.java | 52 +++++++
9 files changed, 250 insertions(+), 136 deletions(-)
diff --git a/metrics/dropwizard-metrics/src/main/java/org/apache/iotdb/metrics/dropwizard/DropwizardMetricManager.java b/metrics/dropwizard-metrics/src/main/java/org/apache/iotdb/metrics/dropwizard/DropwizardMetricManager.java
index 5aff734..4433a9e 100644
--- a/metrics/dropwizard-metrics/src/main/java/org/apache/iotdb/metrics/dropwizard/DropwizardMetricManager.java
+++ b/metrics/dropwizard-metrics/src/main/java/org/apache/iotdb/metrics/dropwizard/DropwizardMetricManager.java
@@ -65,9 +65,13 @@ public class DropwizardMetricManager implements MetricManager {
return DoNothingMetricManager.doNothingCounter;
}
MetricName name = new MetricName(metric, tags);
- return (Counter)
+ IMetric m =
currentMeters.computeIfAbsent(
name, key -> new DropwizardCounter(metricRegistry.counter(name.toFlatString())));
+ if (m instanceof Counter) {
+ return (Counter) m;
+ }
+ throw new IllegalArgumentException(name + " is already used for a different type of metric");
}
@Override
@@ -76,7 +80,7 @@ public class DropwizardMetricManager implements MetricManager {
return DoNothingMetricManager.doNothingGauge;
}
MetricName name = new MetricName(metric, tags);
- return (Gauge)
+ IMetric m =
currentMeters.computeIfAbsent(
name,
key -> {
@@ -85,6 +89,10 @@ public class DropwizardMetricManager implements MetricManager {
name.toFlatString(), dropwizardGauge.getDropwizardCachedGauge());
return dropwizardGauge;
});
+ if (m instanceof Gauge) {
+ return (Gauge) m;
+ }
+ throw new IllegalArgumentException(name + " is already used for a different type of metric");
}
@Override
@@ -93,9 +101,13 @@ public class DropwizardMetricManager implements MetricManager {
return DoNothingMetricManager.doNothingRate;
}
MetricName name = new MetricName(metric, tags);
- return (Rate)
+ IMetric m =
currentMeters.computeIfAbsent(
name, key -> new DropwizardRate(metricRegistry.meter(name.toFlatString())));
+ if (m instanceof Rate) {
+ return (Rate) m;
+ }
+ throw new IllegalArgumentException(name + " is already used for a different type of metric");
}
@Override
@@ -104,9 +116,13 @@ public class DropwizardMetricManager implements MetricManager {
return DoNothingMetricManager.doNothingHistogram;
}
MetricName name = new MetricName(metric, tags);
- return (Histogram)
+ IMetric m =
currentMeters.computeIfAbsent(
name, key -> new DropwizardHistogram(metricRegistry.histogram(name.toFlatString())));
+ if (m instanceof Histogram) {
+ return (Histogram) m;
+ }
+ throw new IllegalArgumentException(name + " is already used for a different type of metric");
}
@Override
@@ -115,21 +131,18 @@ public class DropwizardMetricManager implements MetricManager {
return DoNothingMetricManager.doNothingTimer;
}
MetricName name = new MetricName(metric, tags);
- return (Timer)
+ IMetric m =
currentMeters.computeIfAbsent(
name, key -> new DropwizardTimer(metricRegistry.timer(name.toFlatString())));
+ if (m instanceof Timer) {
+ return (Timer) m;
+ }
+ throw new IllegalArgumentException(name + " is already used for a different type of metric");
}
@Override
public void count(int delta, String metric, String... tags) {
- if (!isEnable) {
- return;
- }
- MetricName name = new MetricName(metric, tags);
- ((Counter)
- currentMeters.computeIfAbsent(
- name, key -> new DropwizardCounter(metricRegistry.counter(name.toFlatString()))))
- .inc(delta);
+ this.count((long) delta, metric, tags);
}
@Override
@@ -138,15 +151,19 @@ public class DropwizardMetricManager implements MetricManager {
return;
}
MetricName name = new MetricName(metric, tags);
- ((Counter)
- currentMeters.computeIfAbsent(
- name, key -> new DropwizardCounter(metricRegistry.counter(name.toFlatString()))))
- .inc(delta);
+ IMetric m =
+ currentMeters.computeIfAbsent(
+ name, key -> new DropwizardCounter(metricRegistry.counter(name.toFlatString())));
+ if (m instanceof Counter) {
+ ((Counter) m).inc(delta);
+ return;
+ }
+ throw new IllegalArgumentException(name + " is already used for a different type of metric");
}
@Override
public void gauge(int value, String metric, String... tags) {
- this.gauge(Long.valueOf(value), metric, tags);
+ this.gauge((long) value, metric, tags);
}
@Override
@@ -155,28 +172,25 @@ public class DropwizardMetricManager implements MetricManager {
return;
}
MetricName name = new MetricName(metric, tags);
- ((Gauge)
- currentMeters.computeIfAbsent(
- name,
- key -> {
- DropwizardGauge dropwizardGauge = new DropwizardGauge();
- metricRegistry.register(
- name.toFlatString(), dropwizardGauge.getDropwizardCachedGauge());
- return dropwizardGauge;
- }))
- .set(value);
+ IMetric m =
+ currentMeters.computeIfAbsent(
+ name,
+ key -> {
+ DropwizardGauge dropwizardGauge = new DropwizardGauge();
+ metricRegistry.register(
+ name.toFlatString(), dropwizardGauge.getDropwizardCachedGauge());
+ return dropwizardGauge;
+ });
+ if (m instanceof Gauge) {
+ ((Gauge) m).set(value);
+ return;
+ }
+ throw new IllegalArgumentException(name + " is already used for a different type of metric");
}
@Override
public void rate(int value, String metric, String... tags) {
- if (!isEnable) {
- return;
- }
- MetricName name = new MetricName(metric, tags);
- ((Rate)
- currentMeters.computeIfAbsent(
- name, key -> new DropwizardRate(metricRegistry.meter(name.toFlatString()))))
- .mark(value);
+ this.rate((long) value, metric, tags);
}
@Override
@@ -185,23 +199,19 @@ public class DropwizardMetricManager implements MetricManager {
return;
}
MetricName name = new MetricName(metric, tags);
- ((Rate)
- currentMeters.computeIfAbsent(
- name, key -> new DropwizardRate(metricRegistry.meter(name.toFlatString()))))
- .mark(value);
+ IMetric m =
+ currentMeters.computeIfAbsent(
+ name, key -> new DropwizardRate(metricRegistry.meter(name.toFlatString())));
+ if (m instanceof Rate) {
+ ((Rate) m).mark(value);
+ return;
+ }
+ throw new IllegalArgumentException(name + " is already used for a different type of metric");
}
@Override
public void histogram(int value, String metric, String... tags) {
- if (!isEnable) {
- return;
- }
- MetricName name = new MetricName(metric, tags);
- ((Histogram)
- currentMeters.computeIfAbsent(
- name,
- key -> new DropwizardHistogram(metricRegistry.histogram(name.toFlatString()))))
- .update(value);
+ this.histogram((long) value, metric, tags);
}
@Override
@@ -210,11 +220,14 @@ public class DropwizardMetricManager implements MetricManager {
return;
}
MetricName name = new MetricName(metric, tags);
- ((Histogram)
- currentMeters.computeIfAbsent(
- name,
- key -> new DropwizardHistogram(metricRegistry.histogram(name.toFlatString()))))
- .update(value);
+ IMetric m =
+ currentMeters.computeIfAbsent(
+ name, key -> new DropwizardHistogram(metricRegistry.histogram(name.toFlatString())));
+ if (m instanceof Histogram) {
+ ((Histogram) m).update(value);
+ return;
+ }
+ throw new IllegalArgumentException(name + " is already used for a different type of metric");
}
@Override
@@ -223,10 +236,15 @@ public class DropwizardMetricManager implements MetricManager {
return;
}
MetricName name = new MetricName(metric, tags);
- ((Timer)
- currentMeters.computeIfAbsent(
- name, key -> new DropwizardTimer(metricRegistry.timer(name.toFlatString()))))
- .update(delta, timeUnit);
+ IMetric m =
+ currentMeters.computeIfAbsent(
+ name, key -> new DropwizardTimer(metricRegistry.timer(name.toFlatString())));
+
+ if (m instanceof Timer) {
+ ((Timer) m).update(delta, timeUnit);
+ return;
+ }
+ throw new IllegalArgumentException(name + " is already used for a different type of metric");
}
@Override
diff --git a/metrics/dropwizard-metrics/src/main/java/org/apache/iotdb/metrics/dropwizard/type/DropwizardHistogram.java b/metrics/dropwizard-metrics/src/main/java/org/apache/iotdb/metrics/dropwizard/type/DropwizardHistogram.java
index edf5f2b..4f02efa 100644
--- a/metrics/dropwizard-metrics/src/main/java/org/apache/iotdb/metrics/dropwizard/type/DropwizardHistogram.java
+++ b/metrics/dropwizard-metrics/src/main/java/org/apache/iotdb/metrics/dropwizard/type/DropwizardHistogram.java
@@ -31,11 +31,6 @@ public class DropwizardHistogram implements Histogram {
}
@Override
- public void update(int value) {
- histogram.update(value);
- }
-
- @Override
public void update(long value) {
histogram.update(value);
}
diff --git a/metrics/dropwizard-metrics/src/test/java/org/apache/iotdb/metrics/dropwizard/DropwizardMetricManagerTest.java b/metrics/dropwizard-metrics/src/test/java/org/apache/iotdb/metrics/dropwizard/DropwizardMetricManagerTest.java
index bd18931..be900d8 100644
--- a/metrics/dropwizard-metrics/src/test/java/org/apache/iotdb/metrics/dropwizard/DropwizardMetricManagerTest.java
+++ b/metrics/dropwizard-metrics/src/test/java/org/apache/iotdb/metrics/dropwizard/DropwizardMetricManagerTest.java
@@ -53,8 +53,19 @@ public class DropwizardMetricManagerTest {
assertEquals(counter1, counter2);
}
+ private void getOrCreateDifferentMetricsWithSameName() {
+ Timer timer = metricManager.getOrCreateTimer("metric", "tag1", "tag2");
+ assertNotNull(timer);
+ metricManager.getOrCreateCounter("metric", "tag1", "tag2");
+ }
+
+ @Test
+ public void getOrCreateDifferentMetricsWithSameNameTest() {
+ assertThrows(IllegalArgumentException.class, this::getOrCreateDifferentMetricsWithSameName);
+ }
+
@Test
- public void getOrCreatGauge() {
+ public void getOrCreateGauge() {
Gauge gauge1 = metricManager.getOrCreateGauge("gauge_test", "tag1", "tag2");
assertNotNull(gauge1);
Gauge gauge2 = metricManager.getOrCreateGauge("gauge_test", "tag1", "tag2");
@@ -62,7 +73,7 @@ public class DropwizardMetricManagerTest {
}
@Test
- public void getOrCreatRate() {
+ public void getOrCreateRate() {
Rate rate1 = metricManager.getOrCreateRate("rate_test", "tag1", "tag2");
assertNotNull(rate1);
Rate rate2 = metricManager.getOrCreateRate("rate_test", "tag1", "tag2");
diff --git a/metrics/interface/src/main/java/org/apache/iotdb/metrics/MetricManager.java b/metrics/interface/src/main/java/org/apache/iotdb/metrics/MetricManager.java
index 20d06f8..1e4126d 100644
--- a/metrics/interface/src/main/java/org/apache/iotdb/metrics/MetricManager.java
+++ b/metrics/interface/src/main/java/org/apache/iotdb/metrics/MetricManager.java
@@ -19,7 +19,11 @@
package org.apache.iotdb.metrics;
-import org.apache.iotdb.metrics.type.*;
+import org.apache.iotdb.metrics.type.Counter;
+import org.apache.iotdb.metrics.type.Gauge;
+import org.apache.iotdb.metrics.type.Histogram;
+import org.apache.iotdb.metrics.type.Rate;
+import org.apache.iotdb.metrics.type.Timer;
import org.apache.iotdb.metrics.utils.PredefinedMetric;
import java.util.List;
diff --git a/metrics/interface/src/main/java/org/apache/iotdb/metrics/impl/DoNothingHistogram.java b/metrics/interface/src/main/java/org/apache/iotdb/metrics/impl/DoNothingHistogram.java
index 793aa3b..a95ca80 100644
--- a/metrics/interface/src/main/java/org/apache/iotdb/metrics/impl/DoNothingHistogram.java
+++ b/metrics/interface/src/main/java/org/apache/iotdb/metrics/impl/DoNothingHistogram.java
@@ -23,10 +23,6 @@ import org.apache.iotdb.metrics.type.Histogram;
import org.apache.iotdb.metrics.type.HistogramSnapshot;
public class DoNothingHistogram implements Histogram {
- @Override
- public void update(int value) {
- // do nothing
- }
@Override
public void update(long value) {
diff --git a/metrics/interface/src/main/java/org/apache/iotdb/metrics/type/Histogram.java b/metrics/interface/src/main/java/org/apache/iotdb/metrics/type/Histogram.java
index fa7e73c..351a4fa 100644
--- a/metrics/interface/src/main/java/org/apache/iotdb/metrics/type/Histogram.java
+++ b/metrics/interface/src/main/java/org/apache/iotdb/metrics/type/Histogram.java
@@ -21,9 +21,6 @@ package org.apache.iotdb.metrics.type;
public interface Histogram extends IMetric {
/** update histogram by value */
- void update(int value);
-
- /** update histogram by value */
void update(long value);
/** get value of histogram */
diff --git a/metrics/micrometer-metrics/src/main/java/org/apache/iotdb/metrics/micrometer/MicrometerMetricManager.java b/metrics/micrometer-metrics/src/main/java/org/apache/iotdb/metrics/micrometer/MicrometerMetricManager.java
index c4edf6f..63f1d1b 100644
--- a/metrics/micrometer-metrics/src/main/java/org/apache/iotdb/metrics/micrometer/MicrometerMetricManager.java
+++ b/metrics/micrometer-metrics/src/main/java/org/apache/iotdb/metrics/micrometer/MicrometerMetricManager.java
@@ -23,16 +23,31 @@ import org.apache.iotdb.metrics.MetricManager;
import org.apache.iotdb.metrics.config.MetricConfig;
import org.apache.iotdb.metrics.config.MetricConfigDescriptor;
import org.apache.iotdb.metrics.impl.DoNothingMetricManager;
-import org.apache.iotdb.metrics.micrometer.type.*;
-import org.apache.iotdb.metrics.type.*;
+import org.apache.iotdb.metrics.micrometer.type.MicrometerCounter;
+import org.apache.iotdb.metrics.micrometer.type.MicrometerGauge;
+import org.apache.iotdb.metrics.micrometer.type.MicrometerHistogram;
+import org.apache.iotdb.metrics.micrometer.type.MicrometerRate;
+import org.apache.iotdb.metrics.micrometer.type.MicrometerTimer;
import org.apache.iotdb.metrics.type.Counter;
import org.apache.iotdb.metrics.type.Gauge;
+import org.apache.iotdb.metrics.type.Histogram;
+import org.apache.iotdb.metrics.type.IMetric;
+import org.apache.iotdb.metrics.type.Rate;
import org.apache.iotdb.metrics.type.Timer;
import org.apache.iotdb.metrics.utils.PredefinedMetric;
import org.apache.iotdb.metrics.utils.ReporterType;
-import io.micrometer.core.instrument.*;
-import io.micrometer.core.instrument.binder.jvm.*;
+import io.micrometer.core.instrument.Clock;
+import io.micrometer.core.instrument.Meter;
+import io.micrometer.core.instrument.Metrics;
+import io.micrometer.core.instrument.Tag;
+import io.micrometer.core.instrument.Tags;
+import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics;
+import io.micrometer.core.instrument.binder.jvm.JvmCompilationMetrics;
+import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics;
+import io.micrometer.core.instrument.binder.jvm.JvmHeapPressureMetrics;
+import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics;
+import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics;
import io.micrometer.jmx.JmxConfig;
import io.micrometer.jmx.JmxMeterRegistry;
import io.micrometer.prometheus.PrometheusConfig;
@@ -91,10 +106,14 @@ public class MicrometerMetricManager implements MetricManager {
if (!isEnable) {
return DoNothingMetricManager.doNothingCounter;
}
- io.micrometer.core.instrument.Counter innerCounter = meterRegistry.counter(metric, tags);
- return (Counter)
+ Meter.Id id = MeterIdUtils.fromMetricName(metric, Meter.Type.COUNTER, tags);
+ IMetric m =
currentMeters.computeIfAbsent(
- innerCounter.getId(), key -> new MicrometerCounter(innerCounter));
+ id, key -> new MicrometerCounter(meterRegistry.counter(metric, tags)));
+ if (m instanceof Counter) {
+ return (Counter) m;
+ }
+ throw new IllegalArgumentException(id + " is already used for a different type of metric");
}
@Override
@@ -103,8 +122,12 @@ public class MicrometerMetricManager implements MetricManager {
return DoNothingMetricManager.doNothingGauge;
}
Meter.Id id = MeterIdUtils.fromMetricName(metric, Meter.Type.GAUGE, tags);
- return (Gauge)
+ IMetric m =
currentMeters.computeIfAbsent(id, key -> new MicrometerGauge(meterRegistry, metric, tags));
+ if (m instanceof Gauge) {
+ return (Gauge) m;
+ }
+ throw new IllegalArgumentException(id + " is already used for a different type of metric");
}
@Override
@@ -113,7 +136,7 @@ public class MicrometerMetricManager implements MetricManager {
return DoNothingMetricManager.doNothingHistogram;
}
Meter.Id id = MeterIdUtils.fromMetricName(metric, Meter.Type.DISTRIBUTION_SUMMARY, tags);
- return (Histogram)
+ IMetric m =
currentMeters.computeIfAbsent(
id,
key -> {
@@ -123,6 +146,10 @@ public class MicrometerMetricManager implements MetricManager {
.register(meterRegistry);
return new MicrometerHistogram(distributionSummary);
});
+ if (m instanceof Histogram) {
+ return (Histogram) m;
+ }
+ throw new IllegalArgumentException(id + " is already used for a different type of metric");
}
/**
@@ -140,11 +167,15 @@ public class MicrometerMetricManager implements MetricManager {
}
Meter.Id id = MeterIdUtils.fromMetricName(metric, Meter.Type.GAUGE, tags);
- return (Rate)
+ IMetric m =
currentMeters.computeIfAbsent(
id,
key ->
new MicrometerRate(meterRegistry.gauge(metric, Tags.of(tags), new AtomicLong(0))));
+ if (m instanceof Rate) {
+ return (Rate) m;
+ }
+ throw new IllegalArgumentException(id + " is already used for a different type of metric");
}
@Override
@@ -153,7 +184,7 @@ public class MicrometerMetricManager implements MetricManager {
return DoNothingMetricManager.doNothingTimer;
}
Meter.Id id = MeterIdUtils.fromMetricName(metric, Meter.Type.TIMER, tags);
- return (Timer)
+ IMetric m =
currentMeters.computeIfAbsent(
id,
key -> {
@@ -164,23 +195,29 @@ public class MicrometerMetricManager implements MetricManager {
logger.info("create getOrCreateTimer {}", metric);
return new MicrometerTimer(timer);
});
+ if (m instanceof Timer) {
+ return (Timer) m;
+ }
+ throw new IllegalArgumentException(id + " is already used for a different type of metric");
}
@Override
public void count(int delta, String metric, String... tags) {
- io.micrometer.core.instrument.Counter innerCounter = meterRegistry.counter(metric, tags);
- innerCounter.increment(delta);
+ this.count((long) delta, metric, tags);
}
@Override
public void count(long delta, String metric, String... tags) {
+ if (!isEnable) {
+ return;
+ }
io.micrometer.core.instrument.Counter innerCounter = meterRegistry.counter(metric, tags);
innerCounter.increment(delta);
}
@Override
public void histogram(int value, String metric, String... tags) {
- this.histogram(Long.valueOf(value), metric, tags);
+ this.histogram((long) value, metric, tags);
}
@Override
@@ -189,31 +226,28 @@ public class MicrometerMetricManager implements MetricManager {
return;
}
Meter.Id id = MeterIdUtils.fromMetricName(metric, Meter.Type.DISTRIBUTION_SUMMARY, tags);
- ((Histogram)
- currentMeters.computeIfAbsent(
- id,
- key -> {
- io.micrometer.core.instrument.DistributionSummary distributionSummary =
- io.micrometer.core.instrument.DistributionSummary.builder(metric)
- .tags(tags)
- .publishPercentileHistogram()
- .publishPercentiles(0)
- .register(meterRegistry);
- return new MicrometerHistogram(distributionSummary);
- }))
- .update(value);
+ IMetric m =
+ currentMeters.computeIfAbsent(
+ id,
+ key -> {
+ io.micrometer.core.instrument.DistributionSummary distributionSummary =
+ io.micrometer.core.instrument.DistributionSummary.builder(metric)
+ .tags(tags)
+ .publishPercentileHistogram()
+ .publishPercentiles(0)
+ .register(meterRegistry);
+ return new MicrometerHistogram(distributionSummary);
+ });
+ if (m instanceof Histogram) {
+ ((Histogram) m).update(value);
+ return;
+ }
+ throw new IllegalArgumentException(id + " is already used for a different type of metric");
}
@Override
public void gauge(int value, String metric, String... tags) {
- if (!isEnable) {
- return;
- }
- Meter.Id id = MeterIdUtils.fromMetricName(metric, Meter.Type.GAUGE, tags);
- ((Gauge)
- (currentMeters.computeIfAbsent(
- id, key -> new MicrometerGauge(meterRegistry, metric, tags))))
- .set(value);
+ this.gauge((long) value, metric, tags);
}
@Override
@@ -222,15 +256,19 @@ public class MicrometerMetricManager implements MetricManager {
return;
}
Meter.Id id = MeterIdUtils.fromMetricName(metric, Meter.Type.GAUGE, tags);
- ((Gauge)
- (currentMeters.computeIfAbsent(
- id, key -> new MicrometerGauge(meterRegistry, metric, tags))))
- .set(value);
+ IMetric m =
+ (currentMeters.computeIfAbsent(
+ id, key -> new MicrometerGauge(meterRegistry, metric, tags)));
+ if (m instanceof Gauge) {
+ ((Gauge) m).set(value);
+ return;
+ }
+ throw new IllegalArgumentException(id + " is already used for a different type of metric");
}
@Override
public void rate(int value, String metric, String... tags) {
- this.rate(Long.valueOf(value), metric, tags);
+ this.rate((long) value, metric, tags);
}
@Override
@@ -239,13 +277,17 @@ public class MicrometerMetricManager implements MetricManager {
return;
}
Meter.Id id = MeterIdUtils.fromMetricName(metric, Meter.Type.GAUGE, tags);
- ((Rate)
- currentMeters.computeIfAbsent(
- id,
- key ->
- new MicrometerRate(
- meterRegistry.gauge(metric, Tags.of(tags), new AtomicLong(0)))))
- .mark(value);
+ IMetric m =
+ currentMeters.computeIfAbsent(
+ id,
+ key ->
+ new MicrometerRate(meterRegistry.gauge(metric, Tags.of(tags), new AtomicLong(0))));
+
+ if (m instanceof Rate) {
+ ((Rate) m).mark(value);
+ return;
+ }
+ throw new IllegalArgumentException(id + " is already used for a different type of metric");
}
@Override
@@ -254,17 +296,21 @@ public class MicrometerMetricManager implements MetricManager {
return;
}
Meter.Id id = MeterIdUtils.fromMetricName(metric, Meter.Type.TIMER, tags);
- ((Timer)
- currentMeters.computeIfAbsent(
- id,
- key -> {
- io.micrometer.core.instrument.Timer timer =
- io.micrometer.core.instrument.Timer.builder(metric)
- .tags(tags)
- .register(meterRegistry);
- return new MicrometerTimer(timer);
- }))
- .update(delta, timeUnit);
+ IMetric m =
+ currentMeters.computeIfAbsent(
+ id,
+ key -> {
+ io.micrometer.core.instrument.Timer timer =
+ io.micrometer.core.instrument.Timer.builder(metric)
+ .tags(tags)
+ .register(meterRegistry);
+ return new MicrometerTimer(timer);
+ });
+ if (m instanceof Timer) {
+ ((Timer) m).update(delta, timeUnit);
+ return;
+ }
+ throw new IllegalArgumentException(id + " is already used for a different type of metric");
}
@Override
diff --git a/metrics/micrometer-metrics/src/main/java/org/apache/iotdb/metrics/micrometer/type/MicrometerHistogram.java b/metrics/micrometer-metrics/src/main/java/org/apache/iotdb/metrics/micrometer/type/MicrometerHistogram.java
index f843fa1..6f90182 100644
--- a/metrics/micrometer-metrics/src/main/java/org/apache/iotdb/metrics/micrometer/type/MicrometerHistogram.java
+++ b/metrics/micrometer-metrics/src/main/java/org/apache/iotdb/metrics/micrometer/type/MicrometerHistogram.java
@@ -31,11 +31,6 @@ public class MicrometerHistogram implements Histogram {
}
@Override
- public void update(int value) {
- distributionSummary.record(value);
- }
-
- @Override
public void update(long value) {
distributionSummary.record(value);
}
diff --git a/metrics/micrometer-metrics/src/test/java/org/apache/iotdb/metrics/micrometer/MicrometerMetricManagerTest.java b/metrics/micrometer-metrics/src/test/java/org/apache/iotdb/metrics/micrometer/MicrometerMetricManagerTest.java
new file mode 100644
index 0000000..f98dcfa
--- /dev/null
+++ b/metrics/micrometer-metrics/src/test/java/org/apache/iotdb/metrics/micrometer/MicrometerMetricManagerTest.java
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.metrics.micrometer;
+
+import org.apache.iotdb.metrics.MetricManager;
+import org.apache.iotdb.metrics.MetricService;
+import org.apache.iotdb.metrics.type.Timer;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
+
+public class MicrometerMetricManagerTest {
+
+ @BeforeClass
+ public static void init() {
+ System.setProperty("line.separator", "\n");
+ // set up path of yml
+ System.setProperty("IOTDB_CONF", "src/test/resources");
+ }
+
+ private void getOrCreateDifferentMetricsWithSameName() {
+ MetricManager metricManager = MetricService.getMetricManager();
+ Timer timer = metricManager.getOrCreateTimer("metric", "tag1", "tag2");
+ assertNotNull(timer);
+ metricManager.getOrCreateCounter("metric", "tag1", "tag2");
+ }
+
+ @Test
+ public void getOrCreateDifferentMetricsWithSameNameTest() {
+ assertThrows(IllegalArgumentException.class, this::getOrCreateDifferentMetricsWithSameName);
+ }
+}