diff options
author | Mayur Kale <mayurkale@google.com> | 2018-10-10 12:31:51 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-10 12:31:51 -0700 |
commit | 9c4df3dbe085a70850cc466849d33d90ffa846f4 (patch) | |
tree | f36c626bae1ec48ac2209be310d4e30d929db7b6 | |
parent | e3b2074a090836985307123517fd2d361c4b503c (diff) | |
download | opencensus-java-9c4df3dbe085a70850cc466849d33d90ffa846f4.tar.gz |
Metrics/TimeSeries: Add createWithOnePoint method to accept only one … (#1486)
* Metrics/TimeSeries: Add createWithOnePoint method to accept only one point
* Fix reviews
* Minor Fix
7 files changed, 101 insertions, 50 deletions
diff --git a/api/src/main/java/io/opencensus/metrics/export/TimeSeries.java b/api/src/main/java/io/opencensus/metrics/export/TimeSeries.java index 959f55ba..bfaeae98 100644 --- a/api/src/main/java/io/opencensus/metrics/export/TimeSeries.java +++ b/api/src/main/java/io/opencensus/metrics/export/TimeSeries.java @@ -52,14 +52,45 @@ public abstract class TimeSeries { */ public static TimeSeries create( List<LabelValue> labelValues, List<Point> points, @Nullable Timestamp startTimestamp) { + Utils.checkNotNull(points, "points"); + Utils.checkListElementNotNull(points, "point"); + return createInternal( + labelValues, Collections.unmodifiableList(new ArrayList<Point>(points)), startTimestamp); + } + + /** + * Creates a {@link TimeSeries}. + * + * @param labelValues the {@code LabelValue}s that uniquely identify this {@code TimeSeries}. + * @param point the single data {@code Point} 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.17 + */ + public static TimeSeries createWithOnePoint( + List<LabelValue> labelValues, Point point, @Nullable Timestamp startTimestamp) { + Utils.checkNotNull(point, "point"); + return createInternal(labelValues, Collections.singletonList(point), startTimestamp); + } + + /** + * 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}. + */ + private static TimeSeries createInternal( + List<LabelValue> labelValues, List<Point> 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<LabelValue>(labelValues)), - Collections.unmodifiableList(new ArrayList<Point>(points)), + points, startTimestamp); } diff --git a/api/src/test/java/io/opencensus/metrics/export/TimeSeriesTest.java b/api/src/test/java/io/opencensus/metrics/export/TimeSeriesTest.java index 31812549..92a2c8cf 100644 --- a/api/src/test/java/io/opencensus/metrics/export/TimeSeriesTest.java +++ b/api/src/test/java/io/opencensus/metrics/export/TimeSeriesTest.java @@ -90,6 +90,40 @@ public class TimeSeriesTest { } @Test + public void testGet_WithOnePointTimeSeries() { + TimeSeries cumulativeTimeSeries = + TimeSeries.createWithOnePoint( + Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), 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 createWithOnePoint_WithNullLabelValueList() { + thrown.expect(NullPointerException.class); + thrown.expectMessage(CoreMatchers.equalTo("labelValues")); + TimeSeries.createWithOnePoint(null, POINT_1, TIMESTAMP_1); + } + + @Test + public void createWithOnePoint_WithNullLabelValue() { + List<LabelValue> labelValues = Arrays.asList(LABEL_VALUE_1, null); + thrown.expect(NullPointerException.class); + thrown.expectMessage(CoreMatchers.equalTo("labelValue")); + TimeSeries.createWithOnePoint(labelValues, POINT_1, TIMESTAMP_1); + } + + @Test + public void createWithOnePoint_WithNullPointList() { + thrown.expect(NullPointerException.class); + thrown.expectMessage(CoreMatchers.equalTo("point")); + TimeSeries.createWithOnePoint(Collections.<LabelValue>emptyList(), null, TIMESTAMP_1); + } + + @Test public void testEquals() { new EqualsTester() .addEqualityGroup( diff --git a/contrib/dropwizard/src/main/java/io/opencensus/contrib/dropwizard/DropWizardMetrics.java b/contrib/dropwizard/src/main/java/io/opencensus/contrib/dropwizard/DropWizardMetrics.java index 7cc52d3f..246be801 100644 --- a/contrib/dropwizard/src/main/java/io/opencensus/contrib/dropwizard/DropWizardMetrics.java +++ b/contrib/dropwizard/src/main/java/io/opencensus/contrib/dropwizard/DropWizardMetrics.java @@ -127,10 +127,8 @@ public class DropWizardMetrics extends MetricProducer { MetricDescriptor.create( metricName, metricDescription, DEFAULT_UNIT, type, Collections.<LabelKey>emptyList()); TimeSeries timeSeries = - TimeSeries.create( - Collections.<LabelValue>emptyList(), - Collections.singletonList(Point.create(value, clock.now())), - null); + TimeSeries.createWithOnePoint( + Collections.<LabelValue>emptyList(), Point.create(value, clock.now()), null); return Metric.create(metricDescriptor, Collections.singletonList(timeSeries)); } @@ -154,10 +152,9 @@ public class DropWizardMetrics extends MetricProducer { Type.GAUGE_INT64, Collections.<LabelKey>emptyList()); TimeSeries timeSeries = - TimeSeries.create( + TimeSeries.createWithOnePoint( Collections.<LabelValue>emptyList(), - Collections.singletonList( - Point.create(Value.longValue(counter.getCount()), clock.now())), + Point.create(Value.longValue(counter.getCount()), clock.now()), null); return Metric.create(metricDescriptor, Collections.singletonList(timeSeries)); } @@ -181,9 +178,9 @@ public class DropWizardMetrics extends MetricProducer { Type.CUMULATIVE_INT64, Collections.<LabelKey>emptyList()); TimeSeries countTimeSeries = - TimeSeries.create( + TimeSeries.createWithOnePoint( Collections.<LabelValue>emptyList(), - Collections.singletonList(Point.create(Value.longValue(meter.getCount()), clock.now())), + Point.create(Value.longValue(meter.getCount()), clock.now()), null); // Collect rate related metric @@ -196,25 +193,21 @@ public class DropWizardMetrics extends MetricProducer { List<TimeSeries> timeSeriesList = Arrays.asList( - TimeSeries.create( + TimeSeries.createWithOnePoint( RATE_MEAN_LABEL_VALUE, - Collections.singletonList( - Point.create(Value.doubleValue(meter.getMeanRate()), clock.now())), + Point.create(Value.doubleValue(meter.getMeanRate()), clock.now()), null), - TimeSeries.create( + TimeSeries.createWithOnePoint( RATE_ONE_MINUTE_LABEL_VALUE, - Collections.singletonList( - Point.create(Value.doubleValue(meter.getOneMinuteRate()), clock.now())), + Point.create(Value.doubleValue(meter.getOneMinuteRate()), clock.now()), null), - TimeSeries.create( + TimeSeries.createWithOnePoint( RATE_FIVE_MINUTE_LABEL_VALUE, - Collections.singletonList( - Point.create(Value.doubleValue(meter.getFiveMinuteRate()), clock.now())), + Point.create(Value.doubleValue(meter.getFiveMinuteRate()), clock.now()), null), - TimeSeries.create( + TimeSeries.createWithOnePoint( RATE_FIFTEEN_MINUTE_LABEL_VALUE, - Collections.singletonList( - Point.create(Value.doubleValue(meter.getFifteenMinuteRate()), clock.now())), + Point.create(Value.doubleValue(meter.getFifteenMinuteRate()), clock.now()), null)); return Collections.unmodifiableList( @@ -287,10 +280,8 @@ public class DropWizardMetrics extends MetricProducer { Type.SUMMARY, Collections.<LabelKey>emptyList()); TimeSeries timeSeries = - TimeSeries.create( - Collections.<LabelValue>emptyList(), - Collections.singletonList(point), - cumulativeStartTimestamp); + TimeSeries.createWithOnePoint( + Collections.<LabelValue>emptyList(), point, cumulativeStartTimestamp); return Metric.create(metricDescriptor, Collections.singletonList(timeSeries)); } 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 index 30e5bf36..06cfbc1e 100644 --- a/impl_core/src/main/java/io/opencensus/implcore/metrics/Gauge.java +++ b/impl_core/src/main/java/io/opencensus/implcore/metrics/Gauge.java @@ -63,10 +63,9 @@ abstract class Gauge { @Override TimeSeries getTimeSeries(Clock clock) { - return TimeSeries.create( + return TimeSeries.createWithOnePoint( getLabelValues(), - Collections.singletonList( - Point.create(Value.doubleValue(function.applyAsDouble(obj)), clock.now())), + Point.create(Value.doubleValue(function.applyAsDouble(obj)), clock.now()), null); } } @@ -92,10 +91,9 @@ abstract class Gauge { @Override TimeSeries getTimeSeries(Clock clock) { - return TimeSeries.create( + return TimeSeries.createWithOnePoint( getLabelValues(), - Collections.singletonList( - Point.create(Value.longValue(function.applyAsLong(obj)), clock.now())), + Point.create(Value.longValue(function.applyAsLong(obj)), clock.now()), null); } } diff --git a/impl_core/src/main/java/io/opencensus/implcore/stats/MutableViewData.java b/impl_core/src/main/java/io/opencensus/implcore/stats/MutableViewData.java index 0d25b292..928675e9 100644 --- a/impl_core/src/main/java/io/opencensus/implcore/stats/MutableViewData.java +++ b/impl_core/src/main/java/io/opencensus/implcore/stats/MutableViewData.java @@ -139,8 +139,7 @@ abstract class MutableViewData { tagValueAggregationMap.entrySet()) { List<LabelValue> labelValues = MetricUtils.tagValuesToLabelValues(entry.getKey()); Point point = entry.getValue().toPoint(now); - timeSeriesList.add( - TimeSeries.create(labelValues, Collections.singletonList(point), startTime)); + timeSeriesList.add(TimeSeries.createWithOnePoint(labelValues, point, startTime)); } return Metric.create(metricDescriptor, timeSeriesList); } 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 index 161ad348..e5e4cbc5 100644 --- a/impl_core/src/test/java/io/opencensus/implcore/metrics/GaugeTest.java +++ b/impl_core/src/test/java/io/opencensus/implcore/metrics/GaugeTest.java @@ -89,10 +89,9 @@ public class GaugeTest { Metric.create( MetricDescriptor.create(NAME, DESCRIPTION, UNIT, Type.GAUGE_INT64, LABEL_KEYS), Collections.singletonList( - TimeSeries.create( + TimeSeries.createWithOnePoint( LABEL_VALUES, - Collections.singletonList( - Point.create(Value.longValue(OBJ.hashCode()), TEST_TIME)), + Point.create(Value.longValue(OBJ.hashCode()), TEST_TIME), null)))); } @@ -103,10 +102,9 @@ public class GaugeTest { Metric.create( MetricDescriptor.create(NAME, DESCRIPTION, UNIT, Type.GAUGE_DOUBLE, LABEL_KEYS), Collections.singletonList( - TimeSeries.create( + TimeSeries.createWithOnePoint( LABEL_VALUES, - Collections.singletonList( - Point.create(Value.doubleValue(OBJ.hashCode()), TEST_TIME)), + Point.create(Value.doubleValue(OBJ.hashCode()), TEST_TIME), null)))); } } 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 3a5cb493..5210b266 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 @@ -159,9 +159,9 @@ public class MetricRegistryImplTest { Type.GAUGE_DOUBLE, Collections.unmodifiableList(Collections.singletonList(LABEL_KEY))), Collections.singletonList( - TimeSeries.create( + TimeSeries.createWithOnePoint( Collections.unmodifiableList(Collections.singletonList(LABEL_VALUES)), - Collections.singletonList(Point.create(Value.doubleValue(5.0), TEST_TIME)), + Point.create(Value.doubleValue(5.0), TEST_TIME), null)))); } @@ -263,9 +263,9 @@ public class MetricRegistryImplTest { Type.GAUGE_INT64, Collections.unmodifiableList(Collections.singletonList(LABEL_KEY))), Collections.singletonList( - TimeSeries.create( + TimeSeries.createWithOnePoint( Collections.unmodifiableList(Collections.singletonList(LABEL_VALUES)), - Collections.singletonList(Point.create(Value.longValue(7), TEST_TIME)), + Point.create(Value.longValue(7), TEST_TIME), null)))); } @@ -305,9 +305,9 @@ public class MetricRegistryImplTest { Type.GAUGE_INT64, Collections.unmodifiableList(Collections.singletonList(LABEL_KEY))), Collections.singletonList( - TimeSeries.create( + TimeSeries.createWithOnePoint( Collections.unmodifiableList(Collections.singletonList(LABEL_VALUES)), - Collections.singletonList(Point.create(Value.longValue(7), TEST_TIME)), + Point.create(Value.longValue(7), TEST_TIME), null))), Metric.create( MetricDescriptor.create( @@ -317,9 +317,9 @@ public class MetricRegistryImplTest { Type.GAUGE_DOUBLE, Collections.unmodifiableList(Collections.singletonList(LABEL_KEY))), Collections.singletonList( - TimeSeries.create( + TimeSeries.createWithOnePoint( Collections.unmodifiableList(Collections.singletonList(LABEL_VALUES)), - Collections.singletonList(Point.create(Value.doubleValue(5.0), TEST_TIME)), + Point.create(Value.doubleValue(5.0), TEST_TIME), null)))); } |