diff options
Diffstat (limited to 'api')
4 files changed, 142 insertions, 2 deletions
diff --git a/api/src/main/java/io/opencensus/metrics/DerivedDoubleGauge.java b/api/src/main/java/io/opencensus/metrics/DerivedDoubleGauge.java index 10119eef..3aaca153 100644 --- a/api/src/main/java/io/opencensus/metrics/DerivedDoubleGauge.java +++ b/api/src/main/java/io/opencensus/metrics/DerivedDoubleGauge.java @@ -40,7 +40,6 @@ import org.checkerframework.checker.nullness.qual.Nullable; * List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc")); * List<LabelValue> labelValues = Arrays.asList(LabelValue.create("Inbound")); * - * // TODO(mayurkale): Plugs-in the DerivedDoubleGauge into the registry. * DerivedDoubleGauge gauge = metricRegistry.addDerivedDoubleGauge( * "queue_size", "Pending jobs in a queue", "1", labelKeys); * diff --git a/api/src/main/java/io/opencensus/metrics/DerivedLongGauge.java b/api/src/main/java/io/opencensus/metrics/DerivedLongGauge.java index 43943fa6..621873f9 100644 --- a/api/src/main/java/io/opencensus/metrics/DerivedLongGauge.java +++ b/api/src/main/java/io/opencensus/metrics/DerivedLongGauge.java @@ -40,7 +40,6 @@ import org.checkerframework.checker.nullness.qual.Nullable; * List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc")); * List<LabelValue> labelValues = Arrays.asList(LabelValue.create("Inbound")); * - * // TODO(mayurkale): Plugs-in the DerivedLongGauge into the registry. * DerivedLongGauge gauge = metricRegistry.addDerivedLongGauge( * "queue_size", "Pending jobs in a queue", "1", labelKeys); * diff --git a/api/src/main/java/io/opencensus/metrics/MetricRegistry.java b/api/src/main/java/io/opencensus/metrics/MetricRegistry.java index 557f886a..5be15594 100644 --- a/api/src/main/java/io/opencensus/metrics/MetricRegistry.java +++ b/api/src/main/java/io/opencensus/metrics/MetricRegistry.java @@ -17,6 +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.List; @@ -63,6 +65,40 @@ public abstract class MetricRegistry { public abstract DoubleGauge addDoubleGauge( String name, String description, String unit, List<LabelKey> labelKeys); + /** + * Builds a new derived long gauge to be added to the registry. This is more convenient form when + * you want to define a gauge by executing a {@link ToLongFunction} on an object. + * + * @param name the name of the metric. + * @param description the description of the metric. + * @param unit the unit of the metric. + * @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 + */ + @ExperimentalApi + public abstract DerivedLongGauge addDerivedLongGauge( + String name, String description, String unit, List<LabelKey> labelKeys); + + /** + * Builds a new derived double gauge to be added to the registry. This is more convenient form + * when you want to define a gauge by executing a {@link ToDoubleFunction} on an object. + * + * @param name the name of the metric. + * @param description the description of the metric. + * @param unit the unit of the metric. + * @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 + */ + @ExperimentalApi + public abstract DerivedDoubleGauge addDerivedDoubleGauge( + String name, String description, String unit, List<LabelKey> labelKeys); + static MetricRegistry newNoopMetricRegistry() { return new NoopMetricRegistry(); } @@ -92,5 +128,29 @@ public abstract class MetricRegistry { Utils.checkNotNull(unit, "unit"), labelKeys); } + + @Override + public DerivedLongGauge addDerivedLongGauge( + String name, String description, String unit, List<LabelKey> labelKeys) { + Utils.checkListElementNotNull( + Utils.checkNotNull(labelKeys, "labelKeys"), "labelKey element should not be null."); + return DerivedLongGauge.newNoopDerivedLongGauge( + Utils.checkNotNull(name, "name"), + Utils.checkNotNull(description, "description"), + Utils.checkNotNull(unit, "unit"), + labelKeys); + } + + @Override + public DerivedDoubleGauge addDerivedDoubleGauge( + String name, String description, String unit, List<LabelKey> labelKeys) { + Utils.checkListElementNotNull( + Utils.checkNotNull(labelKeys, "labelKeys"), "labelKey element should not be null."); + return DerivedDoubleGauge.newNoopDerivedDoubleGauge( + 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 2de04096..d8a26cc8 100644 --- a/api/src/test/java/io/opencensus/metrics/MetricRegistryTest.java +++ b/api/src/test/java/io/opencensus/metrics/MetricRegistryTest.java @@ -33,6 +33,8 @@ public class MetricRegistryTest { private static final String NAME = "name"; private static final String NAME_2 = "name2"; + private static final String NAME_3 = "name3"; + private static final String NAME_4 = "name4"; private static final String DESCRIPTION = "description"; private static final String UNIT = "1"; private static final List<LabelKey> LABEL_KEY = @@ -115,6 +117,78 @@ public class MetricRegistryTest { } @Test + public void noopAddDerivedLongGauge_NullName() { + thrown.expect(NullPointerException.class); + thrown.expectMessage("name"); + metricRegistry.addDerivedLongGauge(null, DESCRIPTION, UNIT, LABEL_KEY); + } + + @Test + public void noopAddDerivedLongGauge_NullDescription() { + thrown.expect(NullPointerException.class); + thrown.expectMessage("description"); + metricRegistry.addDerivedLongGauge(NAME_3, null, UNIT, LABEL_KEY); + } + + @Test + public void noopAddDerivedLongGauge_NullUnit() { + thrown.expect(NullPointerException.class); + thrown.expectMessage("unit"); + metricRegistry.addDerivedLongGauge(NAME_3, DESCRIPTION, null, LABEL_KEY); + } + + @Test + public void noopAddDerivedLongGauge_NullLabels() { + thrown.expect(NullPointerException.class); + thrown.expectMessage("labelKeys"); + metricRegistry.addDerivedLongGauge(NAME_3, DESCRIPTION, UNIT, null); + } + + @Test + public void noopAddDerivedLongGauge_WithNullElement() { + List<LabelKey> labelKeys = Collections.singletonList(null); + thrown.expect(NullPointerException.class); + thrown.expectMessage("labelKey element should not be null."); + metricRegistry.addDerivedLongGauge(NAME_3, DESCRIPTION, UNIT, labelKeys); + } + + @Test + public void noopAddDerivedDoubleGauge_NullName() { + thrown.expect(NullPointerException.class); + thrown.expectMessage("name"); + metricRegistry.addDerivedDoubleGauge(null, DESCRIPTION, UNIT, LABEL_KEY); + } + + @Test + public void noopAddDerivedDoubleGauge_NullDescription() { + thrown.expect(NullPointerException.class); + thrown.expectMessage("description"); + metricRegistry.addDerivedDoubleGauge(NAME_4, null, UNIT, LABEL_KEY); + } + + @Test + public void noopAddDerivedDoubleGauge_NullUnit() { + thrown.expect(NullPointerException.class); + thrown.expectMessage("unit"); + metricRegistry.addDerivedDoubleGauge(NAME_4, DESCRIPTION, null, LABEL_KEY); + } + + @Test + public void noopAddDerivedDoubleGauge_NullLabels() { + thrown.expect(NullPointerException.class); + thrown.expectMessage("labelKeys"); + metricRegistry.addDerivedDoubleGauge(NAME_4, DESCRIPTION, UNIT, null); + } + + @Test + public void noopAddDerivedDoubleGauge_WithNullElement() { + List<LabelKey> labelKeys = Collections.singletonList(null); + thrown.expect(NullPointerException.class); + thrown.expectMessage("labelKey element should not be null."); + metricRegistry.addDerivedDoubleGauge(NAME_4, DESCRIPTION, UNIT, labelKeys); + } + + @Test public void noopSameAs() { LongGauge longGauge = metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY); assertThat(longGauge.getDefaultTimeSeries()).isSameAs(longGauge.getDefaultTimeSeries()); @@ -134,5 +208,13 @@ public class MetricRegistryTest { assertThat(metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEY)) .isInstanceOf( DoubleGauge.newNoopDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEY).getClass()); + assertThat(metricRegistry.addDerivedLongGauge(NAME_3, DESCRIPTION, UNIT, LABEL_KEY)) + .isInstanceOf( + DerivedLongGauge.newNoopDerivedLongGauge(NAME_3, DESCRIPTION, UNIT, LABEL_KEY) + .getClass()); + assertThat(metricRegistry.addDerivedDoubleGauge(NAME_4, DESCRIPTION, UNIT, LABEL_KEY)) + .isInstanceOf( + DerivedDoubleGauge.newNoopDerivedDoubleGauge(NAME_4, DESCRIPTION, UNIT, LABEL_KEY) + .getClass()); } } |