aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMayur Kale <mayurkale@google.com>2018-10-17 14:15:48 -0700
committerGitHub <noreply@github.com>2018-10-17 14:15:48 -0700
commit2b11b167c8d7633acaacc0c6aca6b829e8f4df6d (patch)
treed897760da801edea5416c78eab6ff87c9c3ea81f
parente56a976d242d34b65e224f74d91fbafec83f96ad (diff)
downloadopencensus-java-2b11b167c8d7633acaacc0c6aca6b829e8f4df6d.tar.gz
Plugs-in the LongGauge into the registry (#1498)
* plug-in longGauge into MetricRegistry * Minor fix * Add TODO and Fix build * Fix review comments
-rw-r--r--api/src/main/java/io/opencensus/metrics/DoubleGauge.java4
-rw-r--r--api/src/main/java/io/opencensus/metrics/LongGauge.java8
-rw-r--r--api/src/main/java/io/opencensus/metrics/MetricRegistry.java81
-rw-r--r--api/src/test/java/io/opencensus/metrics/MetricRegistryTest.java163
-rw-r--r--impl_core/src/main/java/io/opencensus/implcore/metrics/MetricRegistryImpl.java91
-rw-r--r--impl_core/src/test/java/io/opencensus/implcore/metrics/MetricRegistryImplTest.java286
6 files changed, 134 insertions, 499 deletions
diff --git a/api/src/main/java/io/opencensus/metrics/DoubleGauge.java b/api/src/main/java/io/opencensus/metrics/DoubleGauge.java
index 91e131ec..62730a06 100644
--- a/api/src/main/java/io/opencensus/metrics/DoubleGauge.java
+++ b/api/src/main/java/io/opencensus/metrics/DoubleGauge.java
@@ -86,12 +86,12 @@ public abstract class DoubleGauge {
* method for manual operations.
*
* @param labelValues the list of label values. The number of label values must be the same to
- * that of the label keys passed to {@link MetricRegistry#addDoubleGauge}.
+ * that of the label keys.
* @return a {@code DoublePoint} the value of single gauge.
* @throws NullPointerException if {@code labelValues} is null OR any element of {@code
* labelValues} is null.
* @throws IllegalArgumentException if number of {@code labelValues}s are not equal to the label
- * keys passed to {@link MetricRegistry#addDoubleGauge}.
+ * keys.
* @since 0.17
*/
public abstract DoublePoint getOrCreateTimeSeries(List<LabelValue> labelValues);
diff --git a/api/src/main/java/io/opencensus/metrics/LongGauge.java b/api/src/main/java/io/opencensus/metrics/LongGauge.java
index 42951d50..f473a8ac 100644
--- a/api/src/main/java/io/opencensus/metrics/LongGauge.java
+++ b/api/src/main/java/io/opencensus/metrics/LongGauge.java
@@ -32,7 +32,7 @@ import javax.annotation.concurrent.ThreadSafe;
* private static final MetricRegistry metricRegistry = Metrics.getMetricRegistry();
*
* List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc"));
- * // TODO(mayurkale): Plugs-in the LongGauge into the registry.
+ *
* LongGauge gauge = metricRegistry.addLongGauge("queue_size", "Pending jobs", "1", labelKeys);
*
* // It is recommended to keep a reference of a point for manual operations.
@@ -56,7 +56,6 @@ import javax.annotation.concurrent.ThreadSafe;
* List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc"));
* List<LabelValue> labelValues = Arrays.asList(LabelValue.create("Inbound"));
*
- * // TODO(mayurkale): Plugs-in the LongGauge into the registry.
* LongGauge gauge = metricRegistry.addLongGauge("queue_size", "Pending jobs", "1", labelKeys);
*
* // It is recommended to keep a reference of a point for manual operations.
@@ -167,11 +166,6 @@ public abstract class LongGauge {
/** Creates a new {@code NoopLongPoint}. */
NoopLongGauge(String name, String description, String unit, List<LabelKey> labelKeys) {
- Utils.checkNotNull(name, "name");
- Utils.checkNotNull(description, "description");
- Utils.checkNotNull(unit, "unit");
- Utils.checkNotNull(labelKeys, "labelKeys should not be null.");
- Utils.checkListElementNotNull(labelKeys, "labelKeys element should not be null.");
labelKeysSize = labelKeys.size();
}
diff --git a/api/src/main/java/io/opencensus/metrics/MetricRegistry.java b/api/src/main/java/io/opencensus/metrics/MetricRegistry.java
index a9987b75..840ad787 100644
--- a/api/src/main/java/io/opencensus/metrics/MetricRegistry.java
+++ b/api/src/main/java/io/opencensus/metrics/MetricRegistry.java
@@ -17,10 +17,8 @@
package io.opencensus.metrics;
import io.opencensus.common.ExperimentalApi;
-import io.opencensus.common.ToDoubleFunction;
-import io.opencensus.common.ToLongFunction;
import io.opencensus.internal.Utils;
-import java.util.LinkedHashMap;
+import java.util.List;
/**
* Creates and manages your application's set of metrics. The default implementation of this creates
@@ -32,44 +30,21 @@ import java.util.LinkedHashMap;
@ExperimentalApi
public abstract class MetricRegistry {
/**
- * Build a new long gauge to be added to the registry.
- *
- * <p>Must be called only once.
+ * Builds a new long gauge to be added to the registry. This is more convenient form when you want
+ * to manually increase and decrease values as per your service requirements.
*
* @param name the name of the metric.
* @param description the description of the metric.
* @param unit the unit of the metric.
- * @param obj the function argument.
- * @param function the function to be called.
+ * @param labelKeys the list of the label keys.
+ * @throws NullPointerException if {@code labelKeys} is null OR any element of {@code labelKeys}
+ * is null OR {@code name}, {@code description}, {@code unit} is null.
+ * @throws IllegalArgumentException if different metric with the same name already registered.
* @since 0.17
*/
- public abstract <T> void addLongGauge(
- String name,
- String description,
- String unit,
- LinkedHashMap<LabelKey, LabelValue> labels,
- T obj,
- ToLongFunction<T> function);
-
- /**
- * Build a new double gauge to be added to the registry.
- *
- * <p>Must be called only once.
- *
- * @param name the name of the metric.
- * @param description the description of the metric.
- * @param unit the unit of the metric.
- * @param obj the function argument.
- * @param function the function to be called.
- * @since 0.17
- */
- public abstract <T> void addDoubleGauge(
- String name,
- String description,
- String unit,
- LinkedHashMap<LabelKey, LabelValue> labels,
- T obj,
- ToDoubleFunction<T> function);
+ @ExperimentalApi
+ public abstract LongGauge addLongGauge(
+ String name, String description, String unit, List<LabelKey> labelKeys);
static MetricRegistry newNoopMetricRegistry() {
return new NoopMetricRegistry();
@@ -78,33 +53,15 @@ public abstract class MetricRegistry {
private static final class NoopMetricRegistry extends MetricRegistry {
@Override
- public <T> void addLongGauge(
- String name,
- String description,
- String unit,
- LinkedHashMap<LabelKey, LabelValue> labels,
- T obj,
- ToLongFunction<T> function) {
- Utils.checkNotNull(name, "name");
- Utils.checkNotNull(description, "description");
- Utils.checkNotNull(unit, "unit");
- Utils.checkNotNull(labels, "labels");
- Utils.checkNotNull(function, "function");
- }
-
- @Override
- public <T> void addDoubleGauge(
- String name,
- String description,
- String unit,
- LinkedHashMap<LabelKey, LabelValue> labels,
- T obj,
- ToDoubleFunction<T> function) {
- Utils.checkNotNull(name, "name");
- Utils.checkNotNull(description, "description");
- Utils.checkNotNull(unit, "unit");
- Utils.checkNotNull(labels, "labels");
- Utils.checkNotNull(function, "function");
+ public LongGauge addLongGauge(
+ String name, String description, String unit, List<LabelKey> labelKeys) {
+ Utils.checkListElementNotNull(
+ Utils.checkNotNull(labelKeys, "labelKeys"), "labelKey element should not be null.");
+ return LongGauge.newNoopLongGauge(
+ Utils.checkNotNull(name, "name"),
+ Utils.checkNotNull(description, "description"),
+ Utils.checkNotNull(unit, "unit"),
+ labelKeys);
}
}
}
diff --git a/api/src/test/java/io/opencensus/metrics/MetricRegistryTest.java b/api/src/test/java/io/opencensus/metrics/MetricRegistryTest.java
index 49e8ce02..0d90fa97 100644
--- a/api/src/test/java/io/opencensus/metrics/MetricRegistryTest.java
+++ b/api/src/test/java/io/opencensus/metrics/MetricRegistryTest.java
@@ -16,9 +16,10 @@
package io.opencensus.metrics;
-import io.opencensus.common.ToDoubleFunction;
-import io.opencensus.common.ToLongFunction;
-import java.util.LinkedHashMap;
+import static com.google.common.truth.Truth.assertThat;
+
+import java.util.Collections;
+import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -30,156 +31,64 @@ import org.junit.runners.JUnit4;
public class MetricRegistryTest {
@Rule public ExpectedException thrown = ExpectedException.none();
+ private static final String NAME = "name";
+ private static final String DESCRIPTION = "description";
+ private static final String UNIT = "1";
+ private static final List<LabelKey> LABEL_KEY =
+ Collections.singletonList(LabelKey.create("key", "key description"));
+ private static final List<LabelValue> LABEL_VALUES =
+ Collections.singletonList(LabelValue.create("value"));
private final MetricRegistry metricRegistry =
MetricsComponent.newNoopMetricsComponent().getMetricRegistry();
@Test
- public void addDoubleGauge_NullName() {
- thrown.expect(NullPointerException.class);
- metricRegistry.addDoubleGauge(
- null,
- "description",
- "1",
- new LinkedHashMap<LabelKey, LabelValue>(),
- null,
- new ToDoubleFunction<Object>() {
- @Override
- public double applyAsDouble(Object value) {
- return 5.0;
- }
- });
- }
-
- @Test
- public void addDoubleGauge_NullDescription() {
+ public void noopAddLongGauge_NullName() {
thrown.expect(NullPointerException.class);
- metricRegistry.addDoubleGauge(
- "name",
- null,
- "1",
- new LinkedHashMap<LabelKey, LabelValue>(),
- null,
- new ToDoubleFunction<Object>() {
- @Override
- public double applyAsDouble(Object value) {
- return 5.0;
- }
- });
+ thrown.expectMessage("name");
+ metricRegistry.addLongGauge(null, DESCRIPTION, UNIT, LABEL_KEY);
}
@Test
- public void addDoubleGauge_NullUnit() {
+ public void noopAddLongGauge_NullDescription() {
thrown.expect(NullPointerException.class);
- metricRegistry.addDoubleGauge(
- "name",
- "description",
- null,
- new LinkedHashMap<LabelKey, LabelValue>(),
- null,
- new ToDoubleFunction<Object>() {
- @Override
- public double applyAsDouble(Object value) {
- return 5.0;
- }
- });
+ thrown.expectMessage("description");
+ metricRegistry.addLongGauge(NAME, null, UNIT, LABEL_KEY);
}
@Test
- public void addDoubleGauge_NullLabels() {
+ public void noopAddLongGauge_NullUnit() {
thrown.expect(NullPointerException.class);
- metricRegistry.addDoubleGauge(
- "name",
- "description",
- "1",
- null,
- null,
- new ToDoubleFunction<Object>() {
- @Override
- public double applyAsDouble(Object value) {
- return 5.0;
- }
- });
+ thrown.expectMessage("unit");
+ metricRegistry.addLongGauge(NAME, DESCRIPTION, null, LABEL_KEY);
}
@Test
- public void addDoubleGauge_NullFunction() {
+ public void noopAddLongGauge_NullLabels() {
thrown.expect(NullPointerException.class);
- metricRegistry.addDoubleGauge(
- "name", "description", "1", new LinkedHashMap<LabelKey, LabelValue>(), null, null);
+ thrown.expectMessage("labelKeys");
+ metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, null);
}
@Test
- public void addLongGauge_NullName() {
+ public void noopAddLongGauge_WithNullElement() {
+ List<LabelKey> labelKeys = Collections.singletonList(null);
thrown.expect(NullPointerException.class);
- metricRegistry.addLongGauge(
- null,
- "description",
- "1",
- new LinkedHashMap<LabelKey, LabelValue>(),
- null,
- new ToLongFunction<Object>() {
- @Override
- public long applyAsLong(Object value) {
- return 5;
- }
- });
+ thrown.expectMessage("labelKey element should not be null.");
+ metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, labelKeys);
}
@Test
- public void addLongGauge_NullDescription() {
- thrown.expect(NullPointerException.class);
- metricRegistry.addLongGauge(
- "name",
- null,
- "1",
- new LinkedHashMap<LabelKey, LabelValue>(),
- null,
- new ToLongFunction<Object>() {
- @Override
- public long applyAsLong(Object value) {
- return 5;
- }
- });
+ public void noopSameAs() {
+ LongGauge longGauge = metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
+ assertThat(longGauge.getDefaultTimeSeries()).isSameAs(longGauge.getDefaultTimeSeries());
+ assertThat(longGauge.getDefaultTimeSeries())
+ .isSameAs(longGauge.getOrCreateTimeSeries(LABEL_VALUES));
}
@Test
- public void addLongGauge_NullUnit() {
- thrown.expect(NullPointerException.class);
- metricRegistry.addLongGauge(
- "name",
- "description",
- null,
- new LinkedHashMap<LabelKey, LabelValue>(),
- null,
- new ToLongFunction<Object>() {
- @Override
- public long applyAsLong(Object value) {
- return 5;
- }
- });
- }
-
- @Test
- public void addLongGauge_NullLabels() {
- thrown.expect(NullPointerException.class);
- metricRegistry.addLongGauge(
- "name",
- "description",
- "1",
- null,
- null,
- new ToLongFunction<Object>() {
- @Override
- public long applyAsLong(Object value) {
- return 5;
- }
- });
- }
-
- @Test
- public void addLongGauge_NullFunction() {
- thrown.expect(NullPointerException.class);
- metricRegistry.addLongGauge(
- "name", "description", "1", new LinkedHashMap<LabelKey, LabelValue>(), null, null);
+ public void noopInstanceOf() {
+ LongGauge longGauge = metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
+ assertThat(longGauge)
+ .isInstanceOf(LongGauge.newNoopLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY).getClass());
}
}
diff --git a/impl_core/src/main/java/io/opencensus/implcore/metrics/MetricRegistryImpl.java b/impl_core/src/main/java/io/opencensus/implcore/metrics/MetricRegistryImpl.java
index 3a30fd38..8f8dab2b 100644
--- a/impl_core/src/main/java/io/opencensus/implcore/metrics/MetricRegistryImpl.java
+++ b/impl_core/src/main/java/io/opencensus/implcore/metrics/MetricRegistryImpl.java
@@ -19,12 +19,9 @@ package io.opencensus.implcore.metrics;
import static com.google.common.base.Preconditions.checkNotNull;
import io.opencensus.common.Clock;
-import io.opencensus.common.ToDoubleFunction;
-import io.opencensus.common.ToLongFunction;
-import io.opencensus.implcore.metrics.Gauge.DoubleGauge;
-import io.opencensus.implcore.metrics.Gauge.LongGauge;
+import io.opencensus.implcore.internal.Utils;
import io.opencensus.metrics.LabelKey;
-import io.opencensus.metrics.LabelValue;
+import io.opencensus.metrics.LongGauge;
import io.opencensus.metrics.MetricRegistry;
import io.opencensus.metrics.export.Metric;
import io.opencensus.metrics.export.MetricProducer;
@@ -32,8 +29,8 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
-import java.util.Set;
+import java.util.List;
+import java.util.Map;
/** Implementation of {@link MetricRegistry}. */
public final class MetricRegistryImpl extends MetricRegistry {
@@ -46,56 +43,38 @@ public final class MetricRegistryImpl extends MetricRegistry {
}
@Override
- public <T> void addLongGauge(
- String name,
- String description,
- String unit,
- LinkedHashMap<LabelKey, LabelValue> labels,
- T obj,
- ToLongFunction<T> function) {
- checkNotNull(labels, "labels");
- registeredMeters.registerMeter(
- new LongGauge<T>(
+ public LongGauge addLongGauge(
+ String name, String description, String unit, List<LabelKey> labelKeys) {
+ Utils.checkListElementNotNull(
+ checkNotNull(labelKeys, "labelKeys"), "labelKey element should not be null.");
+ LongGaugeImpl longGaugeMetric =
+ new LongGaugeImpl(
checkNotNull(name, "name"),
checkNotNull(description, "description"),
checkNotNull(unit, "unit"),
- Collections.unmodifiableList(new ArrayList<LabelKey>(labels.keySet())),
- Collections.unmodifiableList(new ArrayList<LabelValue>(labels.values())),
- obj,
- checkNotNull(function, "function")));
- }
-
- @Override
- public <T> void addDoubleGauge(
- String name,
- String description,
- String unit,
- LinkedHashMap<LabelKey, LabelValue> labels,
- T obj,
- ToDoubleFunction<T> function) {
- checkNotNull(labels, "labels");
- registeredMeters.registerMeter(
- new DoubleGauge<T>(
- checkNotNull(name, "name"),
- checkNotNull(description, "description"),
- checkNotNull(unit, "unit"),
- Collections.unmodifiableList(new ArrayList<LabelKey>(labels.keySet())),
- Collections.unmodifiableList(new ArrayList<LabelValue>(labels.values())),
- obj,
- checkNotNull(function, "function")));
+ Collections.unmodifiableList(new ArrayList<LabelKey>(labelKeys)));
+ registeredMeters.registerMeter(name, longGaugeMetric);
+ return longGaugeMetric;
}
private static final class RegisteredMeters {
- private volatile Set<Gauge> registeredGauges = Collections.emptySet();
+ private volatile Map<String, Meter> registeredMeters = Collections.emptyMap();
- private Set<Gauge> getRegisteredMeters() {
- return registeredGauges;
+ private Map<String, Meter> getRegisteredMeters() {
+ return registeredMeters;
}
- private synchronized void registerMeter(Gauge gauge) {
- Set<Gauge> newGaguesList = new LinkedHashSet<Gauge>(registeredGauges);
- newGaguesList.add(gauge);
- registeredGauges = Collections.unmodifiableSet(newGaguesList);
+ private synchronized void registerMeter(String meterName, Meter meter) {
+ Meter existingMeter = registeredMeters.get(meterName);
+ if (existingMeter != null) {
+ // TODO(mayurkale): Allow users to register the same Meter multiple times without exception.
+ throw new IllegalArgumentException(
+ "A different metric with the same name already registered.");
+ }
+
+ Map<String, Meter> registeredMetersCopy = new LinkedHashMap<String, Meter>(registeredMeters);
+ registeredMetersCopy.put(meterName, meter);
+ registeredMeters = Collections.unmodifiableMap(registeredMetersCopy);
}
}
@@ -110,14 +89,18 @@ public final class MetricRegistryImpl extends MetricRegistry {
@Override
public Collection<Metric> getMetrics() {
- // Get a snapshot of the current registered gauges.
- Set<Gauge> gaguges = registeredMeters.getRegisteredMeters();
- if (gaguges.isEmpty()) {
+ // Get a snapshot of the current registered meters.
+ Map<String, Meter> meters = registeredMeters.getRegisteredMeters();
+ if (meters.isEmpty()) {
return Collections.emptyList();
}
- ArrayList<Metric> metrics = new ArrayList<Metric>();
- for (Gauge gauge : gaguges) {
- metrics.add(gauge.getMetric(clock));
+
+ List<Metric> metrics = new ArrayList<Metric>(meters.size());
+ for (Map.Entry<String, Meter> entry : meters.entrySet()) {
+ Metric metric = entry.getValue().getMetric(clock);
+ if (metric != null) {
+ metrics.add(metric);
+ }
}
return metrics;
}
diff --git a/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricRegistryImplTest.java b/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricRegistryImplTest.java
index 5210b266..c7cd99be 100644
--- a/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricRegistryImplTest.java
+++ b/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricRegistryImplTest.java
@@ -19,10 +19,9 @@ package io.opencensus.implcore.metrics;
import static com.google.common.truth.Truth.assertThat;
import io.opencensus.common.Timestamp;
-import io.opencensus.common.ToDoubleFunction;
-import io.opencensus.common.ToLongFunction;
import io.opencensus.metrics.LabelKey;
import io.opencensus.metrics.LabelValue;
+import io.opencensus.metrics.LongGauge;
import io.opencensus.metrics.export.Metric;
import io.opencensus.metrics.export.MetricDescriptor;
import io.opencensus.metrics.export.MetricDescriptor.Type;
@@ -30,9 +29,9 @@ import io.opencensus.metrics.export.Point;
import io.opencensus.metrics.export.TimeSeries;
import io.opencensus.metrics.export.Value;
import io.opencensus.testing.common.TestClock;
+import java.util.Collection;
import java.util.Collections;
-import java.util.LinkedHashMap;
-import org.junit.Before;
+import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -42,289 +41,82 @@ import org.junit.runners.JUnit4;
/** Unit tests for {@link MetricRegistryImpl}. */
@RunWith(JUnit4.class)
public class MetricRegistryImplTest {
+ @Rule public ExpectedException thrown = ExpectedException.none();
+
private static final String NAME = "name";
private static final String DESCRIPTION = "description";
private static final String UNIT = "1";
- private static final LabelKey LABEL_KEY = LabelKey.create("key", "key description");
- private static final LabelValue LABEL_VALUES = LabelValue.create("value");
- private static final Timestamp TEST_TIME = Timestamp.create(1234, 123);
-
- @Rule public ExpectedException thrown = ExpectedException.none();
+ private static final List<LabelKey> LABEL_KEY =
+ Collections.singletonList(LabelKey.create("key", "key description"));
+ private static final List<LabelValue> LABEL_VALUES =
+ Collections.singletonList(LabelValue.create("value"));
+ private static final Timestamp TEST_TIME = Timestamp.create(1234, 123);
private final TestClock testClock = TestClock.create(TEST_TIME);
private final MetricRegistryImpl metricRegistry = new MetricRegistryImpl(testClock);
- private final LinkedHashMap<LabelKey, LabelValue> labels =
- new LinkedHashMap<LabelKey, LabelValue>();
-
- @Before
- public void setUp() {
- labels.put(LABEL_KEY, LABEL_VALUES);
- }
- @Test
- public void addDoubleGauge_NullName() {
- thrown.expect(NullPointerException.class);
- metricRegistry.addDoubleGauge(
- null,
- DESCRIPTION,
- UNIT,
- labels,
- null,
- new ToDoubleFunction<Object>() {
- @Override
- public double applyAsDouble(Object value) {
- return 5.0;
- }
- });
- }
-
- @Test
- public void addDoubleGauge_NullDescription() {
- thrown.expect(NullPointerException.class);
- metricRegistry.addDoubleGauge(
- NAME,
- null,
- UNIT,
- labels,
- null,
- new ToDoubleFunction<Object>() {
- @Override
- public double applyAsDouble(Object value) {
- return 5.0;
- }
- });
- }
-
- @Test
- public void addDoubleGauge_NullUnit() {
- thrown.expect(NullPointerException.class);
- metricRegistry.addDoubleGauge(
- NAME,
- DESCRIPTION,
- null,
- labels,
- null,
- new ToDoubleFunction<Object>() {
- @Override
- public double applyAsDouble(Object value) {
- return 5.0;
- }
- });
- }
-
- @Test
- public void addDoubleGauge_NullLabels() {
- thrown.expect(NullPointerException.class);
- metricRegistry.addDoubleGauge(
- NAME,
- DESCRIPTION,
- UNIT,
- null,
- null,
- new ToDoubleFunction<Object>() {
- @Override
- public double applyAsDouble(Object value) {
- return 5.0;
- }
- });
- }
-
- @Test
- public void addDoubleGauge_NullFunction() {
- thrown.expect(NullPointerException.class);
- metricRegistry.addDoubleGauge(NAME, DESCRIPTION, UNIT, labels, null, null);
- }
-
- @Test
- public void addDoubleGauge_GetMetrics() {
- metricRegistry.addDoubleGauge(
- NAME,
- DESCRIPTION,
- UNIT,
- labels,
- null,
- new ToDoubleFunction<Object>() {
- @Override
- public double applyAsDouble(Object value) {
- return 5.0;
- }
- });
- assertThat(metricRegistry.getMetricProducer().getMetrics())
- .containsExactly(
- Metric.create(
- MetricDescriptor.create(
- NAME,
- DESCRIPTION,
- UNIT,
- Type.GAUGE_DOUBLE,
- Collections.unmodifiableList(Collections.singletonList(LABEL_KEY))),
- Collections.singletonList(
- TimeSeries.createWithOnePoint(
- Collections.unmodifiableList(Collections.singletonList(LABEL_VALUES)),
- Point.create(Value.doubleValue(5.0), TEST_TIME),
- null))));
- }
+ private static final MetricDescriptor METRIC_DESCRIPTOR =
+ MetricDescriptor.create(NAME, DESCRIPTION, UNIT, Type.GAUGE_INT64, LABEL_KEY);
@Test
public void addLongGauge_NullName() {
thrown.expect(NullPointerException.class);
- metricRegistry.addLongGauge(
- null,
- DESCRIPTION,
- UNIT,
- labels,
- null,
- new ToLongFunction<Object>() {
- @Override
- public long applyAsLong(Object value) {
- return 7;
- }
- });
+ thrown.expectMessage("name");
+ metricRegistry.addLongGauge(null, DESCRIPTION, UNIT, LABEL_KEY);
}
@Test
public void addLongGauge_NullDescription() {
thrown.expect(NullPointerException.class);
- metricRegistry.addLongGauge(
- NAME,
- null,
- UNIT,
- labels,
- null,
- new ToLongFunction<Object>() {
- @Override
- public long applyAsLong(Object value) {
- return 5;
- }
- });
+ thrown.expectMessage("description");
+ metricRegistry.addLongGauge(NAME, null, UNIT, LABEL_KEY);
}
@Test
public void addLongGauge_NullUnit() {
thrown.expect(NullPointerException.class);
- metricRegistry.addLongGauge(
- NAME,
- DESCRIPTION,
- null,
- labels,
- null,
- new ToLongFunction<Object>() {
- @Override
- public long applyAsLong(Object value) {
- return 5;
- }
- });
+ thrown.expectMessage("unit");
+ metricRegistry.addLongGauge(NAME, DESCRIPTION, null, LABEL_KEY);
}
@Test
public void addLongGauge_NullLabels() {
thrown.expect(NullPointerException.class);
- metricRegistry.addLongGauge(
- NAME,
- DESCRIPTION,
- UNIT,
- null,
- null,
- new ToLongFunction<Object>() {
- @Override
- public long applyAsLong(Object value) {
- return 5;
- }
- });
+ thrown.expectMessage("labelKeys");
+ metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, null);
}
@Test
- public void addLongGauge_NullFunction() {
+ public void addLongGauge_WithNullElement() {
+ List<LabelKey> labelKeys = Collections.singletonList(null);
thrown.expect(NullPointerException.class);
- metricRegistry.addLongGauge("name", DESCRIPTION, UNIT, labels, null, null);
+ thrown.expectMessage("labelKey element should not be null.");
+ metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, labelKeys);
}
@Test
public void addLongGauge_GetMetrics() {
- metricRegistry.addLongGauge(
- NAME,
- DESCRIPTION,
- UNIT,
- labels,
- null,
- new ToLongFunction<Object>() {
- @Override
- public long applyAsLong(Object value) {
- return 7;
- }
- });
- assertThat(metricRegistry.getMetricProducer().getMetrics())
- .containsExactly(
- Metric.create(
- MetricDescriptor.create(
- NAME,
- DESCRIPTION,
- UNIT,
- Type.GAUGE_INT64,
- Collections.unmodifiableList(Collections.singletonList(LABEL_KEY))),
- Collections.singletonList(
- TimeSeries.createWithOnePoint(
- Collections.unmodifiableList(Collections.singletonList(LABEL_VALUES)),
- Point.create(Value.longValue(7), TEST_TIME),
- null))));
- }
+ LongGauge longGauge = metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
+ longGauge.getOrCreateTimeSeries(LABEL_VALUES);
- @Test
- public void multipleMetrics_GetMetrics() {
- metricRegistry.addLongGauge(
- NAME,
- DESCRIPTION,
- UNIT,
- labels,
- null,
- new ToLongFunction<Object>() {
- @Override
- public long applyAsLong(Object value) {
- return 7;
- }
- });
- metricRegistry.addDoubleGauge(
- NAME,
- DESCRIPTION,
- UNIT,
- labels,
- null,
- new ToDoubleFunction<Object>() {
- @Override
- public double applyAsDouble(Object value) {
- return 5.0;
- }
- });
- assertThat(metricRegistry.getMetricProducer().getMetrics())
+ Collection<Metric> metricCollections = metricRegistry.getMetricProducer().getMetrics();
+ assertThat(metricCollections.size()).isEqualTo(1);
+ assertThat(metricCollections)
.containsExactly(
- Metric.create(
- MetricDescriptor.create(
- NAME,
- DESCRIPTION,
- UNIT,
- Type.GAUGE_INT64,
- Collections.unmodifiableList(Collections.singletonList(LABEL_KEY))),
- Collections.singletonList(
- TimeSeries.createWithOnePoint(
- Collections.unmodifiableList(Collections.singletonList(LABEL_VALUES)),
- Point.create(Value.longValue(7), TEST_TIME),
- null))),
- Metric.create(
- MetricDescriptor.create(
- NAME,
- DESCRIPTION,
- UNIT,
- Type.GAUGE_DOUBLE,
- Collections.unmodifiableList(Collections.singletonList(LABEL_KEY))),
- Collections.singletonList(
- TimeSeries.createWithOnePoint(
- Collections.unmodifiableList(Collections.singletonList(LABEL_VALUES)),
- Point.create(Value.doubleValue(5.0), TEST_TIME),
- null))));
+ Metric.createWithOneTimeSeries(
+ METRIC_DESCRIPTOR,
+ TimeSeries.createWithOnePoint(
+ LABEL_VALUES, Point.create(Value.longValue(0), TEST_TIME), null)));
}
@Test
public void empty_GetMetrics() {
assertThat(metricRegistry.getMetricProducer().getMetrics()).isEmpty();
}
+
+ @Test
+ public void checkInstanceOf() {
+ LongGauge longGauge = metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
+ assertThat(longGauge).isInstanceOf(LongGaugeImpl.class);
+ }
}