aboutsummaryrefslogtreecommitdiff
path: root/impl_core/src
diff options
context:
space:
mode:
authorBogdan Drutu <bdrutu@google.com>2018-08-14 17:28:24 -0700
committerGitHub <noreply@github.com>2018-08-14 17:28:24 -0700
commitf50596ac77fd0c4eb209ffbc8769b5025a3aa090 (patch)
tree38434828044be22f6eab76ac0d3d4756893775a1 /impl_core/src
parent8d01189b45122350d37b9685c1225bce6b3b20a2 (diff)
downloadopencensus-java-f50596ac77fd0c4eb209ffbc8769b5025a3aa090.tar.gz
Add implementation for gauges. (#1365)
* Add implementation for gauges. * Remove usage of internal class in implcore tests.
Diffstat (limited to 'impl_core/src')
-rw-r--r--impl_core/src/main/java/io/opencensus/implcore/metrics/Gauge.java113
-rw-r--r--impl_core/src/main/java/io/opencensus/implcore/metrics/MetricRegistryImpl.java104
-rw-r--r--impl_core/src/main/java/io/opencensus/implcore/metrics/MetricsComponentImplBase.java8
-rw-r--r--impl_core/src/test/java/io/opencensus/implcore/metrics/GaugeTest.java113
-rw-r--r--impl_core/src/test/java/io/opencensus/implcore/metrics/MetricRegistryImplTest.java335
-rw-r--r--impl_core/src/test/java/io/opencensus/implcore/metrics/MetricsComponentImplBaseTest.java9
6 files changed, 678 insertions, 4 deletions
diff --git a/impl_core/src/main/java/io/opencensus/implcore/metrics/Gauge.java b/impl_core/src/main/java/io/opencensus/implcore/metrics/Gauge.java
new file mode 100644
index 00000000..0a3c68b3
--- /dev/null
+++ b/impl_core/src/main/java/io/opencensus/implcore/metrics/Gauge.java
@@ -0,0 +1,113 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+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.metrics.LabelKey;
+import io.opencensus.metrics.LabelValue;
+import io.opencensus.metrics.Metric;
+import io.opencensus.metrics.MetricDescriptor;
+import io.opencensus.metrics.MetricDescriptor.Type;
+import io.opencensus.metrics.Point;
+import io.opencensus.metrics.TimeSeriesGauge;
+import io.opencensus.metrics.TimeSeriesList;
+import io.opencensus.metrics.Value;
+import java.util.Collections;
+import java.util.List;
+
+abstract class Gauge {
+ private final MetricDescriptor metricDescriptor;
+ private final List<LabelValue> labelValues;
+
+ final Metric getMetric(Clock clock) {
+ return Metric.create(
+ metricDescriptor,
+ TimeSeriesList.TimeSeriesGaugeList.create(
+ Collections.singletonList(getTimeSeriesGauge(clock))));
+ }
+
+ abstract TimeSeriesGauge getTimeSeriesGauge(Clock clock);
+
+ static final class DoubleGauge<T> extends Gauge {
+ private final T obj;
+ private final ToDoubleFunction<T> function;
+
+ DoubleGauge(
+ String name,
+ String description,
+ String unit,
+ List<LabelKey> labelKeys,
+ List<LabelValue> labelValues,
+ T obj,
+ ToDoubleFunction<T> function) {
+ super(
+ MetricDescriptor.create(name, description, unit, Type.GAUGE_DOUBLE, labelKeys),
+ labelValues);
+ this.obj = obj;
+ this.function = function;
+ }
+
+ @Override
+ TimeSeriesGauge getTimeSeriesGauge(Clock clock) {
+ return TimeSeriesGauge.create(
+ getLabelValues(),
+ Collections.singletonList(
+ Point.create(Value.doubleValue(function.applyAsDouble(obj)), clock.now())));
+ }
+ }
+
+ static final class LongGauge<T> extends Gauge {
+ private final T obj;
+ private final ToLongFunction<T> function;
+
+ LongGauge(
+ String name,
+ String description,
+ String unit,
+ List<LabelKey> labelKeys,
+ List<LabelValue> labelValues,
+ T obj,
+ ToLongFunction<T> function) {
+ super(
+ MetricDescriptor.create(name, description, unit, Type.GAUGE_INT64, labelKeys),
+ labelValues);
+ this.obj = obj;
+ this.function = function;
+ }
+
+ @Override
+ TimeSeriesGauge getTimeSeriesGauge(Clock clock) {
+ return TimeSeriesGauge.create(
+ getLabelValues(),
+ Collections.singletonList(
+ Point.create(Value.longValue(function.applyAsLong(obj)), clock.now())));
+ }
+ }
+
+ List<LabelValue> getLabelValues() {
+ return labelValues;
+ }
+
+ Gauge(MetricDescriptor metricDescriptor, List<LabelValue> labelValues) {
+ this.metricDescriptor = checkNotNull(metricDescriptor, "metricDescriptor");
+ this.labelValues = Collections.unmodifiableList(labelValues);
+ }
+}
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
new file mode 100644
index 00000000..478b1524
--- /dev/null
+++ b/impl_core/src/main/java/io/opencensus/implcore/metrics/MetricRegistryImpl.java
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+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.metrics.LabelKey;
+import io.opencensus.metrics.LabelValue;
+import io.opencensus.metrics.Metric;
+import io.opencensus.metrics.MetricRegistry;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+public final class MetricRegistryImpl extends MetricRegistry {
+ private final Clock clock;
+ private volatile Set<Gauge> registeredGauges = Collections.emptySet();
+
+ MetricRegistryImpl(Clock clock) {
+ this.clock = clock;
+ }
+
+ @Override
+ public <T> void addLongGauge(
+ String name,
+ String description,
+ String unit,
+ LinkedHashMap<LabelKey, LabelValue> labels,
+ T obj,
+ ToLongFunction<T> function) {
+ checkNotNull(labels, "labels");
+ registerGauge(
+ new LongGauge<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")));
+ }
+
+ @Override
+ public <T> void addDoubleGauge(
+ String name,
+ String description,
+ String unit,
+ LinkedHashMap<LabelKey, LabelValue> labels,
+ T obj,
+ ToDoubleFunction<T> function) {
+ checkNotNull(labels, "labels");
+ registerGauge(
+ 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")));
+ }
+
+ @Override
+ public Collection<Metric> getMetrics() {
+ // Get a snapshot of the current registered gauges.
+ Set<Gauge> gaguges = registeredGauges;
+ if (gaguges.isEmpty()) {
+ return Collections.emptyList();
+ }
+ ArrayList<Metric> metrics = new ArrayList<Metric>();
+ for (Gauge gauge : gaguges) {
+ metrics.add(gauge.getMetric(clock));
+ }
+ return metrics;
+ }
+
+ private synchronized void registerGauge(Gauge gauge) {
+ Set<Gauge> newGaguesList = new LinkedHashSet<Gauge>(registeredGauges);
+ newGaguesList.add(gauge);
+ registeredGauges = Collections.unmodifiableSet(newGaguesList);
+ }
+}
diff --git a/impl_core/src/main/java/io/opencensus/implcore/metrics/MetricsComponentImplBase.java b/impl_core/src/main/java/io/opencensus/implcore/metrics/MetricsComponentImplBase.java
index 312b702e..46ad028c 100644
--- a/impl_core/src/main/java/io/opencensus/implcore/metrics/MetricsComponentImplBase.java
+++ b/impl_core/src/main/java/io/opencensus/implcore/metrics/MetricsComponentImplBase.java
@@ -16,6 +16,7 @@
package io.opencensus.implcore.metrics;
+import io.opencensus.common.Clock;
import io.opencensus.implcore.metrics.export.ExportComponentImpl;
import io.opencensus.metrics.MetricRegistry;
import io.opencensus.metrics.MetricsComponent;
@@ -24,6 +25,7 @@ import io.opencensus.metrics.MetricsComponent;
public class MetricsComponentImplBase extends MetricsComponent {
private final ExportComponentImpl exportComponent;
+ private final MetricRegistryImpl metricRegistry;
@Override
public ExportComponentImpl getExportComponent() {
@@ -32,11 +34,11 @@ public class MetricsComponentImplBase extends MetricsComponent {
@Override
public MetricRegistry getMetricRegistry() {
- // TODO: implement this method
- throw new UnsupportedOperationException("not implemented");
+ return metricRegistry;
}
- public MetricsComponentImplBase() {
+ protected MetricsComponentImplBase(Clock clock) {
exportComponent = new ExportComponentImpl();
+ metricRegistry = new MetricRegistryImpl(clock);
}
}
diff --git a/impl_core/src/test/java/io/opencensus/implcore/metrics/GaugeTest.java b/impl_core/src/test/java/io/opencensus/implcore/metrics/GaugeTest.java
new file mode 100644
index 00000000..1f6a1881
--- /dev/null
+++ b/impl_core/src/test/java/io/opencensus/implcore/metrics/GaugeTest.java
@@ -0,0 +1,113 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+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.implcore.metrics.Gauge.DoubleGauge;
+import io.opencensus.implcore.metrics.Gauge.LongGauge;
+import io.opencensus.metrics.LabelKey;
+import io.opencensus.metrics.LabelValue;
+import io.opencensus.metrics.Metric;
+import io.opencensus.metrics.MetricDescriptor;
+import io.opencensus.metrics.MetricDescriptor.Type;
+import io.opencensus.metrics.Point;
+import io.opencensus.metrics.TimeSeriesGauge;
+import io.opencensus.metrics.TimeSeriesList;
+import io.opencensus.metrics.Value;
+import io.opencensus.testing.common.TestClock;
+import java.util.Collections;
+import java.util.List;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Unit tests for {@link Gauge}. */
+@RunWith(JUnit4.class)
+public class GaugeTest {
+ private static final String NAME = "name";
+ private static final String DESCRIPTION = "description";
+ private static final String UNIT = "1";
+ private static final List<LabelKey> LABEL_KEYS =
+ Collections.unmodifiableList(
+ Collections.singletonList(LabelKey.create("key", "key description")));
+ private static final List<LabelValue> LABEL_VALUES =
+ Collections.unmodifiableList(Collections.singletonList(LabelValue.create("value")));
+ private static final Object OBJ = new Object();
+ private static final Timestamp TEST_TIME = Timestamp.create(1234, 123);
+
+ private final Gauge longGauge =
+ new LongGauge<Object>(
+ NAME,
+ DESCRIPTION,
+ UNIT,
+ LABEL_KEYS,
+ LABEL_VALUES,
+ OBJ,
+ new ToLongFunction<Object>() {
+ @Override
+ public long applyAsLong(Object value) {
+ return value.hashCode();
+ }
+ });
+ private final Gauge doubleGauge =
+ new DoubleGauge<Object>(
+ NAME,
+ DESCRIPTION,
+ UNIT,
+ LABEL_KEYS,
+ LABEL_VALUES,
+ OBJ,
+ new ToDoubleFunction<Object>() {
+ @Override
+ public double applyAsDouble(Object value) {
+ return value.hashCode();
+ }
+ });
+ private final TestClock testClock = TestClock.create(TEST_TIME);
+
+ @Test
+ public void longGauge_GetMetric() {
+ assertThat(longGauge.getMetric(testClock))
+ .isEqualTo(
+ Metric.create(
+ MetricDescriptor.create(NAME, DESCRIPTION, UNIT, Type.GAUGE_INT64, LABEL_KEYS),
+ TimeSeriesList.TimeSeriesGaugeList.create(
+ Collections.singletonList(
+ TimeSeriesGauge.create(
+ LABEL_VALUES,
+ Collections.singletonList(
+ Point.create(Value.longValue(OBJ.hashCode()), TEST_TIME)))))));
+ }
+
+ @Test
+ public void doubleGauge_GetMetric() {
+ assertThat(doubleGauge.getMetric(testClock))
+ .isEqualTo(
+ Metric.create(
+ MetricDescriptor.create(NAME, DESCRIPTION, UNIT, Type.GAUGE_DOUBLE, LABEL_KEYS),
+ TimeSeriesList.TimeSeriesGaugeList.create(
+ Collections.singletonList(
+ TimeSeriesGauge.create(
+ LABEL_VALUES,
+ Collections.singletonList(
+ Point.create(Value.doubleValue(OBJ.hashCode()), TEST_TIME)))))));
+ }
+}
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
new file mode 100644
index 00000000..fdab47aa
--- /dev/null
+++ b/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricRegistryImplTest.java
@@ -0,0 +1,335 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+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.Metric;
+import io.opencensus.metrics.MetricDescriptor;
+import io.opencensus.metrics.MetricDescriptor.Type;
+import io.opencensus.metrics.Point;
+import io.opencensus.metrics.TimeSeriesGauge;
+import io.opencensus.metrics.TimeSeriesList;
+import io.opencensus.metrics.Value;
+import io.opencensus.testing.common.TestClock;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Unit tests for {@link MetricRegistryImpl}. */
+@RunWith(JUnit4.class)
+public class MetricRegistryImplTest {
+ 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 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.getMetrics())
+ .containsExactly(
+ Metric.create(
+ MetricDescriptor.create(
+ NAME,
+ DESCRIPTION,
+ UNIT,
+ Type.GAUGE_DOUBLE,
+ Collections.unmodifiableList(Collections.singletonList(LABEL_KEY))),
+ TimeSeriesList.TimeSeriesGaugeList.create(
+ Collections.singletonList(
+ TimeSeriesGauge.create(
+ Collections.unmodifiableList(Collections.singletonList(LABEL_VALUES)),
+ Collections.singletonList(
+ Point.create(Value.doubleValue(5.0), TEST_TIME)))))));
+ }
+
+ @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;
+ }
+ });
+ }
+
+ @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;
+ }
+ });
+ }
+
+ @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;
+ }
+ });
+ }
+
+ @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;
+ }
+ });
+ }
+
+ @Test
+ public void addLongGauge_NullFunction() {
+ thrown.expect(NullPointerException.class);
+ metricRegistry.addLongGauge("name", DESCRIPTION, UNIT, labels, null, null);
+ }
+
+ @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.getMetrics())
+ .containsExactly(
+ Metric.create(
+ MetricDescriptor.create(
+ NAME,
+ DESCRIPTION,
+ UNIT,
+ Type.GAUGE_INT64,
+ Collections.unmodifiableList(Collections.singletonList(LABEL_KEY))),
+ TimeSeriesList.TimeSeriesGaugeList.create(
+ Collections.singletonList(
+ TimeSeriesGauge.create(
+ Collections.unmodifiableList(Collections.singletonList(LABEL_VALUES)),
+ Collections.singletonList(
+ Point.create(Value.longValue(7), TEST_TIME)))))));
+ }
+
+ @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.getMetrics())
+ .containsExactly(
+ Metric.create(
+ MetricDescriptor.create(
+ NAME,
+ DESCRIPTION,
+ UNIT,
+ Type.GAUGE_INT64,
+ Collections.unmodifiableList(Collections.singletonList(LABEL_KEY))),
+ TimeSeriesList.TimeSeriesGaugeList.create(
+ Collections.singletonList(
+ TimeSeriesGauge.create(
+ Collections.unmodifiableList(Collections.singletonList(LABEL_VALUES)),
+ Collections.singletonList(
+ Point.create(Value.longValue(7), TEST_TIME)))))),
+ Metric.create(
+ MetricDescriptor.create(
+ NAME,
+ DESCRIPTION,
+ UNIT,
+ Type.GAUGE_DOUBLE,
+ Collections.unmodifiableList(Collections.singletonList(LABEL_KEY))),
+ TimeSeriesList.TimeSeriesGaugeList.create(
+ Collections.singletonList(
+ TimeSeriesGauge.create(
+ Collections.unmodifiableList(Collections.singletonList(LABEL_VALUES)),
+ Collections.singletonList(
+ Point.create(Value.doubleValue(5.0), TEST_TIME)))))));
+ }
+
+ @Test
+ public void empty_GetMetrics() {
+ assertThat(metricRegistry.getMetrics()).isEmpty();
+ }
+}
diff --git a/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricsComponentImplBaseTest.java b/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricsComponentImplBaseTest.java
index a86a53d9..5050625b 100644
--- a/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricsComponentImplBaseTest.java
+++ b/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricsComponentImplBaseTest.java
@@ -19,6 +19,7 @@ package io.opencensus.implcore.metrics;
import static com.google.common.truth.Truth.assertThat;
import io.opencensus.implcore.metrics.export.ExportComponentImpl;
+import io.opencensus.testing.common.TestClock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@@ -26,11 +27,17 @@ import org.junit.runners.JUnit4;
/** Unit tests for {@link MetricsComponentImplBase}. */
@RunWith(JUnit4.class)
public class MetricsComponentImplBaseTest {
+ private final MetricsComponentImplBase metricsComponentImplBase =
+ new MetricsComponentImplBase(TestClock.create());
@Test
public void getExportComponent() {
- MetricsComponentImplBase metricsComponentImplBase = new MetricsComponentImplBase();
assertThat(metricsComponentImplBase.getExportComponent())
.isInstanceOf(ExportComponentImpl.class);
}
+
+ @Test
+ public void getMetricRegistry() {
+ assertThat(metricsComponentImplBase.getMetricRegistry()).isInstanceOf(MetricRegistryImpl.class);
+ }
}