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 /impl_core | |
parent | 2f53b977da6b10b7f8e8c29d83f0478d470b229c (diff) | |
download | opencensus-java-30b613fcbf329bf130ae18267b431c4f820bbe28.tar.gz |
Plugs-in the DoubleGauge into the registry (#1503)
Diffstat (limited to 'impl_core')
-rw-r--r-- | impl_core/src/main/java/io/opencensus/implcore/metrics/MetricRegistryImpl.java | 16 | ||||
-rw-r--r-- | impl_core/src/test/java/io/opencensus/implcore/metrics/MetricRegistryImplTest.java | 95 |
2 files changed, 108 insertions, 3 deletions
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 8f8dab2b..c606d7e9 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 @@ -20,6 +20,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import io.opencensus.common.Clock; import io.opencensus.implcore.internal.Utils; +import io.opencensus.metrics.DoubleGauge; import io.opencensus.metrics.LabelKey; import io.opencensus.metrics.LongGauge; import io.opencensus.metrics.MetricRegistry; @@ -57,6 +58,21 @@ public final class MetricRegistryImpl extends MetricRegistry { return longGaugeMetric; } + @Override + public DoubleGauge addDoubleGauge( + String name, String description, String unit, List<LabelKey> labelKeys) { + Utils.checkListElementNotNull( + checkNotNull(labelKeys, "labelKeys"), "labelKey element should not be null."); + DoubleGaugeImpl doubleGaugeMetric = + new DoubleGaugeImpl( + checkNotNull(name, "name"), + checkNotNull(description, "description"), + checkNotNull(unit, "unit"), + Collections.unmodifiableList(new ArrayList<LabelKey>(labelKeys))); + registeredMeters.registerMeter(name, doubleGaugeMetric); + return doubleGaugeMetric; + } + private static final class RegisteredMeters { private volatile Map<String, Meter> registeredMeters = Collections.emptyMap(); 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 c7cd99be..36440898 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,9 +19,12 @@ package io.opencensus.implcore.metrics; import static com.google.common.truth.Truth.assertThat; import io.opencensus.common.Timestamp; +import io.opencensus.metrics.DoubleGauge; +import io.opencensus.metrics.DoubleGauge.DoublePoint; import io.opencensus.metrics.LabelKey; import io.opencensus.metrics.LabelValue; import io.opencensus.metrics.LongGauge; +import io.opencensus.metrics.LongGauge.LongPoint; import io.opencensus.metrics.export.Metric; import io.opencensus.metrics.export.MetricDescriptor; import io.opencensus.metrics.export.MetricDescriptor.Type; @@ -44,6 +47,7 @@ public class MetricRegistryImplTest { @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 = @@ -55,8 +59,10 @@ public class MetricRegistryImplTest { private final TestClock testClock = TestClock.create(TEST_TIME); private final MetricRegistryImpl metricRegistry = new MetricRegistryImpl(testClock); - private static final MetricDescriptor METRIC_DESCRIPTOR = + private static final MetricDescriptor LONG_METRIC_DESCRIPTOR = MetricDescriptor.create(NAME, DESCRIPTION, UNIT, Type.GAUGE_INT64, LABEL_KEY); + private static final MetricDescriptor DOUBLE_METRIC_DESCRIPTOR = + MetricDescriptor.create(NAME_2, DESCRIPTION, UNIT, Type.GAUGE_DOUBLE, LABEL_KEY); @Test public void addLongGauge_NullName() { @@ -95,6 +101,42 @@ public class MetricRegistryImplTest { } @Test + public void addDoubleGauge_NullName() { + thrown.expect(NullPointerException.class); + thrown.expectMessage("name"); + metricRegistry.addDoubleGauge(null, DESCRIPTION, UNIT, LABEL_KEY); + } + + @Test + public void addDoubleGauge_NullDescription() { + thrown.expect(NullPointerException.class); + thrown.expectMessage("description"); + metricRegistry.addDoubleGauge(NAME_2, null, UNIT, LABEL_KEY); + } + + @Test + public void addDoubleGauge_NullUnit() { + thrown.expect(NullPointerException.class); + thrown.expectMessage("unit"); + metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, null, LABEL_KEY); + } + + @Test + public void addDoubleGauge_NullLabels() { + thrown.expect(NullPointerException.class); + thrown.expectMessage("labelKeys"); + metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, null); + } + + @Test + public void addDoubleGauge_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 addLongGauge_GetMetrics() { LongGauge longGauge = metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY); longGauge.getOrCreateTimeSeries(LABEL_VALUES); @@ -104,19 +146,66 @@ public class MetricRegistryImplTest { assertThat(metricCollections) .containsExactly( Metric.createWithOneTimeSeries( - METRIC_DESCRIPTOR, + LONG_METRIC_DESCRIPTOR, TimeSeries.createWithOnePoint( LABEL_VALUES, Point.create(Value.longValue(0), TEST_TIME), null))); } @Test + public void addDoubleGauge_GetMetrics() { + DoubleGauge doubleGauge = metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEY); + doubleGauge.getOrCreateTimeSeries(LABEL_VALUES); + Collection<Metric> metricCollections = metricRegistry.getMetricProducer().getMetrics(); + assertThat(metricCollections.size()).isEqualTo(1); + assertThat(metricCollections) + .containsExactly( + Metric.createWithOneTimeSeries( + DOUBLE_METRIC_DESCRIPTOR, + TimeSeries.createWithOnePoint( + LABEL_VALUES, Point.create(Value.doubleValue(0.0), TEST_TIME), null))); + } + + @Test public void empty_GetMetrics() { assertThat(metricRegistry.getMetricProducer().getMetrics()).isEmpty(); } @Test public void checkInstanceOf() { + assertThat(metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY)) + .isInstanceOf(LongGaugeImpl.class); + assertThat(metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEY)) + .isInstanceOf(DoubleGaugeImpl.class); + } + + @Test + public void getMetrics() { LongGauge longGauge = metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY); - assertThat(longGauge).isInstanceOf(LongGaugeImpl.class); + LongPoint longPoint = longGauge.getOrCreateTimeSeries(LABEL_VALUES); + longPoint.set(200); + DoubleGauge doubleGauge = metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEY); + DoublePoint doublePoint = doubleGauge.getOrCreateTimeSeries(LABEL_VALUES); + doublePoint.set(-300.13); + + Collection<Metric> metricCollections = metricRegistry.getMetricProducer().getMetrics(); + assertThat(metricCollections.size()).isEqualTo(2); + assertThat(metricCollections) + .containsExactly( + Metric.createWithOneTimeSeries( + LONG_METRIC_DESCRIPTOR, + TimeSeries.createWithOnePoint( + LABEL_VALUES, Point.create(Value.longValue(200), TEST_TIME), null)), + Metric.createWithOneTimeSeries( + DOUBLE_METRIC_DESCRIPTOR, + TimeSeries.createWithOnePoint( + LABEL_VALUES, Point.create(Value.doubleValue(-300.13), TEST_TIME), null))); + } + + @Test + public void registerDifferentMetricSameName() { + metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY); + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("A different metric with the same name already registered."); + metricRegistry.addDoubleGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY); } } |