Skip to content

Commit 4404735

Browse files
committed
[Test] Updated MockNamespacedMetric to pool metrics instances
1 parent d01c9ae commit 4404735

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

logstash-core/src/test/java/org/logstash/instrument/metrics/MockNamespacedMetric.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@
1313
import org.logstash.instrument.metrics.timer.TimerMetric;
1414

1515
import java.util.Objects;
16+
import java.util.concurrent.ConcurrentHashMap;
17+
import java.util.concurrent.ConcurrentMap;
1618

1719
/**
1820
* Trivial implementation of AbstractNamespacedMetricExt where each abstract creation
19-
* metric is implemented by instantiating a newly fresh metric object.
21+
* metric is implemented by pooling metric instances by name.
2022
* */
2123
@SuppressWarnings({"rawtypes", "serializable"})
2224
public class MockNamespacedMetric extends AbstractNamespacedMetricExt {
2325

2426
private static final long serialVersionUID = -6507123659910450215L;
2527

28+
private transient final ConcurrentMap<String, Metric> metrics = new ConcurrentHashMap<>();
29+
2630
public static MockNamespacedMetric create() {
2731
return new MockNamespacedMetric(RubyUtil.RUBY, RubyUtil.NAMESPACED_METRIC_CLASS);
2832
}
@@ -45,21 +49,21 @@ protected RubyArray getNamespaceName(ThreadContext context) {
4549
protected IRubyObject getCounter(ThreadContext context, IRubyObject key) {
4650
Objects.requireNonNull(key);
4751
requireRubySymbol(key, "key");
48-
return RubyUtil.toRubyObject(new LongCounter(key.asJavaString()));
52+
return RubyUtil.toRubyObject(metrics.computeIfAbsent(key.asJavaString(), LongCounter::new));
4953
}
5054

5155
@Override
5256
protected IRubyObject getTimer(ThreadContext context, IRubyObject key) {
5357
Objects.requireNonNull(key);
5458
requireRubySymbol(key, "key");
55-
return RubyUtil.toRubyObject(TimerMetric.create(key.asJavaString()));
59+
return RubyUtil.toRubyObject(metrics.computeIfAbsent(key.asJavaString(), TimerMetric::create));
5660
}
5761

5862
@Override
5963
protected IRubyObject getHistogram(ThreadContext context, IRubyObject key) {
6064
Objects.requireNonNull(key);
6165
requireRubySymbol(key, "key");
62-
return RubyUtil.toRubyObject(new HistogramMetric(key.asJavaString()));
66+
return RubyUtil.toRubyObject(metrics.computeIfAbsent(key.asJavaString(), HistogramMetric::new));
6367
}
6468

6569
@Override

0 commit comments

Comments
 (0)