diff options
author | Mayur Kale <mayurkale@google.com> | 2018-10-17 22:09:40 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-17 22:09:40 -0700 |
commit | 30b613fcbf329bf130ae18267b431c4f820bbe28 (patch) | |
tree | 327465dd6ad7c9d34815ed0baa25209711560027 /api | |
parent | 2f53b977da6b10b7f8e8c29d83f0478d470b229c (diff) | |
download | opencensus-java-30b613fcbf329bf130ae18267b431c4f820bbe28.tar.gz |
Plugs-in the DoubleGauge into the registry (#1503)
Diffstat (limited to 'api')
3 files changed, 77 insertions, 5 deletions
diff --git a/api/src/main/java/io/opencensus/metrics/DoubleGauge.java b/api/src/main/java/io/opencensus/metrics/DoubleGauge.java index de2b053b..32759973 100644 --- a/api/src/main/java/io/opencensus/metrics/DoubleGauge.java +++ b/api/src/main/java/io/opencensus/metrics/DoubleGauge.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 DoubleGauge into the registry. + * * DoubleGauge gauge = metricRegistry.addDoubleGauge("queue_size", * "Pending jobs", "1", labelKeys); * @@ -57,7 +57,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 DoubleGauge into the registry. * DoubleGauge gauge = metricRegistry.addDoubleGauge("queue_size", * "Pending jobs", "1", labelKeys); * @@ -86,7 +85,7 @@ 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. + * that of the label keys passed to {@link MetricRegistry#addDoubleGauge}. * @return a {@code DoublePoint} the value of single gauge. * @throws NullPointerException if {@code labelValues} is null OR any element of {@code * labelValues} is null. diff --git a/api/src/main/java/io/opencensus/metrics/MetricRegistry.java b/api/src/main/java/io/opencensus/metrics/MetricRegistry.java index 840ad787..557f886a 100644 --- a/api/src/main/java/io/opencensus/metrics/MetricRegistry.java +++ b/api/src/main/java/io/opencensus/metrics/MetricRegistry.java @@ -46,6 +46,23 @@ public abstract class MetricRegistry { public abstract LongGauge addLongGauge( String name, String description, String unit, List<LabelKey> labelKeys); + /** + * Builds a new double 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 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 DoubleGauge addDoubleGauge( + String name, String description, String unit, List<LabelKey> labelKeys); + static MetricRegistry newNoopMetricRegistry() { return new NoopMetricRegistry(); } @@ -63,5 +80,17 @@ public abstract class MetricRegistry { Utils.checkNotNull(unit, "unit"), labelKeys); } + + @Override + public DoubleGauge addDoubleGauge( + String name, String description, String unit, List<LabelKey> labelKeys) { + Utils.checkListElementNotNull( + Utils.checkNotNull(labelKeys, "labelKeys"), "labelKey element should not be null."); + return DoubleGauge.newNoopDoubleGauge( + 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 0d90fa97..2de04096 100644 --- a/api/src/test/java/io/opencensus/metrics/MetricRegistryTest.java +++ b/api/src/test/java/io/opencensus/metrics/MetricRegistryTest.java @@ -32,6 +32,7 @@ public class MetricRegistryTest { @Rule public ExpectedException thrown = ExpectedException.none(); private static final String NAME = "name"; + private static final String NAME_2 = "name2"; private static final String DESCRIPTION = "description"; private static final String UNIT = "1"; private static final List<LabelKey> LABEL_KEY = @@ -78,17 +79,60 @@ public class MetricRegistryTest { } @Test + public void noopAddDoubleGauge_NullName() { + thrown.expect(NullPointerException.class); + thrown.expectMessage("name"); + metricRegistry.addDoubleGauge(null, DESCRIPTION, UNIT, LABEL_KEY); + } + + @Test + public void noopAddDoubleGauge_NullDescription() { + thrown.expect(NullPointerException.class); + thrown.expectMessage("description"); + metricRegistry.addDoubleGauge(NAME_2, null, UNIT, LABEL_KEY); + } + + @Test + public void noopAddDoubleGauge_NullUnit() { + thrown.expect(NullPointerException.class); + thrown.expectMessage("unit"); + metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, null, LABEL_KEY); + } + + @Test + public void noopAddDoubleGauge_NullLabels() { + thrown.expect(NullPointerException.class); + thrown.expectMessage("labelKeys"); + metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, null); + } + + @Test + public void noopAddDoubleGauge_WithNullElement() { + List<LabelKey> labelKeys = Collections.singletonList(null); + thrown.expect(NullPointerException.class); + thrown.expectMessage("labelKey element should not be null."); + metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, labelKeys); + } + + @Test 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)); + + DoubleGauge doubleGauge = metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEY); + assertThat(doubleGauge.getDefaultTimeSeries()).isSameAs(doubleGauge.getDefaultTimeSeries()); + assertThat(doubleGauge.getDefaultTimeSeries()) + .isSameAs(doubleGauge.getOrCreateTimeSeries(LABEL_VALUES)); } @Test public void noopInstanceOf() { - LongGauge longGauge = metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY); - assertThat(longGauge) + assertThat(metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY)) .isInstanceOf(LongGauge.newNoopLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY).getClass()); + assertThat(metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEY)) + .isInstanceOf( + DoubleGauge.newNoopDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEY).getClass()); } } |