From a0d52ebe57307337505ed401b868519e56e88a0a Mon Sep 17 00:00:00 2001 From: Smruti Ranjan Sahoo Date: Fri, 3 May 2019 12:57:28 -0700 Subject: [PATCH] Configurable max cardinality per metric --- .../java/io/ultrabrew/metrics/Counter.java | 4 +- .../main/java/io/ultrabrew/metrics/Gauge.java | 4 +- .../io/ultrabrew/metrics/GaugeDouble.java | 4 +- .../io/ultrabrew/metrics/MetricRegistry.java | 89 +++++++++++++++++-- .../main/java/io/ultrabrew/metrics/Timer.java | 4 +- .../ultrabrew/metrics/MetricRegistryTest.java | 2 +- .../metrics/reporters/SLF4JReporterTest.java | 4 +- 7 files changed, 92 insertions(+), 19 deletions(-) diff --git a/core/src/main/java/io/ultrabrew/metrics/Counter.java b/core/src/main/java/io/ultrabrew/metrics/Counter.java index 121e7df..687c90d 100755 --- a/core/src/main/java/io/ultrabrew/metrics/Counter.java +++ b/core/src/main/java/io/ultrabrew/metrics/Counter.java @@ -34,8 +34,8 @@ */ public class Counter extends Metric { - Counter(final MetricRegistry registry, final String id) { - super(registry, id); + Counter(final MetricRegistry registry, final String id, final int maxCardinality) { + super(registry, id, maxCardinality); } /** diff --git a/core/src/main/java/io/ultrabrew/metrics/Gauge.java b/core/src/main/java/io/ultrabrew/metrics/Gauge.java index 8c292af..4314f41 100755 --- a/core/src/main/java/io/ultrabrew/metrics/Gauge.java +++ b/core/src/main/java/io/ultrabrew/metrics/Gauge.java @@ -33,8 +33,8 @@ */ public class Gauge extends Metric { - Gauge(final MetricRegistry registry, final String id) { - super(registry, id); + Gauge(final MetricRegistry registry, final String id, final int maxCardinality) { + super(registry, id, maxCardinality); } /** diff --git a/core/src/main/java/io/ultrabrew/metrics/GaugeDouble.java b/core/src/main/java/io/ultrabrew/metrics/GaugeDouble.java index 5869e2b..56ce9f5 100755 --- a/core/src/main/java/io/ultrabrew/metrics/GaugeDouble.java +++ b/core/src/main/java/io/ultrabrew/metrics/GaugeDouble.java @@ -31,8 +31,8 @@ */ public class GaugeDouble extends Metric { - GaugeDouble(final MetricRegistry registry, final String id) { - super(registry, id); + GaugeDouble(final MetricRegistry registry, final String id, final int maxCardinality) { + super(registry, id, maxCardinality); } /** diff --git a/core/src/main/java/io/ultrabrew/metrics/MetricRegistry.java b/core/src/main/java/io/ultrabrew/metrics/MetricRegistry.java index 9141374..24be3e6 100755 --- a/core/src/main/java/io/ultrabrew/metrics/MetricRegistry.java +++ b/core/src/main/java/io/ultrabrew/metrics/MetricRegistry.java @@ -4,6 +4,8 @@ package io.ultrabrew.metrics; +import static io.ultrabrew.metrics.Metric.DEFAULT_MAX_CARDINALITY; + import java.lang.reflect.InvocationTargetException; import java.util.List; import java.util.Map; @@ -44,7 +46,21 @@ public MetricRegistry() { * exists */ public Counter counter(final String id) { - return getOrCreate(id, Counter.class); + return counter(id, DEFAULT_MAX_CARDINALITY); + } + + /** + * Return the {@link Counter} registered under this id; or create and register a new {@link + * Counter}. + * + * @param id identifier of the measurement + * @param maxCardinality new dimensions will dropped beyond this value + * @return a new or pre-existing {@link Counter} + * @throws IllegalStateException measurement with different type, but same identifier already + * exists + */ + public Counter counter(final String id, final int maxCardinality) { + return getOrCreate(id, Counter.class, maxCardinality); } /** @@ -56,7 +72,20 @@ public Counter counter(final String id) { * exists */ public Gauge gauge(final String id) { - return getOrCreate(id, Gauge.class); + return gauge(id, DEFAULT_MAX_CARDINALITY); + } + + /** + * Return the {@link Gauge} registered under this id; or create and register a new {@link Gauge}. + * + * @param id identifier of the measurement + * @param maxCardinality new dimensions will dropped beyond this value + * @return a new or pre-existing {@link Gauge} + * @throws IllegalStateException measurement with different type, but same identifier already + * exists + */ + public Gauge gauge(final String id, final int maxCardinality) { + return getOrCreate(id, Gauge.class, maxCardinality); } /** @@ -69,7 +98,21 @@ public Gauge gauge(final String id) { * exists */ public GaugeDouble gaugeDouble(final String id) { - return getOrCreate(id, GaugeDouble.class); + return gaugeDouble(id, DEFAULT_MAX_CARDINALITY); + } + + /** + * Return the {@link GaugeDouble} registered under this id; or create and register a new {@link + * GaugeDouble}. + * + * @param id identifier of the measurement + * @param maxCardinality new dimensions will dropped beyond this value + * @return a new or pre-existing {@link GaugeDouble} + * @throws IllegalStateException measurement with different type, but same identifier already + * exists + */ + public GaugeDouble gaugeDouble(final String id, final int maxCardinality) { + return getOrCreate(id, GaugeDouble.class, maxCardinality); } /** @@ -81,7 +124,20 @@ public GaugeDouble gaugeDouble(final String id) { * exists */ public Timer timer(final String id) { - return getOrCreate(id, Timer.class); + return timer(id, DEFAULT_MAX_CARDINALITY); + } + + /** + * Return the {@link Timer} registered under this id; or create and register a new {@link Timer}. + * + * @param id identifier of the measurement + * @param maxCardinality new dimensions will dropped beyond this value + * @return a new or pre-existing {@link Timer} + * @throws IllegalStateException measurement with different type, but same identifier already + * exists + */ + public Timer timer(final String id, final int maxCardinality) { + return getOrCreate(id, Timer.class, maxCardinality); } /** @@ -97,7 +153,24 @@ public Timer timer(final String id) { * exists */ public T custom(final String id, final Class klass) { - return getOrCreate(id, klass); + return custom(id, klass, DEFAULT_MAX_CARDINALITY); + } + + /** + * Return a custom measurement registered under this id; or create and register a new custom + * measurement of given class. The class must have an accessible constructor that takes + * MetricRegistry and String as parameters. + * + * @param id identifier of the measurement + * @param klass custom measurement class extending Metric + * @param maxCardinality new dimensions will dropped beyond this value + * @param custom measurement class extending Metric + * @return a new or pre-existing custom measurement + * @throws IllegalStateException measurement with different type, but same identifier already + * exists + */ + public T custom(final String id, final Class klass, final int maxCardinality) { + return getOrCreate(id, klass, maxCardinality); } /** @@ -109,7 +182,7 @@ public void addReporter(final Reporter reporter) { reporters.add(reporter); } - private T getOrCreate(final String id, final Class klass) { + private T getOrCreate(final String id, final Class klass, final int maxCardinality) { Metric m = measurements.get(id); if (m != null) { return tryCast(klass, m); @@ -120,8 +193,8 @@ private T getOrCreate(final String id, final Class klass) return tryCast(klass, m); } try { - T instance = klass.getDeclaredConstructor(MetricRegistry.class, String.class) - .newInstance(this, id); + T instance = klass.getDeclaredConstructor(MetricRegistry.class, String.class, int.class) + .newInstance(this, id, maxCardinality); measurements.put(id, instance); return instance; } catch (InstantiationException | IllegalAccessException | InvocationTargetException | diff --git a/core/src/main/java/io/ultrabrew/metrics/Timer.java b/core/src/main/java/io/ultrabrew/metrics/Timer.java index 6f7f191..54137d9 100755 --- a/core/src/main/java/io/ultrabrew/metrics/Timer.java +++ b/core/src/main/java/io/ultrabrew/metrics/Timer.java @@ -41,8 +41,8 @@ */ public class Timer extends Metric { - Timer(final MetricRegistry registry, final String id) { - super(registry, id); + Timer(final MetricRegistry registry, final String id, final int maxCardinality) { + super(registry, id, maxCardinality); } /** diff --git a/core/src/test/java/io/ultrabrew/metrics/MetricRegistryTest.java b/core/src/test/java/io/ultrabrew/metrics/MetricRegistryTest.java index c217d98..83dc86e 100755 --- a/core/src/test/java/io/ultrabrew/metrics/MetricRegistryTest.java +++ b/core/src/test/java/io/ultrabrew/metrics/MetricRegistryTest.java @@ -72,7 +72,7 @@ public void testAlreadyDefinedSynchronized() throws InterruptedException { Thread.sleep(10); } - measurements.put("test", new Counter(metricRegistry, "test")); + measurements.put("test", new Counter(metricRegistry, "test", Metric.DEFAULT_MAX_CARDINALITY)); } synchronized (completed) { diff --git a/core/src/test/java/io/ultrabrew/metrics/reporters/SLF4JReporterTest.java b/core/src/test/java/io/ultrabrew/metrics/reporters/SLF4JReporterTest.java index 3d1ff8b..0e59aa9 100755 --- a/core/src/test/java/io/ultrabrew/metrics/reporters/SLF4JReporterTest.java +++ b/core/src/test/java/io/ultrabrew/metrics/reporters/SLF4JReporterTest.java @@ -43,8 +43,8 @@ public void tearDown() { public static class TestMetric extends Metric { - public TestMetric(final MetricRegistry metricRegistry, final String name) { - super(metricRegistry, name); + public TestMetric(final MetricRegistry metricRegistry, final String name, final int maxCardinality) { + super(metricRegistry, name, maxCardinality); } public void send(final long value, final String... tags) {