diff options
author | Bogdan Drutu <bdrutu@google.com> | 2018-10-13 14:21:09 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-13 14:21:09 -0700 |
commit | 7c6a1e66dd8ff52aaf99fb086df656db149b58d5 (patch) | |
tree | 6ea4e057c1f8a4a53e6493672c32ec933746f553 | |
parent | 135dcd99d7fdd42c7d6fe1116a8978ca7e5ec9a7 (diff) | |
download | opencensus-java-7c6a1e66dd8ff52aaf99fb086df656db149b58d5.tar.gz |
Cleanup metrics API exception messages. (#1494)
* Cleanup metrics API exception messages.
* Fix MutableAggregationTest.java
3 files changed, 32 insertions, 28 deletions
diff --git a/api/src/main/java/io/opencensus/metrics/export/Distribution.java b/api/src/main/java/io/opencensus/metrics/export/Distribution.java index eb0add86..d55f101c 100644 --- a/api/src/main/java/io/opencensus/metrics/export/Distribution.java +++ b/api/src/main/java/io/opencensus/metrics/export/Distribution.java @@ -69,16 +69,11 @@ public abstract class Distribution { sumOfSquaredDeviations == 0, "sum of squared deviations should be 0 if count is 0."); } Utils.checkNotNull(bucketOptions, "bucketOptions"); - + List<Bucket> bucketsCopy = + Collections.unmodifiableList(new ArrayList<Bucket>(Utils.checkNotNull(buckets, "buckets"))); + Utils.checkListElementNotNull(bucketsCopy, "bucket"); return new AutoValue_Distribution( - count, sum, sumOfSquaredDeviations, bucketOptions, copyBucketCount(buckets)); - } - - private static List<Bucket> copyBucketCount(List<Bucket> buckets) { - Utils.checkNotNull(buckets, "bucket list should not be null."); - List<Bucket> bucketsCopy = new ArrayList<Bucket>(buckets); - Utils.checkListElementNotNull(bucketsCopy, "bucket should not be null."); - return Collections.unmodifiableList(bucketsCopy); + count, sum, sumOfSquaredDeviations, bucketOptions, bucketsCopy); } /** @@ -204,24 +199,23 @@ public abstract class Distribution { * @since 0.17 */ private static ExplicitOptions create(List<Double> bucketBoundaries) { - Utils.checkNotNull(bucketBoundaries, "bucketBoundaries list should not be null."); - return new AutoValue_Distribution_BucketOptions_ExplicitOptions( - checkBucketBoundsAreSorted(bucketBoundaries)); + Utils.checkNotNull(bucketBoundaries, "bucketBoundaries"); + List<Double> bucketBoundariesCopy = + Collections.unmodifiableList(new ArrayList<Double>(bucketBoundaries)); + checkBucketBoundsAreSorted(bucketBoundariesCopy); + return new AutoValue_Distribution_BucketOptions_ExplicitOptions(bucketBoundariesCopy); } - private static List<Double> checkBucketBoundsAreSorted(List<Double> bucketBoundaries) { - List<Double> bucketBoundariesCopy = new ArrayList<Double>(bucketBoundaries); // Deep copy. - // Check if sorted. - if (bucketBoundariesCopy.size() >= 1) { - double previous = bucketBoundariesCopy.get(0); - Utils.checkArgument(previous > 0, "bucket boundaries should be > 0"); - for (int i = 1; i < bucketBoundariesCopy.size(); i++) { - double next = bucketBoundariesCopy.get(i); + private static void checkBucketBoundsAreSorted(List<Double> bucketBoundaries) { + if (bucketBoundaries.size() >= 1) { + double previous = Utils.checkNotNull(bucketBoundaries.get(0), "bucketBoundary"); + Utils.checkArgument(previous > 0, "bucket boundary should be > 0"); + for (int i = 1; i < bucketBoundaries.size(); i++) { + double next = Utils.checkNotNull(bucketBoundaries.get(i), "bucketBoundary"); Utils.checkArgument(previous < next, "bucket boundaries not sorted."); previous = next; } } - return Collections.unmodifiableList(bucketBoundariesCopy); } /** diff --git a/api/src/test/java/io/opencensus/metrics/export/DistributionTest.java b/api/src/test/java/io/opencensus/metrics/export/DistributionTest.java index ad89d338..85b31498 100644 --- a/api/src/test/java/io/opencensus/metrics/export/DistributionTest.java +++ b/api/src/test/java/io/opencensus/metrics/export/DistributionTest.java @@ -98,9 +98,9 @@ public class DistributionTest { @Test public void createAndGet_ExplicitBucketsNegativeBounds() { - List<Double> bucketBounds = Arrays.asList(-1.0); + List<Double> bucketBounds = Collections.singletonList(-1.0); thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("bucket boundaries should be > 0"); + thrown.expectMessage("bucket boundary should be > 0"); BucketOptions.explicitOptions(bucketBounds); } @@ -252,15 +252,25 @@ public class DistributionTest { } @Test - public void createDistribution_NullBucketBounds() { + public void createDistribution_NullBucketBoundaries() { List<Bucket> buckets = Arrays.asList(Bucket.create(3), Bucket.create(1), Bucket.create(2), Bucket.create(4)); thrown.expect(NullPointerException.class); - thrown.expectMessage("bucketBoundaries list should not be null."); + thrown.expectMessage("bucketBoundaries"); Distribution.create(10, 6.6, 678.54, BucketOptions.explicitOptions(null), buckets); } @Test + public void createDistribution_NullBucketBoundary() { + List<Bucket> buckets = + Arrays.asList(Bucket.create(3), Bucket.create(1), Bucket.create(2), Bucket.create(4)); + thrown.expect(NullPointerException.class); + thrown.expectMessage("bucketBoundary"); + Distribution.create( + 10, 6.6, 678.54, BucketOptions.explicitOptions(Arrays.asList(2.5, null)), buckets); + } + + @Test public void createDistribution_NullBucketOptions() { List<Bucket> buckets = Arrays.asList(Bucket.create(3), Bucket.create(1), Bucket.create(2), Bucket.create(4)); @@ -274,7 +284,7 @@ public class DistributionTest { List<Double> bucketBounds = Arrays.asList(1.0, 2.0, 5.0); BucketOptions bucketOptions = BucketOptions.explicitOptions(bucketBounds); thrown.expect(NullPointerException.class); - thrown.expectMessage("bucket list should not be null."); + thrown.expectMessage("buckets"); Distribution.create(10, 6.6, 678.54, bucketOptions, null); } @@ -285,7 +295,7 @@ public class DistributionTest { List<Bucket> buckets = Arrays.asList(Bucket.create(3), Bucket.create(1), null, Bucket.create(4)); thrown.expect(NullPointerException.class); - thrown.expectMessage("bucket should not be null."); + thrown.expectMessage("bucket"); Distribution.create(10, 6.6, 678.54, bucketOptions, buckets); } diff --git a/impl_core/src/test/java/io/opencensus/implcore/stats/MutableAggregationTest.java b/impl_core/src/test/java/io/opencensus/implcore/stats/MutableAggregationTest.java index bf76b8fb..a6139e53 100644 --- a/impl_core/src/test/java/io/opencensus/implcore/stats/MutableAggregationTest.java +++ b/impl_core/src/test/java/io/opencensus/implcore/stats/MutableAggregationTest.java @@ -300,7 +300,7 @@ public class MutableAggregationTest { .isEqualTo(Point.create(Value.doubleValue(0), TIMESTAMP)); thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("bucket boundaries should be > 0"); + thrown.expectMessage("bucket boundary should be > 0"); assertThat(MutableDistribution.create(BUCKET_BOUNDARIES).toPoint(TIMESTAMP)) .isEqualTo( Point.create( |