From 8d75c101ab0ae0efd5faea9906ce399f00fb4394 Mon Sep 17 00:00:00 2001 From: Yang Song Date: Tue, 21 Aug 2018 13:14:31 -0700 Subject: Metrics: Combine TimeSeriesCumulative and TimeSeriesGauge. (#1380) --- .../main/java/io/opencensus/metrics/Metric.java | 61 ++++++---- .../java/io/opencensus/metrics/TimeSeries.java | 50 +++++++- .../opencensus/metrics/TimeSeriesCumulative.java | 70 ----------- .../io/opencensus/metrics/TimeSeriesGauge.java | 57 --------- .../java/io/opencensus/metrics/TimeSeriesList.java | 134 -------------------- .../java/io/opencensus/metrics/MetricTest.java | 59 ++++----- .../metrics/TimeSeriesCumulativeTest.java | 113 ----------------- .../io/opencensus/metrics/TimeSeriesGaugeTest.java | 104 ---------------- .../io/opencensus/metrics/TimeSeriesListTest.java | 135 --------------------- .../java/io/opencensus/metrics/TimeSeriesTest.java | 116 ++++++++++++++++++ 10 files changed, 225 insertions(+), 674 deletions(-) delete mode 100644 api/src/main/java/io/opencensus/metrics/TimeSeriesCumulative.java delete mode 100644 api/src/main/java/io/opencensus/metrics/TimeSeriesGauge.java delete mode 100644 api/src/main/java/io/opencensus/metrics/TimeSeriesList.java delete mode 100644 api/src/test/java/io/opencensus/metrics/TimeSeriesCumulativeTest.java delete mode 100644 api/src/test/java/io/opencensus/metrics/TimeSeriesGaugeTest.java delete mode 100644 api/src/test/java/io/opencensus/metrics/TimeSeriesListTest.java create mode 100644 api/src/test/java/io/opencensus/metrics/TimeSeriesTest.java (limited to 'api') diff --git a/api/src/main/java/io/opencensus/metrics/Metric.java b/api/src/main/java/io/opencensus/metrics/Metric.java index e8be0b90..9cbd418b 100644 --- a/api/src/main/java/io/opencensus/metrics/Metric.java +++ b/api/src/main/java/io/opencensus/metrics/Metric.java @@ -19,8 +19,10 @@ package io.opencensus.metrics; import com.google.auto.value.AutoValue; import io.opencensus.common.ExperimentalApi; import io.opencensus.internal.Utils; -import io.opencensus.metrics.TimeSeriesList.TimeSeriesCumulativeList; -import io.opencensus.metrics.TimeSeriesList.TimeSeriesGaugeList; +import io.opencensus.metrics.Value.ValueDistribution; +import io.opencensus.metrics.Value.ValueDouble; +import io.opencensus.metrics.Value.ValueLong; +import java.util.List; import javax.annotation.concurrent.Immutable; /** @@ -39,11 +41,11 @@ public abstract class Metric { * Creates a {@link Metric}. * * @param metricDescriptor the {@link MetricDescriptor}. - * @param timeSeriesList the {@link TimeSeriesList} for this metric. + * @param timeSeriesList the {@link TimeSeries} list for this metric. * @return a {@code Metric}. * @since 0.16 */ - public static Metric create(MetricDescriptor metricDescriptor, TimeSeriesList timeSeriesList) { + public static Metric create(MetricDescriptor metricDescriptor, List timeSeriesList) { checkTypeMatch(metricDescriptor.getType(), timeSeriesList); return new AutoValue_Metric(metricDescriptor, timeSeriesList); } @@ -57,32 +59,43 @@ public abstract class Metric { public abstract MetricDescriptor getMetricDescriptor(); /** - * Returns the {@link TimeSeriesList} for this metric. + * Returns the {@link TimeSeries} list for this metric. * - *

The type of the {@link TimeSeriesList} must match {@link MetricDescriptor.Type}. + *

The type of the {@link TimeSeries#getPoints()} must match {@link MetricDescriptor.Type}. * * @return the {@code TimeSeriesList} for this metric. * @since 0.16 */ - public abstract TimeSeriesList getTimeSeriesList(); + public abstract List getTimeSeriesList(); - private static void checkTypeMatch(MetricDescriptor.Type type, TimeSeriesList timeSeriesList) { - switch (type) { - case GAUGE_INT64: - case GAUGE_DOUBLE: - Utils.checkArgument( - timeSeriesList instanceof TimeSeriesGaugeList, - String.format( - "Type mismatch: %s, %s.", type, timeSeriesList.getClass().getSimpleName())); - break; - case CUMULATIVE_DISTRIBUTION: - case CUMULATIVE_DOUBLE: - case CUMULATIVE_INT64: - Utils.checkArgument( - timeSeriesList instanceof TimeSeriesCumulativeList, - String.format( - "Type mismatch: %s, %s.", type, timeSeriesList.getClass().getSimpleName())); - break; + private static void checkTypeMatch(MetricDescriptor.Type type, List timeSeriesList) { + for (TimeSeries timeSeries : timeSeriesList) { + for (Point point : timeSeries.getPoints()) { + Value value = point.getValue(); + String valueClassName = ""; + if (value.getClass().getSuperclass() != null) { // work around nullness check + // AutoValue classes should always have a super class. + valueClassName = value.getClass().getSuperclass().getSimpleName(); + } + switch (type) { + case GAUGE_INT64: + case CUMULATIVE_INT64: + Utils.checkArgument( + value instanceof ValueLong, + String.format("Type mismatch: %s, %s.", type, valueClassName)); + break; + case CUMULATIVE_DOUBLE: + case GAUGE_DOUBLE: + Utils.checkArgument( + value instanceof ValueDouble, + String.format("Type mismatch: %s, %s.", type, valueClassName)); + break; + case CUMULATIVE_DISTRIBUTION: + Utils.checkArgument( + value instanceof ValueDistribution, + String.format("Type mismatch: %s, %s.", type, valueClassName)); + } + } } } } diff --git a/api/src/main/java/io/opencensus/metrics/TimeSeries.java b/api/src/main/java/io/opencensus/metrics/TimeSeries.java index bbbfe0e4..c62b6b72 100644 --- a/api/src/main/java/io/opencensus/metrics/TimeSeries.java +++ b/api/src/main/java/io/opencensus/metrics/TimeSeries.java @@ -16,15 +16,51 @@ package io.opencensus.metrics; +import com.google.auto.value.AutoValue; +import io.opencensus.common.ExperimentalApi; +import io.opencensus.common.Timestamp; +import io.opencensus.internal.Utils; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; -/** A collection of data points that describes the time-varying values of a {@code Metric}. */ +/** + * A collection of data points that describes the time-varying values of a {@code Metric}. + * + * @since 0.16 + */ +@ExperimentalApi @Immutable -abstract class TimeSeries { +@AutoValue +public abstract class TimeSeries { TimeSeries() {} + /** + * Creates a {@link TimeSeries}. + * + * @param labelValues the {@code LabelValue}s that uniquely identify this {@code TimeSeries}. + * @param points the data {@code Point}s of this {@code TimeSeries}. + * @param startTimestamp the start {@code Timestamp} of this {@code TimeSeries}. Must be non-null + * for cumulative {@code Point}s. + * @return a {@code TimeSeries}. + * @since 0.16 + */ + public static TimeSeries create( + List labelValues, List points, @Nullable Timestamp startTimestamp) { + // Fail fast on null lists to prevent NullPointerException when copying the lists. + Utils.checkNotNull(labelValues, "labelValues"); + Utils.checkNotNull(points, "points"); + Utils.checkListElementNotNull(labelValues, "labelValue"); + Utils.checkListElementNotNull(points, "point"); + return new AutoValue_TimeSeries( + Collections.unmodifiableList(new ArrayList(labelValues)), + Collections.unmodifiableList(new ArrayList(points)), + startTimestamp); + } + /** * Returns the set of {@link LabelValue}s that uniquely identify this {@link TimeSeries}. * @@ -45,4 +81,14 @@ abstract class TimeSeries { * @since 0.16 */ public abstract List getPoints(); + + /** + * Returns the start {@link Timestamp} of this {@link TimeSeries} if the {@link Point}s are + * cumulative, or {@code null} if the {@link Point}s are gauge. + * + * @return the start {@code Timestamp} or {@code null}. + * @since 0.16 + */ + @Nullable + public abstract Timestamp getStartTimestamp(); } diff --git a/api/src/main/java/io/opencensus/metrics/TimeSeriesCumulative.java b/api/src/main/java/io/opencensus/metrics/TimeSeriesCumulative.java deleted file mode 100644 index 5f48b770..00000000 --- a/api/src/main/java/io/opencensus/metrics/TimeSeriesCumulative.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * 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.metrics; - -import com.google.auto.value.AutoValue; -import io.opencensus.common.ExperimentalApi; -import io.opencensus.common.Timestamp; -import io.opencensus.internal.Utils; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import javax.annotation.concurrent.Immutable; - -/** - * A collection of data points that describes the time-varying values of a cumulative {@code - * Metric}. - * - * @since 0.16 - */ -@ExperimentalApi -@Immutable -@AutoValue -public abstract class TimeSeriesCumulative extends TimeSeries { - - TimeSeriesCumulative() {} - - /** - * Creates a {@link TimeSeriesCumulative}. - * - * @param labelValues the {@code LabelValue}s that uniquely identify this {@code TimeSeries}. - * @param points the data {@code Point}s of this {@code TimeSeries}. - * @param startTimestamp the start {@code Timestamp} of this {@code TimeSeriesCumulative}. - * @return a {@code TimeSeriesCumulative}. - * @since 0.16 - */ - public static TimeSeriesCumulative create( - List labelValues, List points, Timestamp startTimestamp) { - // Fail fast on null lists to prevent NullPointerException when copying the lists. - Utils.checkNotNull(labelValues, "labelValues"); - Utils.checkNotNull(points, "points"); - Utils.checkListElementNotNull(labelValues, "labelValue"); - Utils.checkListElementNotNull(points, "point"); - return new AutoValue_TimeSeriesCumulative( - Collections.unmodifiableList(new ArrayList(labelValues)), - Collections.unmodifiableList(new ArrayList(points)), - startTimestamp); - } - - /** - * Returns the start {@link Timestamp} of this {@link TimeSeriesCumulative}. - * - * @return the start {@code Timestamp}. - * @since 0.16 - */ - public abstract Timestamp getStartTimestamp(); -} diff --git a/api/src/main/java/io/opencensus/metrics/TimeSeriesGauge.java b/api/src/main/java/io/opencensus/metrics/TimeSeriesGauge.java deleted file mode 100644 index 717505a4..00000000 --- a/api/src/main/java/io/opencensus/metrics/TimeSeriesGauge.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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.metrics; - -import com.google.auto.value.AutoValue; -import io.opencensus.common.ExperimentalApi; -import io.opencensus.internal.Utils; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import javax.annotation.concurrent.Immutable; - -/** - * A collection of data points that describes the time-varying values of a gauge {@code Metric}. - * - * @since 0.16 - */ -@ExperimentalApi -@Immutable -@AutoValue -public abstract class TimeSeriesGauge extends TimeSeries { - - TimeSeriesGauge() {} - - /** - * Creates a {@link TimeSeriesGauge}. - * - * @param labelValues the {@code LabelValue}s that uniquely identify this {@code TimeSeries}. - * @param points the data {@code Point}s of this {@code TimeSeries}. - * @return a {@code TimeSeriesGauge}. - * @since 0.16 - */ - public static TimeSeriesGauge create(List labelValues, List points) { - // Fail fast on null lists to prevent NullPointerException when copying the lists. - Utils.checkNotNull(labelValues, "labelValues"); - Utils.checkNotNull(points, "points"); - Utils.checkListElementNotNull(labelValues, "labelValue"); - Utils.checkListElementNotNull(points, "point"); - return new AutoValue_TimeSeriesGauge( - Collections.unmodifiableList(new ArrayList(labelValues)), - Collections.unmodifiableList(new ArrayList(points))); - } -} diff --git a/api/src/main/java/io/opencensus/metrics/TimeSeriesList.java b/api/src/main/java/io/opencensus/metrics/TimeSeriesList.java deleted file mode 100644 index 6138eac2..00000000 --- a/api/src/main/java/io/opencensus/metrics/TimeSeriesList.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * 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.metrics; - -import com.google.auto.value.AutoValue; -import io.opencensus.common.ExperimentalApi; -import io.opencensus.common.Function; -import io.opencensus.internal.Utils; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import javax.annotation.concurrent.Immutable; - -/** - * Class that holds a list of either {@link TimeSeriesGauge} or {@link TimeSeriesCumulative}. - * - * @since 0.16 - */ -@ExperimentalApi -@Immutable -public abstract class TimeSeriesList { - - TimeSeriesList() {} - - /** - * Applies the given match function to the underlying data type. - * - * @since 0.16 - */ - public abstract T match( - Function gaugeListFunction, - Function cumulativeListFunction, - Function defaultFunction); - - /** - * Class that holds a list of {@link TimeSeriesGauge}. - * - * @since 0.16 - */ - @ExperimentalApi - @Immutable - @AutoValue - public abstract static class TimeSeriesGaugeList extends TimeSeriesList { - - TimeSeriesGaugeList() {} - - @Override - public final T match( - Function gaugeListFunction, - Function cumulativeListFunction, - Function defaultFunction) { - return gaugeListFunction.apply(this); - } - - /** - * Creates a {@link TimeSeriesGaugeList}. - * - * @param list a list of {@link TimeSeriesGauge}. - * @return a {code TimeSeriesGaugeList}. - * @since 0.16 - */ - public static TimeSeriesGaugeList create(List list) { - Utils.checkNotNull(list, "list"); - Utils.checkListElementNotNull(list, "timeSeriesGauge"); - return new AutoValue_TimeSeriesList_TimeSeriesGaugeList( - Collections.unmodifiableList(new ArrayList(list))); - } - - /** - * Returns the list of {@link TimeSeriesGauge}. - * - * @return the list of {@code TimeSeriesGauge}. - * @since 0.16 - */ - public abstract List getList(); - } - - /** - * Class that holds a list of {@link TimeSeriesCumulative}. - * - * @since 0.16 - */ - @ExperimentalApi - @Immutable - @AutoValue - public abstract static class TimeSeriesCumulativeList extends TimeSeriesList { - - TimeSeriesCumulativeList() {} - - @Override - public final T match( - Function gaugeListFunction, - Function cumulativeListFunction, - Function defaultFunction) { - return cumulativeListFunction.apply(this); - } - - /** - * Creates a {@link TimeSeriesCumulativeList}. - * - * @param list a list of {@link TimeSeriesCumulative}. - * @return a {code TimeSeriesCumulativeList}. - * @since 0.16 - */ - public static TimeSeriesCumulativeList create(List list) { - Utils.checkNotNull(list, "list"); - Utils.checkListElementNotNull(list, "timeSeriesCumulative"); - return new AutoValue_TimeSeriesList_TimeSeriesCumulativeList( - Collections.unmodifiableList(new ArrayList(list))); - } - - /** - * Returns the list of {@link TimeSeriesCumulative}. - * - * @return the list of {@code TimeSeriesCumulative}. - * @since 0.16 - */ - public abstract List getList(); - } -} diff --git a/api/src/test/java/io/opencensus/metrics/MetricTest.java b/api/src/test/java/io/opencensus/metrics/MetricTest.java index 59838ef4..37deed4b 100644 --- a/api/src/test/java/io/opencensus/metrics/MetricTest.java +++ b/api/src/test/java/io/opencensus/metrics/MetricTest.java @@ -21,10 +21,9 @@ import static com.google.common.truth.Truth.assertThat; import com.google.common.testing.EqualsTester; import io.opencensus.common.Timestamp; import io.opencensus.metrics.MetricDescriptor.Type; -import io.opencensus.metrics.TimeSeriesList.TimeSeriesCumulativeList; -import io.opencensus.metrics.TimeSeriesList.TimeSeriesGaugeList; import java.util.Arrays; import java.util.Collections; +import java.util.List; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -61,47 +60,41 @@ public class MetricTest { private static final Point POINT_1 = Point.create(VALUE_DOUBLE_1, TIMESTAMP_2); 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 TimeSeriesGauge GAUGE_TIME_SERIES_1 = - TimeSeriesGauge.create(Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(POINT_1)); - private static final TimeSeriesGauge GAUGE_TIME_SERIES_2 = - TimeSeriesGauge.create(Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(POINT_2)); - private static final TimeSeriesCumulative CUMULATIVE_TIME_SERIES = - TimeSeriesCumulative.create( - Arrays.asList(LABEL_VALUE_EMPTY), Arrays.asList(POINT_3), TIMESTAMP_1); - private static final TimeSeriesGaugeList TIME_SERIES_GAUGE_LIST = - TimeSeriesGaugeList.create(Arrays.asList(GAUGE_TIME_SERIES_1, GAUGE_TIME_SERIES_2)); - private static final TimeSeriesCumulativeList TIME_SERIES_CUMULATIVE_LIST = - TimeSeriesCumulativeList.create(Arrays.asList(CUMULATIVE_TIME_SERIES)); + private static final TimeSeries GAUGE_TIME_SERIES_1 = + TimeSeries.create(Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(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); + private static final TimeSeries CUMULATIVE_TIME_SERIES = + TimeSeries.create(Arrays.asList(LABEL_VALUE_EMPTY), Arrays.asList(POINT_3), TIMESTAMP_1); @Test public void testGet() { - Metric metric = Metric.create(METRIC_DESCRIPTOR_1, TIME_SERIES_GAUGE_LIST); + Metric metric = + Metric.create(METRIC_DESCRIPTOR_1, Arrays.asList(GAUGE_TIME_SERIES_1, GAUGE_TIME_SERIES_2)); assertThat(metric.getMetricDescriptor()).isEqualTo(METRIC_DESCRIPTOR_1); - assertThat(metric.getTimeSeriesList()).isEqualTo(TIME_SERIES_GAUGE_LIST); + assertThat(metric.getTimeSeriesList()) + .containsExactly(GAUGE_TIME_SERIES_1, GAUGE_TIME_SERIES_2) + .inOrder(); } @Test - public void typeMismatch_GaugeDouble_TimeSeriesCumulative() { + public void typeMismatch_GaugeDouble_Long() { typeMismatch( METRIC_DESCRIPTOR_1, - TIME_SERIES_CUMULATIVE_LIST, - String.format( - "Type mismatch: %s, %s.", - Type.GAUGE_DOUBLE, TIME_SERIES_CUMULATIVE_LIST.getClass().getSimpleName())); + Arrays.asList(CUMULATIVE_TIME_SERIES), + String.format("Type mismatch: %s, %s.", Type.GAUGE_DOUBLE, "ValueLong")); } @Test - public void typeMismatch_CumulativeInt64_TimeSeriesGauge() { + public void typeMismatch_CumulativeInt64_Double() { typeMismatch( METRIC_DESCRIPTOR_2, - TIME_SERIES_GAUGE_LIST, - String.format( - "Type mismatch: %s, %s.", - Type.CUMULATIVE_INT64, TIME_SERIES_GAUGE_LIST.getClass().getSimpleName())); + Arrays.asList(GAUGE_TIME_SERIES_1), + String.format("Type mismatch: %s, %s.", Type.CUMULATIVE_INT64, "ValueDouble")); } private void typeMismatch( - MetricDescriptor metricDescriptor, TimeSeriesList timeSeriesList, String errorMessage) { + MetricDescriptor metricDescriptor, List timeSeriesList, String errorMessage) { thrown.expect(IllegalArgumentException.class); thrown.expectMessage(errorMessage); Metric.create(metricDescriptor, timeSeriesList); @@ -110,18 +103,14 @@ public class MetricTest { @Test public void testEquals() { new EqualsTester() - .addEqualityGroup( - Metric.create(METRIC_DESCRIPTOR_1, TIME_SERIES_GAUGE_LIST), - Metric.create(METRIC_DESCRIPTOR_1, TIME_SERIES_GAUGE_LIST)) .addEqualityGroup( Metric.create( - METRIC_DESCRIPTOR_1, - TimeSeriesGaugeList.create(Collections.emptyList()))) - .addEqualityGroup(Metric.create(METRIC_DESCRIPTOR_2, TIME_SERIES_CUMULATIVE_LIST)) - .addEqualityGroup( + METRIC_DESCRIPTOR_1, Arrays.asList(GAUGE_TIME_SERIES_1, GAUGE_TIME_SERIES_2)), Metric.create( - METRIC_DESCRIPTOR_2, - TimeSeriesCumulativeList.create(Collections.emptyList()))) + METRIC_DESCRIPTOR_1, Arrays.asList(GAUGE_TIME_SERIES_1, GAUGE_TIME_SERIES_2))) + .addEqualityGroup(Metric.create(METRIC_DESCRIPTOR_1, Collections.emptyList())) + .addEqualityGroup(Metric.create(METRIC_DESCRIPTOR_2, Arrays.asList(CUMULATIVE_TIME_SERIES))) + .addEqualityGroup(Metric.create(METRIC_DESCRIPTOR_2, Collections.emptyList())) .testEquals(); } } diff --git a/api/src/test/java/io/opencensus/metrics/TimeSeriesCumulativeTest.java b/api/src/test/java/io/opencensus/metrics/TimeSeriesCumulativeTest.java deleted file mode 100644 index 3532a5a5..00000000 --- a/api/src/test/java/io/opencensus/metrics/TimeSeriesCumulativeTest.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * 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.metrics; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.common.testing.EqualsTester; -import io.opencensus.common.Timestamp; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import org.hamcrest.CoreMatchers; -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 TimeSeriesCumulative}. */ -@RunWith(JUnit4.class) -public class TimeSeriesCumulativeTest { - - @Rule public ExpectedException thrown = ExpectedException.none(); - - private static final LabelValue LABEL_VALUE_1 = LabelValue.create("value1"); - private static final LabelValue LABEL_VALUE_2 = LabelValue.create("value2"); - private static final Value VALUE_LONG = Value.longValue(12345678); - private static final Value VALUE_DOUBLE = Value.doubleValue(-345.77); - private static final Timestamp TIMESTAMP_1 = Timestamp.fromMillis(1000); - private static final Timestamp TIMESTAMP_2 = Timestamp.fromMillis(2000); - private static final Timestamp TIMESTAMP_3 = Timestamp.fromMillis(3000); - private static final Point POINT_1 = Point.create(VALUE_DOUBLE, TIMESTAMP_2); - private static final Point POINT_2 = Point.create(VALUE_LONG, TIMESTAMP_3); - - @Test - public void testGet_TimeSeriesCumulative() { - TimeSeriesCumulative cumulativeTimeSeries = - TimeSeriesCumulative.create( - Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(POINT_1), TIMESTAMP_1); - assertThat(cumulativeTimeSeries.getStartTimestamp()).isEqualTo(TIMESTAMP_1); - assertThat(cumulativeTimeSeries.getLabelValues()) - .containsExactly(LABEL_VALUE_1, LABEL_VALUE_2) - .inOrder(); - assertThat(cumulativeTimeSeries.getPoints()).containsExactly(POINT_1).inOrder(); - } - - @Test - public void create_WithNullLabelValueList() { - thrown.expect(NullPointerException.class); - thrown.expectMessage(CoreMatchers.equalTo("labelValues")); - TimeSeriesCumulative.create(null, Collections.emptyList(), TIMESTAMP_1); - } - - @Test - public void create_WithNullLabelValue() { - List labelValues = Arrays.asList(LABEL_VALUE_1, null); - thrown.expect(NullPointerException.class); - thrown.expectMessage(CoreMatchers.equalTo("labelValue")); - TimeSeriesCumulative.create(labelValues, Collections.emptyList(), TIMESTAMP_1); - } - - @Test - public void create_WithNullPointList() { - thrown.expect(NullPointerException.class); - thrown.expectMessage(CoreMatchers.equalTo("points")); - TimeSeriesCumulative.create(Collections.emptyList(), null, TIMESTAMP_1); - } - - @Test - public void create_WithNullPoint() { - List points = Arrays.asList(POINT_1, null); - thrown.expect(NullPointerException.class); - thrown.expectMessage(CoreMatchers.equalTo("point")); - TimeSeriesCumulative.create(Collections.emptyList(), points, TIMESTAMP_1); - } - - @Test - public void testEquals() { - new EqualsTester() - .addEqualityGroup( - TimeSeriesCumulative.create( - Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(POINT_1), TIMESTAMP_1), - TimeSeriesCumulative.create( - Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(POINT_1), TIMESTAMP_1)) - .addEqualityGroup( - TimeSeriesCumulative.create( - Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(POINT_1), TIMESTAMP_2)) - .addEqualityGroup( - TimeSeriesCumulative.create( - Arrays.asList(LABEL_VALUE_1), Arrays.asList(POINT_1), TIMESTAMP_2)) - .addEqualityGroup( - TimeSeriesCumulative.create( - Arrays.asList(LABEL_VALUE_1), Arrays.asList(POINT_2), TIMESTAMP_2)) - .addEqualityGroup( - TimeSeriesCumulative.create( - Arrays.asList(LABEL_VALUE_1), Arrays.asList(POINT_1, POINT_2), TIMESTAMP_2)) - .testEquals(); - } -} diff --git a/api/src/test/java/io/opencensus/metrics/TimeSeriesGaugeTest.java b/api/src/test/java/io/opencensus/metrics/TimeSeriesGaugeTest.java deleted file mode 100644 index 83f7c267..00000000 --- a/api/src/test/java/io/opencensus/metrics/TimeSeriesGaugeTest.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * 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.metrics; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.common.testing.EqualsTester; -import io.opencensus.common.Timestamp; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import org.hamcrest.CoreMatchers; -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 TimeSeriesGauge}. */ -@RunWith(JUnit4.class) -public class TimeSeriesGaugeTest { - - @Rule public ExpectedException thrown = ExpectedException.none(); - - private static final LabelValue LABEL_VALUE_1 = LabelValue.create("value1"); - private static final LabelValue LABEL_VALUE_2 = LabelValue.create("value2"); - private static final Value VALUE_LONG = Value.longValue(12345678); - private static final Value VALUE_DOUBLE = Value.doubleValue(-345.77); - private static final Timestamp TIMESTAMP_1 = Timestamp.fromMillis(1000); - private static final Timestamp TIMESTAMP_2 = Timestamp.fromMillis(2000); - private static final Point POINT_1 = Point.create(VALUE_DOUBLE, TIMESTAMP_1); - private static final Point POINT_2 = Point.create(VALUE_LONG, TIMESTAMP_2); - - @Test - public void testGet_TimeSeriesGauge() { - TimeSeriesGauge gaugeTimeSeries = - TimeSeriesGauge.create( - Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(POINT_1, POINT_2)); - assertThat(gaugeTimeSeries.getLabelValues()) - .containsExactly(LABEL_VALUE_1, LABEL_VALUE_2) - .inOrder(); - assertThat(gaugeTimeSeries.getPoints()).containsExactly(POINT_1, POINT_2).inOrder(); - } - - @Test - public void create_WithNullLabelValueList() { - thrown.expect(NullPointerException.class); - thrown.expectMessage(CoreMatchers.equalTo("labelValues")); - TimeSeriesGauge.create(null, Collections.emptyList()); - } - - @Test - public void create_WithNullLabelValue() { - List labelValues = Arrays.asList(LABEL_VALUE_1, null); - thrown.expect(NullPointerException.class); - thrown.expectMessage(CoreMatchers.equalTo("labelValue")); - TimeSeriesGauge.create(labelValues, Collections.emptyList()); - } - - @Test - public void create_WithNullPointList() { - thrown.expect(NullPointerException.class); - thrown.expectMessage(CoreMatchers.equalTo("points")); - TimeSeriesGauge.create(Collections.emptyList(), null); - } - - @Test - public void create_WithNullPoint() { - List points = Arrays.asList(POINT_1, null); - thrown.expect(NullPointerException.class); - thrown.expectMessage(CoreMatchers.equalTo("point")); - TimeSeriesGauge.create(Collections.emptyList(), points); - } - - @Test - public void testEquals() { - new EqualsTester() - .addEqualityGroup( - TimeSeriesGauge.create( - Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(POINT_1, POINT_2)), - TimeSeriesGauge.create( - Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(POINT_1, POINT_2))) - .addEqualityGroup( - TimeSeriesGauge.create( - Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(POINT_1))) - .addEqualityGroup( - TimeSeriesGauge.create(Arrays.asList(LABEL_VALUE_1), Arrays.asList(POINT_1))) - .testEquals(); - } -} diff --git a/api/src/test/java/io/opencensus/metrics/TimeSeriesListTest.java b/api/src/test/java/io/opencensus/metrics/TimeSeriesListTest.java deleted file mode 100644 index 46ace50f..00000000 --- a/api/src/test/java/io/opencensus/metrics/TimeSeriesListTest.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * 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.metrics; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.common.testing.EqualsTester; -import io.opencensus.common.Functions; -import io.opencensus.common.Timestamp; -import io.opencensus.metrics.TimeSeriesList.TimeSeriesCumulativeList; -import io.opencensus.metrics.TimeSeriesList.TimeSeriesGaugeList; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import org.hamcrest.CoreMatchers; -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 TimeSeries}. */ -@RunWith(JUnit4.class) -public class TimeSeriesListTest { - - @Rule public ExpectedException thrown = ExpectedException.none(); - - private static final LabelValue LABEL_VALUE_1 = LabelValue.create("value1"); - private static final LabelValue LABEL_VALUE_2 = LabelValue.create("value2"); - private static final LabelValue LABEL_VALUE_EMPTY = LabelValue.create(""); - private static final Value VALUE_LONG = Value.longValue(12345678); - private static final Value VALUE_DOUBLE = Value.doubleValue(-345.77); - private static final Timestamp TIMESTAMP_1 = Timestamp.fromMillis(1000); - private static final Timestamp TIMESTAMP_2 = Timestamp.fromMillis(2000); - private static final Timestamp TIMESTAMP_3 = Timestamp.fromMillis(3000); - private static final Point POINT_1 = Point.create(VALUE_DOUBLE, TIMESTAMP_2); - private static final Point POINT_2 = Point.create(VALUE_LONG, TIMESTAMP_3); - private static final TimeSeriesGauge TIME_SERIES_GAUGE = - TimeSeriesGauge.create( - Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(POINT_1, POINT_2)); - private static final TimeSeriesCumulative TIME_SERIES_CUMULATIVE = - TimeSeriesCumulative.create( - Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_EMPTY), Arrays.asList(POINT_1), TIMESTAMP_1); - - @Test - public void testGet_TimeSeriesGaugeList() { - TimeSeriesGaugeList gaugeTimeSeriesList = - TimeSeriesGaugeList.create(Collections.singletonList(TIME_SERIES_GAUGE)); - assertThat(gaugeTimeSeriesList.getList()).containsExactly(TIME_SERIES_GAUGE); - } - - @Test - public void testGet_TimeSeriesCumulativeList() { - TimeSeriesCumulativeList cumulativeTimeSeriesList = - TimeSeriesCumulativeList.create(Collections.singletonList(TIME_SERIES_CUMULATIVE)); - assertThat(cumulativeTimeSeriesList.getList()).containsExactly(TIME_SERIES_CUMULATIVE); - } - - @Test - public void createGaugeList_WithNullList() { - thrown.expect(NullPointerException.class); - thrown.expectMessage(CoreMatchers.equalTo("list")); - TimeSeriesGaugeList.create(null); - } - - @Test - public void createGaugeList_WithNullTimeSeries() { - thrown.expect(NullPointerException.class); - thrown.expectMessage(CoreMatchers.equalTo("timeSeriesGauge")); - TimeSeriesGaugeList.create(Arrays.asList(TIME_SERIES_GAUGE, null)); - } - - @Test - public void createCumulativeList_WithNullList() { - thrown.expect(NullPointerException.class); - thrown.expectMessage(CoreMatchers.equalTo("list")); - TimeSeriesCumulativeList.create(null); - } - - @Test - public void createCumulativeList_WithNullTimeSeries() { - thrown.expect(NullPointerException.class); - thrown.expectMessage(CoreMatchers.equalTo("timeSeriesCumulative")); - TimeSeriesCumulativeList.create(Arrays.asList(TIME_SERIES_CUMULATIVE, null)); - } - - @Test - public void testEquals() { - new EqualsTester() - .addEqualityGroup( - TimeSeriesGaugeList.create(Collections.singletonList(TIME_SERIES_GAUGE)), - TimeSeriesGaugeList.create(Collections.singletonList(TIME_SERIES_GAUGE))) - .addEqualityGroup( - TimeSeriesCumulativeList.create(Collections.singletonList(TIME_SERIES_CUMULATIVE)), - TimeSeriesCumulativeList.create(Collections.singletonList(TIME_SERIES_CUMULATIVE))) - .addEqualityGroup(TimeSeriesGaugeList.create(Collections.emptyList())) - .addEqualityGroup( - TimeSeriesCumulativeList.create(Collections.emptyList())) - .testEquals(); - } - - @Test - public void testMatch() { - TimeSeriesList gaugeTimeSeriesList = - TimeSeriesGaugeList.create(Collections.singletonList(TIME_SERIES_GAUGE)); - TimeSeriesList cumulativeTimeSeriesList = - TimeSeriesCumulativeList.create(Collections.singletonList(TIME_SERIES_CUMULATIVE)); - - final List actual = new ArrayList(); - for (TimeSeriesList timeSeriesList : - Arrays.asList(cumulativeTimeSeriesList, gaugeTimeSeriesList)) { - actual.add( - timeSeriesList.match( - Functions.returnConstant("TimeSeriesGaugeList"), - Functions.returnConstant("TimeSeriesCumulativeList"), - Functions.throwAssertionError())); - } - assertThat(actual).containsExactly("TimeSeriesCumulativeList", "TimeSeriesGaugeList").inOrder(); - } -} diff --git a/api/src/test/java/io/opencensus/metrics/TimeSeriesTest.java b/api/src/test/java/io/opencensus/metrics/TimeSeriesTest.java new file mode 100644 index 00000000..07dff97d --- /dev/null +++ b/api/src/test/java/io/opencensus/metrics/TimeSeriesTest.java @@ -0,0 +1,116 @@ +/* + * 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.metrics; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.common.testing.EqualsTester; +import io.opencensus.common.Timestamp; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import org.hamcrest.CoreMatchers; +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 TimeSeries}. */ +@RunWith(JUnit4.class) +public class TimeSeriesTest { + + @Rule public ExpectedException thrown = ExpectedException.none(); + + private static final LabelValue LABEL_VALUE_1 = LabelValue.create("value1"); + private static final LabelValue LABEL_VALUE_2 = LabelValue.create("value2"); + private static final Value VALUE_LONG = Value.longValue(12345678); + private static final Value VALUE_DOUBLE = Value.doubleValue(-345.77); + private static final Timestamp TIMESTAMP_1 = Timestamp.fromMillis(1000); + private static final Timestamp TIMESTAMP_2 = Timestamp.fromMillis(2000); + private static final Timestamp TIMESTAMP_3 = Timestamp.fromMillis(3000); + private static final Point POINT_1 = Point.create(VALUE_DOUBLE, TIMESTAMP_2); + private static final Point POINT_2 = Point.create(VALUE_LONG, TIMESTAMP_3); + + @Test + public void testGet_TimeSeries() { + TimeSeries cumulativeTimeSeries = + TimeSeries.create( + Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(POINT_1), TIMESTAMP_1); + assertThat(cumulativeTimeSeries.getStartTimestamp()).isEqualTo(TIMESTAMP_1); + assertThat(cumulativeTimeSeries.getLabelValues()) + .containsExactly(LABEL_VALUE_1, LABEL_VALUE_2) + .inOrder(); + assertThat(cumulativeTimeSeries.getPoints()).containsExactly(POINT_1).inOrder(); + } + + @Test + public void create_WithNullLabelValueList() { + thrown.expect(NullPointerException.class); + thrown.expectMessage(CoreMatchers.equalTo("labelValues")); + TimeSeries.create(null, Collections.emptyList(), TIMESTAMP_1); + } + + @Test + public void create_WithNullLabelValue() { + List labelValues = Arrays.asList(LABEL_VALUE_1, null); + thrown.expect(NullPointerException.class); + thrown.expectMessage(CoreMatchers.equalTo("labelValue")); + TimeSeries.create(labelValues, Collections.emptyList(), TIMESTAMP_1); + } + + @Test + public void create_WithNullPointList() { + thrown.expect(NullPointerException.class); + thrown.expectMessage(CoreMatchers.equalTo("points")); + TimeSeries.create(Collections.emptyList(), null, TIMESTAMP_1); + } + + @Test + public void create_WithNullPoint() { + List points = Arrays.asList(POINT_1, null); + thrown.expect(NullPointerException.class); + thrown.expectMessage(CoreMatchers.equalTo("point")); + TimeSeries.create(Collections.emptyList(), points, TIMESTAMP_1); + } + + @Test + public void testEquals() { + new EqualsTester() + .addEqualityGroup( + TimeSeries.create( + Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(POINT_1), TIMESTAMP_1), + TimeSeries.create( + Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(POINT_1), TIMESTAMP_1)) + .addEqualityGroup( + 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), Arrays.asList(POINT_1), null)) + .addEqualityGroup( + TimeSeries.create( + Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(POINT_1), TIMESTAMP_2)) + .addEqualityGroup( + TimeSeries.create(Arrays.asList(LABEL_VALUE_1), Arrays.asList(POINT_1), TIMESTAMP_2)) + .addEqualityGroup( + TimeSeries.create(Arrays.asList(LABEL_VALUE_1), Arrays.asList(POINT_2), TIMESTAMP_2)) + .addEqualityGroup( + TimeSeries.create( + Arrays.asList(LABEL_VALUE_1), Arrays.asList(POINT_1, POINT_2), TIMESTAMP_2)) + .testEquals(); + } +} -- cgit v1.2.3