From 3e4352b3684b4646a82eef4f2da66038531d821e Mon Sep 17 00:00:00 2001 From: Yang Song Date: Mon, 16 Apr 2018 12:13:59 -0700 Subject: Move toMillis(Duration) to Utils so that it can be reused. (#1114) * Add toMillis() method to TimeUtils. * Reuse toMillis() method in impl and exporters. * Add a note about overflow and precision loss. * Move toMillis() to Duration. --- .../java/io/opencensus/implcore/stats/IntervalBucket.java | 3 +-- .../java/io/opencensus/implcore/stats/MutableViewData.java | 14 +++----------- 2 files changed, 4 insertions(+), 13 deletions(-) (limited to 'impl_core/src/main/java/io/opencensus/implcore/stats') diff --git a/impl_core/src/main/java/io/opencensus/implcore/stats/IntervalBucket.java b/impl_core/src/main/java/io/opencensus/implcore/stats/IntervalBucket.java index a35948a7..f114bea6 100644 --- a/impl_core/src/main/java/io/opencensus/implcore/stats/IntervalBucket.java +++ b/impl_core/src/main/java/io/opencensus/implcore/stats/IntervalBucket.java @@ -18,7 +18,6 @@ package io.opencensus.implcore.stats; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; -import static io.opencensus.implcore.stats.MutableViewData.toMillis; import com.google.common.collect.Maps; import io.opencensus.common.Duration; @@ -82,7 +81,7 @@ final class IntervalBucket { checkArgument( elapsedTime.compareTo(ZERO) >= 0 && elapsedTime.compareTo(duration) < 0, "This bucket must be current."); - return ((double) toMillis(elapsedTime)) / toMillis(duration); + return ((double) Duration.toMillis(elapsedTime)) / Duration.toMillis(duration); } void clearStats() { 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 588b4897..f0938c06 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 @@ -66,9 +66,6 @@ import org.checkerframework.checker.nullness.qual.Nullable; @SuppressWarnings("deprecation") abstract class MutableViewData { - private static final long MILLIS_PER_SECOND = 1000L; - private static final long NANOS_PER_MILLI = 1000 * 1000; - @javax.annotation.Nullable @VisibleForTesting static final TagValue UNKNOWN_TAG_VALUE = null; @VisibleForTesting static final Timestamp ZERO_TIMESTAMP = Timestamp.create(0, 0); @@ -149,11 +146,6 @@ abstract class MutableViewData { return tagValues; } - // Returns the milliseconds representation of a Duration. - static long toMillis(Duration duration) { - return duration.getSeconds() * MILLIS_PER_SECOND + duration.getNanos() / NANOS_PER_MILLI; - } - /** * Create an empty {@link MutableAggregation} based on the given {@link Aggregation}. * @@ -295,7 +287,7 @@ abstract class MutableViewData { super(view); Duration totalDuration = ((View.AggregationWindow.Interval) view.getWindow()).getDuration(); this.totalDuration = totalDuration; - this.bucketDuration = Duration.fromMillis(toMillis(totalDuration) / N); + this.bucketDuration = Duration.fromMillis(Duration.toMillis(totalDuration) / N); // When initializing. add N empty buckets prior to the start timestamp of this // IntervalMutableViewData, so that the last bucket will be the current one in effect. @@ -354,8 +346,8 @@ abstract class MutableViewData { checkArgument( now.compareTo(startOfLastBucket) >= 0, "Current time must be within or after the last bucket."); - long elapsedTimeMillis = toMillis(now.subtractTimestamp(startOfLastBucket)); - long numOfPadBuckets = elapsedTimeMillis / toMillis(bucketDuration); + long elapsedTimeMillis = Duration.toMillis(now.subtractTimestamp(startOfLastBucket)); + long numOfPadBuckets = elapsedTimeMillis / Duration.toMillis(bucketDuration); shiftBucketList(numOfPadBuckets, now); } -- cgit v1.2.3