aboutsummaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
authorMayur Kale <mayurkale@google.com>2018-10-14 17:36:19 -0700
committerGitHub <noreply@github.com>2018-10-14 17:36:19 -0700
commit418e675ee250299b77370620229bf7d3cd6c6830 (patch)
treecc1d1ff1105fe5f1c7b387e97984a0fb0908d9d3 /api
parent53b1b60633cc3704bdaf0bd84efb3d2503578b20 (diff)
downloadopencensus-java-418e675ee250299b77370620229bf7d3cd6c6830.tar.gz
Metrics/Metric: Add a version of create that accepts only one timeseries. (#1495)
* Metrics/Metric: Add createWithOneTimeSeries method * Fix review comments
Diffstat (limited to 'api')
-rw-r--r--api/src/main/java/io/opencensus/metrics/export/Metric.java35
-rw-r--r--api/src/test/java/io/opencensus/metrics/export/MetricTest.java45
2 files changed, 69 insertions, 11 deletions
diff --git a/api/src/main/java/io/opencensus/metrics/export/Metric.java b/api/src/main/java/io/opencensus/metrics/export/Metric.java
index 5e2fa5e8..7b93fc86 100644
--- a/api/src/main/java/io/opencensus/metrics/export/Metric.java
+++ b/api/src/main/java/io/opencensus/metrics/export/Metric.java
@@ -49,12 +49,39 @@ public abstract class Metric {
* @since 0.17
*/
public static Metric create(MetricDescriptor metricDescriptor, List<TimeSeries> timeSeriesList) {
+ Utils.checkListElementNotNull(
+ Utils.checkNotNull(timeSeriesList, "timeSeriesList"), "timeSeries");
+ return createInternal(
+ metricDescriptor, Collections.unmodifiableList(new ArrayList<TimeSeries>(timeSeriesList)));
+ }
+
+ /**
+ * Creates a {@link Metric}.
+ *
+ * @param metricDescriptor the {@link MetricDescriptor}.
+ * @param timeSeries the single {@link TimeSeries} for this metric.
+ * @return a {@code Metric}.
+ * @since 0.17
+ */
+ public static Metric createWithOneTimeSeries(
+ MetricDescriptor metricDescriptor, TimeSeries timeSeries) {
+ return createInternal(
+ metricDescriptor, Collections.singletonList(Utils.checkNotNull(timeSeries, "timeSeries")));
+ }
+
+ /**
+ * Creates a {@link Metric}.
+ *
+ * @param metricDescriptor the {@link MetricDescriptor}.
+ * @param timeSeriesList the {@link TimeSeries} list for this metric.
+ * @return a {@code Metric}.
+ * @since 0.17
+ */
+ private static Metric createInternal(
+ MetricDescriptor metricDescriptor, List<TimeSeries> timeSeriesList) {
Utils.checkNotNull(metricDescriptor, "metricDescriptor");
- Utils.checkNotNull(timeSeriesList, "timeSeriesList");
- Utils.checkListElementNotNull(timeSeriesList, "timeSeries");
checkTypeMatch(metricDescriptor.getType(), timeSeriesList);
- return new AutoValue_Metric(
- metricDescriptor, Collections.unmodifiableList(new ArrayList<TimeSeries>(timeSeriesList)));
+ return new AutoValue_Metric(metricDescriptor, timeSeriesList);
}
/**
diff --git a/api/src/test/java/io/opencensus/metrics/export/MetricTest.java b/api/src/test/java/io/opencensus/metrics/export/MetricTest.java
index 52fa2c47..ed205289 100644
--- a/api/src/test/java/io/opencensus/metrics/export/MetricTest.java
+++ b/api/src/test/java/io/opencensus/metrics/export/MetricTest.java
@@ -50,7 +50,11 @@ public class MetricTest {
METRIC_NAME_1, DESCRIPTION, UNIT, Type.GAUGE_DOUBLE, Arrays.asList(KEY_1, KEY_2));
private static final MetricDescriptor METRIC_DESCRIPTOR_2 =
MetricDescriptor.create(
- METRIC_NAME_2, DESCRIPTION, UNIT, Type.CUMULATIVE_INT64, Arrays.asList(KEY_1));
+ METRIC_NAME_2,
+ DESCRIPTION,
+ UNIT,
+ Type.CUMULATIVE_INT64,
+ Collections.singletonList(KEY_1));
private static final LabelValue LABEL_VALUE_1 = LabelValue.create("value1");
private static final LabelValue LABEL_VALUE_2 = LabelValue.create("value1");
private static final LabelValue LABEL_VALUE_EMPTY = LabelValue.create("");
@@ -64,11 +68,16 @@ public class MetricTest {
private static final Point POINT_2 = Point.create(VALUE_DOUBLE_2, TIMESTAMP_3);
private static final Point POINT_3 = Point.create(VALUE_LONG, TIMESTAMP_3);
private static final TimeSeries GAUGE_TIME_SERIES_1 =
- TimeSeries.create(Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(POINT_1), null);
+ TimeSeries.create(
+ Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Collections.singletonList(POINT_1), null);
private static final TimeSeries GAUGE_TIME_SERIES_2 =
- TimeSeries.create(Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(POINT_2), null);
+ TimeSeries.create(
+ Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Collections.singletonList(POINT_2), null);
private static final TimeSeries CUMULATIVE_TIME_SERIES =
- TimeSeries.create(Arrays.asList(LABEL_VALUE_EMPTY), Arrays.asList(POINT_3), TIMESTAMP_1);
+ TimeSeries.create(
+ Collections.singletonList(LABEL_VALUE_EMPTY),
+ Collections.singletonList(POINT_3),
+ TIMESTAMP_1);
@Test
public void testGet() {
@@ -84,7 +93,7 @@ public class MetricTest {
public void typeMismatch_GaugeDouble_Long() {
typeMismatch(
METRIC_DESCRIPTOR_1,
- Arrays.asList(CUMULATIVE_TIME_SERIES),
+ Collections.singletonList(CUMULATIVE_TIME_SERIES),
String.format("Type mismatch: %s, %s.", Type.GAUGE_DOUBLE, "ValueLong"));
}
@@ -92,7 +101,7 @@ public class MetricTest {
public void typeMismatch_CumulativeInt64_Double() {
typeMismatch(
METRIC_DESCRIPTOR_2,
- Arrays.asList(GAUGE_TIME_SERIES_1),
+ Collections.singletonList(GAUGE_TIME_SERIES_1),
String.format("Type mismatch: %s, %s.", Type.CUMULATIVE_INT64, "ValueDouble"));
}
@@ -134,6 +143,27 @@ public class MetricTest {
}
@Test
+ public void createWithOneTimeSeries_WithNullTimeSeries() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("timeSeries");
+ Metric.createWithOneTimeSeries(METRIC_DESCRIPTOR_1, null);
+ }
+
+ @Test
+ public void createWithOneTimeSeries_WithNullMetricDescriptor() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("metricDescriptor");
+ Metric.createWithOneTimeSeries(null, GAUGE_TIME_SERIES_1);
+ }
+
+ @Test
+ public void testGet_WithOneTimeSeries() {
+ Metric metric = Metric.createWithOneTimeSeries(METRIC_DESCRIPTOR_1, GAUGE_TIME_SERIES_1);
+ assertThat(metric.getMetricDescriptor()).isEqualTo(METRIC_DESCRIPTOR_1);
+ assertThat(metric.getTimeSeriesList()).containsExactly(GAUGE_TIME_SERIES_1);
+ }
+
+ @Test
public void testEquals() {
new EqualsTester()
.addEqualityGroup(
@@ -142,7 +172,8 @@ public class MetricTest {
Metric.create(
METRIC_DESCRIPTOR_1, Arrays.asList(GAUGE_TIME_SERIES_1, GAUGE_TIME_SERIES_2)))
.addEqualityGroup(Metric.create(METRIC_DESCRIPTOR_1, Collections.<TimeSeries>emptyList()))
- .addEqualityGroup(Metric.create(METRIC_DESCRIPTOR_2, Arrays.asList(CUMULATIVE_TIME_SERIES)))
+ .addEqualityGroup(
+ Metric.createWithOneTimeSeries(METRIC_DESCRIPTOR_2, CUMULATIVE_TIME_SERIES))
.addEqualityGroup(Metric.create(METRIC_DESCRIPTOR_2, Collections.<TimeSeries>emptyList()))
.testEquals();
}