Skip to content

Commit

Permalink
Merge pull request #20 from smrutilal2/enrich
Browse files Browse the repository at this point in the history
Configurable max cardinality per metric
  • Loading branch information
smrutilal2 authored May 4, 2019
2 parents 726e0ee + a0d52eb commit 9044a17
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 19 deletions.
4 changes: 2 additions & 2 deletions core/src/main/java/io/ultrabrew/metrics/Counter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/io/ultrabrew/metrics/Gauge.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/io/ultrabrew/metrics/GaugeDouble.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
89 changes: 81 additions & 8 deletions core/src/main/java/io/ultrabrew/metrics/MetricRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -97,7 +153,24 @@ public Timer timer(final String id) {
* exists
*/
public <T extends Metric> T custom(final String id, final Class<T> 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 <T> 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 extends Metric> T custom(final String id, final Class<T> klass, final int maxCardinality) {
return getOrCreate(id, klass, maxCardinality);
}

/**
Expand All @@ -109,7 +182,7 @@ public void addReporter(final Reporter reporter) {
reporters.add(reporter);
}

private <T extends Metric> T getOrCreate(final String id, final Class<T> klass) {
private <T extends Metric> T getOrCreate(final String id, final Class<T> klass, final int maxCardinality) {
Metric m = measurements.get(id);
if (m != null) {
return tryCast(klass, m);
Expand All @@ -120,8 +193,8 @@ private <T extends Metric> T getOrCreate(final String id, final Class<T> 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 |
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/io/ultrabrew/metrics/Timer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 9044a17

Please sign in to comment.