From 4ea0352fd0a66eea809727b3e0f440fab2c357a9 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Thu, 23 Feb 2017 07:58:23 -0800 Subject: Move core to the equivalent maven path. --- BUILD | 40 +-- .../google/instrumentation/common/Duration.java | 112 ------- .../google/instrumentation/common/Function.java | 25 -- .../common/NonThrowingCloseable.java | 22 -- .../google/instrumentation/common/Provider.java | 55 ---- .../google/instrumentation/common/Timestamp.java | 115 ------- .../instrumentation/common/TimestampFactory.java | 28 -- .../stats/DistributionAggregation.java | 166 ---------- .../stats/DistributionAggregationDescriptor.java | 74 ----- .../instrumentation/stats/IntervalAggregation.java | 103 ------ .../stats/IntervalAggregationDescriptor.java | 86 ----- .../stats/MeasurementDescriptor.java | 161 --------- .../instrumentation/stats/MeasurementMap.java | 146 --------- .../instrumentation/stats/MeasurementValue.java | 49 --- .../google/instrumentation/stats/RpcConstants.java | 361 --------------------- .../com/google/instrumentation/stats/Stats.java | 46 --- .../google/instrumentation/stats/StatsContext.java | 80 ----- .../instrumentation/stats/StatsContextFactory.java | 38 --- .../google/instrumentation/stats/StatsManager.java | 31 -- .../google/instrumentation/stats/StringUtil.java | 56 ---- .../java/com/google/instrumentation/stats/Tag.java | 68 ---- .../com/google/instrumentation/stats/TagKey.java | 56 ---- .../com/google/instrumentation/stats/TagValue.java | 56 ---- .../com/google/instrumentation/stats/View.java | 148 --------- .../instrumentation/stats/ViewDescriptor.java | 172 ---------- .../instrumentation/common/DurationTest.java | 65 ---- .../instrumentation/common/ProviderTest.java | 61 ---- .../instrumentation/common/TimestampTest.java | 64 ---- .../DistributionAggregationDescriptorTest.java | 46 --- .../stats/DistributionAggregationTest.java | 77 ----- .../stats/IntervalAggregationDescriptorTest.java | 77 ----- .../stats/IntervalAggregationTest.java | 65 ---- .../stats/MeasurementDescriptorTest.java | 69 ---- .../instrumentation/stats/MeasurementMapTest.java | 121 ------- .../instrumentation/stats/RpcConstantsTest.java | 52 --- .../stats/StatsContextFactoryTest.java | 31 -- .../instrumentation/stats/StatsContextTest.java | 150 --------- .../google/instrumentation/stats/StatsTest.java | 36 -- .../instrumentation/stats/StringUtilTest.java | 47 --- .../google/instrumentation/stats/TagKeyTest.java | 59 ---- .../com/google/instrumentation/stats/TagTest.java | 64 ---- .../google/instrumentation/stats/TagValueTest.java | 59 ---- .../instrumentation/stats/ViewDescriptorTest.java | 98 ------ .../com/google/instrumentation/stats/ViewTest.java | 143 -------- core/pom.xml | 13 +- .../google/instrumentation/common/Duration.java | 112 +++++++ .../google/instrumentation/common/Function.java | 25 ++ .../common/NonThrowingCloseable.java | 22 ++ .../google/instrumentation/common/Provider.java | 55 ++++ .../google/instrumentation/common/Timestamp.java | 115 +++++++ .../instrumentation/common/TimestampFactory.java | 28 ++ .../stats/DistributionAggregation.java | 166 ++++++++++ .../stats/DistributionAggregationDescriptor.java | 74 +++++ .../instrumentation/stats/IntervalAggregation.java | 103 ++++++ .../stats/IntervalAggregationDescriptor.java | 86 +++++ .../stats/MeasurementDescriptor.java | 161 +++++++++ .../instrumentation/stats/MeasurementMap.java | 146 +++++++++ .../instrumentation/stats/MeasurementValue.java | 49 +++ .../google/instrumentation/stats/RpcConstants.java | 361 +++++++++++++++++++++ .../com/google/instrumentation/stats/Stats.java | 46 +++ .../google/instrumentation/stats/StatsContext.java | 80 +++++ .../instrumentation/stats/StatsContextFactory.java | 38 +++ .../google/instrumentation/stats/StatsManager.java | 31 ++ .../google/instrumentation/stats/StringUtil.java | 56 ++++ .../java/com/google/instrumentation/stats/Tag.java | 68 ++++ .../com/google/instrumentation/stats/TagKey.java | 56 ++++ .../com/google/instrumentation/stats/TagValue.java | 56 ++++ .../com/google/instrumentation/stats/View.java | 148 +++++++++ .../instrumentation/stats/ViewDescriptor.java | 172 ++++++++++ .../instrumentation/common/DurationTest.java | 65 ++++ .../instrumentation/common/ProviderTest.java | 61 ++++ .../instrumentation/common/TimestampTest.java | 64 ++++ .../DistributionAggregationDescriptorTest.java | 46 +++ .../stats/DistributionAggregationTest.java | 77 +++++ .../stats/IntervalAggregationDescriptorTest.java | 77 +++++ .../stats/IntervalAggregationTest.java | 65 ++++ .../stats/MeasurementDescriptorTest.java | 69 ++++ .../instrumentation/stats/MeasurementMapTest.java | 121 +++++++ .../instrumentation/stats/RpcConstantsTest.java | 52 +++ .../stats/StatsContextFactoryTest.java | 31 ++ .../instrumentation/stats/StatsContextTest.java | 150 +++++++++ .../google/instrumentation/stats/StatsTest.java | 36 ++ .../instrumentation/stats/StringUtilTest.java | 47 +++ .../google/instrumentation/stats/TagKeyTest.java | 59 ++++ .../com/google/instrumentation/stats/TagTest.java | 64 ++++ .../google/instrumentation/stats/TagValueTest.java | 59 ++++ .../instrumentation/stats/ViewDescriptorTest.java | 98 ++++++ .../com/google/instrumentation/stats/ViewTest.java | 143 ++++++++ 88 files changed, 3670 insertions(+), 3659 deletions(-) delete mode 100644 core/java/com/google/instrumentation/common/Duration.java delete mode 100644 core/java/com/google/instrumentation/common/Function.java delete mode 100644 core/java/com/google/instrumentation/common/NonThrowingCloseable.java delete mode 100644 core/java/com/google/instrumentation/common/Provider.java delete mode 100644 core/java/com/google/instrumentation/common/Timestamp.java delete mode 100644 core/java/com/google/instrumentation/common/TimestampFactory.java delete mode 100644 core/java/com/google/instrumentation/stats/DistributionAggregation.java delete mode 100644 core/java/com/google/instrumentation/stats/DistributionAggregationDescriptor.java delete mode 100644 core/java/com/google/instrumentation/stats/IntervalAggregation.java delete mode 100644 core/java/com/google/instrumentation/stats/IntervalAggregationDescriptor.java delete mode 100644 core/java/com/google/instrumentation/stats/MeasurementDescriptor.java delete mode 100644 core/java/com/google/instrumentation/stats/MeasurementMap.java delete mode 100644 core/java/com/google/instrumentation/stats/MeasurementValue.java delete mode 100644 core/java/com/google/instrumentation/stats/RpcConstants.java delete mode 100644 core/java/com/google/instrumentation/stats/Stats.java delete mode 100644 core/java/com/google/instrumentation/stats/StatsContext.java delete mode 100644 core/java/com/google/instrumentation/stats/StatsContextFactory.java delete mode 100644 core/java/com/google/instrumentation/stats/StatsManager.java delete mode 100644 core/java/com/google/instrumentation/stats/StringUtil.java delete mode 100644 core/java/com/google/instrumentation/stats/Tag.java delete mode 100644 core/java/com/google/instrumentation/stats/TagKey.java delete mode 100644 core/java/com/google/instrumentation/stats/TagValue.java delete mode 100644 core/java/com/google/instrumentation/stats/View.java delete mode 100644 core/java/com/google/instrumentation/stats/ViewDescriptor.java delete mode 100644 core/javatests/com/google/instrumentation/common/DurationTest.java delete mode 100644 core/javatests/com/google/instrumentation/common/ProviderTest.java delete mode 100644 core/javatests/com/google/instrumentation/common/TimestampTest.java delete mode 100644 core/javatests/com/google/instrumentation/stats/DistributionAggregationDescriptorTest.java delete mode 100644 core/javatests/com/google/instrumentation/stats/DistributionAggregationTest.java delete mode 100644 core/javatests/com/google/instrumentation/stats/IntervalAggregationDescriptorTest.java delete mode 100644 core/javatests/com/google/instrumentation/stats/IntervalAggregationTest.java delete mode 100644 core/javatests/com/google/instrumentation/stats/MeasurementDescriptorTest.java delete mode 100644 core/javatests/com/google/instrumentation/stats/MeasurementMapTest.java delete mode 100644 core/javatests/com/google/instrumentation/stats/RpcConstantsTest.java delete mode 100644 core/javatests/com/google/instrumentation/stats/StatsContextFactoryTest.java delete mode 100644 core/javatests/com/google/instrumentation/stats/StatsContextTest.java delete mode 100644 core/javatests/com/google/instrumentation/stats/StatsTest.java delete mode 100644 core/javatests/com/google/instrumentation/stats/StringUtilTest.java delete mode 100644 core/javatests/com/google/instrumentation/stats/TagKeyTest.java delete mode 100644 core/javatests/com/google/instrumentation/stats/TagTest.java delete mode 100644 core/javatests/com/google/instrumentation/stats/TagValueTest.java delete mode 100644 core/javatests/com/google/instrumentation/stats/ViewDescriptorTest.java delete mode 100644 core/javatests/com/google/instrumentation/stats/ViewTest.java create mode 100644 core/src/main/java/com/google/instrumentation/common/Duration.java create mode 100644 core/src/main/java/com/google/instrumentation/common/Function.java create mode 100644 core/src/main/java/com/google/instrumentation/common/NonThrowingCloseable.java create mode 100644 core/src/main/java/com/google/instrumentation/common/Provider.java create mode 100644 core/src/main/java/com/google/instrumentation/common/Timestamp.java create mode 100644 core/src/main/java/com/google/instrumentation/common/TimestampFactory.java create mode 100644 core/src/main/java/com/google/instrumentation/stats/DistributionAggregation.java create mode 100644 core/src/main/java/com/google/instrumentation/stats/DistributionAggregationDescriptor.java create mode 100644 core/src/main/java/com/google/instrumentation/stats/IntervalAggregation.java create mode 100644 core/src/main/java/com/google/instrumentation/stats/IntervalAggregationDescriptor.java create mode 100644 core/src/main/java/com/google/instrumentation/stats/MeasurementDescriptor.java create mode 100644 core/src/main/java/com/google/instrumentation/stats/MeasurementMap.java create mode 100644 core/src/main/java/com/google/instrumentation/stats/MeasurementValue.java create mode 100644 core/src/main/java/com/google/instrumentation/stats/RpcConstants.java create mode 100644 core/src/main/java/com/google/instrumentation/stats/Stats.java create mode 100644 core/src/main/java/com/google/instrumentation/stats/StatsContext.java create mode 100644 core/src/main/java/com/google/instrumentation/stats/StatsContextFactory.java create mode 100644 core/src/main/java/com/google/instrumentation/stats/StatsManager.java create mode 100644 core/src/main/java/com/google/instrumentation/stats/StringUtil.java create mode 100644 core/src/main/java/com/google/instrumentation/stats/Tag.java create mode 100644 core/src/main/java/com/google/instrumentation/stats/TagKey.java create mode 100644 core/src/main/java/com/google/instrumentation/stats/TagValue.java create mode 100644 core/src/main/java/com/google/instrumentation/stats/View.java create mode 100644 core/src/main/java/com/google/instrumentation/stats/ViewDescriptor.java create mode 100644 core/src/test/java/com/google/instrumentation/common/DurationTest.java create mode 100644 core/src/test/java/com/google/instrumentation/common/ProviderTest.java create mode 100644 core/src/test/java/com/google/instrumentation/common/TimestampTest.java create mode 100644 core/src/test/java/com/google/instrumentation/stats/DistributionAggregationDescriptorTest.java create mode 100644 core/src/test/java/com/google/instrumentation/stats/DistributionAggregationTest.java create mode 100644 core/src/test/java/com/google/instrumentation/stats/IntervalAggregationDescriptorTest.java create mode 100644 core/src/test/java/com/google/instrumentation/stats/IntervalAggregationTest.java create mode 100644 core/src/test/java/com/google/instrumentation/stats/MeasurementDescriptorTest.java create mode 100644 core/src/test/java/com/google/instrumentation/stats/MeasurementMapTest.java create mode 100644 core/src/test/java/com/google/instrumentation/stats/RpcConstantsTest.java create mode 100644 core/src/test/java/com/google/instrumentation/stats/StatsContextFactoryTest.java create mode 100644 core/src/test/java/com/google/instrumentation/stats/StatsContextTest.java create mode 100644 core/src/test/java/com/google/instrumentation/stats/StatsTest.java create mode 100644 core/src/test/java/com/google/instrumentation/stats/StringUtilTest.java create mode 100644 core/src/test/java/com/google/instrumentation/stats/TagKeyTest.java create mode 100644 core/src/test/java/com/google/instrumentation/stats/TagTest.java create mode 100644 core/src/test/java/com/google/instrumentation/stats/TagValueTest.java create mode 100644 core/src/test/java/com/google/instrumentation/stats/ViewDescriptorTest.java create mode 100644 core/src/test/java/com/google/instrumentation/stats/ViewTest.java diff --git a/BUILD b/BUILD index e14102f2..49f7d0d1 100644 --- a/BUILD +++ b/BUILD @@ -14,7 +14,7 @@ java_library( name = "common-core", - srcs = glob(["core/java/com/google/instrumentation/common/*.java"]), + srcs = glob(["core/src/main/java/com/google/instrumentation/common/*.java"]), deps = ["@jsr305//jar"], ) @@ -25,7 +25,7 @@ java_library( java_library( name = "stats-core", - srcs = glob(["core/java/com/google/instrumentation/stats/*.java"]), + srcs = glob(["core/src/main/java/com/google/instrumentation/stats/*.java"]), deps = [ ":common-core", "@jsr305//jar", @@ -60,7 +60,7 @@ java_binary( java_test( name = "DistributionAggregationDescriptorTest", srcs = [ - "core/javatests/com/google/instrumentation/stats/DistributionAggregationDescriptorTest.java", + "core/src/test/java/com/google/instrumentation/stats/DistributionAggregationDescriptorTest.java", ], deps = [ ":stats-core", @@ -75,7 +75,7 @@ java_test( java_test( name = "DistributionAggregationTest", srcs = [ - "core/javatests/com/google/instrumentation/stats/DistributionAggregationTest.java", + "core/src/test/java/com/google/instrumentation/stats/DistributionAggregationTest.java", ], deps = [ ":stats-core", @@ -90,7 +90,7 @@ java_test( java_test( name = "IntervalAggregationDescriptorTest", srcs = [ - "core/javatests/com/google/instrumentation/stats/IntervalAggregationDescriptorTest.java", + "core/src/test/java/com/google/instrumentation/stats/IntervalAggregationDescriptorTest.java", ], deps = [ ":common-core", @@ -106,7 +106,7 @@ java_test( java_test( name = "IntervalAggregationTest", srcs = [ - "core/javatests/com/google/instrumentation/stats/IntervalAggregationTest.java", + "core/src/test/java/com/google/instrumentation/stats/IntervalAggregationTest.java", ], deps = [ ":common-core", @@ -121,7 +121,7 @@ java_test( java_test( name = "MeasurementMapTest", - srcs = ["core/javatests/com/google/instrumentation/stats/MeasurementMapTest.java"], + srcs = ["core/src/test/java/com/google/instrumentation/stats/MeasurementMapTest.java"], deps = [ ":stats-core", "@guava//jar", @@ -133,7 +133,7 @@ java_test( java_test( name = "MeasurementDescriptorTest", - srcs = ["core/javatests/com/google/instrumentation/stats/MeasurementDescriptorTest.java"], + srcs = ["core/src/test/java/com/google/instrumentation/stats/MeasurementDescriptorTest.java"], deps = [ ":stats-core", "@guava//jar", @@ -146,7 +146,7 @@ java_test( java_test( name = "RpcConstantsTest", - srcs = ["core/javatests/com/google/instrumentation/stats/RpcConstantsTest.java"], + srcs = ["core/src/test/java/com/google/instrumentation/stats/RpcConstantsTest.java"], deps = [ ":stats-core", ":stats-core_impl", @@ -159,7 +159,7 @@ java_test( java_test( name = "StatsContextTest", - srcs = ["core/javatests/com/google/instrumentation/stats/StatsContextTest.java"], + srcs = ["core/src/test/java/com/google/instrumentation/stats/StatsContextTest.java"], deps = [ ":stats-core", ":stats-core_impl", @@ -173,7 +173,7 @@ java_test( java_test( name = "StatsContextFactoryTest", - srcs = ["core/javatests/com/google/instrumentation/stats/StatsContextFactoryTest.java"], + srcs = ["core/src/test/java/com/google/instrumentation/stats/StatsContextFactoryTest.java"], deps = [ ":stats-core", ":stats-core_impl", @@ -186,7 +186,7 @@ java_test( java_test( name = "StringUtilTest", - srcs = ["core/javatests/com/google/instrumentation/stats/StringUtilTest.java"], + srcs = ["core/src/test/java/com/google/instrumentation/stats/StringUtilTest.java"], deps = [ ":stats-core", ":stats-core_impl", @@ -200,7 +200,7 @@ java_test( java_test( name = "TagKeyTest", - srcs = ["core/javatests/com/google/instrumentation/stats/TagKeyTest.java"], + srcs = ["core/src/test/java/com/google/instrumentation/stats/TagKeyTest.java"], deps = [ ":stats-core", "@guava//jar", @@ -213,7 +213,7 @@ java_test( java_test( name = "TagTest", - srcs = ["core/javatests/com/google/instrumentation/stats/TagTest.java"], + srcs = ["core/src/test/java/com/google/instrumentation/stats/TagTest.java"], deps = [ ":stats-core", "@guava//jar", @@ -226,7 +226,7 @@ java_test( java_test( name = "TagValueTest", - srcs = ["core/javatests/com/google/instrumentation/stats/TagValueTest.java"], + srcs = ["core/src/test/java/com/google/instrumentation/stats/TagValueTest.java"], deps = [ ":stats-core", "@guava//jar", @@ -239,7 +239,7 @@ java_test( java_test( name = "DurationTest", - srcs = ["core/javatests/com/google/instrumentation/common/DurationTest.java"], + srcs = ["core/src/test/java/com/google/instrumentation/common/DurationTest.java"], deps = [ ":common-core", "@guava//jar", @@ -252,7 +252,7 @@ java_test( java_test( name = "ProviderTest", - srcs = ["core/javatests/com/google/instrumentation/common/ProviderTest.java"], + srcs = ["core/src/test/java/com/google/instrumentation/common/ProviderTest.java"], deps = [ ":common-core", ":stats-core", @@ -265,7 +265,7 @@ java_test( java_test( name = "TimestampTest", - srcs = ["core/javatests/com/google/instrumentation/common/TimestampTest.java"], + srcs = ["core/src/test/java/com/google/instrumentation/common/TimestampTest.java"], deps = [ ":common-core", "@guava//jar", @@ -278,7 +278,7 @@ java_test( java_test( name = "ViewDescriptorTest", - srcs = ["core/javatests/com/google/instrumentation/stats/ViewDescriptorTest.java"], + srcs = ["core/src/test/java/com/google/instrumentation/stats/ViewDescriptorTest.java"], deps = [ ":common-core", ":stats-core", @@ -292,7 +292,7 @@ java_test( java_test( name = "ViewTest", - srcs = ["core/javatests/com/google/instrumentation/stats/ViewTest.java"], + srcs = ["core/src/test/java/com/google/instrumentation/stats/ViewTest.java"], deps = [ ":common-core", ":stats-core", diff --git a/core/java/com/google/instrumentation/common/Duration.java b/core/java/com/google/instrumentation/common/Duration.java deleted file mode 100644 index 657575da..00000000 --- a/core/java/com/google/instrumentation/common/Duration.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.common; - -/** - * Represents a signed, fixed-length span of time represented as a count of seconds and fractions - * of seconds at nanosecond resolution. It is independent of any calendar and concepts like "day" - * or "month". Range is approximately +-10,000 years. - */ -public class Duration { - /** - * Creates a new time duration from given seconds and nanoseconds. - * - * @param seconds Signed seconds of the span of time. Must be from -315,576,000,000 - * to +315,576,000,000 inclusive. - * - * @param nanos Signed fractions of a second at nanosecond resolution of the span - * of time. Durations less than one second are represented with a 0 - * `seconds` field and a positive or negative `nanos` field. For durations - * of one second or more, a non-zero value for the `nanos` field must be - * of the same sign as the `seconds` field. Must be from -999,999,999 - * to +999,999,999 inclusive. - * - * @return new {@link Duration} with specified fields. For invalid inputs, a {@link Duration} of - * zero is returned. - */ - public static Duration create(long seconds, int nanos) { - if (seconds < -MAX_SECONDS || seconds > MAX_SECONDS) { - return new Duration(0, 0); - } - if (nanos < -MAX_NANOS || nanos > MAX_NANOS) { - return new Duration(0, 0); - } - if ((seconds < 0 && nanos > 0) || (seconds > 0 && nanos < 0)) { - return new Duration(0, 0); - } - return new Duration(seconds, nanos); - } - - /** - * Creates a new {@link Duration} from given milliseconds. - */ - public static Duration fromMillis(long millis) { - long seconds = millis / NUM_MILLIS_PER_SECOND; - int nanos = (int) (millis % NUM_MILLIS_PER_SECOND) * NUM_NANOS_PER_MILLI; - return new Duration(seconds, nanos); - } - - /** - * Returns the number of seconds in the {@link Duration}. - */ - public long getSeconds() { - return seconds; - } - - /** - * Returns the number of nanoseconds in the {@link Duration}. - */ - public int getNanos() { - return nanos; - } - - @Override - public boolean equals(Object obj) { - if (obj == this) { - return true; - } - - if (!(obj instanceof Duration)) { - return false; - } - - Duration that = (Duration) obj; - return seconds == that.seconds && nanos == that.nanos; - } - - @Override - public int hashCode() { - int result = 17; - result = 31 * result + (int) (seconds ^ (seconds >>> 32)); - result = 31 * result + nanos; - return result; - } - - @Override - public String toString() { - return "Duration<" + seconds + "," + nanos + ">"; - } - - private static final long MAX_SECONDS = 315576000000L; - private static final int MAX_NANOS = 999999999; - private static final long NUM_MILLIS_PER_SECOND = 1000L; - private static final int NUM_NANOS_PER_MILLI = 1000000; - private final long seconds; - private final int nanos; - - private Duration(long seconds, int nanos) { - this.seconds = seconds; - this.nanos = nanos; - } -} diff --git a/core/java/com/google/instrumentation/common/Function.java b/core/java/com/google/instrumentation/common/Function.java deleted file mode 100644 index 16471a55..00000000 --- a/core/java/com/google/instrumentation/common/Function.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.common; - -/** - * Used to specify matching functions for use encoding tagged unions (i.e. sum types) in Java. See - * {@link com.google.instrumentation.stats.ViewDescriptor} for an example of it's use. - * - *

Note: This class is based on the java.util.Function class added in Java 1.8. We cannot use - * the Function from Java 1.8 because this library is Java 1.6 compatible. - */ -public interface Function { - B apply(A arg); -} diff --git a/core/java/com/google/instrumentation/common/NonThrowingCloseable.java b/core/java/com/google/instrumentation/common/NonThrowingCloseable.java deleted file mode 100644 index 46ac71a5..00000000 --- a/core/java/com/google/instrumentation/common/NonThrowingCloseable.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.google.instrumentation.common; - -import java.io.Closeable; - -/** - * An {@link Closeable} which cannot throw a checked exception. - * - *

This is useful because such a reversion otherwise requires the caller to catch the - * (impossible) Exception in the try-with-resources. - * - *

Example of usage: - * - *

- *   try (NonThrowingAutoCloseable ctx = tryEnter()) {
- *     ...
- *   }
- * 
- */ -public interface NonThrowingCloseable extends Closeable { - @Override - void close(); -} diff --git a/core/java/com/google/instrumentation/common/Provider.java b/core/java/com/google/instrumentation/common/Provider.java deleted file mode 100644 index d32fbad9..00000000 --- a/core/java/com/google/instrumentation/common/Provider.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.common; - -import java.util.logging.Level; -import java.util.logging.Logger; -import javax.annotation.Nullable; - -/** - * Instrumentation specific service provider mechanism. - * - *
{@code
- * // Initialize a static variable using reflection.
- * static final Foo foo = Provider.newInstance("Foo", new NoopFoo());
- * }
- */ -public final class Provider { - private static final Logger logger = Logger.getLogger(Provider.class.getName()); - - /** - * Returns a new instance of the class specified with {@code name} by invoking the empty-argument - * constructor via reflections. If the specified class is not found, the {@code defaultValue} is - * returned. - */ - @SuppressWarnings("unchecked") - @Nullable - public static T newInstance(String name, @Nullable T defaultValue) { - try { - Class provider = Class.forName(name); - T result = (T) provider.getConstructor().newInstance(); - logger.fine("Loaded: " + name); - return result; - } catch (ClassNotFoundException e) { - logger.log(Level.FINE, "Falling back to " + defaultValue, e); - return defaultValue; - } catch (Exception e) { - if (e instanceof RuntimeException) { - throw (RuntimeException) e; - } else { - throw new RuntimeException(e); - } - } - } -} diff --git a/core/java/com/google/instrumentation/common/Timestamp.java b/core/java/com/google/instrumentation/common/Timestamp.java deleted file mode 100644 index 6fad30f6..00000000 --- a/core/java/com/google/instrumentation/common/Timestamp.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.common; - -/** - * A representation of an instant in time. The instant is the number of nanoseconds after the number - * of seconds since the Unix Epoch. - */ -public class Timestamp { - /** - * Creates a new timestamp from given seconds and nanoseconds. - * - * @param seconds Represents seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z. Must - * be from from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive. - * - * @param nanos Non-negative fractions of a second at nanosecond resolution. Negative - * second values with fractions must still have non-negative nanos values that count forward - * in time. Must be from 0 to 999,999,999 inclusive. - * - * @return new {@link Timestamp} with specified fields. For invalid inputs, a {@link Timestamp} - * of zero is returned. - */ - public static Timestamp create(long seconds, int nanos) { - if (seconds < -MAX_SECONDS || seconds > MAX_SECONDS) { - return new Timestamp(0, 0); - } - if (nanos < 0 || nanos > MAX_NANOS) { - return new Timestamp(0, 0); - } - return new Timestamp(seconds, nanos); - } - - /** - * Creates a new timestamp from given milliseconds. - */ - public static Timestamp fromMillis(long millis) { - long seconds = millis / NUM_MILLIS_PER_SECOND; - int nanos = (int) (millis % NUM_MILLIS_PER_SECOND) * NUM_NANOS_PER_MILLI; - if (nanos < 0) { - return new Timestamp(seconds - 1, MAX_NANOS + nanos + 1); - } else { - return new Timestamp(seconds, nanos); - } - } - - /** - * Returns the number of seconds since the Unix Epoch represented by this timestamp. - * - * @return the number of seconds since the Unix Epoch. - */ - public long getSeconds() { - return seconds; - } - - /** - * Returns the number of nanoseconds after the number of seconds since the Unix Epoch represented - * by this timestamp. - * - * @return the number of nanoseconds after the number of seconds since the Unix Epoch. - */ - public int getNanos() { - return nanos; - } - - @Override - public boolean equals(Object obj) { - if (obj == this) { - return true; - } - - if (!(obj instanceof Timestamp)) { - return false; - } - - Timestamp that = (Timestamp) obj; - return seconds == that.seconds && nanos == that.nanos; - } - - @Override - public int hashCode() { - int result = 17; - result = 31 * result + (int) (seconds ^ (seconds >>> 32)); - result = 31 * result + nanos; - return result; - } - - @Override - public String toString() { - return "Timestamp<" + seconds + "," + nanos + ">"; - } - - private static final long MAX_SECONDS = 315576000000L; - private static final int MAX_NANOS = 999999999; - private static final long NUM_MILLIS_PER_SECOND = 1000L; - private static final int NUM_NANOS_PER_MILLI = 1000000; - private final long seconds; - private final int nanos; - - private Timestamp(long seconds, int nanos) { - this.seconds = seconds; - this.nanos = nanos; - } - -} diff --git a/core/java/com/google/instrumentation/common/TimestampFactory.java b/core/java/com/google/instrumentation/common/TimestampFactory.java deleted file mode 100644 index 2ae9d4e3..00000000 --- a/core/java/com/google/instrumentation/common/TimestampFactory.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2017, Google Inc. - * 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 com.google.instrumentation.common; - -/** - * An interface for a factory that generates timestamps. - * - * @see Timestamp - */ -public interface TimestampFactory { - /** - * Returns a new timestamp that represents the instant this method was called. - * - * @return the new timestamp. - */ - Timestamp now(); -} diff --git a/core/java/com/google/instrumentation/stats/DistributionAggregation.java b/core/java/com/google/instrumentation/stats/DistributionAggregation.java deleted file mode 100644 index 47f19d5d..00000000 --- a/core/java/com/google/instrumentation/stats/DistributionAggregation.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import javax.annotation.Nullable; - -/** - * An aggregation of data based on distributions. - * - *

A distribution contains summary statistics for a population of values and, optionally, a - * histogram representing the distribution of those values across a specified set of histogram - * buckets, as defined in {@link DistributionAggregationDescriptor#getBucketBoundaries()}. - * - *

Although not forbidden, it is generally a bad idea to include non-finite values (infinities - * or NaNs) in the population of values, as this will render the {@code mean} meaningless. - */ -public final class DistributionAggregation { - /** - * Constructs a new {@link DistributionAggregation}. - */ - public static final DistributionAggregation create( - long count, double mean, double sum, Range range, List tags) { - return new DistributionAggregation(count, mean, sum, range, tags, null); - } - - /** - * Constructs a new {@link DistributionAggregation} with the optional {@code bucketCount}s. - */ - public static final DistributionAggregation create( - long count, double mean, double sum, Range range, List tags, List bucketCounts) { - return new DistributionAggregation(count, mean, sum, range, tags, - Collections.unmodifiableList(new ArrayList(bucketCounts))); - } - - /** - * {@link Tag}s associated with this {@link DistributionAggregation}. - * - *

Note: The returned list is unmodifiable, attempts to update it will throw an - * UnsupportedOperationException. - */ - public final List getTags() { - return tags; - } - - /** - * The number of values in the population. Must be non-negative. - */ - public long getCount() { - return count; - } - - /** - * The arithmetic mean of the values in the population. If {@link #getCount()} is zero then this - * value must also be zero. - */ - public double getMean() { - return mean; - } - - /** - * The sum of the values in the population. If {@link #getCount()} is zero then this values must - * also be zero. - */ - public double getSum() { - return sum; - } - - /** - * The range of the population values. If {@link #getCount()} is zero then this returned range is - * implementation-dependent. - */ - public Range getRange() { - return range; - } - - /** - * A Distribution may optionally contain a histogram of the values in the population. The - * histogram is given in {@link #getBucketCounts()} as counts of values that fall into one of a - * sequence of non-overlapping buckets, described by - * {@link DistributionAggregationDescriptor#getBucketBoundaries()}. - * The sum of the values in {@link #getBucketCounts()} must equal the value in - * {@link #getCount()}. - * - *

Bucket counts are given in order under the numbering scheme described - * above (the underflow bucket has number 0; the finite buckets, if any, - * have numbers 1 through N-2; the overflow bucket has number N-1). - * - *

The size of {@link #getBucketCounts()} must be no greater than N as defined in - * {@link DistributionAggregationDescriptor#getBucketBoundaries()}. - * - *

Any suffix of trailing buckets containing only zero may be omitted. - * - *

{@link #getBucketCounts()} will return null iff the associated - * {@link DistributionAggregationDescriptor#getBucketBoundaries()} returns null. - */ - @Nullable - public List getBucketCounts() { - return bucketCounts; - } - - private final long count; - private final double mean; - private final double sum; - private final Range range; - private final List tags; - private final List bucketCounts; - - private DistributionAggregation( - long count, double mean, double sum, Range range, List tags, - @Nullable List bucketCounts) { - this.count = count; - this.mean = mean; - this.sum = sum; - this.range = range; - this.tags = tags; - this.bucketCounts = bucketCounts; - } - - /** - * Describes a range of population values. - */ - public static final class Range { - /** - * Constructs a new {@link Range}. - */ - public static final Range create(double min, double max) { - return new Range(min, max); - } - - /** - * The minimum of the population values. - */ - public double getMin() { - return min; - } - - /** - * The maximum of the population values. - */ - public double getMax() { - return max; - } - - private double min; - private double max; - - private Range(double min, double max) { - this.min = min; - this.max = max; - } - } -} diff --git a/core/java/com/google/instrumentation/stats/DistributionAggregationDescriptor.java b/core/java/com/google/instrumentation/stats/DistributionAggregationDescriptor.java deleted file mode 100644 index 586124ad..00000000 --- a/core/java/com/google/instrumentation/stats/DistributionAggregationDescriptor.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import javax.annotation.Nullable; - -/** - * Describes data aggregations based on distributions. - * - *

A distribution aggregation may optionally contain a histogram of the values in the - * population. The bucket boundaries for that histogram are described by - * {@link getBucketBoundaries}, which defines {@code getBucketBoundaries.size() + 1 (= N)} - * buckets. The boundaries for bucket index i are: - *

- * i.e. an underflow bucket (number 0), zero or more finite buckets (1 through N - 2, and an - * overflow bucket (N - 1), with inclusive lower bounds and exclusive upper bounds. - * - *

Note: If N = 1, there are no finite buckets and the single bucket is both the overflow - * and underflow bucket. - */ -public final class DistributionAggregationDescriptor { - /** - * Constructs a new {@link DistributionAggregationDescriptor} with the optional - * histogram bucket boundaries. - */ - public static DistributionAggregationDescriptor create(List bucketBoundaries) { - return new DistributionAggregationDescriptor( - Collections.unmodifiableList(new ArrayList(bucketBoundaries))); - } - - /** - * Constructs a new {@link DistributionAggregationDescriptor} without the optional - * histogram bucket boundaries. - */ - public static DistributionAggregationDescriptor create() { - return new DistributionAggregationDescriptor(null); - } - - /** - * The optional histogram bucket boundaries for a distribution. - * - *

Note: The returned list is unmodifiable, attempts to update it will throw an - * UnsupportedOperationException. - */ - @Nullable - public List getBucketBoundaries() { - return bucketBoundaries; - } - - @Nullable - private final List bucketBoundaries; - - private DistributionAggregationDescriptor(@Nullable List bucketBoundaries) { - this.bucketBoundaries = bucketBoundaries; - } -} diff --git a/core/java/com/google/instrumentation/stats/IntervalAggregation.java b/core/java/com/google/instrumentation/stats/IntervalAggregation.java deleted file mode 100644 index 9bd26923..00000000 --- a/core/java/com/google/instrumentation/stats/IntervalAggregation.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import com.google.instrumentation.common.Duration; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - * Contains summary stats over various time intervals. - */ -public final class IntervalAggregation { - /** - * Constructs new {@link IntervalAggregation}. - * TODO(dpo): Determine what we should do it intervals is empty. - */ - public static final IntervalAggregation create(List tags, List intervals) { - return new IntervalAggregation(tags, intervals); - } - - /** - * {@link Tag}s associated with this aggregation. - * - *

Note: The returned list is unmodifiable, attempts to update it will throw an - * UnsupportedOperationException. - */ - public final List getTags() { - return tags; - } - - /** - * Sequence of intervals for this aggregation. - */ - public List getIntervals() { - return intervals; - } - - private final List tags; - private final List intervals; - - private IntervalAggregation(List tags, List intervals) { - this.tags = tags; - this.intervals = Collections.unmodifiableList(new ArrayList(intervals)); - } - - /** - * Summary statistic over a single time interval. - */ - public static final class Interval { - /** - * Constructs a new {@link Interval}. - * - *

Note: {@code intervalSize} must be positive otherwise behavior is unspecified. - */ - public static Interval create(Duration intervalSize, double count, double sum) { - return new Interval(intervalSize, count, sum); - } - - /** - * The interval duration. - */ - public Duration getIntervalSize() { - return intervalSize; - } - - /** - * The number of measurements in this interval. - */ - public double getCount() { - return count; - } - - /** - * The cumulative sum of measurements in this interval. - */ - public double getSum() { - return sum; - } - - private final Duration intervalSize; - private final double count; - private final double sum; - - private Interval(Duration intervalSize, double count, double sum) { - this.intervalSize = intervalSize; - this.count = count; - this.sum = sum; - } - } -} diff --git a/core/java/com/google/instrumentation/stats/IntervalAggregationDescriptor.java b/core/java/com/google/instrumentation/stats/IntervalAggregationDescriptor.java deleted file mode 100644 index b6c2dbe6..00000000 --- a/core/java/com/google/instrumentation/stats/IntervalAggregationDescriptor.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import com.google.instrumentation.common.Duration; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - * Describes data aggregations based on time intervals. - */ -public final class IntervalAggregationDescriptor { - /** - * Constructs a new {@link IntervalAggregationDescriptor}. - * - *

The given {@code numSubIntervals} must be in the range [2, 20], see - * {@link #getNumSubIntervals()} for more details. - * - *

The given {@code intervalSizes} must have at least one entry. - */ - public static IntervalAggregationDescriptor create( - int numSubIntervals, List intervalSizes) { - if (numSubIntervals < 2 || numSubIntervals > 20) { - throw new IllegalArgumentException( - "The number of subintervals must be in the range [2, 20]."); - } - if (intervalSizes.isEmpty()) { - throw new IllegalArgumentException("There must be at least one interval size."); - } - return new IntervalAggregationDescriptor( - numSubIntervals, - Collections.unmodifiableList(new ArrayList(intervalSizes))); - } - - /** - * Constructs a new {@link IntervalAggregationDescriptor} with the number of sub intervals set - * to the default value of 5. - */ - public static IntervalAggregationDescriptor create(List intervalSizes) { - return create(5, intervalSizes); - } - - /** - * The number of sub intervals. - * - *

The number of internal sub-intervals to use when collecting stats for each interval. The - * max error in interval measurements will be approximately 1/getNumSubIntervals() - * (although in practice, this will only be approached in the presence of very large and bursty - * workload changes), and underlying memory usage will be roughly proportional to the value of - * this field. Must be in the range [2, 20]. A value of 5 will be used if this is unspecified. - */ - public int getNumSubIntervals() { - return numSubIntervals; - } - - /** - * The time intervals to record for the aggregation. - * - *

Note: The returned list is unmodifiable, attempts to update it will throw an - * UnsupportedOperationException. - */ - public List getIntervalSizes() { - return intervalSizes; - } - - private final int numSubIntervals; - private final List intervalSizes; - - private IntervalAggregationDescriptor(int numSubIntervals, List intervalSizes) { - this.numSubIntervals = numSubIntervals; - this.intervalSizes = intervalSizes; - } -} diff --git a/core/java/com/google/instrumentation/stats/MeasurementDescriptor.java b/core/java/com/google/instrumentation/stats/MeasurementDescriptor.java deleted file mode 100644 index d44c3637..00000000 --- a/core/java/com/google/instrumentation/stats/MeasurementDescriptor.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - * MeasurementDescriptor. - * - *

Note: MeasurementDescriptor names are {@link String}s with enforced restrictions. - */ -public final class MeasurementDescriptor { - public static final int MAX_LENGTH = StringUtil.MAX_LENGTH; - - /** - * Constructs a new {@link MeasurementDescriptor}. - */ - public static MeasurementDescriptor create( - String name, String description, MeasurementUnit unit) { - return new MeasurementDescriptor(name, description, unit); - } - - /** - * Name of measurement, e.g. rpc_latency, cpu. Must be unique. - */ - public String getName() { - return name; - } - - /** - * Detailed description of the measurement, used in documentation. - */ - public String getDescription() { - return description; - } - - /** - * The units in which {@link MeasurementDescriptor} values are measured. - */ - public MeasurementUnit getUnit() { - return unit; - } - - private final String name; - private final String description; - private final MeasurementUnit unit; - - private MeasurementDescriptor(String name, String description, MeasurementUnit unit) { - this.name = StringUtil.sanitize(name); - this.description = description; - this.unit = unit; - } - - /** - * Fundamental units of measurement. - */ - public enum BasicUnit { - SCALAR, - BITS, - BYTES, - SECONDS, - CORES; - } - - /** - * MeasurementUnit lets you build compound units of the form - * 10^n * (A * B * ...) / (X * Y * ...), - * where the elements in the numerator and denominator are all BasicUnits. A - * MeasurementUnit must have at least one BasicUnit in its numerator. - * - *

To specify multiplication in the numerator or denominator, simply specify - * multiple numerator or denominator fields. For example: - * - *

- byte-seconds (i.e. bytes * seconds): - * numerator: BYTES - * numerator: SECS - * - *

- events/sec^2 (i.e. rate of change of events/sec): - * numerator: SCALAR - * denominator: SECS - * denominator: SECS - * - *

To specify multiples (in power of 10) of units, specify a non-zero power10 - * value, for example: - * - *

- MB/s (i.e. megabytes / s): - * power10: 6 - * numerator: BYTES - * denominator: SECS - * - *

- nanoseconds - * power10: -9 - * numerator: SECS - */ - public static final class MeasurementUnit { - /** - * Constructs a {@link MeasurementUnit}. - */ - public static MeasurementUnit create( - int power10, List numerators, List denominators) { - return new MeasurementUnit(power10, numerators, denominators); - } - - /** - * Constructs a {@link MeasurementUnit} without the optional {@code denominators}. - */ - public static MeasurementUnit create(int power10, List numerators) { - return new MeasurementUnit(power10, numerators, new ArrayList()); - } - - /** - * Unit multiplier (i.e. 10^power10). - */ - public int getPower10() { - return power10; - } - - /** - * Unit Numerators. - * - *

Note: The returned list is unmodifiable and attempts to update it will throw an - * UnsupportedOperationException. - */ - public List getNumerators() { - return numerators; - } - - /** - * Unit Denominators. - * - *

Note: The returned list is unmodifiable and attempts to update it will throw an - * UnsupportedOperationException. - */ - public final List getDenominators() { - return denominators; - } - - private final int power10; - private final List numerators; - private final List denominators; - - private MeasurementUnit(int power10, List numerators, List denominators) { - this.power10 = power10; - this.numerators = Collections.unmodifiableList(new ArrayList(numerators)); - this.denominators = Collections.unmodifiableList(new ArrayList(denominators)); - } - } -} diff --git a/core/java/com/google/instrumentation/stats/MeasurementMap.java b/core/java/com/google/instrumentation/stats/MeasurementMap.java deleted file mode 100644 index 65a55aa6..00000000 --- a/core/java/com/google/instrumentation/stats/MeasurementMap.java +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.NoSuchElementException; - -/** - * A map from {@link MeasurementDescriptor}'s to measured values. - */ -public final class MeasurementMap implements Iterable { - /** - * Constructs a {@link MeasurementMap} from the given {@link MeasurementDescriptor} - * and associated value. - */ - public static MeasurementMap of(MeasurementDescriptor measurement, double value) { - return builder().put(measurement, value).build(); - } - - /** - * Constructs a {@link MeasurementMap} from the given {@link MeasurementDescriptor}'s - * and associated values. - */ - public static MeasurementMap of(MeasurementDescriptor measurement1, double value1, - MeasurementDescriptor measurement2, double value2) { - return builder().put(measurement1, value1).put(measurement2, value2).build(); - } - - /** - * Constructs a {@link MeasurementMap} from the given {@link MeasurementDescriptor}'s - * and associated values. - */ - public static MeasurementMap of(MeasurementDescriptor measurement1, double value1, - MeasurementDescriptor measurement2, double value2, - MeasurementDescriptor measurement3, double value3) { - return builder().put(measurement1, value1).put(measurement2, value2).put(measurement3, value3) - .build(); - } - - /** - * Returns a {@link Builder} for the {@link MeasurementMap} class. - */ - public static Builder builder() { - return new Builder(); - } - - /** - * Returns the number of measurements in this {@link MeasurementMap}. - */ - public int size() { - return measurements.size(); - } - - /** - * Returns an {@link Iterator} over the measurement/value mappings in this {@link MeasurementMap}. - * The {@code Iterator} does not support {@link Iterator#remove()}. - */ - @Override - public Iterator iterator() { - return new MeasurementMapIterator(); - } - - private final ArrayList measurements; - - private MeasurementMap(ArrayList measurements) { - this.measurements = measurements; - } - - /** - * Builder for the {@link MeasurementMap} class. - */ - public static class Builder { - /** - * Associates the {@link MeasurementDescriptor} with the given value. Subsequent updates to the - * same {@link MeasurementDescriptor} are ignored. - * - * @param measurement the {@link MeasurementDescriptor} - * @param value the value to be associated with {@code measurement} - * @return this - */ - public Builder put(MeasurementDescriptor measurement, double value) { - measurements.add(MeasurementValue.create(measurement, value)); - return this; - } - - /** - * Constructs a {@link MeasurementMap} from the current measurements. - */ - public MeasurementMap build() { - // Note: this makes adding measurements quadratic but is fastest for the sizes of - // MeasurementMaps that we should see. We may want to go to a strategy of sort/eliminate - // for larger MeasurementMaps. - for (int i = 0; i < measurements.size(); i++) { - String current = measurements.get(i).getMeasurement().getName(); - for (int j = i + 1; j < measurements.size(); j++) { - if (current.equals(measurements.get(j).getMeasurement().getName())) { - measurements.remove(j); - j--; - } - } - } - return new MeasurementMap(measurements); - } - - private final ArrayList measurements = new ArrayList(); - - private Builder() { - } - } - - // Provides an unmodifiable Iterator over this instance's measurements. - private final class MeasurementMapIterator implements Iterator { - @Override - public boolean hasNext() { - return position < length; - } - - @Override - public MeasurementValue next() { - if (position >= measurements.size()) { - throw new NoSuchElementException(); - } - return measurements.get(position++); - } - - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - - private final int length = measurements.size(); - private int position = 0; - } -} diff --git a/core/java/com/google/instrumentation/stats/MeasurementValue.java b/core/java/com/google/instrumentation/stats/MeasurementValue.java deleted file mode 100644 index 3502b97b..00000000 --- a/core/java/com/google/instrumentation/stats/MeasurementValue.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -/** - * Immutable representation of a MeasurementValue. - */ -public class MeasurementValue { - - /** - * Constructs a measured value. - */ - public static MeasurementValue create(MeasurementDescriptor name, double value) { - return new MeasurementValue(name, value); - } - - /** - * Extracts the measured {@link MeasurementDescriptor}. - */ - public MeasurementDescriptor getMeasurement() { - return name; - } - - /** - * Extracts the associated value. - */ - public double getValue() { - return value; - } - - private final MeasurementDescriptor name; - private final double value; - - private MeasurementValue(MeasurementDescriptor name, double value) { - this.name = name; - this.value = value; - } -} diff --git a/core/java/com/google/instrumentation/stats/RpcConstants.java b/core/java/com/google/instrumentation/stats/RpcConstants.java deleted file mode 100644 index b9e09586..00000000 --- a/core/java/com/google/instrumentation/stats/RpcConstants.java +++ /dev/null @@ -1,361 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import com.google.instrumentation.common.Duration; -import com.google.instrumentation.stats.MeasurementDescriptor.BasicUnit; -import com.google.instrumentation.stats.MeasurementDescriptor.MeasurementUnit; -import com.google.instrumentation.stats.ViewDescriptor.DistributionViewDescriptor; -import com.google.instrumentation.stats.ViewDescriptor.IntervalViewDescriptor; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -/** - * Constants for collecting rpc stats. - */ -public final class RpcConstants { - // Rpc tag keys. - public static final TagKey RPC_STATUS = TagKey.create("OpStatus"); - public static final TagKey RPC_CLIENT_METHOD = TagKey.create("method"); - public static final TagKey RPC_SERVER_METHOD = TagKey.create("method"); - - // Constants used to define the following MeasurementDescriptors. - private static final List bytes = Arrays.asList(BasicUnit.BYTES); - private static final List scalar = Arrays.asList(BasicUnit.SCALAR); - private static final List seconds = Arrays.asList(BasicUnit.SECONDS); - - // Census defined rpc client {@link MeasurementDescriptor}s. - public static final MeasurementDescriptor RPC_CLIENT_ERROR_COUNT = - MeasurementDescriptor.create( - "/rpc/client/error_count", - "RPC Errors", - MeasurementUnit.create(0, scalar)); - public static final MeasurementDescriptor RPC_CLIENT_REQUEST_BYTES = - MeasurementDescriptor.create( - "/rpc/client/request_bytes", - "Request MB", - MeasurementUnit.create(6, bytes)); - public static final MeasurementDescriptor RPC_CLIENT_RESPONSE_BYTES = - MeasurementDescriptor.create( - "/rpc/client/response_bytes", - "Response MB", - MeasurementUnit.create(6, bytes)); - // TODO(dpo): verify this encoding - public static final MeasurementDescriptor RPC_CLIENT_ROUNDTRIP_LATENCY = - MeasurementDescriptor.create( - "/rpc/client/roundtrip_latency", - "RPC roundtrip latency us", - MeasurementUnit.create(-6, seconds)); - public static final MeasurementDescriptor RPC_CLIENT_SERVER_ELAPSED_TIME = - MeasurementDescriptor.create( - "/rpc/client/server_elapsed_time", - "Server elapsed time in msecs", - MeasurementUnit.create(-3, seconds)); - public static final MeasurementDescriptor RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES = - MeasurementDescriptor.create( - "/rpc/client/uncompressed_request_bytes", - "Uncompressed Request MB", - MeasurementUnit.create(6, bytes)); - public static final MeasurementDescriptor RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES = - MeasurementDescriptor.create( - "/rpc/client/uncompressed_response_bytes", - "Uncompressed Response MB", - MeasurementUnit.create(6, bytes)); - - // Census defined rpc server {@link MeasurementDescriptor}s. - public static final MeasurementDescriptor RPC_SERVER_ERROR_COUNT = - MeasurementDescriptor.create( - "/rpc/server/error_count", - "RPC Errors", - MeasurementUnit.create(0, scalar)); - public static final MeasurementDescriptor RPC_SERVER_REQUEST_BYTES = - MeasurementDescriptor.create( - "/rpc/server/request_bytes", - "Request MB", - MeasurementUnit.create(6, bytes)); - public static final MeasurementDescriptor RPC_SERVER_RESPONSE_BYTES = - MeasurementDescriptor.create( - "/rpc/server/response_bytes", - "Response MB", - MeasurementUnit.create(6, bytes)); - public static final MeasurementDescriptor RPC_SERVER_SERVER_ELAPSED_TIME = - MeasurementDescriptor.create( - "/rpc/server/server_elapsed_time", - "Server elapsed time in msecs", - MeasurementUnit.create(-3, seconds)); - public static final MeasurementDescriptor RPC_SERVER_SERVER_LATENCY = - MeasurementDescriptor.create( - "/rpc/server/server_latency", - "Latency in msecs", - MeasurementUnit.create(-3, seconds)); - public static final MeasurementDescriptor RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES = - MeasurementDescriptor.create( - "/rpc/server/uncompressed_request_bytes", - "Uncompressed Request MB", - MeasurementUnit.create(6, bytes)); - public static final MeasurementDescriptor RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES = - MeasurementDescriptor.create( - "/rpc/server/uncompressed_response_bytes", - "Uncompressed Response MB", - MeasurementUnit.create(6, bytes)); - public static final MeasurementDescriptor RPC_SERVER_STARTED_COUNT = - MeasurementDescriptor.create( - "/rpc/server/started_count", - "Number of RPCs started", - MeasurementUnit.create(0, scalar)); - public static final MeasurementDescriptor RPC_SERVER_FINISHED_COUNT = - MeasurementDescriptor.create( - "/rpc/server/finished_count", - "Number of RPCs finished", - MeasurementUnit.create(0, scalar)); - - // Common histogram bucket boundaries for bytes received/sets DistributionViewDescriptors. - static final List RPC_BYTES_BUCKET_BOUNDARIES = Collections.unmodifiableList( - Arrays.asList(0.0, 1024.0, 2048.0, 4096.0, 16384.0, 65536.0, 262144.0, 1048576.0, 4194304.0, - 16777216.0, 67108864.0, 268435456.0, 1073741824.0, 4294967296.0)); - - // Common histogram bucket boundaries for latency and elapsed-time DistributionViewDescriptors. - static final List RPC_MILLIS_BUCKET_BOUNDARIES = Collections.unmodifiableList( - Arrays.asList(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 8.0, 10.0, 13.0, 16.0, 20.0, 25.0, 30.0, - 40.0, 50.0, 65.0, 80.0, 100.0, 130.0, 160.0, 200.0, 250.0, 300.0, 400.0, 500.0, 650.0, - 800.0, 1000.0, 2000.0, 5000.0, 10000.0, 20000.0, 50000.0, 100000.0)); - - // Census defined rpc client {@link ViewDescriptor}s. - public static final DistributionViewDescriptor RPC_CLIENT_ERROR_COUNT_VIEW = - DistributionViewDescriptor.create( - "rpc client error_count", - "RPC Errors", - RPC_CLIENT_ERROR_COUNT, - DistributionAggregationDescriptor.create(), - Arrays.asList(RPC_STATUS, RPC_CLIENT_METHOD)); - public static final DistributionViewDescriptor RPC_CLIENT_ROUNDTRIP_LATENCY_VIEW = - DistributionViewDescriptor.create( - "rpc client roundtrip_latency", - "Latency in msecs", - RPC_CLIENT_ROUNDTRIP_LATENCY, - DistributionAggregationDescriptor.create(RPC_MILLIS_BUCKET_BOUNDARIES), - Arrays.asList(RPC_CLIENT_METHOD)); - public static final DistributionViewDescriptor RPC_CLIENT_SERVER_ELAPSED_TIME_VIEW = - DistributionViewDescriptor.create( - "rpc client server_elapsed_time", - "Server elapsed time in msecs", - RPC_CLIENT_SERVER_ELAPSED_TIME, - DistributionAggregationDescriptor.create(RPC_MILLIS_BUCKET_BOUNDARIES), - Arrays.asList(RPC_CLIENT_METHOD)); - public static final DistributionViewDescriptor RPC_CLIENT_REQUEST_BYTES_VIEW = - DistributionViewDescriptor.create( - "rpc client request_bytes", - "Request MB", - RPC_CLIENT_REQUEST_BYTES, - DistributionAggregationDescriptor.create(RPC_BYTES_BUCKET_BOUNDARIES), - Arrays.asList(RPC_CLIENT_METHOD)); - public static final DistributionViewDescriptor RPC_CLIENT_RESPONSE_BYTES_VIEW = - DistributionViewDescriptor.create( - "rpc client response_bytes", - "Response MB", - RPC_CLIENT_RESPONSE_BYTES, - DistributionAggregationDescriptor.create(RPC_BYTES_BUCKET_BOUNDARIES), - Arrays.asList(RPC_CLIENT_METHOD)); - public static final DistributionViewDescriptor RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES_VIEW = - DistributionViewDescriptor.create( - "rpc client uncompressed_request_bytes", - "Uncompressed Request MB", - RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES, - DistributionAggregationDescriptor.create(RPC_BYTES_BUCKET_BOUNDARIES), - Arrays.asList(RPC_CLIENT_METHOD)); - public static final DistributionViewDescriptor RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES_VIEW = - DistributionViewDescriptor.create( - "rpc client uncompressed_response_bytes", - "Uncompressed Response MB", - RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES, - DistributionAggregationDescriptor.create(RPC_BYTES_BUCKET_BOUNDARIES), - Arrays.asList(RPC_CLIENT_METHOD)); - - - // Census defined rpc server {@link ViewDescriptor}s. - public static final DistributionViewDescriptor RPC_SERVER_ERROR_COUNT_VIEW = - DistributionViewDescriptor.create( - "rpc server error_count", - "RPC Errors", - RPC_SERVER_ERROR_COUNT, - DistributionAggregationDescriptor.create(), - Arrays.asList(RPC_STATUS, RPC_SERVER_METHOD)); - public static final DistributionViewDescriptor RPC_SERVER_SERVER_LATENCY_VIEW = - DistributionViewDescriptor.create( - "rpc server server_latency", - "Latency in msecs", - RPC_SERVER_SERVER_LATENCY, - DistributionAggregationDescriptor.create(RPC_MILLIS_BUCKET_BOUNDARIES), - Arrays.asList(RPC_SERVER_METHOD)); - public static final DistributionViewDescriptor RPC_SERVER_SERVER_ELAPSED_TIME_VIEW = - DistributionViewDescriptor.create( - "rpc server elapsed_time", - "Server elapsed time in msecs", - RPC_SERVER_SERVER_ELAPSED_TIME, - DistributionAggregationDescriptor.create(RPC_MILLIS_BUCKET_BOUNDARIES), - Arrays.asList(RPC_SERVER_METHOD)); - public static final DistributionViewDescriptor RPC_SERVER_REQUEST_BYTES_VIEW = - DistributionViewDescriptor.create( - "rpc server request_bytes", - "Request MB", - RPC_SERVER_REQUEST_BYTES, - DistributionAggregationDescriptor.create(RPC_BYTES_BUCKET_BOUNDARIES), - Arrays.asList(RPC_SERVER_METHOD)); - public static final DistributionViewDescriptor RPC_SERVER_RESPONSE_BYTES_VIEW = - DistributionViewDescriptor.create( - "rpc server response_bytes", - "Response MB", - RPC_SERVER_RESPONSE_BYTES, - DistributionAggregationDescriptor.create(RPC_BYTES_BUCKET_BOUNDARIES), - Arrays.asList(RPC_SERVER_METHOD)); - public static final DistributionViewDescriptor RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES_VIEW = - DistributionViewDescriptor.create( - "rpc server uncompressed_request_bytes", - "Uncompressed Request MB", - RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES, - DistributionAggregationDescriptor.create(RPC_BYTES_BUCKET_BOUNDARIES), - Arrays.asList(RPC_SERVER_METHOD)); - public static final DistributionViewDescriptor RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES_VIEW = - DistributionViewDescriptor.create( - "rpc server uncompressed_response_bytes", - "Uncompressed Response MB", - RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES, - DistributionAggregationDescriptor.create(RPC_BYTES_BUCKET_BOUNDARIES), - Arrays.asList(RPC_SERVER_METHOD)); - - // Interval Stats - static final Duration MINUTE = Duration.create(60, 0); - static final Duration HOUR = Duration.create(60 * 60, 0); - - // Census defined rpc client {@link IntervalViewDescriptor}s. - public static final IntervalViewDescriptor RPC_CLIENT_ROUNDTRIP_LATENCY_INTERVAL_VIEW = - IntervalViewDescriptor.create( - "rpc client roundtrip_latency", - "Minute and Hour stats for latency in msecs", - RPC_CLIENT_ROUNDTRIP_LATENCY, - IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), - Arrays.asList(RPC_CLIENT_METHOD)); - - public static final IntervalViewDescriptor RPC_CLIENT_REQUEST_BYTES_INTERVAL_VIEW = - IntervalViewDescriptor.create( - "rpc client request_bytes", - "Minute and Hour stats for request in MBs", - RPC_CLIENT_REQUEST_BYTES, - IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), - Arrays.asList(RPC_CLIENT_METHOD)); - - public static final IntervalViewDescriptor RPC_CLIENT_RESPONSE_BYTES_INTERVAL_VIEW = - IntervalViewDescriptor.create( - "rpc client response_bytes", - "Minute and Hour stats for response size MBs", - RPC_CLIENT_RESPONSE_BYTES, - IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), - Arrays.asList(RPC_CLIENT_METHOD)); - - public static final IntervalViewDescriptor RPC_CLIENT_ERROR_COUNT_INTERVAL_VIEW = - IntervalViewDescriptor.create( - "rpc client error_count", - "Minute and Hour stats for rpc errors", - RPC_CLIENT_ERROR_COUNT, - IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), - Arrays.asList(RPC_CLIENT_METHOD)); - - public static final IntervalViewDescriptor RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES_INTERVAL_VIEW = - IntervalViewDescriptor.create( - "rpc client uncompressed_request_bytes", - "Minute and Hour stats for uncompressed request size in MB", - RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES, - IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), - Arrays.asList(RPC_CLIENT_METHOD)); - - public static final IntervalViewDescriptor RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES_INTERVAL_VIEW = - IntervalViewDescriptor.create( - "rpc client uncompressed_response_bytes", - "Minute and Hour stats for uncompressed response size in MBs", - RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES, - IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), - Arrays.asList(RPC_CLIENT_METHOD)); - - public static final IntervalViewDescriptor RPC_CLIENT_SERVER_ELAPSED_TIME_INTERVAL_VIEW = - IntervalViewDescriptor.create( - "rpc client server_elapsed_time", - "Minute and Hour stats for server elapsed time in msecs", - RPC_CLIENT_SERVER_ELAPSED_TIME, - IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), - Arrays.asList(RPC_CLIENT_METHOD)); - - // Census defined rpc server {@link IntervalViewDescriptor}s. - public static final IntervalViewDescriptor RPC_SERVER_SERVER_LATENCY_INTERVAL_VIEW = - IntervalViewDescriptor.create( - "rpc server server_latency", - "Minute and Hour stats for server latency in msecs", - RPC_SERVER_SERVER_LATENCY, - IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), - Arrays.asList(RPC_SERVER_METHOD)); - - public static final IntervalViewDescriptor RPC_SERVER_REQUEST_BYTES_INTERVAL_VIEW = - IntervalViewDescriptor.create( - "rpc server request_bytes", - "Minute and Hour stats for request size in MB", - RPC_SERVER_REQUEST_BYTES, - IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), - Arrays.asList(RPC_SERVER_METHOD)); - - public static final IntervalViewDescriptor RPC_SERVER_RESPONSE_BYTES_INTERVAL_VIEW = - IntervalViewDescriptor.create( - "rpc server response_bytes", - "Minute and Hour stats for response size in MBs", - RPC_SERVER_RESPONSE_BYTES, - IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), - Arrays.asList(RPC_SERVER_METHOD)); - - public static final IntervalViewDescriptor RPC_SERVER_ERROR_COUNT_INTERVAL_VIEW = - IntervalViewDescriptor.create( - "rpc server error_count", - "Minute and Hour stats for rpc errors", - RPC_SERVER_ERROR_COUNT, - IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), - Arrays.asList(RPC_SERVER_METHOD)); - - public static final IntervalViewDescriptor RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES_INTERVAL_VIEW = - IntervalViewDescriptor.create( - "rpc server uncompressed_request_bytes", - "Minute and Hour stats for uncompressed request size in MBs", - RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES, - IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), - Arrays.asList(RPC_SERVER_METHOD)); - - public static final IntervalViewDescriptor RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES_INTERVAL_VIEW = - IntervalViewDescriptor.create( - "rpc server uncompressed_response_bytes", - "Minute and Hour stats for uncompressed response size in MBs", - RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES, - IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), - Arrays.asList(RPC_SERVER_METHOD)); - - public static final IntervalViewDescriptor RPC_SERVER_SERVER_ELAPSED_TIME_INTERVAL_VIEW = - IntervalViewDescriptor.create( - "rpc server server_elapsed_time", - "Minute and Hour stats for server elapsed time in msecs", - RPC_SERVER_SERVER_ELAPSED_TIME, - IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), - Arrays.asList(RPC_SERVER_METHOD)); - - // Visible for testing - RpcConstants() { - throw new AssertionError(); - } -} diff --git a/core/java/com/google/instrumentation/stats/Stats.java b/core/java/com/google/instrumentation/stats/Stats.java deleted file mode 100644 index e9313253..00000000 --- a/core/java/com/google/instrumentation/stats/Stats.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import com.google.instrumentation.common.Provider; - -/** - * {@link Stats}. - */ -public final class Stats { - private static final StatsContextFactory CONTEXT_FACTORY = Provider.newInstance( - "com.google.instrumentation.stats.StatsContextFactoryImpl", null); - - private static final StatsManager STATS_MANAGER = Provider.newInstance( - "com.google.instrumentation.stats.StatsManagerImpl", null); - - /** - * Returns the default {@link StatsContextFactory}. - */ - public static StatsContextFactory getStatsContextFactory() { - return CONTEXT_FACTORY; - } - - /** - * Returns the default {@link StatsManager}. - */ - public static StatsManager getStatsManager() { - return STATS_MANAGER; - } - - // VisibleForTesting - Stats() { - throw new AssertionError(); - } -} diff --git a/core/java/com/google/instrumentation/stats/StatsContext.java b/core/java/com/google/instrumentation/stats/StatsContext.java deleted file mode 100644 index 0a9f9674..00000000 --- a/core/java/com/google/instrumentation/stats/StatsContext.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import java.io.IOException; -import java.io.OutputStream; - -/** - * An immutable context for stats operations. - */ -public abstract class StatsContext { - /** - * Returns a builder based on this {@link StatsContext}. - */ - public abstract Builder builder(); - - /** Shorthand for builder().set(k1, v1).build() */ - public final StatsContext with(TagKey k1, TagValue v1) { - return builder().set(k1, v1).build(); - } - - /** Shorthand for builder().set(k1, v1).set(k2, v2).build() */ - public final StatsContext with(TagKey k1, TagValue v1, TagKey k2, TagValue v2) { - return builder().set(k1, v1).set(k2, v2).build(); - } - - /** Shorthand for builder().set(k1, v1).set(k2, v2).set(k3, v3).build() */ - public final StatsContext with( - TagKey k1, TagValue v1, TagKey k2, TagValue v2, TagKey k3, TagValue v3) { - return builder().set(k1, v1).set(k2, v2).set(k3, v3).build(); - } - - /** - * Records the given measurements against this {@link StatsContext}. - * - * @param measurements the measurements to record against the saved {@link StatsContext} - * @return this - */ - public abstract StatsContext record(MeasurementMap measurements); - - /** - * Serializes the {@link StatsContext} into the on-the-wire representation. - * - *

The inverse of {@link StatsContextFactory#deserialize(java.io.InputStream)} and should be - * based on the {@link StatsContext} protobuf representation. - * - * @param output the {@link OutputStream} to add the serialized form of this {@link StatsContext}. - */ - public abstract void serialize(OutputStream output) throws IOException; - - /** - * Builder for {@link StatsContext}. - */ - public abstract static class Builder { - /** - * Associates the given tag key with the given tag value. - * - * @param key the key - * @param value the value to be associated with {@code key} - * @return {@code this} - */ - public abstract Builder set(TagKey key, TagValue value); - - /** - * Builds a {@link StatsContext} from the specified keys and values. - */ - public abstract StatsContext build(); - } -} diff --git a/core/java/com/google/instrumentation/stats/StatsContextFactory.java b/core/java/com/google/instrumentation/stats/StatsContextFactory.java deleted file mode 100644 index 581ca847..00000000 --- a/core/java/com/google/instrumentation/stats/StatsContextFactory.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import java.io.InputStream; -import java.io.IOException; - -/** - * Factory class for {@link StatsContext}. - */ -public abstract class StatsContextFactory { - /** - * Creates a {@link StatsContext} from the given on-the-wire encoded representation. - * - *

Should be the inverse of {@link StatsContext#serialize(java.io.OutputStream)}. The - * serialized representation should be based on the {@link StatsContext} protobuf representation. - * - * @param input on-the-wire representation of a {@link StatsContext} - * @return a {@link StatsContext} deserialized from {@code input} - */ - public abstract StatsContext deserialize(InputStream input) throws IOException; - - /** - * Returns the default {@link StatsContext}. - */ - public abstract StatsContext getDefault(); -} diff --git a/core/java/com/google/instrumentation/stats/StatsManager.java b/core/java/com/google/instrumentation/stats/StatsManager.java deleted file mode 100644 index 59253768..00000000 --- a/core/java/com/google/instrumentation/stats/StatsManager.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -/** - * Provides facillities to register {@link ViewDescriptor}s for collecting stats and retrieving - * stats data as a {@link View}. - */ -public abstract class StatsManager { - /** - * Pull model for stats. Registers a {@link ViewDescriptor} that will collect data to be accessed - * via {@link #getView(ViewDescriptor)}. - */ - public abstract void registerView(ViewDescriptor viewDescriptor); - - /** - * Returns the current stats data, {@link View}, associated with the given {@link ViewDescriptor}. - */ - public abstract View getView(ViewDescriptor viewDescriptor); -} diff --git a/core/java/com/google/instrumentation/stats/StringUtil.java b/core/java/com/google/instrumentation/stats/StringUtil.java deleted file mode 100644 index 180c2411..00000000 --- a/core/java/com/google/instrumentation/stats/StringUtil.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -/** - * Utility methods for working with tag keys, tag values, and metric names. - */ -final class StringUtil { - - static final int MAX_LENGTH = 255; - static final char UNPRINTABLE_CHAR_SUBSTITUTE = '_'; - - static String sanitize(String str) { - if (str.length() > MAX_LENGTH) { - str = str.substring(0, MAX_LENGTH); - } - if (isPrintableString(str)) { - return str; - } - StringBuilder builder = new StringBuilder(str.length()); - for (int i = 0; i < str.length(); i++) { - char ch = str.charAt(i); - builder.append(isPrintableChar(ch) ? ch : UNPRINTABLE_CHAR_SUBSTITUTE); - } - return builder.toString(); - } - - private static boolean isPrintableString(String str) { - for (int i = 0; i < str.length(); i++) { - if (!isPrintableChar(str.charAt(i))) { - return false; - } - } - return true; - } - - private static boolean isPrintableChar(char ch) { - return ch >= ' ' && ch <= '~'; - } - - // Visible for testing - StringUtil() { - throw new AssertionError(); - } -} diff --git a/core/java/com/google/instrumentation/stats/Tag.java b/core/java/com/google/instrumentation/stats/Tag.java deleted file mode 100644 index 1bda1c85..00000000 --- a/core/java/com/google/instrumentation/stats/Tag.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -/** - * A pair of consisting of an associated {@link TagKey} and {@link TagValue}. - */ -public final class Tag { - /** - * Constructs a new {@link Tag} from the given key and value. - */ - public static Tag create(TagKey key, TagValue value) { - return new Tag(key, value); - } - - /** - * Returns the associated tag key. - */ - public TagKey getKey() { - return key; - } - - /** - * Returns the associated tag key. - */ - public TagValue getValue() { - return value; - } - - @Override - public boolean equals(Object obj) { - return (obj instanceof Tag) - && key.equals(((Tag) obj).key) - && value.equals(((Tag) obj).value); - } - - @Override - public int hashCode() { - int result = 17; - result = 31 * result + key.hashCode(); - result = 31 * result + value.hashCode(); - return result; - } - - @Override - public String toString() { - return "Tag<" + key + "," + value + ">"; - } - - private final TagKey key; - private final TagValue value; - - private Tag(TagKey key, TagValue value) { - this.key = key; - this.value = value; - } -} diff --git a/core/java/com/google/instrumentation/stats/TagKey.java b/core/java/com/google/instrumentation/stats/TagKey.java deleted file mode 100644 index bce969e6..00000000 --- a/core/java/com/google/instrumentation/stats/TagKey.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -/** - * Census Tag keys. - * - *

TagKey's are {@link String}s with enforced restrictions. - */ -public final class TagKey { - public static final int MAX_LENGTH = StringUtil.MAX_LENGTH; - - /** - * Constructs a new {@link TagKey} from the given string. The string will be sanitize such that: - *

    - *
  1. length is restricted to {@link MAX_LENGTH}, strings longer than that will be truncated. - *
  2. characters are restricted to printable ascii characters, non-printable characters will be - * replaced by an underscore '_'. - *
- */ - public static TagKey create(String key) { - return new TagKey(key); - } - - @Override - public boolean equals(Object obj) { - return (obj instanceof TagKey) && key.equals(((TagKey) obj).key); - } - - @Override - public int hashCode() { - return key.hashCode(); - } - - @Override - public String toString() { - return key; - } - - private final String key; - - private TagKey(String key) { - this.key = StringUtil.sanitize(key); - } -} diff --git a/core/java/com/google/instrumentation/stats/TagValue.java b/core/java/com/google/instrumentation/stats/TagValue.java deleted file mode 100644 index c12b184d..00000000 --- a/core/java/com/google/instrumentation/stats/TagValue.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -/** - * Census Tag values. - * - *

TagValue's are {@link String}s with enforced restrictions. - */ -public final class TagValue { - public static final int MAX_LENGTH = StringUtil.MAX_LENGTH; - - /** - * Constructs a new {@link TagValue} from the given string. The string will be sanitize such that: - *

    - *
  1. length is restricted to {@link MAX_LENGTH}, strings longer than that will be truncated. - *
  2. characters are restricted to printable ascii characters, non-printable characters will be - * replaced by an underscore '_'. - *
- */ - public static TagValue create(String value) { - return new TagValue(value); - } - - @Override - public boolean equals(Object obj) { - return (obj instanceof TagValue) && value.equals(((TagValue) obj).value); - } - - @Override - public int hashCode() { - return value.hashCode(); - } - - @Override - public String toString() { - return value; - } - - private final String value; - - private TagValue(String value) { - this.value = StringUtil.sanitize(value); - } -} diff --git a/core/java/com/google/instrumentation/stats/View.java b/core/java/com/google/instrumentation/stats/View.java deleted file mode 100644 index 90996960..00000000 --- a/core/java/com/google/instrumentation/stats/View.java +++ /dev/null @@ -1,148 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import com.google.instrumentation.common.Function; -import com.google.instrumentation.common.Timestamp; -import com.google.instrumentation.stats.ViewDescriptor.DistributionViewDescriptor; -import com.google.instrumentation.stats.ViewDescriptor.IntervalViewDescriptor; - -import java.util.List; - -/** - * The aggregated data for a particular {@link ViewDescriptor}. - */ -public abstract class View { - /** - * The {@link ViewDescriptor} associated with this {@link View}. - */ - public abstract ViewDescriptor getViewDescriptor(); - - /** - * Applies the given match function to the underlying data type. - */ - public abstract T match( - Function p0, - Function p1); - - // Prevents this class from being subclassed anywhere else. - private View() { - } - - /** - * A {@link View} for distribution-based aggregations. - */ - public static final class DistributionView extends View { - /** - * Constructs a new {@link DistributionView}. - */ - public static DistributionView create(DistributionViewDescriptor distributionViewDescriptor, - List distributionAggregations, Timestamp start, Timestamp end) { - return new DistributionView(distributionViewDescriptor, distributionAggregations, start, end); - } - - /** - * The {@link DistributionAggregation}s associated with this {@link DistributionView}. - * - *

Note: The returned list is unmodifiable, attempts to update it will throw an - * UnsupportedOperationException. - */ - public List getDistributionAggregations() { - return distributionAggregations; - } - - /** - * Returns start timestamp for this aggregation. - */ - public Timestamp getStart() { - return start; - } - - /** - * Returns end timestamp for this aggregation. - */ - public Timestamp getEnd() { - return end; - } - - @Override - public DistributionViewDescriptor getViewDescriptor() { - return distributionViewDescriptor; - } - - @Override - public T match( - Function p0, - Function p1) { - return p0.apply(this); - } - - private final DistributionViewDescriptor distributionViewDescriptor; - private final List distributionAggregations; - private final Timestamp start; - private final Timestamp end; - - private DistributionView(DistributionViewDescriptor distributionViewDescriptor, - List distributionAggregations, Timestamp start, Timestamp end) { - this.distributionViewDescriptor = distributionViewDescriptor; - this.distributionAggregations = distributionAggregations; - this.start = start; - this.end = end; - } - } - - /** - * A {@link View} for interval-base aggregations. - */ - public static final class IntervalView extends View { - /** - * Constructs a new {@link IntervalView}. - */ - public static IntervalView create(IntervalViewDescriptor intervalViewDescriptor, - List intervalAggregations) { - return new IntervalView(intervalViewDescriptor, intervalAggregations); - } - - /** - * The {@link IntervalAggregation}s associated with this {@link IntervalView}. - * - *

Note: The returned list is unmodifiable, attempts to update it will throw an - * UnsupportedOperationException. - */ - public List getIntervalAggregations() { - return intervalAggregations; - } - - @Override - public IntervalViewDescriptor getViewDescriptor() { - return intervalViewDescriptor; - } - - @Override - public T match( - Function p0, - Function p1) { - return p1.apply(this); - } - - private final IntervalViewDescriptor intervalViewDescriptor; - private final List intervalAggregations; - - private IntervalView(IntervalViewDescriptor intervalViewDescriptor, - List intervalAggregations) { - this.intervalViewDescriptor = intervalViewDescriptor; - this.intervalAggregations = intervalAggregations; - } - } -} diff --git a/core/java/com/google/instrumentation/stats/ViewDescriptor.java b/core/java/com/google/instrumentation/stats/ViewDescriptor.java deleted file mode 100644 index ae2ccee4..00000000 --- a/core/java/com/google/instrumentation/stats/ViewDescriptor.java +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import com.google.instrumentation.common.Function; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - * A ViewDescriptor specifies an aggregation and a set of tag keys. The aggregation will be broken - * down by the unique set of matching tag values for each measurement. - */ -public abstract class ViewDescriptor { - /** - * Name of view. Must be unique. - */ - public final String getName() { - return name; - } - - /** - * More detailed description, for documentation purposes. - */ - public final String getDescription() { - return description; - } - - /** - * Measurement type of this view. - */ - public final MeasurementDescriptor getMeasurementDescriptor() { - return measurementDescriptor; - } - - /** - * Tag keys to match with the associated {@link MeasurementDescriptor}. If no keys are specified, - * then all stats are recorded. Keys must be unique. - * - *

Note: The returned list is unmodifiable, attempts to update it will throw an - * UnsupportedOperationException. - */ - public final List getTagKeys() { - return tagKeys; - } - - /** - * Applies the given match function to the underlying data type. - */ - public abstract T match( - Function p0, - Function p1); - - - private final String name; - private final String description; - private final MeasurementDescriptor measurementDescriptor; - private final List tagKeys; - - private ViewDescriptor( - String name, - String description, - MeasurementDescriptor measurementDescriptor, - List tagKeys) { - this.name = name; - this.description = description; - this.measurementDescriptor = measurementDescriptor; - this.tagKeys = Collections.unmodifiableList(new ArrayList(tagKeys)); - } - - /** - * A {@link ViewDescriptor} for distribution-base aggregations. - */ - public static class DistributionViewDescriptor extends ViewDescriptor { - /** - * Constructs a new {@link DistributionViewDescriptor}. - */ - public static DistributionViewDescriptor create( - String name, - String description, - MeasurementDescriptor measurementDescriptor, - DistributionAggregationDescriptor distributionAggregationDescriptor, - List tagKeys) { - return new DistributionViewDescriptor( - name, description, measurementDescriptor, distributionAggregationDescriptor, tagKeys); - } - - /** - * The {@link DistributionAggregationDescriptor} associated with this - * {@link DistributionViewDescriptor}. - */ - public DistributionAggregationDescriptor getDistributionAggregationDescriptor() { - return distributionAggregationDescriptor; - } - - @Override - public T match( - Function p0, - Function p1) { - return p0.apply(this); - } - - private final DistributionAggregationDescriptor distributionAggregationDescriptor; - - private DistributionViewDescriptor( - String name, - String description, - MeasurementDescriptor measurementDescriptor, - DistributionAggregationDescriptor distributionAggregationDescriptor, - List tagKeys) { - super(name, description, measurementDescriptor, tagKeys); - this.distributionAggregationDescriptor = distributionAggregationDescriptor; - } - } - - /** - * A {@link ViewDescriptor} for interval-based aggregations. - */ - public static class IntervalViewDescriptor extends ViewDescriptor { - /** - * Constructs a new {@link IntervalViewDescriptor}. - */ - public static IntervalViewDescriptor create( - String name, - String description, - MeasurementDescriptor measurementDescriptor, - IntervalAggregationDescriptor intervalAggregationDescriptor, - List tagKeys) { - return new IntervalViewDescriptor( - name, description, measurementDescriptor, intervalAggregationDescriptor, tagKeys); - } - - /** - * The {@link IntervalAggregationDescriptor} associated with this - * {@link IntervalViewDescriptor}. - */ - public IntervalAggregationDescriptor getIntervalAggregationDescriptor() { - return intervalAggregationDescriptor; - } - - @Override - public T match( - Function p0, - Function p1) { - return p1.apply(this); - } - - private final IntervalAggregationDescriptor intervalAggregationDescriptor; - - private IntervalViewDescriptor( - String name, - String description, - MeasurementDescriptor measurementDescriptor, - IntervalAggregationDescriptor intervalAggregationDescriptor, - List tagKeys) { - super(name, description, measurementDescriptor, tagKeys); - this.intervalAggregationDescriptor = intervalAggregationDescriptor; - } - } -} diff --git a/core/javatests/com/google/instrumentation/common/DurationTest.java b/core/javatests/com/google/instrumentation/common/DurationTest.java deleted file mode 100644 index 4ba9806c..00000000 --- a/core/javatests/com/google/instrumentation/common/DurationTest.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.google.instrumentation.common; - -import static com.google.common.truth.Truth.assertThat; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Unit tests for {@link Duration}. */ -@RunWith(JUnit4.class) -public class DurationTest { - @Test - public void testDurationCreate() { - assertThat(Duration.create(24, 42).getSeconds()).isEqualTo(24); - assertThat(Duration.create(24, 42).getNanos()).isEqualTo(42); - assertThat(Duration.create(-24, -42).getSeconds()).isEqualTo(-24); - assertThat(Duration.create(-24, -42).getNanos()).isEqualTo(-42); - assertThat(Duration.create(315576000000L, 999999999).getSeconds()) - .isEqualTo(315576000000L); - assertThat(Duration.create(315576000000L, 999999999).getNanos()) - .isEqualTo(999999999); - assertThat(Duration.create(-315576000000L, -999999999).getSeconds()) - .isEqualTo(-315576000000L); - assertThat(Duration.create(-315576000000L, -999999999).getNanos()) - .isEqualTo(-999999999); - } - - @Test - public void testDurationCreateInvalidInput() { - assertThat(Duration.create(-315576000001L, 0)).isEqualTo(Duration.create(0, 0)); - assertThat(Duration.create(315576000001L, 0)).isEqualTo(Duration.create(0, 0)); - assertThat(Duration.create(0, 1000000000)).isEqualTo(Duration.create(0, 0)); - assertThat(Duration.create(0, -1000000000)).isEqualTo(Duration.create(0, 0)); - assertThat(Duration.create(-1, 1)).isEqualTo(Duration.create(0, 0)); - assertThat(Duration.create(1, -1)).isEqualTo(Duration.create(0, 0)); - } - - @Test - public void testDurationFromMillis() { - assertThat(Duration.fromMillis(0)).isEqualTo(Duration.create(0, 0)); - assertThat(Duration.fromMillis(987)).isEqualTo(Duration.create(0, 987000000)); - assertThat(Duration.fromMillis(3456)).isEqualTo(Duration.create(3, 456000000)); - } - - @Test - public void testDurationFromMillisNegative() { - assertThat(Duration.fromMillis(-1)).isEqualTo(Duration.create(0, -1000000)); - assertThat(Duration.fromMillis(-999)).isEqualTo(Duration.create(0, -999000000)); - assertThat(Duration.fromMillis(-1000)).isEqualTo(Duration.create(-1, 0)); - assertThat(Duration.fromMillis(-3456)).isEqualTo(Duration.create(-3, -456000000)); - } - - @Test - public void testDurationEqual() { - // Positive tests. - assertThat(Duration.create(0, 0)).isEqualTo(Duration.create(0, 0)); - assertThat(Duration.create(24, 42)).isEqualTo(Duration.create(24, 42)); - assertThat(Duration.create(-24, -42)).isEqualTo(Duration.create(-24, -42)); - // Negative tests. - assertThat(Duration.create(25, 42)).isNotEqualTo(Duration.create(24, 42)); - assertThat(Duration.create(24, 43)).isNotEqualTo(Duration.create(24, 42)); - assertThat(Duration.create(-25, -42)).isNotEqualTo(Duration.create(-24, -42)); - assertThat(Duration.create(-24, -43)).isNotEqualTo(Duration.create(-24, -42)); - } -} diff --git a/core/javatests/com/google/instrumentation/common/ProviderTest.java b/core/javatests/com/google/instrumentation/common/ProviderTest.java deleted file mode 100644 index f125de25..00000000 --- a/core/javatests/com/google/instrumentation/common/ProviderTest.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.common; - -import static com.google.common.truth.Truth.assertThat; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Tests for {@link Provider} - */ -@RunWith(JUnit4.class) -public class ProviderTest { - @Test - public void testGoodClass() throws Exception { - GetGen getGen0 = - Provider.newInstance("com.google.instrumentation.common.ProviderTest$GetGen", null); - assertThat(getGen0).isNotNull(); - assertThat(getGen0.getGen()).isEqualTo(0); - for (int i = 1; i < 10; i++) { - GetGen getGenI = - Provider.newInstance("com.google.instrumentation.common.ProviderTest$GetGen", null); - assertThat(getGenI).isNotNull(); - assertThat(getGenI.getGen()).isEqualTo(i); - } - assertThat(getGen0.getGen()).isEqualTo(0); - } - - @Test - public void testBadClass() throws Exception { - GetGen getGen = - Provider.newInstance("com.google.instrumentation.common.ProviderTest$BadClass", null); - assertThat(getGen).isNull(); - } - - static class GetGen { - static int genCount = 0; - int gen; - - public GetGen() { - gen = genCount++; - } - - public int getGen() { - return gen; - } - } -} diff --git a/core/javatests/com/google/instrumentation/common/TimestampTest.java b/core/javatests/com/google/instrumentation/common/TimestampTest.java deleted file mode 100644 index 1e994def..00000000 --- a/core/javatests/com/google/instrumentation/common/TimestampTest.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.google.instrumentation.common; - -import static com.google.common.truth.Truth.assertThat; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Unit tests for {@link Timestamp}. */ -@RunWith(JUnit4.class) -public class TimestampTest { - @Test - public void testTimestampCreate() { - assertThat(Timestamp.create(24, 42).getSeconds()).isEqualTo(24); - assertThat(Timestamp.create(24, 42).getNanos()).isEqualTo(42); - assertThat(Timestamp.create(-24, 42).getSeconds()).isEqualTo(-24); - assertThat(Timestamp.create(-24, 42).getNanos()).isEqualTo(42); - assertThat(Timestamp.create(315576000000L, 999999999).getSeconds()) - .isEqualTo(315576000000L); - assertThat(Timestamp.create(315576000000L, 999999999).getNanos()) - .isEqualTo(999999999); - assertThat(Timestamp.create(-315576000000L, 999999999).getSeconds()) - .isEqualTo(-315576000000L); - assertThat(Timestamp.create(-315576000000L, 999999999).getNanos()) - .isEqualTo(999999999); - } - - @Test - public void testTimestampCreateInvalidInput() { - assertThat(Timestamp.create(-315576000001L, 0)).isEqualTo(Timestamp.create(0, 0)); - assertThat(Timestamp.create(315576000001L, 0)).isEqualTo(Timestamp.create(0, 0)); - assertThat(Timestamp.create(1, 1000000000)).isEqualTo(Timestamp.create(0, 0)); - assertThat(Timestamp.create(1, -1)).isEqualTo(Timestamp.create(0, 0)); - assertThat(Timestamp.create(-1, 1000000000)).isEqualTo(Timestamp.create(0, 0)); - assertThat(Timestamp.create(-1, -1)).isEqualTo(Timestamp.create(0, 0)); - } - - @Test - public void testTimestampFromMillis() { - assertThat(Timestamp.fromMillis(0)).isEqualTo(Timestamp.create(0, 0)); - assertThat(Timestamp.fromMillis(987)).isEqualTo(Timestamp.create(0, 987000000)); - assertThat(Timestamp.fromMillis(3456)).isEqualTo(Timestamp.create(3, 456000000)); - } - - @Test - public void testTimestampFromMillisNegative() { - assertThat(Timestamp.fromMillis(-1)).isEqualTo(Timestamp.create(-1, 999000000)); - assertThat(Timestamp.fromMillis(-999)).isEqualTo(Timestamp.create(-1, 1000000)); - assertThat(Timestamp.fromMillis(-3456)).isEqualTo(Timestamp.create(-4, 544000000)); - } - - @Test - public void testTimestampEqual() { - // Positive tests. - assertThat(Timestamp.create(0, 0)).isEqualTo(Timestamp.create(0, 0)); - assertThat(Timestamp.create(24, 42)).isEqualTo(Timestamp.create(24, 42)); - assertThat(Timestamp.create(-24, 42)).isEqualTo(Timestamp.create(-24, 42)); - // Negative tests. - assertThat(Timestamp.create(25, 42)).isNotEqualTo(Timestamp.create(24, 42)); - assertThat(Timestamp.create(24, 43)).isNotEqualTo(Timestamp.create(24, 42)); - assertThat(Timestamp.create(-25, 42)).isNotEqualTo(Timestamp.create(-24, 42)); - assertThat(Timestamp.create(-24, 43)).isNotEqualTo(Timestamp.create(-24, 42)); - } -} diff --git a/core/javatests/com/google/instrumentation/stats/DistributionAggregationDescriptorTest.java b/core/javatests/com/google/instrumentation/stats/DistributionAggregationDescriptorTest.java deleted file mode 100644 index abc57c5b..00000000 --- a/core/javatests/com/google/instrumentation/stats/DistributionAggregationDescriptorTest.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import static com.google.common.truth.Truth.assertThat; - -import java.util.Arrays; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Tests for {@link DistributionAggregationDescriptor} - */ -@RunWith(JUnit4.class) -public final class DistributionAggregationDescriptorTest { - @Test - public void testDistributionAggregationDescriptorEmpty() { - DistributionAggregationDescriptor dDescriptor = DistributionAggregationDescriptor.create(); - assertThat(dDescriptor.getBucketBoundaries()).isNull(); - } - - @Test - public void testDistributionAggregationDescriptor() { - Double[] buckets = new Double[] { 0.1, 2.2, 33.3 }; - DistributionAggregationDescriptor dDescriptor = - DistributionAggregationDescriptor.create(Arrays.asList(buckets)); - assertThat(dDescriptor.getBucketBoundaries()).isNotNull(); - assertThat(dDescriptor.getBucketBoundaries()).hasSize(buckets.length); - for (int i = 0; i < buckets.length; i++) { - assertThat(dDescriptor.getBucketBoundaries().get(i)) - .isWithin(0.00000001).of(buckets[i]); - } - } -} diff --git a/core/javatests/com/google/instrumentation/stats/DistributionAggregationTest.java b/core/javatests/com/google/instrumentation/stats/DistributionAggregationTest.java deleted file mode 100644 index 55e0e4e0..00000000 --- a/core/javatests/com/google/instrumentation/stats/DistributionAggregationTest.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.instrumentation.stats.DistributionAggregation.Range; - -import java.util.Arrays; -import java.util.List; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Tests for class {@link DistributionAggregation}. - */ -@RunWith(JUnit4.class) -public final class DistributionAggregationTest { - @Test - public void testDistributionAggregationWithOutBuckets() { - DistributionAggregation aggr = DistributionAggregation.create(10, 5.0, 30.0, - Range.create(1.0, 5.0), TAGS); - - assertThat(aggr.getCount()).isEqualTo(10); - assertThat(aggr.getMean()).isEqualTo(5.0); - assertThat(aggr.getSum()).isEqualTo(30.0); - assertThat(aggr.getRange().getMin()).isEqualTo(1.0); - assertThat(aggr.getRange().getMax()).isEqualTo(5.0); - assertThat(aggr.getTags()).hasSize(TAGS.size()); - for (int i = 0; i < aggr.getTags().size(); i++) { - assertThat(aggr.getTags().get(i)).isEqualTo(TAGS.get(i)); - } - assertThat(aggr.getBucketCounts()).isNull(); - } - - @Test - public void testDistributionAggregationWithBuckets() { - List buckets = Arrays.asList(2L, 2L, 2L, 2L, 2L); - DistributionAggregation aggr = DistributionAggregation.create(10, 5.0, 30.0, - Range.create(1.0, 5.0), TAGS, buckets); - - assertThat(aggr.getCount()).isEqualTo(10); - assertThat(aggr.getMean()).isEqualTo(5.0); - assertThat(aggr.getSum()).isEqualTo(30.0); - assertThat(aggr.getRange().getMin()).isEqualTo(1.0); - assertThat(aggr.getRange().getMax()).isEqualTo(5.0); - assertThat(aggr.getBucketCounts()).isNotNull(); - assertThat(aggr.getBucketCounts()).hasSize(buckets.size()); - assertThat(aggr.getTags()).hasSize(TAGS.size()); - for (int i = 0; i < aggr.getTags().size(); i++) { - assertThat(aggr.getTags().get(i)).isEqualTo(TAGS.get(i)); - } - for (int i = 0; i < aggr.getBucketCounts().size(); i++) { - assertThat(aggr.getBucketCounts().get(i)).isEqualTo(buckets.get(i)); - } - } - - private static final TagKey K1 = TagKey.create("k1"); - private static final TagKey K2 = TagKey.create("k2"); - - private static final TagValue V1 = TagValue.create("v1"); - private static final TagValue V2 = TagValue.create("v2"); - - private static final List TAGS = Arrays.asList(Tag.create(K1, V1), Tag.create(K2, V2)); -} diff --git a/core/javatests/com/google/instrumentation/stats/IntervalAggregationDescriptorTest.java b/core/javatests/com/google/instrumentation/stats/IntervalAggregationDescriptorTest.java deleted file mode 100644 index 3c8a5e45..00000000 --- a/core/javatests/com/google/instrumentation/stats/IntervalAggregationDescriptorTest.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.instrumentation.common.Duration; -import java.util.Arrays; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Tests for {@link IntervalAggregationDescriptor} - */ -@RunWith(JUnit4.class) -public final class IntervalAggregationDescriptorTest { - @Test - public void testIntervalAggregationDescriptor() { - Duration[] intervals = - new Duration[] { Duration.fromMillis(1), Duration.fromMillis(22), Duration.fromMillis(333)}; - IntervalAggregationDescriptor iDescriptor = - IntervalAggregationDescriptor.create(12, Arrays.asList(intervals)); - assertThat(iDescriptor.getNumSubIntervals()).isEqualTo(12); - assertThat(iDescriptor.getIntervalSizes()).isNotNull(); - assertThat(iDescriptor.getIntervalSizes()).hasSize(intervals.length); - for (int i = 0; i < intervals.length; i++) { - assertThat(iDescriptor.getIntervalSizes().get(i)).isEqualTo(intervals[i]); - } - } - - @Test - public void testIntervalAggregationDescriptorWithDefaultNumSubIntervals() { - assertThat( - IntervalAggregationDescriptor.create( - Arrays.asList(Duration.fromMillis(1))).getNumSubIntervals()) - .isEqualTo(5); - } - - @Test - public void testIntervalAggregationDescriptorNumSubIntervalsRange() { - assertThat( - IntervalAggregationDescriptor.create( - 2, Arrays.asList(Duration.fromMillis(1))).getNumSubIntervals()) - .isEqualTo(2); - assertThat( - IntervalAggregationDescriptor.create( - 20, Arrays.asList(Duration.fromMillis(1))).getNumSubIntervals()) - .isEqualTo(20); - } - - @Test(expected = IllegalArgumentException.class) - public void testIntervalAggregationDescriptorLowNumSubIntervals() { - IntervalAggregationDescriptor.create(1, Arrays.asList(Duration.fromMillis(1))); - } - - @Test(expected = IllegalArgumentException.class) - public void testIntervalAggregationDescriptorHighNumSubIntervals() { - IntervalAggregationDescriptor.create(21, Arrays.asList(Duration.fromMillis(1))); - } - - @Test(expected = IllegalArgumentException.class) - public void testIntervalAggregationDescriptorEmptyIntervalSizes() { - IntervalAggregationDescriptor.create(Arrays.asList(new Duration[] { })); - } -} diff --git a/core/javatests/com/google/instrumentation/stats/IntervalAggregationTest.java b/core/javatests/com/google/instrumentation/stats/IntervalAggregationTest.java deleted file mode 100644 index a436ff0b..00000000 --- a/core/javatests/com/google/instrumentation/stats/IntervalAggregationTest.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.instrumentation.common.Duration; -import com.google.instrumentation.stats.IntervalAggregation.Interval; -import java.util.Arrays; -import java.util.List; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Tests for class {@link IntervalAggregation}. - */ -@RunWith(JUnit4.class) -public final class IntervalAggregationTest { - @Test - public void testIntervalAggregation() { - List intervals = Arrays.asList( - Interval.create(Duration.fromMillis(10), 100.0, 1000.0), - Interval.create(Duration.fromMillis(11), 101.0, 1001.0)); - IntervalAggregation aggr = IntervalAggregation.create(TAGS, intervals); - - assertThat(aggr.getTags()).hasSize(TAGS.size()); - for (int i = 0; i < aggr.getTags().size(); i++) { - assertThat(aggr.getTags().get(i)).isEqualTo(TAGS.get(i)); - } - assertThat(aggr.getIntervals()).hasSize(intervals.size()); - for (int i = 0; i < aggr.getIntervals().size(); i++) { - assertThat(aggr.getIntervals().get(i)).isEqualTo(intervals.get(i)); - } - } - - @Test - public void testInterval() { - Duration duration = Duration.fromMillis(10); - Interval interval = Interval.create(duration, 100.0, 1000.0); - - assertThat(interval.getIntervalSize()).isEqualTo(duration); - assertThat(interval.getCount()).isWithin(0.00000001).of(100.0); - assertThat(interval.getSum()).isWithin(0.00000001).of(1000.0); - } - - private static final TagKey K1 = TagKey.create("k1"); - private static final TagKey K2 = TagKey.create("k2"); - - private static final TagValue V1 = TagValue.create("v1"); - private static final TagValue V2 = TagValue.create("v2"); - - private static final List TAGS = Arrays.asList(Tag.create(K1, V1), Tag.create(K2, V2)); -} diff --git a/core/javatests/com/google/instrumentation/stats/MeasurementDescriptorTest.java b/core/javatests/com/google/instrumentation/stats/MeasurementDescriptorTest.java deleted file mode 100644 index 9fa16f37..00000000 --- a/core/javatests/com/google/instrumentation/stats/MeasurementDescriptorTest.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.instrumentation.stats.MeasurementDescriptor.BasicUnit; -import com.google.instrumentation.stats.MeasurementDescriptor.MeasurementUnit; -import java.util.Arrays; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Tests for {@link MeasurementDescriptor} - */ -@RunWith(JUnit4.class) -public final class MeasurementDescriptorTest { - @Test - public void testNameMaxLength() { - char[] name = new char[MeasurementDescriptor.MAX_LENGTH]; - char[] truncName = new char[MeasurementDescriptor.MAX_LENGTH + 10]; - Arrays.fill(name, 'n'); - Arrays.fill(truncName, 'n'); - assertThat(makeSimpleDescriptor(new String(name)).getName()) - .isEqualTo(makeSimpleDescriptor(new String(truncName)).getName()); - } - - @Test - public void testNameBadChar() { - assertThat(makeSimpleDescriptor("\2ab\3cd").getName()) - .isEqualTo(StringUtil.UNPRINTABLE_CHAR_SUBSTITUTE + "ab" - + StringUtil.UNPRINTABLE_CHAR_SUBSTITUTE + "cd"); - } - - @Test - public void testComponents() { - MeasurementDescriptor measurement = MeasurementDescriptor.create( - "Foo", - "The description of Foo", - MeasurementUnit.create( - 6, Arrays.asList(BasicUnit.BITS), Arrays.asList(BasicUnit.SECONDS))); - assertThat(measurement.getName()).isEqualTo("Foo"); - assertThat(measurement.getDescription()).isEqualTo("The description of Foo"); - assertThat(measurement.getUnit().getPower10()).isEqualTo(6); - assertThat(measurement.getUnit().getNumerators()).hasSize(1); - assertThat(measurement.getUnit().getNumerators().get(0)).isEqualTo(BasicUnit.BITS); - assertThat(measurement.getUnit().getDenominators()).hasSize(1); - assertThat(measurement.getUnit().getDenominators().get(0)).isEqualTo(BasicUnit.SECONDS); - } - - private static final MeasurementDescriptor makeSimpleDescriptor(String name) { - return MeasurementDescriptor.create( - name, - name + " description", - MeasurementUnit.create(1, Arrays.asList(BasicUnit.SCALAR))); - } -} diff --git a/core/javatests/com/google/instrumentation/stats/MeasurementMapTest.java b/core/javatests/com/google/instrumentation/stats/MeasurementMapTest.java deleted file mode 100644 index 068cdfc2..00000000 --- a/core/javatests/com/google/instrumentation/stats/MeasurementMapTest.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.common.collect.ImmutableList; -import com.google.instrumentation.stats.MeasurementDescriptor.BasicUnit; -import com.google.instrumentation.stats.MeasurementDescriptor.MeasurementUnit; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Iterator; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Tests for {@link MeasurementMap} - */ -@RunWith(JUnit4.class) -public class MeasurementMapTest { - @Test - public void testOf1() { - ImmutableList expected = ImmutableList.of(MeasurementValue.create(M1, 44.4)); - MeasurementMap metrics = MeasurementMap.of(M1, 44.4); - assertEquals(expected, metrics); - } - - @Test - public void testOf2() { - ImmutableList expected = ImmutableList.of( - MeasurementValue.create(M1, 44.4), MeasurementValue.create(M2, 66.6)); - MeasurementMap metrics = MeasurementMap.of(M1, 44.4, M2, 66.6); - assertEquals(expected, metrics); - } - - @Test - public void testOf3() { - ImmutableList expected = ImmutableList.of( - MeasurementValue.create(M1, 44.4), - MeasurementValue.create(M2, 66.6), - MeasurementValue.create(M3, 88.8)); - MeasurementMap metrics = MeasurementMap.of( - M1, 44.4, M2, 66.6, M3, 88.8); - assertEquals(expected, metrics); - } - - @Test - public void testBuilderEmpty() { - ImmutableList expected = ImmutableList.of(); - MeasurementMap metrics = MeasurementMap.builder().build(); - assertEquals(expected, metrics); - } - - @Test - public void testBuilder() { - ArrayList expected = new ArrayList(10); - MeasurementMap.Builder builder = MeasurementMap.builder(); - for (int i = 1; i <= 10; i++) { - expected.add(MeasurementValue.create(makeSimpleMeasurement("m" + i), i * 11.1)); - builder.put(makeSimpleMeasurement("m" + i), i * 11.1); - assertEquals(expected, builder.build()); - } - } - - @Test - public void testDuplicateMeasurementDescriptors() { - assertEquals(MeasurementMap.of(M1, 1.0, M1, 1.0), MeasurementMap.of(M1, 1.0)); - assertEquals(MeasurementMap.of(M1, 1.0, M1, 2.0), MeasurementMap.of(M1, 1.0)); - assertEquals(MeasurementMap.of(M1, 1.0, M1, 2.0, M1, 3.0), MeasurementMap.of(M1, 1.0)); - assertEquals(MeasurementMap.of(M1, 1.0, M2, 2.0, M1, 3.0), MeasurementMap.of(M1, 1.0, M2, 2.0)); - assertEquals(MeasurementMap.of(M1, 1.0, M1, 2.0, M2, 2.0), MeasurementMap.of(M1, 1.0, M2, 2.0)); - } - - @Test - public void testSize() { - MeasurementMap.Builder builder = MeasurementMap.builder(); - for (int i = 1; i <= 10; i++) { - builder.put(makeSimpleMeasurement("m" + i), i * 11.1); - assertThat(builder.build()).hasSize(i); - } - } - - private static final MeasurementUnit simpleMeasurementUnit = - MeasurementUnit.create(1, Arrays.asList(BasicUnit.SCALAR)); - private static final MeasurementDescriptor M1 = makeSimpleMeasurement("m1"); - private static final MeasurementDescriptor M2 = makeSimpleMeasurement("m2"); - private static final MeasurementDescriptor M3 = makeSimpleMeasurement("m3"); - - private static final MeasurementDescriptor makeSimpleMeasurement(String measurement) { - return MeasurementDescriptor.create( - measurement, measurement + " description", simpleMeasurementUnit); - } - - private static void assertEquals( - Iterable expected, Iterable actual) { - Iterator e = expected.iterator(); - Iterator a = actual.iterator(); - while (e.hasNext() && a.hasNext()) { - MeasurementValue expectedMeasurement = e.next(); - MeasurementValue actualMeasurement = a.next(); - assertThat(expectedMeasurement.getMeasurement().getName()) - .isEqualTo(actualMeasurement.getMeasurement().getName()); - assertThat(expectedMeasurement.getValue()) - .isWithin(0.00000001).of(actualMeasurement.getValue()); - } - assertThat(e.hasNext()).isFalse(); - assertThat(a.hasNext()).isFalse(); - } -} diff --git a/core/javatests/com/google/instrumentation/stats/RpcConstantsTest.java b/core/javatests/com/google/instrumentation/stats/RpcConstantsTest.java deleted file mode 100644 index 50ec1298..00000000 --- a/core/javatests/com/google/instrumentation/stats/RpcConstantsTest.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import static com.google.common.truth.Truth.assertThat; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Tests for {@link RpcConstants} - */ -@RunWith(JUnit4.class) -public final class RpcConstantsTest { - @Test - public void testConstants() { - assertThat(RpcConstants.RPC_STATUS).isNotNull(); - assertThat(RpcConstants.RPC_CLIENT_METHOD).isNotNull(); - assertThat(RpcConstants.RPC_SERVER_METHOD).isNotNull(); - - assertThat(RpcConstants.RPC_CLIENT_ERROR_COUNT).isNotNull(); - assertThat(RpcConstants.RPC_CLIENT_ROUNDTRIP_LATENCY).isNotNull(); - assertThat(RpcConstants.RPC_CLIENT_REQUEST_BYTES).isNotNull(); - assertThat(RpcConstants.RPC_CLIENT_RESPONSE_BYTES).isNotNull(); - assertThat(RpcConstants.RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES).isNotNull(); - assertThat(RpcConstants.RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES).isNotNull(); - - assertThat(RpcConstants.RPC_SERVER_ERROR_COUNT).isNotNull(); - assertThat(RpcConstants.RPC_SERVER_REQUEST_BYTES).isNotNull(); - assertThat(RpcConstants.RPC_SERVER_RESPONSE_BYTES).isNotNull(); - assertThat(RpcConstants.RPC_SERVER_SERVER_LATENCY).isNotNull(); - assertThat(RpcConstants.RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES).isNotNull(); - assertThat(RpcConstants.RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES).isNotNull(); - } - - @Test(expected = AssertionError.class) - public void testConstructor() { - new RpcConstants(); - } -} diff --git a/core/javatests/com/google/instrumentation/stats/StatsContextFactoryTest.java b/core/javatests/com/google/instrumentation/stats/StatsContextFactoryTest.java deleted file mode 100644 index 12efa98f..00000000 --- a/core/javatests/com/google/instrumentation/stats/StatsContextFactoryTest.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Tests for {@link StatsContextFactory}. - */ -@RunWith(JUnit4.class) -public class StatsContextFactoryTest { - @Test(expected = IOException.class) - public void testDeserializeEmpty() throws Exception { - Stats.getStatsContextFactory().deserialize(new ByteArrayInputStream(new byte[0])); - } -} diff --git a/core/javatests/com/google/instrumentation/stats/StatsContextTest.java b/core/javatests/com/google/instrumentation/stats/StatsContextTest.java deleted file mode 100644 index 04531297..00000000 --- a/core/javatests/com/google/instrumentation/stats/StatsContextTest.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.common.testing.EqualsTester; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Tests for {@link StatsContext}. - */ -@RunWith(JUnit4.class) -public class StatsContextTest { - private static final StatsContext DEFAULT = Stats.getStatsContextFactory().getDefault(); - - private static final MeasurementDescriptor[] StatsMeasurementDescriptors = { - RpcConstants.RPC_CLIENT_REQUEST_BYTES, RpcConstants.RPC_CLIENT_RESPONSE_BYTES, - RpcConstants.RPC_CLIENT_ROUNDTRIP_LATENCY, RpcConstants.RPC_SERVER_REQUEST_BYTES, - RpcConstants.RPC_SERVER_RESPONSE_BYTES, RpcConstants.RPC_SERVER_SERVER_LATENCY - }; - - private static final TagKey K_EMPTY = TagKey.create(""); - private static final TagKey K1 = TagKey.create("k1"); - private static final TagKey K2 = TagKey.create("k2"); - private static final TagKey K3 = TagKey.create("k3"); - private static final TagKey K4 = TagKey.create("k4"); - private static final TagKey K10 = TagKey.create("k10"); - - private static final TagValue V_EMPTY = TagValue.create(""); - private static final TagValue V1 = TagValue.create("v1"); - private static final TagValue V2 = TagValue.create("v2"); - private static final TagValue V3 = TagValue.create("v3"); - private static final TagValue V4 = TagValue.create("v4"); - private static final TagValue V10 = TagValue.create("v10"); - private static final TagValue V20 = TagValue.create("v20"); - private static final TagValue V30 = TagValue.create("v30"); - private static final TagValue V100 = TagValue.create("v100"); - - @Test - public void testWith() { - assertThat(DEFAULT.builder().set(K1, V1).build()).isEqualTo(DEFAULT.with(K1, V1)); - - assertThat(DEFAULT.builder().set(K1, V1).set(K2, V2).build()) - .isEqualTo(DEFAULT.with(K1, V1, K2, V2)); - - assertThat(DEFAULT.builder().set(K1, V1).set(K2, V2).set(K3, V3).build()) - .isEqualTo(DEFAULT.with(K1, V1, K2, V2, K3, V3)); - } - - @Test - public void testWithComposed() { - StatsContext context1 = DEFAULT.with(K1, V1); - assertThat(DEFAULT.builder().set(K1, V1).build()).isEqualTo(context1); - - StatsContext context2 = context1.with(K1, V10, K2, V2); - assertThat(DEFAULT.with(K1, V10, K2, V2)).isEqualTo(context2); - - StatsContext context3 = context2.with(K1, V100, K2, V20, K3, V3); - assertThat(DEFAULT.with(K1, V100, K2, V20, K3, V3)).isEqualTo(context3); - - StatsContext context4 = context3.with(K3, V30, K4, V4); - assertThat(DEFAULT.builder().set(K1, V100).set(K2, V20).set(K3, V30).set(K4, V4).build()) - .isEqualTo(context4); - } - - - @Test - public void testRecordEachMeasurement() { - StatsContext context = DEFAULT.with(K1, V1); - double value = 44.0; - for (MeasurementDescriptor descriptor : StatsMeasurementDescriptors) { - MeasurementMap measurements = MeasurementMap.of(descriptor, value); - context.record(measurements); - //verify(context.context).record(measurements); - value++; - } - } - - @Test - public void testRecordAllMeasurements() { - StatsContext context = DEFAULT.with(K1, V1); - double value = 44.0; - MeasurementMap.Builder builder = MeasurementMap.builder(); - for (MeasurementDescriptor descriptor : StatsMeasurementDescriptors) { - MeasurementMap measurements = builder.put(descriptor, value).build(); - context.record(measurements); - //verify(context.context).record(measurements); - value++; - } - } - - @Test - public void testSerialize() throws Exception { - testSerialization(DEFAULT.builder().build()); - testSerialization(DEFAULT.with(K1, V1)); - testSerialization(DEFAULT.with(K1, V1, K2, V2, K3, V3)); - testSerialization(DEFAULT.with(K1, V_EMPTY)); - testSerialization(DEFAULT.with(K_EMPTY, V1)); - testSerialization(DEFAULT.with(K_EMPTY, V_EMPTY)); - } - - // Tests for Object overrides. - - @Test - public void testEquals() { - new EqualsTester() - .addEqualityGroup(DEFAULT, DEFAULT) - .addEqualityGroup(DEFAULT.with(K1, V1), DEFAULT.with(K1, V1)) - .addEqualityGroup( - DEFAULT.with(K1, V1, K2, V2), - DEFAULT.with(K1, V1, K2, V2), - DEFAULT.with(K2, V2, K1, V1)) - .addEqualityGroup(DEFAULT.with(K10, V1)) - .addEqualityGroup(DEFAULT.with(K1, V10)) - .addEqualityGroup("foo") - .testEquals(); - } - - @Test - public void testToString() { - assertThat(DEFAULT.with(K1, V1).toString()).isEqualTo(DEFAULT.with(K1, V1).toString()); - assertThat(DEFAULT.with(K10, V1).toString()).isNotEqualTo(DEFAULT.with(K1, V1).toString()); - assertThat(DEFAULT.with(K1, V10).toString()).isNotEqualTo(DEFAULT.with(K1, V1).toString()); - } - - private static void testSerialization(StatsContext expected) throws Exception { - ByteArrayOutputStream output = new ByteArrayOutputStream(); - expected.serialize(output); - ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray()); - StatsContext actual = Stats.getStatsContextFactory().deserialize(input); - assertThat(actual).isEqualTo(expected); - } -} diff --git a/core/javatests/com/google/instrumentation/stats/StatsTest.java b/core/javatests/com/google/instrumentation/stats/StatsTest.java deleted file mode 100644 index 2bbf5cef..00000000 --- a/core/javatests/com/google/instrumentation/stats/StatsTest.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import static com.google.common.truth.Truth.assertThat; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Tests for {@link RpcConstants} - */ -@RunWith(JUnit4.class) -public final class StatsTest { - @Test - public void getStatsContextFactory() { - assertThat(Stats.getStatsContextFactory()).isNotNull(); - } - - @Test(expected = AssertionError.class) - public void testConstructor() { - new Stats(); - } -} diff --git a/core/javatests/com/google/instrumentation/stats/StringUtilTest.java b/core/javatests/com/google/instrumentation/stats/StringUtilTest.java deleted file mode 100644 index 8d954098..00000000 --- a/core/javatests/com/google/instrumentation/stats/StringUtilTest.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import static com.google.common.truth.Truth.assertThat; - -import java.util.Arrays; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Tests for {@link StringUtil}. - */ -@RunWith(JUnit4.class) -public final class StringUtilTest { - @Test - public void testMaxLength() { - char[] string = new char[StringUtil.MAX_LENGTH]; - char[] truncString = new char[StringUtil.MAX_LENGTH + 10]; - Arrays.fill(string, 'v'); - Arrays.fill(truncString, 'v'); - assertThat(StringUtil.sanitize(new String(truncString))).isEqualTo(new String(string)); - } - - @Test - public void testBadChar() { - String string = "\2ab\3cd"; - assertThat(StringUtil.sanitize(string)).isEqualTo("_ab_cd"); - } - - @Test(expected = AssertionError.class) - public void testConstructor() { - new StringUtil(); - } -} diff --git a/core/javatests/com/google/instrumentation/stats/TagKeyTest.java b/core/javatests/com/google/instrumentation/stats/TagKeyTest.java deleted file mode 100644 index 36f469b8..00000000 --- a/core/javatests/com/google/instrumentation/stats/TagKeyTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.common.testing.EqualsTester; -import java.util.Arrays; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Tests for {@link TagKey} - */ -@RunWith(JUnit4.class) -public final class TagKeyTest { - @Test - public void testKeyMaxLength() { - char[] key = new char[TagKey.MAX_LENGTH]; - char[] truncKey = new char[TagKey.MAX_LENGTH + 10]; - Arrays.fill(key, 'k'); - Arrays.fill(truncKey, 'k'); - assertThat(TagKey.create(new String(key)).toString()) - .isEqualTo(TagKey.create(new String(truncKey)).toString()); - } - - @Test - public void testKeyBadChar() { - String key = "\2ab\3cd"; - assertThat(TagKey.create(key).toString()) - .isEqualTo(StringUtil.UNPRINTABLE_CHAR_SUBSTITUTE + "ab" - + StringUtil.UNPRINTABLE_CHAR_SUBSTITUTE + "cd"); - } - - @Test - public void testEquals() { - new EqualsTester() - .addEqualityGroup(TagKey.create("foo"), TagKey.create("foo")) - .addEqualityGroup(TagKey.create("bar")) - .testEquals(); - } - - @Test - public void testToString() { - assertThat(TagKey.create("foo").toString()).isEqualTo("foo"); - } -} diff --git a/core/javatests/com/google/instrumentation/stats/TagTest.java b/core/javatests/com/google/instrumentation/stats/TagTest.java deleted file mode 100644 index 010b8a31..00000000 --- a/core/javatests/com/google/instrumentation/stats/TagTest.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.common.testing.EqualsTester; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Tests for {@link Tag} - */ -@RunWith(JUnit4.class) -public final class TagTest { - @Test - public void testTagTest() { - Tag tag = Tag.create(K1, V1); - assertThat(tag.getKey()).isEqualTo(K1); - assertThat(tag.getValue()).isEqualTo(V1); - } - - @Test - public void testEquals() { - new EqualsTester() - .addEqualityGroup(Tag.create(K1, V1), Tag.create(K1, V1)) - .addEqualityGroup(Tag.create(K2, V1)) - .addEqualityGroup(Tag.create(K1, V2)) - .testEquals(); - } - - @Test - public void testHashCode() { - new EqualsTester() - .addEqualityGroup(Tag.create(K1, V1).hashCode(), Tag.create(K1, V1).hashCode()) - .addEqualityGroup(Tag.create(K2, V1).hashCode()) - .addEqualityGroup(Tag.create(K1, V2).hashCode()) - .testEquals(); - } - - @Test - public void testToString() { - assertThat(Tag.create(K1, V1).toString()).isEqualTo("Tag"); - assertThat(Tag.create(K2, V1).toString()).isEqualTo("Tag"); - assertThat(Tag.create(K1, V2).toString()).isEqualTo("Tag"); - } - - private static final TagKey K1 = TagKey.create("k1"); - private static final TagKey K2 = TagKey.create("k2"); - private static final TagValue V1 = TagValue.create("v1"); - private static final TagValue V2 = TagValue.create("v2"); -} diff --git a/core/javatests/com/google/instrumentation/stats/TagValueTest.java b/core/javatests/com/google/instrumentation/stats/TagValueTest.java deleted file mode 100644 index 63b98bf5..00000000 --- a/core/javatests/com/google/instrumentation/stats/TagValueTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.common.testing.EqualsTester; -import java.util.Arrays; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Tests for {@link TagValue} - */ -@RunWith(JUnit4.class) -public final class TagValueTest { - @Test - public void testValueMaxLength() { - char[] value = new char[TagValue.MAX_LENGTH]; - char[] truncValue = new char[TagValue.MAX_LENGTH + 10]; - Arrays.fill(value, 'v'); - Arrays.fill(truncValue, 'v'); - assertThat(TagValue.create(new String(value)).toString()) - .isEqualTo(TagValue.create(new String(truncValue)).toString()); - } - - @Test - public void testValueBadChar() { - String value = "\2ab\3cd"; - assertThat(TagValue.create(value).toString()) - .isEqualTo(StringUtil.UNPRINTABLE_CHAR_SUBSTITUTE + "ab" - + StringUtil.UNPRINTABLE_CHAR_SUBSTITUTE + "cd"); - } - - @Test - public void testEquals() { - new EqualsTester() - .addEqualityGroup(TagValue.create("foo"), TagValue.create("foo")) - .addEqualityGroup(TagValue.create("bar")) - .testEquals(); - } - - @Test - public void testToString() { - assertThat(TagValue.create("foo").toString()).isEqualTo("foo"); - } -} diff --git a/core/javatests/com/google/instrumentation/stats/ViewDescriptorTest.java b/core/javatests/com/google/instrumentation/stats/ViewDescriptorTest.java deleted file mode 100644 index f0781bf6..00000000 --- a/core/javatests/com/google/instrumentation/stats/ViewDescriptorTest.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import static com.google.common.truth.Truth.assertThat; - -import static org.junit.Assert.assertTrue; - -import com.google.instrumentation.common.Duration; -import com.google.instrumentation.common.Function; -import com.google.instrumentation.stats.MeasurementDescriptor.BasicUnit; -import com.google.instrumentation.stats.MeasurementDescriptor.MeasurementUnit; -import com.google.instrumentation.stats.ViewDescriptor.DistributionViewDescriptor; -import com.google.instrumentation.stats.ViewDescriptor.IntervalViewDescriptor; - -import java.util.Arrays; -import java.util.List; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Tests for {@link ViewDescriptor} - */ -@RunWith(JUnit4.class) -public final class ViewDescriptorTest { - @Test - public void testDistributionViewDescriptor() { - DistributionAggregationDescriptor dAggrDescriptor = DistributionAggregationDescriptor.create(); - final ViewDescriptor viewDescriptor = DistributionViewDescriptor.create( - name, description, measurementDescriptor, dAggrDescriptor, keys); - - assertThat(viewDescriptor.getName()).isEqualTo(name); - assertThat(viewDescriptor.getDescription()).isEqualTo(description); - assertThat(viewDescriptor.getMeasurementDescriptor().getName()) - .isEqualTo(measurementDescriptor.getName()); - assertThat(viewDescriptor.getTagKeys()).hasSize(2); - assertThat(viewDescriptor.getTagKeys().get(0).toString()).isEqualTo("foo"); - assertThat(viewDescriptor.getTagKeys().get(1).toString()).isEqualTo("bar"); - assertTrue(viewDescriptor.match( - new Function () { - @Override public Boolean apply(DistributionViewDescriptor dViewDescriptor) { - return dViewDescriptor == viewDescriptor; - } - }, - new Function () { - @Override public Boolean apply(IntervalViewDescriptor iViewDescriptor) { - return false; - } - })); - } - - @Test - public void testIntervalViewDescriptor() { - IntervalAggregationDescriptor iAggrDescriptor = IntervalAggregationDescriptor.create( - Arrays.asList(Duration.fromMillis(1), Duration.fromMillis(22), Duration.fromMillis(333))); - final ViewDescriptor viewDescriptor = IntervalViewDescriptor.create( - name, description, measurementDescriptor, iAggrDescriptor, keys); - - assertThat(viewDescriptor.getName()).isEqualTo(name); - assertThat(viewDescriptor.getDescription()).isEqualTo(description); - assertThat(viewDescriptor.getMeasurementDescriptor().getName()) - .isEqualTo(measurementDescriptor.getName()); - assertThat(viewDescriptor.getTagKeys()).hasSize(2); - assertThat(viewDescriptor.getTagKeys().get(0).toString()).isEqualTo("foo"); - assertThat(viewDescriptor.getTagKeys().get(1).toString()).isEqualTo("bar"); - assertTrue(viewDescriptor.match( - new Function () { - @Override public Boolean apply(DistributionViewDescriptor dViewDescriptor) { - return false; - } - }, - new Function () { - @Override public Boolean apply(IntervalViewDescriptor iViewDescriptor) { - return iViewDescriptor == viewDescriptor; - } - })); - } - - private final String name = "test-view-name"; - private final String description = "test-view-name description"; - private final MeasurementDescriptor measurementDescriptor = MeasurementDescriptor.create( - "measurement", - "measurement description", - MeasurementUnit.create(1, Arrays.asList(BasicUnit.SCALAR))); - private final List keys = Arrays.asList(TagKey.create("foo"), TagKey.create("bar")); -} diff --git a/core/javatests/com/google/instrumentation/stats/ViewTest.java b/core/javatests/com/google/instrumentation/stats/ViewTest.java deleted file mode 100644 index 5a057918..00000000 --- a/core/javatests/com/google/instrumentation/stats/ViewTest.java +++ /dev/null @@ -1,143 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * 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 com.google.instrumentation.stats; - -import static com.google.common.truth.Truth.assertThat; - -import static org.junit.Assert.assertTrue; - -import com.google.instrumentation.common.Duration; -import com.google.instrumentation.common.Function; -import com.google.instrumentation.common.Timestamp; -import com.google.instrumentation.stats.DistributionAggregation.Range; -import com.google.instrumentation.stats.IntervalAggregation.Interval; -import com.google.instrumentation.stats.MeasurementDescriptor.BasicUnit; -import com.google.instrumentation.stats.MeasurementDescriptor.MeasurementUnit; -import com.google.instrumentation.stats.View.DistributionView; -import com.google.instrumentation.stats.View.IntervalView; -import com.google.instrumentation.stats.ViewDescriptor.DistributionViewDescriptor; -import com.google.instrumentation.stats.ViewDescriptor.IntervalViewDescriptor; - -import java.util.Arrays; -import java.util.List; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Tests for class {@link View}. - */ -@RunWith(JUnit4.class) -public final class ViewTest { - @Test - public void testDistributionView() { - DistributionAggregationDescriptor aggregationDescriptor = - DistributionAggregationDescriptor.create(Arrays.asList(10.0, 20.0, 30.0, 40.0)); - final DistributionViewDescriptor viewDescriptor = - DistributionViewDescriptor.create( - name, description, measurementDescriptor, aggregationDescriptor, tagKeys); - List aggregations = Arrays.asList( - DistributionAggregation.create(5, 5.0, 15.0, Range.create(1.0, 5.0), tags1, - Arrays.asList(1L, 1L, 1L, 1L, 1L)), - DistributionAggregation.create(10, 5.0, 30.0, Range.create(1.0, 5.0), tags2, - Arrays.asList(2L, 2L, 2L, 2L, 2L))); - final Timestamp start = Timestamp.fromMillis(1000); - final Timestamp end = Timestamp.fromMillis(2000); - final View view = DistributionView.create(viewDescriptor, aggregations, start, end); - - assertThat(view.getViewDescriptor()).isEqualTo(viewDescriptor); - assertTrue(view.match( - new Function () { - @Override public Boolean apply(DistributionView dView) { - return dView == view - && dView.getViewDescriptor().equals(viewDescriptor) - && shallowListEquals(dView.getDistributionAggregations(), aggregations) - && dView.getStart().equals(start) - && dView.getEnd().equals(end); - } - }, - new Function () { - @Override public Boolean apply(IntervalView iView) { - return false; - } - })); - } - - @Test - public void testIntervalView() { - IntervalAggregationDescriptor aggregationDescriptor = - IntervalAggregationDescriptor.create(Arrays.asList(Duration.fromMillis(111))); - final IntervalViewDescriptor viewDescriptor = - IntervalViewDescriptor.create( - name, description, measurementDescriptor, aggregationDescriptor, tagKeys); - final List aggregations = Arrays.asList( - IntervalAggregation.create(tags1, Arrays.asList( - Interval.create(Duration.fromMillis(111), 10, 100))), - IntervalAggregation.create(tags2, Arrays.asList( - Interval.create(Duration.fromMillis(111), 10, 100)))); - - final View view = IntervalView.create(viewDescriptor, aggregations); - assertThat(view.getViewDescriptor()).isEqualTo(viewDescriptor); - assertTrue(view.match( - new Function () { - @Override public Boolean apply(DistributionView dView) { - return false; - } - }, - new Function () { - @Override public Boolean apply(IntervalView iView) { - return iView == view - && iView.getViewDescriptor().equals(viewDescriptor) - && shallowListEquals(iView.getIntervalAggregations(), aggregations); - } - })); - } - - // tag keys - private static final TagKey K1 = TagKey.create("k1"); - private static final TagKey K2 = TagKey.create("k2"); - private final List tagKeys = Arrays.asList(K1, K2); - - // tag values - private static final TagValue V1 = TagValue.create("v1"); - private static final TagValue V2 = TagValue.create("v2"); - private static final TagValue V10 = TagValue.create("v10"); - private static final TagValue V20 = TagValue.create("v20"); - - // tags - List tags1 = Arrays.asList(Tag.create(K1, V1), Tag.create(K2, V2)); - List tags2 = Arrays.asList(Tag.create(K1, V10), Tag.create(K2, V20)); - - // name - private final String name = "test-view-descriptor"; - // description - private final String description = "test-view-descriptor description"; - // measurement descriptor - private final MeasurementDescriptor measurementDescriptor = MeasurementDescriptor.create( - "measurement-descriptor", - "measurement-descriptor description", - MeasurementUnit.create(1, Arrays.asList(BasicUnit.SCALAR))); - - private static final boolean shallowListEquals(List l1, List l2) { - if (l1.size() != l2.size()) { - return false; - } - for (int i = 0; i < l1.size(); i++) { - if (l1.get(i) != l2.get(i)) { - return false; - } - } - return true; - } -} diff --git a/core/pom.xml b/core/pom.xml index 7402ef01..bc29d6f9 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -21,7 +21,6 @@ - java maven-compiler-plugin @@ -30,6 +29,18 @@ 1.6 1.6 + + + default-testCompile + test-compile + + testCompile + + + true + + + maven-jar-plugin diff --git a/core/src/main/java/com/google/instrumentation/common/Duration.java b/core/src/main/java/com/google/instrumentation/common/Duration.java new file mode 100644 index 00000000..657575da --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/common/Duration.java @@ -0,0 +1,112 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.common; + +/** + * Represents a signed, fixed-length span of time represented as a count of seconds and fractions + * of seconds at nanosecond resolution. It is independent of any calendar and concepts like "day" + * or "month". Range is approximately +-10,000 years. + */ +public class Duration { + /** + * Creates a new time duration from given seconds and nanoseconds. + * + * @param seconds Signed seconds of the span of time. Must be from -315,576,000,000 + * to +315,576,000,000 inclusive. + * + * @param nanos Signed fractions of a second at nanosecond resolution of the span + * of time. Durations less than one second are represented with a 0 + * `seconds` field and a positive or negative `nanos` field. For durations + * of one second or more, a non-zero value for the `nanos` field must be + * of the same sign as the `seconds` field. Must be from -999,999,999 + * to +999,999,999 inclusive. + * + * @return new {@link Duration} with specified fields. For invalid inputs, a {@link Duration} of + * zero is returned. + */ + public static Duration create(long seconds, int nanos) { + if (seconds < -MAX_SECONDS || seconds > MAX_SECONDS) { + return new Duration(0, 0); + } + if (nanos < -MAX_NANOS || nanos > MAX_NANOS) { + return new Duration(0, 0); + } + if ((seconds < 0 && nanos > 0) || (seconds > 0 && nanos < 0)) { + return new Duration(0, 0); + } + return new Duration(seconds, nanos); + } + + /** + * Creates a new {@link Duration} from given milliseconds. + */ + public static Duration fromMillis(long millis) { + long seconds = millis / NUM_MILLIS_PER_SECOND; + int nanos = (int) (millis % NUM_MILLIS_PER_SECOND) * NUM_NANOS_PER_MILLI; + return new Duration(seconds, nanos); + } + + /** + * Returns the number of seconds in the {@link Duration}. + */ + public long getSeconds() { + return seconds; + } + + /** + * Returns the number of nanoseconds in the {@link Duration}. + */ + public int getNanos() { + return nanos; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + + if (!(obj instanceof Duration)) { + return false; + } + + Duration that = (Duration) obj; + return seconds == that.seconds && nanos == that.nanos; + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + (int) (seconds ^ (seconds >>> 32)); + result = 31 * result + nanos; + return result; + } + + @Override + public String toString() { + return "Duration<" + seconds + "," + nanos + ">"; + } + + private static final long MAX_SECONDS = 315576000000L; + private static final int MAX_NANOS = 999999999; + private static final long NUM_MILLIS_PER_SECOND = 1000L; + private static final int NUM_NANOS_PER_MILLI = 1000000; + private final long seconds; + private final int nanos; + + private Duration(long seconds, int nanos) { + this.seconds = seconds; + this.nanos = nanos; + } +} diff --git a/core/src/main/java/com/google/instrumentation/common/Function.java b/core/src/main/java/com/google/instrumentation/common/Function.java new file mode 100644 index 00000000..16471a55 --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/common/Function.java @@ -0,0 +1,25 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.common; + +/** + * Used to specify matching functions for use encoding tagged unions (i.e. sum types) in Java. See + * {@link com.google.instrumentation.stats.ViewDescriptor} for an example of it's use. + * + *

Note: This class is based on the java.util.Function class added in Java 1.8. We cannot use + * the Function from Java 1.8 because this library is Java 1.6 compatible. + */ +public interface Function { + B apply(A arg); +} diff --git a/core/src/main/java/com/google/instrumentation/common/NonThrowingCloseable.java b/core/src/main/java/com/google/instrumentation/common/NonThrowingCloseable.java new file mode 100644 index 00000000..46ac71a5 --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/common/NonThrowingCloseable.java @@ -0,0 +1,22 @@ +package com.google.instrumentation.common; + +import java.io.Closeable; + +/** + * An {@link Closeable} which cannot throw a checked exception. + * + *

This is useful because such a reversion otherwise requires the caller to catch the + * (impossible) Exception in the try-with-resources. + * + *

Example of usage: + * + *

+ *   try (NonThrowingAutoCloseable ctx = tryEnter()) {
+ *     ...
+ *   }
+ * 
+ */ +public interface NonThrowingCloseable extends Closeable { + @Override + void close(); +} diff --git a/core/src/main/java/com/google/instrumentation/common/Provider.java b/core/src/main/java/com/google/instrumentation/common/Provider.java new file mode 100644 index 00000000..d32fbad9 --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/common/Provider.java @@ -0,0 +1,55 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.common; + +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.annotation.Nullable; + +/** + * Instrumentation specific service provider mechanism. + * + *
{@code
+ * // Initialize a static variable using reflection.
+ * static final Foo foo = Provider.newInstance("Foo", new NoopFoo());
+ * }
+ */ +public final class Provider { + private static final Logger logger = Logger.getLogger(Provider.class.getName()); + + /** + * Returns a new instance of the class specified with {@code name} by invoking the empty-argument + * constructor via reflections. If the specified class is not found, the {@code defaultValue} is + * returned. + */ + @SuppressWarnings("unchecked") + @Nullable + public static T newInstance(String name, @Nullable T defaultValue) { + try { + Class provider = Class.forName(name); + T result = (T) provider.getConstructor().newInstance(); + logger.fine("Loaded: " + name); + return result; + } catch (ClassNotFoundException e) { + logger.log(Level.FINE, "Falling back to " + defaultValue, e); + return defaultValue; + } catch (Exception e) { + if (e instanceof RuntimeException) { + throw (RuntimeException) e; + } else { + throw new RuntimeException(e); + } + } + } +} diff --git a/core/src/main/java/com/google/instrumentation/common/Timestamp.java b/core/src/main/java/com/google/instrumentation/common/Timestamp.java new file mode 100644 index 00000000..6fad30f6 --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/common/Timestamp.java @@ -0,0 +1,115 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.common; + +/** + * A representation of an instant in time. The instant is the number of nanoseconds after the number + * of seconds since the Unix Epoch. + */ +public class Timestamp { + /** + * Creates a new timestamp from given seconds and nanoseconds. + * + * @param seconds Represents seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z. Must + * be from from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive. + * + * @param nanos Non-negative fractions of a second at nanosecond resolution. Negative + * second values with fractions must still have non-negative nanos values that count forward + * in time. Must be from 0 to 999,999,999 inclusive. + * + * @return new {@link Timestamp} with specified fields. For invalid inputs, a {@link Timestamp} + * of zero is returned. + */ + public static Timestamp create(long seconds, int nanos) { + if (seconds < -MAX_SECONDS || seconds > MAX_SECONDS) { + return new Timestamp(0, 0); + } + if (nanos < 0 || nanos > MAX_NANOS) { + return new Timestamp(0, 0); + } + return new Timestamp(seconds, nanos); + } + + /** + * Creates a new timestamp from given milliseconds. + */ + public static Timestamp fromMillis(long millis) { + long seconds = millis / NUM_MILLIS_PER_SECOND; + int nanos = (int) (millis % NUM_MILLIS_PER_SECOND) * NUM_NANOS_PER_MILLI; + if (nanos < 0) { + return new Timestamp(seconds - 1, MAX_NANOS + nanos + 1); + } else { + return new Timestamp(seconds, nanos); + } + } + + /** + * Returns the number of seconds since the Unix Epoch represented by this timestamp. + * + * @return the number of seconds since the Unix Epoch. + */ + public long getSeconds() { + return seconds; + } + + /** + * Returns the number of nanoseconds after the number of seconds since the Unix Epoch represented + * by this timestamp. + * + * @return the number of nanoseconds after the number of seconds since the Unix Epoch. + */ + public int getNanos() { + return nanos; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + + if (!(obj instanceof Timestamp)) { + return false; + } + + Timestamp that = (Timestamp) obj; + return seconds == that.seconds && nanos == that.nanos; + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + (int) (seconds ^ (seconds >>> 32)); + result = 31 * result + nanos; + return result; + } + + @Override + public String toString() { + return "Timestamp<" + seconds + "," + nanos + ">"; + } + + private static final long MAX_SECONDS = 315576000000L; + private static final int MAX_NANOS = 999999999; + private static final long NUM_MILLIS_PER_SECOND = 1000L; + private static final int NUM_NANOS_PER_MILLI = 1000000; + private final long seconds; + private final int nanos; + + private Timestamp(long seconds, int nanos) { + this.seconds = seconds; + this.nanos = nanos; + } + +} diff --git a/core/src/main/java/com/google/instrumentation/common/TimestampFactory.java b/core/src/main/java/com/google/instrumentation/common/TimestampFactory.java new file mode 100644 index 00000000..2ae9d4e3 --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/common/TimestampFactory.java @@ -0,0 +1,28 @@ +/* + * Copyright 2017, Google Inc. + * 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 com.google.instrumentation.common; + +/** + * An interface for a factory that generates timestamps. + * + * @see Timestamp + */ +public interface TimestampFactory { + /** + * Returns a new timestamp that represents the instant this method was called. + * + * @return the new timestamp. + */ + Timestamp now(); +} diff --git a/core/src/main/java/com/google/instrumentation/stats/DistributionAggregation.java b/core/src/main/java/com/google/instrumentation/stats/DistributionAggregation.java new file mode 100644 index 00000000..47f19d5d --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/stats/DistributionAggregation.java @@ -0,0 +1,166 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import javax.annotation.Nullable; + +/** + * An aggregation of data based on distributions. + * + *

A distribution contains summary statistics for a population of values and, optionally, a + * histogram representing the distribution of those values across a specified set of histogram + * buckets, as defined in {@link DistributionAggregationDescriptor#getBucketBoundaries()}. + * + *

Although not forbidden, it is generally a bad idea to include non-finite values (infinities + * or NaNs) in the population of values, as this will render the {@code mean} meaningless. + */ +public final class DistributionAggregation { + /** + * Constructs a new {@link DistributionAggregation}. + */ + public static final DistributionAggregation create( + long count, double mean, double sum, Range range, List tags) { + return new DistributionAggregation(count, mean, sum, range, tags, null); + } + + /** + * Constructs a new {@link DistributionAggregation} with the optional {@code bucketCount}s. + */ + public static final DistributionAggregation create( + long count, double mean, double sum, Range range, List tags, List bucketCounts) { + return new DistributionAggregation(count, mean, sum, range, tags, + Collections.unmodifiableList(new ArrayList(bucketCounts))); + } + + /** + * {@link Tag}s associated with this {@link DistributionAggregation}. + * + *

Note: The returned list is unmodifiable, attempts to update it will throw an + * UnsupportedOperationException. + */ + public final List getTags() { + return tags; + } + + /** + * The number of values in the population. Must be non-negative. + */ + public long getCount() { + return count; + } + + /** + * The arithmetic mean of the values in the population. If {@link #getCount()} is zero then this + * value must also be zero. + */ + public double getMean() { + return mean; + } + + /** + * The sum of the values in the population. If {@link #getCount()} is zero then this values must + * also be zero. + */ + public double getSum() { + return sum; + } + + /** + * The range of the population values. If {@link #getCount()} is zero then this returned range is + * implementation-dependent. + */ + public Range getRange() { + return range; + } + + /** + * A Distribution may optionally contain a histogram of the values in the population. The + * histogram is given in {@link #getBucketCounts()} as counts of values that fall into one of a + * sequence of non-overlapping buckets, described by + * {@link DistributionAggregationDescriptor#getBucketBoundaries()}. + * The sum of the values in {@link #getBucketCounts()} must equal the value in + * {@link #getCount()}. + * + *

Bucket counts are given in order under the numbering scheme described + * above (the underflow bucket has number 0; the finite buckets, if any, + * have numbers 1 through N-2; the overflow bucket has number N-1). + * + *

The size of {@link #getBucketCounts()} must be no greater than N as defined in + * {@link DistributionAggregationDescriptor#getBucketBoundaries()}. + * + *

Any suffix of trailing buckets containing only zero may be omitted. + * + *

{@link #getBucketCounts()} will return null iff the associated + * {@link DistributionAggregationDescriptor#getBucketBoundaries()} returns null. + */ + @Nullable + public List getBucketCounts() { + return bucketCounts; + } + + private final long count; + private final double mean; + private final double sum; + private final Range range; + private final List tags; + private final List bucketCounts; + + private DistributionAggregation( + long count, double mean, double sum, Range range, List tags, + @Nullable List bucketCounts) { + this.count = count; + this.mean = mean; + this.sum = sum; + this.range = range; + this.tags = tags; + this.bucketCounts = bucketCounts; + } + + /** + * Describes a range of population values. + */ + public static final class Range { + /** + * Constructs a new {@link Range}. + */ + public static final Range create(double min, double max) { + return new Range(min, max); + } + + /** + * The minimum of the population values. + */ + public double getMin() { + return min; + } + + /** + * The maximum of the population values. + */ + public double getMax() { + return max; + } + + private double min; + private double max; + + private Range(double min, double max) { + this.min = min; + this.max = max; + } + } +} diff --git a/core/src/main/java/com/google/instrumentation/stats/DistributionAggregationDescriptor.java b/core/src/main/java/com/google/instrumentation/stats/DistributionAggregationDescriptor.java new file mode 100644 index 00000000..586124ad --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/stats/DistributionAggregationDescriptor.java @@ -0,0 +1,74 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import javax.annotation.Nullable; + +/** + * Describes data aggregations based on distributions. + * + *

A distribution aggregation may optionally contain a histogram of the values in the + * population. The bucket boundaries for that histogram are described by + * {@link getBucketBoundaries}, which defines {@code getBucketBoundaries.size() + 1 (= N)} + * buckets. The boundaries for bucket index i are: + *

    + *
  • [-infinity, bounds[i]) for i == 0 + *
  • [bounds[i-1], bounds[i]) for 0 < i < N-2 + *
  • [bounds[i-1], +infinity) for i == N-1 + *
+ * i.e. an underflow bucket (number 0), zero or more finite buckets (1 through N - 2, and an + * overflow bucket (N - 1), with inclusive lower bounds and exclusive upper bounds. + * + *

Note: If N = 1, there are no finite buckets and the single bucket is both the overflow + * and underflow bucket. + */ +public final class DistributionAggregationDescriptor { + /** + * Constructs a new {@link DistributionAggregationDescriptor} with the optional + * histogram bucket boundaries. + */ + public static DistributionAggregationDescriptor create(List bucketBoundaries) { + return new DistributionAggregationDescriptor( + Collections.unmodifiableList(new ArrayList(bucketBoundaries))); + } + + /** + * Constructs a new {@link DistributionAggregationDescriptor} without the optional + * histogram bucket boundaries. + */ + public static DistributionAggregationDescriptor create() { + return new DistributionAggregationDescriptor(null); + } + + /** + * The optional histogram bucket boundaries for a distribution. + * + *

Note: The returned list is unmodifiable, attempts to update it will throw an + * UnsupportedOperationException. + */ + @Nullable + public List getBucketBoundaries() { + return bucketBoundaries; + } + + @Nullable + private final List bucketBoundaries; + + private DistributionAggregationDescriptor(@Nullable List bucketBoundaries) { + this.bucketBoundaries = bucketBoundaries; + } +} diff --git a/core/src/main/java/com/google/instrumentation/stats/IntervalAggregation.java b/core/src/main/java/com/google/instrumentation/stats/IntervalAggregation.java new file mode 100644 index 00000000..9bd26923 --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/stats/IntervalAggregation.java @@ -0,0 +1,103 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import com.google.instrumentation.common.Duration; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * Contains summary stats over various time intervals. + */ +public final class IntervalAggregation { + /** + * Constructs new {@link IntervalAggregation}. + * TODO(dpo): Determine what we should do it intervals is empty. + */ + public static final IntervalAggregation create(List tags, List intervals) { + return new IntervalAggregation(tags, intervals); + } + + /** + * {@link Tag}s associated with this aggregation. + * + *

Note: The returned list is unmodifiable, attempts to update it will throw an + * UnsupportedOperationException. + */ + public final List getTags() { + return tags; + } + + /** + * Sequence of intervals for this aggregation. + */ + public List getIntervals() { + return intervals; + } + + private final List tags; + private final List intervals; + + private IntervalAggregation(List tags, List intervals) { + this.tags = tags; + this.intervals = Collections.unmodifiableList(new ArrayList(intervals)); + } + + /** + * Summary statistic over a single time interval. + */ + public static final class Interval { + /** + * Constructs a new {@link Interval}. + * + *

Note: {@code intervalSize} must be positive otherwise behavior is unspecified. + */ + public static Interval create(Duration intervalSize, double count, double sum) { + return new Interval(intervalSize, count, sum); + } + + /** + * The interval duration. + */ + public Duration getIntervalSize() { + return intervalSize; + } + + /** + * The number of measurements in this interval. + */ + public double getCount() { + return count; + } + + /** + * The cumulative sum of measurements in this interval. + */ + public double getSum() { + return sum; + } + + private final Duration intervalSize; + private final double count; + private final double sum; + + private Interval(Duration intervalSize, double count, double sum) { + this.intervalSize = intervalSize; + this.count = count; + this.sum = sum; + } + } +} diff --git a/core/src/main/java/com/google/instrumentation/stats/IntervalAggregationDescriptor.java b/core/src/main/java/com/google/instrumentation/stats/IntervalAggregationDescriptor.java new file mode 100644 index 00000000..b6c2dbe6 --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/stats/IntervalAggregationDescriptor.java @@ -0,0 +1,86 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import com.google.instrumentation.common.Duration; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * Describes data aggregations based on time intervals. + */ +public final class IntervalAggregationDescriptor { + /** + * Constructs a new {@link IntervalAggregationDescriptor}. + * + *

The given {@code numSubIntervals} must be in the range [2, 20], see + * {@link #getNumSubIntervals()} for more details. + * + *

The given {@code intervalSizes} must have at least one entry. + */ + public static IntervalAggregationDescriptor create( + int numSubIntervals, List intervalSizes) { + if (numSubIntervals < 2 || numSubIntervals > 20) { + throw new IllegalArgumentException( + "The number of subintervals must be in the range [2, 20]."); + } + if (intervalSizes.isEmpty()) { + throw new IllegalArgumentException("There must be at least one interval size."); + } + return new IntervalAggregationDescriptor( + numSubIntervals, + Collections.unmodifiableList(new ArrayList(intervalSizes))); + } + + /** + * Constructs a new {@link IntervalAggregationDescriptor} with the number of sub intervals set + * to the default value of 5. + */ + public static IntervalAggregationDescriptor create(List intervalSizes) { + return create(5, intervalSizes); + } + + /** + * The number of sub intervals. + * + *

The number of internal sub-intervals to use when collecting stats for each interval. The + * max error in interval measurements will be approximately 1/getNumSubIntervals() + * (although in practice, this will only be approached in the presence of very large and bursty + * workload changes), and underlying memory usage will be roughly proportional to the value of + * this field. Must be in the range [2, 20]. A value of 5 will be used if this is unspecified. + */ + public int getNumSubIntervals() { + return numSubIntervals; + } + + /** + * The time intervals to record for the aggregation. + * + *

Note: The returned list is unmodifiable, attempts to update it will throw an + * UnsupportedOperationException. + */ + public List getIntervalSizes() { + return intervalSizes; + } + + private final int numSubIntervals; + private final List intervalSizes; + + private IntervalAggregationDescriptor(int numSubIntervals, List intervalSizes) { + this.numSubIntervals = numSubIntervals; + this.intervalSizes = intervalSizes; + } +} diff --git a/core/src/main/java/com/google/instrumentation/stats/MeasurementDescriptor.java b/core/src/main/java/com/google/instrumentation/stats/MeasurementDescriptor.java new file mode 100644 index 00000000..d44c3637 --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/stats/MeasurementDescriptor.java @@ -0,0 +1,161 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * MeasurementDescriptor. + * + *

Note: MeasurementDescriptor names are {@link String}s with enforced restrictions. + */ +public final class MeasurementDescriptor { + public static final int MAX_LENGTH = StringUtil.MAX_LENGTH; + + /** + * Constructs a new {@link MeasurementDescriptor}. + */ + public static MeasurementDescriptor create( + String name, String description, MeasurementUnit unit) { + return new MeasurementDescriptor(name, description, unit); + } + + /** + * Name of measurement, e.g. rpc_latency, cpu. Must be unique. + */ + public String getName() { + return name; + } + + /** + * Detailed description of the measurement, used in documentation. + */ + public String getDescription() { + return description; + } + + /** + * The units in which {@link MeasurementDescriptor} values are measured. + */ + public MeasurementUnit getUnit() { + return unit; + } + + private final String name; + private final String description; + private final MeasurementUnit unit; + + private MeasurementDescriptor(String name, String description, MeasurementUnit unit) { + this.name = StringUtil.sanitize(name); + this.description = description; + this.unit = unit; + } + + /** + * Fundamental units of measurement. + */ + public enum BasicUnit { + SCALAR, + BITS, + BYTES, + SECONDS, + CORES; + } + + /** + * MeasurementUnit lets you build compound units of the form + * 10^n * (A * B * ...) / (X * Y * ...), + * where the elements in the numerator and denominator are all BasicUnits. A + * MeasurementUnit must have at least one BasicUnit in its numerator. + * + *

To specify multiplication in the numerator or denominator, simply specify + * multiple numerator or denominator fields. For example: + * + *

- byte-seconds (i.e. bytes * seconds): + * numerator: BYTES + * numerator: SECS + * + *

- events/sec^2 (i.e. rate of change of events/sec): + * numerator: SCALAR + * denominator: SECS + * denominator: SECS + * + *

To specify multiples (in power of 10) of units, specify a non-zero power10 + * value, for example: + * + *

- MB/s (i.e. megabytes / s): + * power10: 6 + * numerator: BYTES + * denominator: SECS + * + *

- nanoseconds + * power10: -9 + * numerator: SECS + */ + public static final class MeasurementUnit { + /** + * Constructs a {@link MeasurementUnit}. + */ + public static MeasurementUnit create( + int power10, List numerators, List denominators) { + return new MeasurementUnit(power10, numerators, denominators); + } + + /** + * Constructs a {@link MeasurementUnit} without the optional {@code denominators}. + */ + public static MeasurementUnit create(int power10, List numerators) { + return new MeasurementUnit(power10, numerators, new ArrayList()); + } + + /** + * Unit multiplier (i.e. 10^power10). + */ + public int getPower10() { + return power10; + } + + /** + * Unit Numerators. + * + *

Note: The returned list is unmodifiable and attempts to update it will throw an + * UnsupportedOperationException. + */ + public List getNumerators() { + return numerators; + } + + /** + * Unit Denominators. + * + *

Note: The returned list is unmodifiable and attempts to update it will throw an + * UnsupportedOperationException. + */ + public final List getDenominators() { + return denominators; + } + + private final int power10; + private final List numerators; + private final List denominators; + + private MeasurementUnit(int power10, List numerators, List denominators) { + this.power10 = power10; + this.numerators = Collections.unmodifiableList(new ArrayList(numerators)); + this.denominators = Collections.unmodifiableList(new ArrayList(denominators)); + } + } +} diff --git a/core/src/main/java/com/google/instrumentation/stats/MeasurementMap.java b/core/src/main/java/com/google/instrumentation/stats/MeasurementMap.java new file mode 100644 index 00000000..65a55aa6 --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/stats/MeasurementMap.java @@ -0,0 +1,146 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.NoSuchElementException; + +/** + * A map from {@link MeasurementDescriptor}'s to measured values. + */ +public final class MeasurementMap implements Iterable { + /** + * Constructs a {@link MeasurementMap} from the given {@link MeasurementDescriptor} + * and associated value. + */ + public static MeasurementMap of(MeasurementDescriptor measurement, double value) { + return builder().put(measurement, value).build(); + } + + /** + * Constructs a {@link MeasurementMap} from the given {@link MeasurementDescriptor}'s + * and associated values. + */ + public static MeasurementMap of(MeasurementDescriptor measurement1, double value1, + MeasurementDescriptor measurement2, double value2) { + return builder().put(measurement1, value1).put(measurement2, value2).build(); + } + + /** + * Constructs a {@link MeasurementMap} from the given {@link MeasurementDescriptor}'s + * and associated values. + */ + public static MeasurementMap of(MeasurementDescriptor measurement1, double value1, + MeasurementDescriptor measurement2, double value2, + MeasurementDescriptor measurement3, double value3) { + return builder().put(measurement1, value1).put(measurement2, value2).put(measurement3, value3) + .build(); + } + + /** + * Returns a {@link Builder} for the {@link MeasurementMap} class. + */ + public static Builder builder() { + return new Builder(); + } + + /** + * Returns the number of measurements in this {@link MeasurementMap}. + */ + public int size() { + return measurements.size(); + } + + /** + * Returns an {@link Iterator} over the measurement/value mappings in this {@link MeasurementMap}. + * The {@code Iterator} does not support {@link Iterator#remove()}. + */ + @Override + public Iterator iterator() { + return new MeasurementMapIterator(); + } + + private final ArrayList measurements; + + private MeasurementMap(ArrayList measurements) { + this.measurements = measurements; + } + + /** + * Builder for the {@link MeasurementMap} class. + */ + public static class Builder { + /** + * Associates the {@link MeasurementDescriptor} with the given value. Subsequent updates to the + * same {@link MeasurementDescriptor} are ignored. + * + * @param measurement the {@link MeasurementDescriptor} + * @param value the value to be associated with {@code measurement} + * @return this + */ + public Builder put(MeasurementDescriptor measurement, double value) { + measurements.add(MeasurementValue.create(measurement, value)); + return this; + } + + /** + * Constructs a {@link MeasurementMap} from the current measurements. + */ + public MeasurementMap build() { + // Note: this makes adding measurements quadratic but is fastest for the sizes of + // MeasurementMaps that we should see. We may want to go to a strategy of sort/eliminate + // for larger MeasurementMaps. + for (int i = 0; i < measurements.size(); i++) { + String current = measurements.get(i).getMeasurement().getName(); + for (int j = i + 1; j < measurements.size(); j++) { + if (current.equals(measurements.get(j).getMeasurement().getName())) { + measurements.remove(j); + j--; + } + } + } + return new MeasurementMap(measurements); + } + + private final ArrayList measurements = new ArrayList(); + + private Builder() { + } + } + + // Provides an unmodifiable Iterator over this instance's measurements. + private final class MeasurementMapIterator implements Iterator { + @Override + public boolean hasNext() { + return position < length; + } + + @Override + public MeasurementValue next() { + if (position >= measurements.size()) { + throw new NoSuchElementException(); + } + return measurements.get(position++); + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + + private final int length = measurements.size(); + private int position = 0; + } +} diff --git a/core/src/main/java/com/google/instrumentation/stats/MeasurementValue.java b/core/src/main/java/com/google/instrumentation/stats/MeasurementValue.java new file mode 100644 index 00000000..3502b97b --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/stats/MeasurementValue.java @@ -0,0 +1,49 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +/** + * Immutable representation of a MeasurementValue. + */ +public class MeasurementValue { + + /** + * Constructs a measured value. + */ + public static MeasurementValue create(MeasurementDescriptor name, double value) { + return new MeasurementValue(name, value); + } + + /** + * Extracts the measured {@link MeasurementDescriptor}. + */ + public MeasurementDescriptor getMeasurement() { + return name; + } + + /** + * Extracts the associated value. + */ + public double getValue() { + return value; + } + + private final MeasurementDescriptor name; + private final double value; + + private MeasurementValue(MeasurementDescriptor name, double value) { + this.name = name; + this.value = value; + } +} diff --git a/core/src/main/java/com/google/instrumentation/stats/RpcConstants.java b/core/src/main/java/com/google/instrumentation/stats/RpcConstants.java new file mode 100644 index 00000000..b9e09586 --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/stats/RpcConstants.java @@ -0,0 +1,361 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import com.google.instrumentation.common.Duration; +import com.google.instrumentation.stats.MeasurementDescriptor.BasicUnit; +import com.google.instrumentation.stats.MeasurementDescriptor.MeasurementUnit; +import com.google.instrumentation.stats.ViewDescriptor.DistributionViewDescriptor; +import com.google.instrumentation.stats.ViewDescriptor.IntervalViewDescriptor; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** + * Constants for collecting rpc stats. + */ +public final class RpcConstants { + // Rpc tag keys. + public static final TagKey RPC_STATUS = TagKey.create("OpStatus"); + public static final TagKey RPC_CLIENT_METHOD = TagKey.create("method"); + public static final TagKey RPC_SERVER_METHOD = TagKey.create("method"); + + // Constants used to define the following MeasurementDescriptors. + private static final List bytes = Arrays.asList(BasicUnit.BYTES); + private static final List scalar = Arrays.asList(BasicUnit.SCALAR); + private static final List seconds = Arrays.asList(BasicUnit.SECONDS); + + // Census defined rpc client {@link MeasurementDescriptor}s. + public static final MeasurementDescriptor RPC_CLIENT_ERROR_COUNT = + MeasurementDescriptor.create( + "/rpc/client/error_count", + "RPC Errors", + MeasurementUnit.create(0, scalar)); + public static final MeasurementDescriptor RPC_CLIENT_REQUEST_BYTES = + MeasurementDescriptor.create( + "/rpc/client/request_bytes", + "Request MB", + MeasurementUnit.create(6, bytes)); + public static final MeasurementDescriptor RPC_CLIENT_RESPONSE_BYTES = + MeasurementDescriptor.create( + "/rpc/client/response_bytes", + "Response MB", + MeasurementUnit.create(6, bytes)); + // TODO(dpo): verify this encoding + public static final MeasurementDescriptor RPC_CLIENT_ROUNDTRIP_LATENCY = + MeasurementDescriptor.create( + "/rpc/client/roundtrip_latency", + "RPC roundtrip latency us", + MeasurementUnit.create(-6, seconds)); + public static final MeasurementDescriptor RPC_CLIENT_SERVER_ELAPSED_TIME = + MeasurementDescriptor.create( + "/rpc/client/server_elapsed_time", + "Server elapsed time in msecs", + MeasurementUnit.create(-3, seconds)); + public static final MeasurementDescriptor RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES = + MeasurementDescriptor.create( + "/rpc/client/uncompressed_request_bytes", + "Uncompressed Request MB", + MeasurementUnit.create(6, bytes)); + public static final MeasurementDescriptor RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES = + MeasurementDescriptor.create( + "/rpc/client/uncompressed_response_bytes", + "Uncompressed Response MB", + MeasurementUnit.create(6, bytes)); + + // Census defined rpc server {@link MeasurementDescriptor}s. + public static final MeasurementDescriptor RPC_SERVER_ERROR_COUNT = + MeasurementDescriptor.create( + "/rpc/server/error_count", + "RPC Errors", + MeasurementUnit.create(0, scalar)); + public static final MeasurementDescriptor RPC_SERVER_REQUEST_BYTES = + MeasurementDescriptor.create( + "/rpc/server/request_bytes", + "Request MB", + MeasurementUnit.create(6, bytes)); + public static final MeasurementDescriptor RPC_SERVER_RESPONSE_BYTES = + MeasurementDescriptor.create( + "/rpc/server/response_bytes", + "Response MB", + MeasurementUnit.create(6, bytes)); + public static final MeasurementDescriptor RPC_SERVER_SERVER_ELAPSED_TIME = + MeasurementDescriptor.create( + "/rpc/server/server_elapsed_time", + "Server elapsed time in msecs", + MeasurementUnit.create(-3, seconds)); + public static final MeasurementDescriptor RPC_SERVER_SERVER_LATENCY = + MeasurementDescriptor.create( + "/rpc/server/server_latency", + "Latency in msecs", + MeasurementUnit.create(-3, seconds)); + public static final MeasurementDescriptor RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES = + MeasurementDescriptor.create( + "/rpc/server/uncompressed_request_bytes", + "Uncompressed Request MB", + MeasurementUnit.create(6, bytes)); + public static final MeasurementDescriptor RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES = + MeasurementDescriptor.create( + "/rpc/server/uncompressed_response_bytes", + "Uncompressed Response MB", + MeasurementUnit.create(6, bytes)); + public static final MeasurementDescriptor RPC_SERVER_STARTED_COUNT = + MeasurementDescriptor.create( + "/rpc/server/started_count", + "Number of RPCs started", + MeasurementUnit.create(0, scalar)); + public static final MeasurementDescriptor RPC_SERVER_FINISHED_COUNT = + MeasurementDescriptor.create( + "/rpc/server/finished_count", + "Number of RPCs finished", + MeasurementUnit.create(0, scalar)); + + // Common histogram bucket boundaries for bytes received/sets DistributionViewDescriptors. + static final List RPC_BYTES_BUCKET_BOUNDARIES = Collections.unmodifiableList( + Arrays.asList(0.0, 1024.0, 2048.0, 4096.0, 16384.0, 65536.0, 262144.0, 1048576.0, 4194304.0, + 16777216.0, 67108864.0, 268435456.0, 1073741824.0, 4294967296.0)); + + // Common histogram bucket boundaries for latency and elapsed-time DistributionViewDescriptors. + static final List RPC_MILLIS_BUCKET_BOUNDARIES = Collections.unmodifiableList( + Arrays.asList(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 8.0, 10.0, 13.0, 16.0, 20.0, 25.0, 30.0, + 40.0, 50.0, 65.0, 80.0, 100.0, 130.0, 160.0, 200.0, 250.0, 300.0, 400.0, 500.0, 650.0, + 800.0, 1000.0, 2000.0, 5000.0, 10000.0, 20000.0, 50000.0, 100000.0)); + + // Census defined rpc client {@link ViewDescriptor}s. + public static final DistributionViewDescriptor RPC_CLIENT_ERROR_COUNT_VIEW = + DistributionViewDescriptor.create( + "rpc client error_count", + "RPC Errors", + RPC_CLIENT_ERROR_COUNT, + DistributionAggregationDescriptor.create(), + Arrays.asList(RPC_STATUS, RPC_CLIENT_METHOD)); + public static final DistributionViewDescriptor RPC_CLIENT_ROUNDTRIP_LATENCY_VIEW = + DistributionViewDescriptor.create( + "rpc client roundtrip_latency", + "Latency in msecs", + RPC_CLIENT_ROUNDTRIP_LATENCY, + DistributionAggregationDescriptor.create(RPC_MILLIS_BUCKET_BOUNDARIES), + Arrays.asList(RPC_CLIENT_METHOD)); + public static final DistributionViewDescriptor RPC_CLIENT_SERVER_ELAPSED_TIME_VIEW = + DistributionViewDescriptor.create( + "rpc client server_elapsed_time", + "Server elapsed time in msecs", + RPC_CLIENT_SERVER_ELAPSED_TIME, + DistributionAggregationDescriptor.create(RPC_MILLIS_BUCKET_BOUNDARIES), + Arrays.asList(RPC_CLIENT_METHOD)); + public static final DistributionViewDescriptor RPC_CLIENT_REQUEST_BYTES_VIEW = + DistributionViewDescriptor.create( + "rpc client request_bytes", + "Request MB", + RPC_CLIENT_REQUEST_BYTES, + DistributionAggregationDescriptor.create(RPC_BYTES_BUCKET_BOUNDARIES), + Arrays.asList(RPC_CLIENT_METHOD)); + public static final DistributionViewDescriptor RPC_CLIENT_RESPONSE_BYTES_VIEW = + DistributionViewDescriptor.create( + "rpc client response_bytes", + "Response MB", + RPC_CLIENT_RESPONSE_BYTES, + DistributionAggregationDescriptor.create(RPC_BYTES_BUCKET_BOUNDARIES), + Arrays.asList(RPC_CLIENT_METHOD)); + public static final DistributionViewDescriptor RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES_VIEW = + DistributionViewDescriptor.create( + "rpc client uncompressed_request_bytes", + "Uncompressed Request MB", + RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES, + DistributionAggregationDescriptor.create(RPC_BYTES_BUCKET_BOUNDARIES), + Arrays.asList(RPC_CLIENT_METHOD)); + public static final DistributionViewDescriptor RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES_VIEW = + DistributionViewDescriptor.create( + "rpc client uncompressed_response_bytes", + "Uncompressed Response MB", + RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES, + DistributionAggregationDescriptor.create(RPC_BYTES_BUCKET_BOUNDARIES), + Arrays.asList(RPC_CLIENT_METHOD)); + + + // Census defined rpc server {@link ViewDescriptor}s. + public static final DistributionViewDescriptor RPC_SERVER_ERROR_COUNT_VIEW = + DistributionViewDescriptor.create( + "rpc server error_count", + "RPC Errors", + RPC_SERVER_ERROR_COUNT, + DistributionAggregationDescriptor.create(), + Arrays.asList(RPC_STATUS, RPC_SERVER_METHOD)); + public static final DistributionViewDescriptor RPC_SERVER_SERVER_LATENCY_VIEW = + DistributionViewDescriptor.create( + "rpc server server_latency", + "Latency in msecs", + RPC_SERVER_SERVER_LATENCY, + DistributionAggregationDescriptor.create(RPC_MILLIS_BUCKET_BOUNDARIES), + Arrays.asList(RPC_SERVER_METHOD)); + public static final DistributionViewDescriptor RPC_SERVER_SERVER_ELAPSED_TIME_VIEW = + DistributionViewDescriptor.create( + "rpc server elapsed_time", + "Server elapsed time in msecs", + RPC_SERVER_SERVER_ELAPSED_TIME, + DistributionAggregationDescriptor.create(RPC_MILLIS_BUCKET_BOUNDARIES), + Arrays.asList(RPC_SERVER_METHOD)); + public static final DistributionViewDescriptor RPC_SERVER_REQUEST_BYTES_VIEW = + DistributionViewDescriptor.create( + "rpc server request_bytes", + "Request MB", + RPC_SERVER_REQUEST_BYTES, + DistributionAggregationDescriptor.create(RPC_BYTES_BUCKET_BOUNDARIES), + Arrays.asList(RPC_SERVER_METHOD)); + public static final DistributionViewDescriptor RPC_SERVER_RESPONSE_BYTES_VIEW = + DistributionViewDescriptor.create( + "rpc server response_bytes", + "Response MB", + RPC_SERVER_RESPONSE_BYTES, + DistributionAggregationDescriptor.create(RPC_BYTES_BUCKET_BOUNDARIES), + Arrays.asList(RPC_SERVER_METHOD)); + public static final DistributionViewDescriptor RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES_VIEW = + DistributionViewDescriptor.create( + "rpc server uncompressed_request_bytes", + "Uncompressed Request MB", + RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES, + DistributionAggregationDescriptor.create(RPC_BYTES_BUCKET_BOUNDARIES), + Arrays.asList(RPC_SERVER_METHOD)); + public static final DistributionViewDescriptor RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES_VIEW = + DistributionViewDescriptor.create( + "rpc server uncompressed_response_bytes", + "Uncompressed Response MB", + RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES, + DistributionAggregationDescriptor.create(RPC_BYTES_BUCKET_BOUNDARIES), + Arrays.asList(RPC_SERVER_METHOD)); + + // Interval Stats + static final Duration MINUTE = Duration.create(60, 0); + static final Duration HOUR = Duration.create(60 * 60, 0); + + // Census defined rpc client {@link IntervalViewDescriptor}s. + public static final IntervalViewDescriptor RPC_CLIENT_ROUNDTRIP_LATENCY_INTERVAL_VIEW = + IntervalViewDescriptor.create( + "rpc client roundtrip_latency", + "Minute and Hour stats for latency in msecs", + RPC_CLIENT_ROUNDTRIP_LATENCY, + IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), + Arrays.asList(RPC_CLIENT_METHOD)); + + public static final IntervalViewDescriptor RPC_CLIENT_REQUEST_BYTES_INTERVAL_VIEW = + IntervalViewDescriptor.create( + "rpc client request_bytes", + "Minute and Hour stats for request in MBs", + RPC_CLIENT_REQUEST_BYTES, + IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), + Arrays.asList(RPC_CLIENT_METHOD)); + + public static final IntervalViewDescriptor RPC_CLIENT_RESPONSE_BYTES_INTERVAL_VIEW = + IntervalViewDescriptor.create( + "rpc client response_bytes", + "Minute and Hour stats for response size MBs", + RPC_CLIENT_RESPONSE_BYTES, + IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), + Arrays.asList(RPC_CLIENT_METHOD)); + + public static final IntervalViewDescriptor RPC_CLIENT_ERROR_COUNT_INTERVAL_VIEW = + IntervalViewDescriptor.create( + "rpc client error_count", + "Minute and Hour stats for rpc errors", + RPC_CLIENT_ERROR_COUNT, + IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), + Arrays.asList(RPC_CLIENT_METHOD)); + + public static final IntervalViewDescriptor RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES_INTERVAL_VIEW = + IntervalViewDescriptor.create( + "rpc client uncompressed_request_bytes", + "Minute and Hour stats for uncompressed request size in MB", + RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES, + IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), + Arrays.asList(RPC_CLIENT_METHOD)); + + public static final IntervalViewDescriptor RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES_INTERVAL_VIEW = + IntervalViewDescriptor.create( + "rpc client uncompressed_response_bytes", + "Minute and Hour stats for uncompressed response size in MBs", + RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES, + IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), + Arrays.asList(RPC_CLIENT_METHOD)); + + public static final IntervalViewDescriptor RPC_CLIENT_SERVER_ELAPSED_TIME_INTERVAL_VIEW = + IntervalViewDescriptor.create( + "rpc client server_elapsed_time", + "Minute and Hour stats for server elapsed time in msecs", + RPC_CLIENT_SERVER_ELAPSED_TIME, + IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), + Arrays.asList(RPC_CLIENT_METHOD)); + + // Census defined rpc server {@link IntervalViewDescriptor}s. + public static final IntervalViewDescriptor RPC_SERVER_SERVER_LATENCY_INTERVAL_VIEW = + IntervalViewDescriptor.create( + "rpc server server_latency", + "Minute and Hour stats for server latency in msecs", + RPC_SERVER_SERVER_LATENCY, + IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), + Arrays.asList(RPC_SERVER_METHOD)); + + public static final IntervalViewDescriptor RPC_SERVER_REQUEST_BYTES_INTERVAL_VIEW = + IntervalViewDescriptor.create( + "rpc server request_bytes", + "Minute and Hour stats for request size in MB", + RPC_SERVER_REQUEST_BYTES, + IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), + Arrays.asList(RPC_SERVER_METHOD)); + + public static final IntervalViewDescriptor RPC_SERVER_RESPONSE_BYTES_INTERVAL_VIEW = + IntervalViewDescriptor.create( + "rpc server response_bytes", + "Minute and Hour stats for response size in MBs", + RPC_SERVER_RESPONSE_BYTES, + IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), + Arrays.asList(RPC_SERVER_METHOD)); + + public static final IntervalViewDescriptor RPC_SERVER_ERROR_COUNT_INTERVAL_VIEW = + IntervalViewDescriptor.create( + "rpc server error_count", + "Minute and Hour stats for rpc errors", + RPC_SERVER_ERROR_COUNT, + IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), + Arrays.asList(RPC_SERVER_METHOD)); + + public static final IntervalViewDescriptor RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES_INTERVAL_VIEW = + IntervalViewDescriptor.create( + "rpc server uncompressed_request_bytes", + "Minute and Hour stats for uncompressed request size in MBs", + RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES, + IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), + Arrays.asList(RPC_SERVER_METHOD)); + + public static final IntervalViewDescriptor RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES_INTERVAL_VIEW = + IntervalViewDescriptor.create( + "rpc server uncompressed_response_bytes", + "Minute and Hour stats for uncompressed response size in MBs", + RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES, + IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), + Arrays.asList(RPC_SERVER_METHOD)); + + public static final IntervalViewDescriptor RPC_SERVER_SERVER_ELAPSED_TIME_INTERVAL_VIEW = + IntervalViewDescriptor.create( + "rpc server server_elapsed_time", + "Minute and Hour stats for server elapsed time in msecs", + RPC_SERVER_SERVER_ELAPSED_TIME, + IntervalAggregationDescriptor.create(Arrays.asList(MINUTE, HOUR)), + Arrays.asList(RPC_SERVER_METHOD)); + + // Visible for testing + RpcConstants() { + throw new AssertionError(); + } +} diff --git a/core/src/main/java/com/google/instrumentation/stats/Stats.java b/core/src/main/java/com/google/instrumentation/stats/Stats.java new file mode 100644 index 00000000..e9313253 --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/stats/Stats.java @@ -0,0 +1,46 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import com.google.instrumentation.common.Provider; + +/** + * {@link Stats}. + */ +public final class Stats { + private static final StatsContextFactory CONTEXT_FACTORY = Provider.newInstance( + "com.google.instrumentation.stats.StatsContextFactoryImpl", null); + + private static final StatsManager STATS_MANAGER = Provider.newInstance( + "com.google.instrumentation.stats.StatsManagerImpl", null); + + /** + * Returns the default {@link StatsContextFactory}. + */ + public static StatsContextFactory getStatsContextFactory() { + return CONTEXT_FACTORY; + } + + /** + * Returns the default {@link StatsManager}. + */ + public static StatsManager getStatsManager() { + return STATS_MANAGER; + } + + // VisibleForTesting + Stats() { + throw new AssertionError(); + } +} diff --git a/core/src/main/java/com/google/instrumentation/stats/StatsContext.java b/core/src/main/java/com/google/instrumentation/stats/StatsContext.java new file mode 100644 index 00000000..0a9f9674 --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/stats/StatsContext.java @@ -0,0 +1,80 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * An immutable context for stats operations. + */ +public abstract class StatsContext { + /** + * Returns a builder based on this {@link StatsContext}. + */ + public abstract Builder builder(); + + /** Shorthand for builder().set(k1, v1).build() */ + public final StatsContext with(TagKey k1, TagValue v1) { + return builder().set(k1, v1).build(); + } + + /** Shorthand for builder().set(k1, v1).set(k2, v2).build() */ + public final StatsContext with(TagKey k1, TagValue v1, TagKey k2, TagValue v2) { + return builder().set(k1, v1).set(k2, v2).build(); + } + + /** Shorthand for builder().set(k1, v1).set(k2, v2).set(k3, v3).build() */ + public final StatsContext with( + TagKey k1, TagValue v1, TagKey k2, TagValue v2, TagKey k3, TagValue v3) { + return builder().set(k1, v1).set(k2, v2).set(k3, v3).build(); + } + + /** + * Records the given measurements against this {@link StatsContext}. + * + * @param measurements the measurements to record against the saved {@link StatsContext} + * @return this + */ + public abstract StatsContext record(MeasurementMap measurements); + + /** + * Serializes the {@link StatsContext} into the on-the-wire representation. + * + *

The inverse of {@link StatsContextFactory#deserialize(java.io.InputStream)} and should be + * based on the {@link StatsContext} protobuf representation. + * + * @param output the {@link OutputStream} to add the serialized form of this {@link StatsContext}. + */ + public abstract void serialize(OutputStream output) throws IOException; + + /** + * Builder for {@link StatsContext}. + */ + public abstract static class Builder { + /** + * Associates the given tag key with the given tag value. + * + * @param key the key + * @param value the value to be associated with {@code key} + * @return {@code this} + */ + public abstract Builder set(TagKey key, TagValue value); + + /** + * Builds a {@link StatsContext} from the specified keys and values. + */ + public abstract StatsContext build(); + } +} diff --git a/core/src/main/java/com/google/instrumentation/stats/StatsContextFactory.java b/core/src/main/java/com/google/instrumentation/stats/StatsContextFactory.java new file mode 100644 index 00000000..581ca847 --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/stats/StatsContextFactory.java @@ -0,0 +1,38 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import java.io.InputStream; +import java.io.IOException; + +/** + * Factory class for {@link StatsContext}. + */ +public abstract class StatsContextFactory { + /** + * Creates a {@link StatsContext} from the given on-the-wire encoded representation. + * + *

Should be the inverse of {@link StatsContext#serialize(java.io.OutputStream)}. The + * serialized representation should be based on the {@link StatsContext} protobuf representation. + * + * @param input on-the-wire representation of a {@link StatsContext} + * @return a {@link StatsContext} deserialized from {@code input} + */ + public abstract StatsContext deserialize(InputStream input) throws IOException; + + /** + * Returns the default {@link StatsContext}. + */ + public abstract StatsContext getDefault(); +} diff --git a/core/src/main/java/com/google/instrumentation/stats/StatsManager.java b/core/src/main/java/com/google/instrumentation/stats/StatsManager.java new file mode 100644 index 00000000..59253768 --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/stats/StatsManager.java @@ -0,0 +1,31 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +/** + * Provides facillities to register {@link ViewDescriptor}s for collecting stats and retrieving + * stats data as a {@link View}. + */ +public abstract class StatsManager { + /** + * Pull model for stats. Registers a {@link ViewDescriptor} that will collect data to be accessed + * via {@link #getView(ViewDescriptor)}. + */ + public abstract void registerView(ViewDescriptor viewDescriptor); + + /** + * Returns the current stats data, {@link View}, associated with the given {@link ViewDescriptor}. + */ + public abstract View getView(ViewDescriptor viewDescriptor); +} diff --git a/core/src/main/java/com/google/instrumentation/stats/StringUtil.java b/core/src/main/java/com/google/instrumentation/stats/StringUtil.java new file mode 100644 index 00000000..180c2411 --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/stats/StringUtil.java @@ -0,0 +1,56 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +/** + * Utility methods for working with tag keys, tag values, and metric names. + */ +final class StringUtil { + + static final int MAX_LENGTH = 255; + static final char UNPRINTABLE_CHAR_SUBSTITUTE = '_'; + + static String sanitize(String str) { + if (str.length() > MAX_LENGTH) { + str = str.substring(0, MAX_LENGTH); + } + if (isPrintableString(str)) { + return str; + } + StringBuilder builder = new StringBuilder(str.length()); + for (int i = 0; i < str.length(); i++) { + char ch = str.charAt(i); + builder.append(isPrintableChar(ch) ? ch : UNPRINTABLE_CHAR_SUBSTITUTE); + } + return builder.toString(); + } + + private static boolean isPrintableString(String str) { + for (int i = 0; i < str.length(); i++) { + if (!isPrintableChar(str.charAt(i))) { + return false; + } + } + return true; + } + + private static boolean isPrintableChar(char ch) { + return ch >= ' ' && ch <= '~'; + } + + // Visible for testing + StringUtil() { + throw new AssertionError(); + } +} diff --git a/core/src/main/java/com/google/instrumentation/stats/Tag.java b/core/src/main/java/com/google/instrumentation/stats/Tag.java new file mode 100644 index 00000000..1bda1c85 --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/stats/Tag.java @@ -0,0 +1,68 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +/** + * A pair of consisting of an associated {@link TagKey} and {@link TagValue}. + */ +public final class Tag { + /** + * Constructs a new {@link Tag} from the given key and value. + */ + public static Tag create(TagKey key, TagValue value) { + return new Tag(key, value); + } + + /** + * Returns the associated tag key. + */ + public TagKey getKey() { + return key; + } + + /** + * Returns the associated tag key. + */ + public TagValue getValue() { + return value; + } + + @Override + public boolean equals(Object obj) { + return (obj instanceof Tag) + && key.equals(((Tag) obj).key) + && value.equals(((Tag) obj).value); + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + key.hashCode(); + result = 31 * result + value.hashCode(); + return result; + } + + @Override + public String toString() { + return "Tag<" + key + "," + value + ">"; + } + + private final TagKey key; + private final TagValue value; + + private Tag(TagKey key, TagValue value) { + this.key = key; + this.value = value; + } +} diff --git a/core/src/main/java/com/google/instrumentation/stats/TagKey.java b/core/src/main/java/com/google/instrumentation/stats/TagKey.java new file mode 100644 index 00000000..bce969e6 --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/stats/TagKey.java @@ -0,0 +1,56 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +/** + * Census Tag keys. + * + *

TagKey's are {@link String}s with enforced restrictions. + */ +public final class TagKey { + public static final int MAX_LENGTH = StringUtil.MAX_LENGTH; + + /** + * Constructs a new {@link TagKey} from the given string. The string will be sanitize such that: + *

    + *
  1. length is restricted to {@link MAX_LENGTH}, strings longer than that will be truncated. + *
  2. characters are restricted to printable ascii characters, non-printable characters will be + * replaced by an underscore '_'. + *
+ */ + public static TagKey create(String key) { + return new TagKey(key); + } + + @Override + public boolean equals(Object obj) { + return (obj instanceof TagKey) && key.equals(((TagKey) obj).key); + } + + @Override + public int hashCode() { + return key.hashCode(); + } + + @Override + public String toString() { + return key; + } + + private final String key; + + private TagKey(String key) { + this.key = StringUtil.sanitize(key); + } +} diff --git a/core/src/main/java/com/google/instrumentation/stats/TagValue.java b/core/src/main/java/com/google/instrumentation/stats/TagValue.java new file mode 100644 index 00000000..c12b184d --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/stats/TagValue.java @@ -0,0 +1,56 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +/** + * Census Tag values. + * + *

TagValue's are {@link String}s with enforced restrictions. + */ +public final class TagValue { + public static final int MAX_LENGTH = StringUtil.MAX_LENGTH; + + /** + * Constructs a new {@link TagValue} from the given string. The string will be sanitize such that: + *

    + *
  1. length is restricted to {@link MAX_LENGTH}, strings longer than that will be truncated. + *
  2. characters are restricted to printable ascii characters, non-printable characters will be + * replaced by an underscore '_'. + *
+ */ + public static TagValue create(String value) { + return new TagValue(value); + } + + @Override + public boolean equals(Object obj) { + return (obj instanceof TagValue) && value.equals(((TagValue) obj).value); + } + + @Override + public int hashCode() { + return value.hashCode(); + } + + @Override + public String toString() { + return value; + } + + private final String value; + + private TagValue(String value) { + this.value = StringUtil.sanitize(value); + } +} diff --git a/core/src/main/java/com/google/instrumentation/stats/View.java b/core/src/main/java/com/google/instrumentation/stats/View.java new file mode 100644 index 00000000..90996960 --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/stats/View.java @@ -0,0 +1,148 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import com.google.instrumentation.common.Function; +import com.google.instrumentation.common.Timestamp; +import com.google.instrumentation.stats.ViewDescriptor.DistributionViewDescriptor; +import com.google.instrumentation.stats.ViewDescriptor.IntervalViewDescriptor; + +import java.util.List; + +/** + * The aggregated data for a particular {@link ViewDescriptor}. + */ +public abstract class View { + /** + * The {@link ViewDescriptor} associated with this {@link View}. + */ + public abstract ViewDescriptor getViewDescriptor(); + + /** + * Applies the given match function to the underlying data type. + */ + public abstract T match( + Function p0, + Function p1); + + // Prevents this class from being subclassed anywhere else. + private View() { + } + + /** + * A {@link View} for distribution-based aggregations. + */ + public static final class DistributionView extends View { + /** + * Constructs a new {@link DistributionView}. + */ + public static DistributionView create(DistributionViewDescriptor distributionViewDescriptor, + List distributionAggregations, Timestamp start, Timestamp end) { + return new DistributionView(distributionViewDescriptor, distributionAggregations, start, end); + } + + /** + * The {@link DistributionAggregation}s associated with this {@link DistributionView}. + * + *

Note: The returned list is unmodifiable, attempts to update it will throw an + * UnsupportedOperationException. + */ + public List getDistributionAggregations() { + return distributionAggregations; + } + + /** + * Returns start timestamp for this aggregation. + */ + public Timestamp getStart() { + return start; + } + + /** + * Returns end timestamp for this aggregation. + */ + public Timestamp getEnd() { + return end; + } + + @Override + public DistributionViewDescriptor getViewDescriptor() { + return distributionViewDescriptor; + } + + @Override + public T match( + Function p0, + Function p1) { + return p0.apply(this); + } + + private final DistributionViewDescriptor distributionViewDescriptor; + private final List distributionAggregations; + private final Timestamp start; + private final Timestamp end; + + private DistributionView(DistributionViewDescriptor distributionViewDescriptor, + List distributionAggregations, Timestamp start, Timestamp end) { + this.distributionViewDescriptor = distributionViewDescriptor; + this.distributionAggregations = distributionAggregations; + this.start = start; + this.end = end; + } + } + + /** + * A {@link View} for interval-base aggregations. + */ + public static final class IntervalView extends View { + /** + * Constructs a new {@link IntervalView}. + */ + public static IntervalView create(IntervalViewDescriptor intervalViewDescriptor, + List intervalAggregations) { + return new IntervalView(intervalViewDescriptor, intervalAggregations); + } + + /** + * The {@link IntervalAggregation}s associated with this {@link IntervalView}. + * + *

Note: The returned list is unmodifiable, attempts to update it will throw an + * UnsupportedOperationException. + */ + public List getIntervalAggregations() { + return intervalAggregations; + } + + @Override + public IntervalViewDescriptor getViewDescriptor() { + return intervalViewDescriptor; + } + + @Override + public T match( + Function p0, + Function p1) { + return p1.apply(this); + } + + private final IntervalViewDescriptor intervalViewDescriptor; + private final List intervalAggregations; + + private IntervalView(IntervalViewDescriptor intervalViewDescriptor, + List intervalAggregations) { + this.intervalViewDescriptor = intervalViewDescriptor; + this.intervalAggregations = intervalAggregations; + } + } +} diff --git a/core/src/main/java/com/google/instrumentation/stats/ViewDescriptor.java b/core/src/main/java/com/google/instrumentation/stats/ViewDescriptor.java new file mode 100644 index 00000000..ae2ccee4 --- /dev/null +++ b/core/src/main/java/com/google/instrumentation/stats/ViewDescriptor.java @@ -0,0 +1,172 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import com.google.instrumentation.common.Function; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * A ViewDescriptor specifies an aggregation and a set of tag keys. The aggregation will be broken + * down by the unique set of matching tag values for each measurement. + */ +public abstract class ViewDescriptor { + /** + * Name of view. Must be unique. + */ + public final String getName() { + return name; + } + + /** + * More detailed description, for documentation purposes. + */ + public final String getDescription() { + return description; + } + + /** + * Measurement type of this view. + */ + public final MeasurementDescriptor getMeasurementDescriptor() { + return measurementDescriptor; + } + + /** + * Tag keys to match with the associated {@link MeasurementDescriptor}. If no keys are specified, + * then all stats are recorded. Keys must be unique. + * + *

Note: The returned list is unmodifiable, attempts to update it will throw an + * UnsupportedOperationException. + */ + public final List getTagKeys() { + return tagKeys; + } + + /** + * Applies the given match function to the underlying data type. + */ + public abstract T match( + Function p0, + Function p1); + + + private final String name; + private final String description; + private final MeasurementDescriptor measurementDescriptor; + private final List tagKeys; + + private ViewDescriptor( + String name, + String description, + MeasurementDescriptor measurementDescriptor, + List tagKeys) { + this.name = name; + this.description = description; + this.measurementDescriptor = measurementDescriptor; + this.tagKeys = Collections.unmodifiableList(new ArrayList(tagKeys)); + } + + /** + * A {@link ViewDescriptor} for distribution-base aggregations. + */ + public static class DistributionViewDescriptor extends ViewDescriptor { + /** + * Constructs a new {@link DistributionViewDescriptor}. + */ + public static DistributionViewDescriptor create( + String name, + String description, + MeasurementDescriptor measurementDescriptor, + DistributionAggregationDescriptor distributionAggregationDescriptor, + List tagKeys) { + return new DistributionViewDescriptor( + name, description, measurementDescriptor, distributionAggregationDescriptor, tagKeys); + } + + /** + * The {@link DistributionAggregationDescriptor} associated with this + * {@link DistributionViewDescriptor}. + */ + public DistributionAggregationDescriptor getDistributionAggregationDescriptor() { + return distributionAggregationDescriptor; + } + + @Override + public T match( + Function p0, + Function p1) { + return p0.apply(this); + } + + private final DistributionAggregationDescriptor distributionAggregationDescriptor; + + private DistributionViewDescriptor( + String name, + String description, + MeasurementDescriptor measurementDescriptor, + DistributionAggregationDescriptor distributionAggregationDescriptor, + List tagKeys) { + super(name, description, measurementDescriptor, tagKeys); + this.distributionAggregationDescriptor = distributionAggregationDescriptor; + } + } + + /** + * A {@link ViewDescriptor} for interval-based aggregations. + */ + public static class IntervalViewDescriptor extends ViewDescriptor { + /** + * Constructs a new {@link IntervalViewDescriptor}. + */ + public static IntervalViewDescriptor create( + String name, + String description, + MeasurementDescriptor measurementDescriptor, + IntervalAggregationDescriptor intervalAggregationDescriptor, + List tagKeys) { + return new IntervalViewDescriptor( + name, description, measurementDescriptor, intervalAggregationDescriptor, tagKeys); + } + + /** + * The {@link IntervalAggregationDescriptor} associated with this + * {@link IntervalViewDescriptor}. + */ + public IntervalAggregationDescriptor getIntervalAggregationDescriptor() { + return intervalAggregationDescriptor; + } + + @Override + public T match( + Function p0, + Function p1) { + return p1.apply(this); + } + + private final IntervalAggregationDescriptor intervalAggregationDescriptor; + + private IntervalViewDescriptor( + String name, + String description, + MeasurementDescriptor measurementDescriptor, + IntervalAggregationDescriptor intervalAggregationDescriptor, + List tagKeys) { + super(name, description, measurementDescriptor, tagKeys); + this.intervalAggregationDescriptor = intervalAggregationDescriptor; + } + } +} diff --git a/core/src/test/java/com/google/instrumentation/common/DurationTest.java b/core/src/test/java/com/google/instrumentation/common/DurationTest.java new file mode 100644 index 00000000..4ba9806c --- /dev/null +++ b/core/src/test/java/com/google/instrumentation/common/DurationTest.java @@ -0,0 +1,65 @@ +package com.google.instrumentation.common; + +import static com.google.common.truth.Truth.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link Duration}. */ +@RunWith(JUnit4.class) +public class DurationTest { + @Test + public void testDurationCreate() { + assertThat(Duration.create(24, 42).getSeconds()).isEqualTo(24); + assertThat(Duration.create(24, 42).getNanos()).isEqualTo(42); + assertThat(Duration.create(-24, -42).getSeconds()).isEqualTo(-24); + assertThat(Duration.create(-24, -42).getNanos()).isEqualTo(-42); + assertThat(Duration.create(315576000000L, 999999999).getSeconds()) + .isEqualTo(315576000000L); + assertThat(Duration.create(315576000000L, 999999999).getNanos()) + .isEqualTo(999999999); + assertThat(Duration.create(-315576000000L, -999999999).getSeconds()) + .isEqualTo(-315576000000L); + assertThat(Duration.create(-315576000000L, -999999999).getNanos()) + .isEqualTo(-999999999); + } + + @Test + public void testDurationCreateInvalidInput() { + assertThat(Duration.create(-315576000001L, 0)).isEqualTo(Duration.create(0, 0)); + assertThat(Duration.create(315576000001L, 0)).isEqualTo(Duration.create(0, 0)); + assertThat(Duration.create(0, 1000000000)).isEqualTo(Duration.create(0, 0)); + assertThat(Duration.create(0, -1000000000)).isEqualTo(Duration.create(0, 0)); + assertThat(Duration.create(-1, 1)).isEqualTo(Duration.create(0, 0)); + assertThat(Duration.create(1, -1)).isEqualTo(Duration.create(0, 0)); + } + + @Test + public void testDurationFromMillis() { + assertThat(Duration.fromMillis(0)).isEqualTo(Duration.create(0, 0)); + assertThat(Duration.fromMillis(987)).isEqualTo(Duration.create(0, 987000000)); + assertThat(Duration.fromMillis(3456)).isEqualTo(Duration.create(3, 456000000)); + } + + @Test + public void testDurationFromMillisNegative() { + assertThat(Duration.fromMillis(-1)).isEqualTo(Duration.create(0, -1000000)); + assertThat(Duration.fromMillis(-999)).isEqualTo(Duration.create(0, -999000000)); + assertThat(Duration.fromMillis(-1000)).isEqualTo(Duration.create(-1, 0)); + assertThat(Duration.fromMillis(-3456)).isEqualTo(Duration.create(-3, -456000000)); + } + + @Test + public void testDurationEqual() { + // Positive tests. + assertThat(Duration.create(0, 0)).isEqualTo(Duration.create(0, 0)); + assertThat(Duration.create(24, 42)).isEqualTo(Duration.create(24, 42)); + assertThat(Duration.create(-24, -42)).isEqualTo(Duration.create(-24, -42)); + // Negative tests. + assertThat(Duration.create(25, 42)).isNotEqualTo(Duration.create(24, 42)); + assertThat(Duration.create(24, 43)).isNotEqualTo(Duration.create(24, 42)); + assertThat(Duration.create(-25, -42)).isNotEqualTo(Duration.create(-24, -42)); + assertThat(Duration.create(-24, -43)).isNotEqualTo(Duration.create(-24, -42)); + } +} diff --git a/core/src/test/java/com/google/instrumentation/common/ProviderTest.java b/core/src/test/java/com/google/instrumentation/common/ProviderTest.java new file mode 100644 index 00000000..f125de25 --- /dev/null +++ b/core/src/test/java/com/google/instrumentation/common/ProviderTest.java @@ -0,0 +1,61 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.common; + +import static com.google.common.truth.Truth.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Tests for {@link Provider} + */ +@RunWith(JUnit4.class) +public class ProviderTest { + @Test + public void testGoodClass() throws Exception { + GetGen getGen0 = + Provider.newInstance("com.google.instrumentation.common.ProviderTest$GetGen", null); + assertThat(getGen0).isNotNull(); + assertThat(getGen0.getGen()).isEqualTo(0); + for (int i = 1; i < 10; i++) { + GetGen getGenI = + Provider.newInstance("com.google.instrumentation.common.ProviderTest$GetGen", null); + assertThat(getGenI).isNotNull(); + assertThat(getGenI.getGen()).isEqualTo(i); + } + assertThat(getGen0.getGen()).isEqualTo(0); + } + + @Test + public void testBadClass() throws Exception { + GetGen getGen = + Provider.newInstance("com.google.instrumentation.common.ProviderTest$BadClass", null); + assertThat(getGen).isNull(); + } + + static class GetGen { + static int genCount = 0; + int gen; + + public GetGen() { + gen = genCount++; + } + + public int getGen() { + return gen; + } + } +} diff --git a/core/src/test/java/com/google/instrumentation/common/TimestampTest.java b/core/src/test/java/com/google/instrumentation/common/TimestampTest.java new file mode 100644 index 00000000..1e994def --- /dev/null +++ b/core/src/test/java/com/google/instrumentation/common/TimestampTest.java @@ -0,0 +1,64 @@ +package com.google.instrumentation.common; + +import static com.google.common.truth.Truth.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link Timestamp}. */ +@RunWith(JUnit4.class) +public class TimestampTest { + @Test + public void testTimestampCreate() { + assertThat(Timestamp.create(24, 42).getSeconds()).isEqualTo(24); + assertThat(Timestamp.create(24, 42).getNanos()).isEqualTo(42); + assertThat(Timestamp.create(-24, 42).getSeconds()).isEqualTo(-24); + assertThat(Timestamp.create(-24, 42).getNanos()).isEqualTo(42); + assertThat(Timestamp.create(315576000000L, 999999999).getSeconds()) + .isEqualTo(315576000000L); + assertThat(Timestamp.create(315576000000L, 999999999).getNanos()) + .isEqualTo(999999999); + assertThat(Timestamp.create(-315576000000L, 999999999).getSeconds()) + .isEqualTo(-315576000000L); + assertThat(Timestamp.create(-315576000000L, 999999999).getNanos()) + .isEqualTo(999999999); + } + + @Test + public void testTimestampCreateInvalidInput() { + assertThat(Timestamp.create(-315576000001L, 0)).isEqualTo(Timestamp.create(0, 0)); + assertThat(Timestamp.create(315576000001L, 0)).isEqualTo(Timestamp.create(0, 0)); + assertThat(Timestamp.create(1, 1000000000)).isEqualTo(Timestamp.create(0, 0)); + assertThat(Timestamp.create(1, -1)).isEqualTo(Timestamp.create(0, 0)); + assertThat(Timestamp.create(-1, 1000000000)).isEqualTo(Timestamp.create(0, 0)); + assertThat(Timestamp.create(-1, -1)).isEqualTo(Timestamp.create(0, 0)); + } + + @Test + public void testTimestampFromMillis() { + assertThat(Timestamp.fromMillis(0)).isEqualTo(Timestamp.create(0, 0)); + assertThat(Timestamp.fromMillis(987)).isEqualTo(Timestamp.create(0, 987000000)); + assertThat(Timestamp.fromMillis(3456)).isEqualTo(Timestamp.create(3, 456000000)); + } + + @Test + public void testTimestampFromMillisNegative() { + assertThat(Timestamp.fromMillis(-1)).isEqualTo(Timestamp.create(-1, 999000000)); + assertThat(Timestamp.fromMillis(-999)).isEqualTo(Timestamp.create(-1, 1000000)); + assertThat(Timestamp.fromMillis(-3456)).isEqualTo(Timestamp.create(-4, 544000000)); + } + + @Test + public void testTimestampEqual() { + // Positive tests. + assertThat(Timestamp.create(0, 0)).isEqualTo(Timestamp.create(0, 0)); + assertThat(Timestamp.create(24, 42)).isEqualTo(Timestamp.create(24, 42)); + assertThat(Timestamp.create(-24, 42)).isEqualTo(Timestamp.create(-24, 42)); + // Negative tests. + assertThat(Timestamp.create(25, 42)).isNotEqualTo(Timestamp.create(24, 42)); + assertThat(Timestamp.create(24, 43)).isNotEqualTo(Timestamp.create(24, 42)); + assertThat(Timestamp.create(-25, 42)).isNotEqualTo(Timestamp.create(-24, 42)); + assertThat(Timestamp.create(-24, 43)).isNotEqualTo(Timestamp.create(-24, 42)); + } +} diff --git a/core/src/test/java/com/google/instrumentation/stats/DistributionAggregationDescriptorTest.java b/core/src/test/java/com/google/instrumentation/stats/DistributionAggregationDescriptorTest.java new file mode 100644 index 00000000..abc57c5b --- /dev/null +++ b/core/src/test/java/com/google/instrumentation/stats/DistributionAggregationDescriptorTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import static com.google.common.truth.Truth.assertThat; + +import java.util.Arrays; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Tests for {@link DistributionAggregationDescriptor} + */ +@RunWith(JUnit4.class) +public final class DistributionAggregationDescriptorTest { + @Test + public void testDistributionAggregationDescriptorEmpty() { + DistributionAggregationDescriptor dDescriptor = DistributionAggregationDescriptor.create(); + assertThat(dDescriptor.getBucketBoundaries()).isNull(); + } + + @Test + public void testDistributionAggregationDescriptor() { + Double[] buckets = new Double[] { 0.1, 2.2, 33.3 }; + DistributionAggregationDescriptor dDescriptor = + DistributionAggregationDescriptor.create(Arrays.asList(buckets)); + assertThat(dDescriptor.getBucketBoundaries()).isNotNull(); + assertThat(dDescriptor.getBucketBoundaries()).hasSize(buckets.length); + for (int i = 0; i < buckets.length; i++) { + assertThat(dDescriptor.getBucketBoundaries().get(i)) + .isWithin(0.00000001).of(buckets[i]); + } + } +} diff --git a/core/src/test/java/com/google/instrumentation/stats/DistributionAggregationTest.java b/core/src/test/java/com/google/instrumentation/stats/DistributionAggregationTest.java new file mode 100644 index 00000000..55e0e4e0 --- /dev/null +++ b/core/src/test/java/com/google/instrumentation/stats/DistributionAggregationTest.java @@ -0,0 +1,77 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.instrumentation.stats.DistributionAggregation.Range; + +import java.util.Arrays; +import java.util.List; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Tests for class {@link DistributionAggregation}. + */ +@RunWith(JUnit4.class) +public final class DistributionAggregationTest { + @Test + public void testDistributionAggregationWithOutBuckets() { + DistributionAggregation aggr = DistributionAggregation.create(10, 5.0, 30.0, + Range.create(1.0, 5.0), TAGS); + + assertThat(aggr.getCount()).isEqualTo(10); + assertThat(aggr.getMean()).isEqualTo(5.0); + assertThat(aggr.getSum()).isEqualTo(30.0); + assertThat(aggr.getRange().getMin()).isEqualTo(1.0); + assertThat(aggr.getRange().getMax()).isEqualTo(5.0); + assertThat(aggr.getTags()).hasSize(TAGS.size()); + for (int i = 0; i < aggr.getTags().size(); i++) { + assertThat(aggr.getTags().get(i)).isEqualTo(TAGS.get(i)); + } + assertThat(aggr.getBucketCounts()).isNull(); + } + + @Test + public void testDistributionAggregationWithBuckets() { + List buckets = Arrays.asList(2L, 2L, 2L, 2L, 2L); + DistributionAggregation aggr = DistributionAggregation.create(10, 5.0, 30.0, + Range.create(1.0, 5.0), TAGS, buckets); + + assertThat(aggr.getCount()).isEqualTo(10); + assertThat(aggr.getMean()).isEqualTo(5.0); + assertThat(aggr.getSum()).isEqualTo(30.0); + assertThat(aggr.getRange().getMin()).isEqualTo(1.0); + assertThat(aggr.getRange().getMax()).isEqualTo(5.0); + assertThat(aggr.getBucketCounts()).isNotNull(); + assertThat(aggr.getBucketCounts()).hasSize(buckets.size()); + assertThat(aggr.getTags()).hasSize(TAGS.size()); + for (int i = 0; i < aggr.getTags().size(); i++) { + assertThat(aggr.getTags().get(i)).isEqualTo(TAGS.get(i)); + } + for (int i = 0; i < aggr.getBucketCounts().size(); i++) { + assertThat(aggr.getBucketCounts().get(i)).isEqualTo(buckets.get(i)); + } + } + + private static final TagKey K1 = TagKey.create("k1"); + private static final TagKey K2 = TagKey.create("k2"); + + private static final TagValue V1 = TagValue.create("v1"); + private static final TagValue V2 = TagValue.create("v2"); + + private static final List TAGS = Arrays.asList(Tag.create(K1, V1), Tag.create(K2, V2)); +} diff --git a/core/src/test/java/com/google/instrumentation/stats/IntervalAggregationDescriptorTest.java b/core/src/test/java/com/google/instrumentation/stats/IntervalAggregationDescriptorTest.java new file mode 100644 index 00000000..3c8a5e45 --- /dev/null +++ b/core/src/test/java/com/google/instrumentation/stats/IntervalAggregationDescriptorTest.java @@ -0,0 +1,77 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.instrumentation.common.Duration; +import java.util.Arrays; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Tests for {@link IntervalAggregationDescriptor} + */ +@RunWith(JUnit4.class) +public final class IntervalAggregationDescriptorTest { + @Test + public void testIntervalAggregationDescriptor() { + Duration[] intervals = + new Duration[] { Duration.fromMillis(1), Duration.fromMillis(22), Duration.fromMillis(333)}; + IntervalAggregationDescriptor iDescriptor = + IntervalAggregationDescriptor.create(12, Arrays.asList(intervals)); + assertThat(iDescriptor.getNumSubIntervals()).isEqualTo(12); + assertThat(iDescriptor.getIntervalSizes()).isNotNull(); + assertThat(iDescriptor.getIntervalSizes()).hasSize(intervals.length); + for (int i = 0; i < intervals.length; i++) { + assertThat(iDescriptor.getIntervalSizes().get(i)).isEqualTo(intervals[i]); + } + } + + @Test + public void testIntervalAggregationDescriptorWithDefaultNumSubIntervals() { + assertThat( + IntervalAggregationDescriptor.create( + Arrays.asList(Duration.fromMillis(1))).getNumSubIntervals()) + .isEqualTo(5); + } + + @Test + public void testIntervalAggregationDescriptorNumSubIntervalsRange() { + assertThat( + IntervalAggregationDescriptor.create( + 2, Arrays.asList(Duration.fromMillis(1))).getNumSubIntervals()) + .isEqualTo(2); + assertThat( + IntervalAggregationDescriptor.create( + 20, Arrays.asList(Duration.fromMillis(1))).getNumSubIntervals()) + .isEqualTo(20); + } + + @Test(expected = IllegalArgumentException.class) + public void testIntervalAggregationDescriptorLowNumSubIntervals() { + IntervalAggregationDescriptor.create(1, Arrays.asList(Duration.fromMillis(1))); + } + + @Test(expected = IllegalArgumentException.class) + public void testIntervalAggregationDescriptorHighNumSubIntervals() { + IntervalAggregationDescriptor.create(21, Arrays.asList(Duration.fromMillis(1))); + } + + @Test(expected = IllegalArgumentException.class) + public void testIntervalAggregationDescriptorEmptyIntervalSizes() { + IntervalAggregationDescriptor.create(Arrays.asList(new Duration[] { })); + } +} diff --git a/core/src/test/java/com/google/instrumentation/stats/IntervalAggregationTest.java b/core/src/test/java/com/google/instrumentation/stats/IntervalAggregationTest.java new file mode 100644 index 00000000..a436ff0b --- /dev/null +++ b/core/src/test/java/com/google/instrumentation/stats/IntervalAggregationTest.java @@ -0,0 +1,65 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.instrumentation.common.Duration; +import com.google.instrumentation.stats.IntervalAggregation.Interval; +import java.util.Arrays; +import java.util.List; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Tests for class {@link IntervalAggregation}. + */ +@RunWith(JUnit4.class) +public final class IntervalAggregationTest { + @Test + public void testIntervalAggregation() { + List intervals = Arrays.asList( + Interval.create(Duration.fromMillis(10), 100.0, 1000.0), + Interval.create(Duration.fromMillis(11), 101.0, 1001.0)); + IntervalAggregation aggr = IntervalAggregation.create(TAGS, intervals); + + assertThat(aggr.getTags()).hasSize(TAGS.size()); + for (int i = 0; i < aggr.getTags().size(); i++) { + assertThat(aggr.getTags().get(i)).isEqualTo(TAGS.get(i)); + } + assertThat(aggr.getIntervals()).hasSize(intervals.size()); + for (int i = 0; i < aggr.getIntervals().size(); i++) { + assertThat(aggr.getIntervals().get(i)).isEqualTo(intervals.get(i)); + } + } + + @Test + public void testInterval() { + Duration duration = Duration.fromMillis(10); + Interval interval = Interval.create(duration, 100.0, 1000.0); + + assertThat(interval.getIntervalSize()).isEqualTo(duration); + assertThat(interval.getCount()).isWithin(0.00000001).of(100.0); + assertThat(interval.getSum()).isWithin(0.00000001).of(1000.0); + } + + private static final TagKey K1 = TagKey.create("k1"); + private static final TagKey K2 = TagKey.create("k2"); + + private static final TagValue V1 = TagValue.create("v1"); + private static final TagValue V2 = TagValue.create("v2"); + + private static final List TAGS = Arrays.asList(Tag.create(K1, V1), Tag.create(K2, V2)); +} diff --git a/core/src/test/java/com/google/instrumentation/stats/MeasurementDescriptorTest.java b/core/src/test/java/com/google/instrumentation/stats/MeasurementDescriptorTest.java new file mode 100644 index 00000000..9fa16f37 --- /dev/null +++ b/core/src/test/java/com/google/instrumentation/stats/MeasurementDescriptorTest.java @@ -0,0 +1,69 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.instrumentation.stats.MeasurementDescriptor.BasicUnit; +import com.google.instrumentation.stats.MeasurementDescriptor.MeasurementUnit; +import java.util.Arrays; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Tests for {@link MeasurementDescriptor} + */ +@RunWith(JUnit4.class) +public final class MeasurementDescriptorTest { + @Test + public void testNameMaxLength() { + char[] name = new char[MeasurementDescriptor.MAX_LENGTH]; + char[] truncName = new char[MeasurementDescriptor.MAX_LENGTH + 10]; + Arrays.fill(name, 'n'); + Arrays.fill(truncName, 'n'); + assertThat(makeSimpleDescriptor(new String(name)).getName()) + .isEqualTo(makeSimpleDescriptor(new String(truncName)).getName()); + } + + @Test + public void testNameBadChar() { + assertThat(makeSimpleDescriptor("\2ab\3cd").getName()) + .isEqualTo(StringUtil.UNPRINTABLE_CHAR_SUBSTITUTE + "ab" + + StringUtil.UNPRINTABLE_CHAR_SUBSTITUTE + "cd"); + } + + @Test + public void testComponents() { + MeasurementDescriptor measurement = MeasurementDescriptor.create( + "Foo", + "The description of Foo", + MeasurementUnit.create( + 6, Arrays.asList(BasicUnit.BITS), Arrays.asList(BasicUnit.SECONDS))); + assertThat(measurement.getName()).isEqualTo("Foo"); + assertThat(measurement.getDescription()).isEqualTo("The description of Foo"); + assertThat(measurement.getUnit().getPower10()).isEqualTo(6); + assertThat(measurement.getUnit().getNumerators()).hasSize(1); + assertThat(measurement.getUnit().getNumerators().get(0)).isEqualTo(BasicUnit.BITS); + assertThat(measurement.getUnit().getDenominators()).hasSize(1); + assertThat(measurement.getUnit().getDenominators().get(0)).isEqualTo(BasicUnit.SECONDS); + } + + private static final MeasurementDescriptor makeSimpleDescriptor(String name) { + return MeasurementDescriptor.create( + name, + name + " description", + MeasurementUnit.create(1, Arrays.asList(BasicUnit.SCALAR))); + } +} diff --git a/core/src/test/java/com/google/instrumentation/stats/MeasurementMapTest.java b/core/src/test/java/com/google/instrumentation/stats/MeasurementMapTest.java new file mode 100644 index 00000000..068cdfc2 --- /dev/null +++ b/core/src/test/java/com/google/instrumentation/stats/MeasurementMapTest.java @@ -0,0 +1,121 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.common.collect.ImmutableList; +import com.google.instrumentation.stats.MeasurementDescriptor.BasicUnit; +import com.google.instrumentation.stats.MeasurementDescriptor.MeasurementUnit; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Tests for {@link MeasurementMap} + */ +@RunWith(JUnit4.class) +public class MeasurementMapTest { + @Test + public void testOf1() { + ImmutableList expected = ImmutableList.of(MeasurementValue.create(M1, 44.4)); + MeasurementMap metrics = MeasurementMap.of(M1, 44.4); + assertEquals(expected, metrics); + } + + @Test + public void testOf2() { + ImmutableList expected = ImmutableList.of( + MeasurementValue.create(M1, 44.4), MeasurementValue.create(M2, 66.6)); + MeasurementMap metrics = MeasurementMap.of(M1, 44.4, M2, 66.6); + assertEquals(expected, metrics); + } + + @Test + public void testOf3() { + ImmutableList expected = ImmutableList.of( + MeasurementValue.create(M1, 44.4), + MeasurementValue.create(M2, 66.6), + MeasurementValue.create(M3, 88.8)); + MeasurementMap metrics = MeasurementMap.of( + M1, 44.4, M2, 66.6, M3, 88.8); + assertEquals(expected, metrics); + } + + @Test + public void testBuilderEmpty() { + ImmutableList expected = ImmutableList.of(); + MeasurementMap metrics = MeasurementMap.builder().build(); + assertEquals(expected, metrics); + } + + @Test + public void testBuilder() { + ArrayList expected = new ArrayList(10); + MeasurementMap.Builder builder = MeasurementMap.builder(); + for (int i = 1; i <= 10; i++) { + expected.add(MeasurementValue.create(makeSimpleMeasurement("m" + i), i * 11.1)); + builder.put(makeSimpleMeasurement("m" + i), i * 11.1); + assertEquals(expected, builder.build()); + } + } + + @Test + public void testDuplicateMeasurementDescriptors() { + assertEquals(MeasurementMap.of(M1, 1.0, M1, 1.0), MeasurementMap.of(M1, 1.0)); + assertEquals(MeasurementMap.of(M1, 1.0, M1, 2.0), MeasurementMap.of(M1, 1.0)); + assertEquals(MeasurementMap.of(M1, 1.0, M1, 2.0, M1, 3.0), MeasurementMap.of(M1, 1.0)); + assertEquals(MeasurementMap.of(M1, 1.0, M2, 2.0, M1, 3.0), MeasurementMap.of(M1, 1.0, M2, 2.0)); + assertEquals(MeasurementMap.of(M1, 1.0, M1, 2.0, M2, 2.0), MeasurementMap.of(M1, 1.0, M2, 2.0)); + } + + @Test + public void testSize() { + MeasurementMap.Builder builder = MeasurementMap.builder(); + for (int i = 1; i <= 10; i++) { + builder.put(makeSimpleMeasurement("m" + i), i * 11.1); + assertThat(builder.build()).hasSize(i); + } + } + + private static final MeasurementUnit simpleMeasurementUnit = + MeasurementUnit.create(1, Arrays.asList(BasicUnit.SCALAR)); + private static final MeasurementDescriptor M1 = makeSimpleMeasurement("m1"); + private static final MeasurementDescriptor M2 = makeSimpleMeasurement("m2"); + private static final MeasurementDescriptor M3 = makeSimpleMeasurement("m3"); + + private static final MeasurementDescriptor makeSimpleMeasurement(String measurement) { + return MeasurementDescriptor.create( + measurement, measurement + " description", simpleMeasurementUnit); + } + + private static void assertEquals( + Iterable expected, Iterable actual) { + Iterator e = expected.iterator(); + Iterator a = actual.iterator(); + while (e.hasNext() && a.hasNext()) { + MeasurementValue expectedMeasurement = e.next(); + MeasurementValue actualMeasurement = a.next(); + assertThat(expectedMeasurement.getMeasurement().getName()) + .isEqualTo(actualMeasurement.getMeasurement().getName()); + assertThat(expectedMeasurement.getValue()) + .isWithin(0.00000001).of(actualMeasurement.getValue()); + } + assertThat(e.hasNext()).isFalse(); + assertThat(a.hasNext()).isFalse(); + } +} diff --git a/core/src/test/java/com/google/instrumentation/stats/RpcConstantsTest.java b/core/src/test/java/com/google/instrumentation/stats/RpcConstantsTest.java new file mode 100644 index 00000000..50ec1298 --- /dev/null +++ b/core/src/test/java/com/google/instrumentation/stats/RpcConstantsTest.java @@ -0,0 +1,52 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import static com.google.common.truth.Truth.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Tests for {@link RpcConstants} + */ +@RunWith(JUnit4.class) +public final class RpcConstantsTest { + @Test + public void testConstants() { + assertThat(RpcConstants.RPC_STATUS).isNotNull(); + assertThat(RpcConstants.RPC_CLIENT_METHOD).isNotNull(); + assertThat(RpcConstants.RPC_SERVER_METHOD).isNotNull(); + + assertThat(RpcConstants.RPC_CLIENT_ERROR_COUNT).isNotNull(); + assertThat(RpcConstants.RPC_CLIENT_ROUNDTRIP_LATENCY).isNotNull(); + assertThat(RpcConstants.RPC_CLIENT_REQUEST_BYTES).isNotNull(); + assertThat(RpcConstants.RPC_CLIENT_RESPONSE_BYTES).isNotNull(); + assertThat(RpcConstants.RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES).isNotNull(); + assertThat(RpcConstants.RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES).isNotNull(); + + assertThat(RpcConstants.RPC_SERVER_ERROR_COUNT).isNotNull(); + assertThat(RpcConstants.RPC_SERVER_REQUEST_BYTES).isNotNull(); + assertThat(RpcConstants.RPC_SERVER_RESPONSE_BYTES).isNotNull(); + assertThat(RpcConstants.RPC_SERVER_SERVER_LATENCY).isNotNull(); + assertThat(RpcConstants.RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES).isNotNull(); + assertThat(RpcConstants.RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES).isNotNull(); + } + + @Test(expected = AssertionError.class) + public void testConstructor() { + new RpcConstants(); + } +} diff --git a/core/src/test/java/com/google/instrumentation/stats/StatsContextFactoryTest.java b/core/src/test/java/com/google/instrumentation/stats/StatsContextFactoryTest.java new file mode 100644 index 00000000..12efa98f --- /dev/null +++ b/core/src/test/java/com/google/instrumentation/stats/StatsContextFactoryTest.java @@ -0,0 +1,31 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Tests for {@link StatsContextFactory}. + */ +@RunWith(JUnit4.class) +public class StatsContextFactoryTest { + @Test(expected = IOException.class) + public void testDeserializeEmpty() throws Exception { + Stats.getStatsContextFactory().deserialize(new ByteArrayInputStream(new byte[0])); + } +} diff --git a/core/src/test/java/com/google/instrumentation/stats/StatsContextTest.java b/core/src/test/java/com/google/instrumentation/stats/StatsContextTest.java new file mode 100644 index 00000000..04531297 --- /dev/null +++ b/core/src/test/java/com/google/instrumentation/stats/StatsContextTest.java @@ -0,0 +1,150 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.common.testing.EqualsTester; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Tests for {@link StatsContext}. + */ +@RunWith(JUnit4.class) +public class StatsContextTest { + private static final StatsContext DEFAULT = Stats.getStatsContextFactory().getDefault(); + + private static final MeasurementDescriptor[] StatsMeasurementDescriptors = { + RpcConstants.RPC_CLIENT_REQUEST_BYTES, RpcConstants.RPC_CLIENT_RESPONSE_BYTES, + RpcConstants.RPC_CLIENT_ROUNDTRIP_LATENCY, RpcConstants.RPC_SERVER_REQUEST_BYTES, + RpcConstants.RPC_SERVER_RESPONSE_BYTES, RpcConstants.RPC_SERVER_SERVER_LATENCY + }; + + private static final TagKey K_EMPTY = TagKey.create(""); + private static final TagKey K1 = TagKey.create("k1"); + private static final TagKey K2 = TagKey.create("k2"); + private static final TagKey K3 = TagKey.create("k3"); + private static final TagKey K4 = TagKey.create("k4"); + private static final TagKey K10 = TagKey.create("k10"); + + private static final TagValue V_EMPTY = TagValue.create(""); + private static final TagValue V1 = TagValue.create("v1"); + private static final TagValue V2 = TagValue.create("v2"); + private static final TagValue V3 = TagValue.create("v3"); + private static final TagValue V4 = TagValue.create("v4"); + private static final TagValue V10 = TagValue.create("v10"); + private static final TagValue V20 = TagValue.create("v20"); + private static final TagValue V30 = TagValue.create("v30"); + private static final TagValue V100 = TagValue.create("v100"); + + @Test + public void testWith() { + assertThat(DEFAULT.builder().set(K1, V1).build()).isEqualTo(DEFAULT.with(K1, V1)); + + assertThat(DEFAULT.builder().set(K1, V1).set(K2, V2).build()) + .isEqualTo(DEFAULT.with(K1, V1, K2, V2)); + + assertThat(DEFAULT.builder().set(K1, V1).set(K2, V2).set(K3, V3).build()) + .isEqualTo(DEFAULT.with(K1, V1, K2, V2, K3, V3)); + } + + @Test + public void testWithComposed() { + StatsContext context1 = DEFAULT.with(K1, V1); + assertThat(DEFAULT.builder().set(K1, V1).build()).isEqualTo(context1); + + StatsContext context2 = context1.with(K1, V10, K2, V2); + assertThat(DEFAULT.with(K1, V10, K2, V2)).isEqualTo(context2); + + StatsContext context3 = context2.with(K1, V100, K2, V20, K3, V3); + assertThat(DEFAULT.with(K1, V100, K2, V20, K3, V3)).isEqualTo(context3); + + StatsContext context4 = context3.with(K3, V30, K4, V4); + assertThat(DEFAULT.builder().set(K1, V100).set(K2, V20).set(K3, V30).set(K4, V4).build()) + .isEqualTo(context4); + } + + + @Test + public void testRecordEachMeasurement() { + StatsContext context = DEFAULT.with(K1, V1); + double value = 44.0; + for (MeasurementDescriptor descriptor : StatsMeasurementDescriptors) { + MeasurementMap measurements = MeasurementMap.of(descriptor, value); + context.record(measurements); + //verify(context.context).record(measurements); + value++; + } + } + + @Test + public void testRecordAllMeasurements() { + StatsContext context = DEFAULT.with(K1, V1); + double value = 44.0; + MeasurementMap.Builder builder = MeasurementMap.builder(); + for (MeasurementDescriptor descriptor : StatsMeasurementDescriptors) { + MeasurementMap measurements = builder.put(descriptor, value).build(); + context.record(measurements); + //verify(context.context).record(measurements); + value++; + } + } + + @Test + public void testSerialize() throws Exception { + testSerialization(DEFAULT.builder().build()); + testSerialization(DEFAULT.with(K1, V1)); + testSerialization(DEFAULT.with(K1, V1, K2, V2, K3, V3)); + testSerialization(DEFAULT.with(K1, V_EMPTY)); + testSerialization(DEFAULT.with(K_EMPTY, V1)); + testSerialization(DEFAULT.with(K_EMPTY, V_EMPTY)); + } + + // Tests for Object overrides. + + @Test + public void testEquals() { + new EqualsTester() + .addEqualityGroup(DEFAULT, DEFAULT) + .addEqualityGroup(DEFAULT.with(K1, V1), DEFAULT.with(K1, V1)) + .addEqualityGroup( + DEFAULT.with(K1, V1, K2, V2), + DEFAULT.with(K1, V1, K2, V2), + DEFAULT.with(K2, V2, K1, V1)) + .addEqualityGroup(DEFAULT.with(K10, V1)) + .addEqualityGroup(DEFAULT.with(K1, V10)) + .addEqualityGroup("foo") + .testEquals(); + } + + @Test + public void testToString() { + assertThat(DEFAULT.with(K1, V1).toString()).isEqualTo(DEFAULT.with(K1, V1).toString()); + assertThat(DEFAULT.with(K10, V1).toString()).isNotEqualTo(DEFAULT.with(K1, V1).toString()); + assertThat(DEFAULT.with(K1, V10).toString()).isNotEqualTo(DEFAULT.with(K1, V1).toString()); + } + + private static void testSerialization(StatsContext expected) throws Exception { + ByteArrayOutputStream output = new ByteArrayOutputStream(); + expected.serialize(output); + ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray()); + StatsContext actual = Stats.getStatsContextFactory().deserialize(input); + assertThat(actual).isEqualTo(expected); + } +} diff --git a/core/src/test/java/com/google/instrumentation/stats/StatsTest.java b/core/src/test/java/com/google/instrumentation/stats/StatsTest.java new file mode 100644 index 00000000..2bbf5cef --- /dev/null +++ b/core/src/test/java/com/google/instrumentation/stats/StatsTest.java @@ -0,0 +1,36 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import static com.google.common.truth.Truth.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Tests for {@link RpcConstants} + */ +@RunWith(JUnit4.class) +public final class StatsTest { + @Test + public void getStatsContextFactory() { + assertThat(Stats.getStatsContextFactory()).isNotNull(); + } + + @Test(expected = AssertionError.class) + public void testConstructor() { + new Stats(); + } +} diff --git a/core/src/test/java/com/google/instrumentation/stats/StringUtilTest.java b/core/src/test/java/com/google/instrumentation/stats/StringUtilTest.java new file mode 100644 index 00000000..8d954098 --- /dev/null +++ b/core/src/test/java/com/google/instrumentation/stats/StringUtilTest.java @@ -0,0 +1,47 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import static com.google.common.truth.Truth.assertThat; + +import java.util.Arrays; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Tests for {@link StringUtil}. + */ +@RunWith(JUnit4.class) +public final class StringUtilTest { + @Test + public void testMaxLength() { + char[] string = new char[StringUtil.MAX_LENGTH]; + char[] truncString = new char[StringUtil.MAX_LENGTH + 10]; + Arrays.fill(string, 'v'); + Arrays.fill(truncString, 'v'); + assertThat(StringUtil.sanitize(new String(truncString))).isEqualTo(new String(string)); + } + + @Test + public void testBadChar() { + String string = "\2ab\3cd"; + assertThat(StringUtil.sanitize(string)).isEqualTo("_ab_cd"); + } + + @Test(expected = AssertionError.class) + public void testConstructor() { + new StringUtil(); + } +} diff --git a/core/src/test/java/com/google/instrumentation/stats/TagKeyTest.java b/core/src/test/java/com/google/instrumentation/stats/TagKeyTest.java new file mode 100644 index 00000000..36f469b8 --- /dev/null +++ b/core/src/test/java/com/google/instrumentation/stats/TagKeyTest.java @@ -0,0 +1,59 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.common.testing.EqualsTester; +import java.util.Arrays; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Tests for {@link TagKey} + */ +@RunWith(JUnit4.class) +public final class TagKeyTest { + @Test + public void testKeyMaxLength() { + char[] key = new char[TagKey.MAX_LENGTH]; + char[] truncKey = new char[TagKey.MAX_LENGTH + 10]; + Arrays.fill(key, 'k'); + Arrays.fill(truncKey, 'k'); + assertThat(TagKey.create(new String(key)).toString()) + .isEqualTo(TagKey.create(new String(truncKey)).toString()); + } + + @Test + public void testKeyBadChar() { + String key = "\2ab\3cd"; + assertThat(TagKey.create(key).toString()) + .isEqualTo(StringUtil.UNPRINTABLE_CHAR_SUBSTITUTE + "ab" + + StringUtil.UNPRINTABLE_CHAR_SUBSTITUTE + "cd"); + } + + @Test + public void testEquals() { + new EqualsTester() + .addEqualityGroup(TagKey.create("foo"), TagKey.create("foo")) + .addEqualityGroup(TagKey.create("bar")) + .testEquals(); + } + + @Test + public void testToString() { + assertThat(TagKey.create("foo").toString()).isEqualTo("foo"); + } +} diff --git a/core/src/test/java/com/google/instrumentation/stats/TagTest.java b/core/src/test/java/com/google/instrumentation/stats/TagTest.java new file mode 100644 index 00000000..010b8a31 --- /dev/null +++ b/core/src/test/java/com/google/instrumentation/stats/TagTest.java @@ -0,0 +1,64 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.common.testing.EqualsTester; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Tests for {@link Tag} + */ +@RunWith(JUnit4.class) +public final class TagTest { + @Test + public void testTagTest() { + Tag tag = Tag.create(K1, V1); + assertThat(tag.getKey()).isEqualTo(K1); + assertThat(tag.getValue()).isEqualTo(V1); + } + + @Test + public void testEquals() { + new EqualsTester() + .addEqualityGroup(Tag.create(K1, V1), Tag.create(K1, V1)) + .addEqualityGroup(Tag.create(K2, V1)) + .addEqualityGroup(Tag.create(K1, V2)) + .testEquals(); + } + + @Test + public void testHashCode() { + new EqualsTester() + .addEqualityGroup(Tag.create(K1, V1).hashCode(), Tag.create(K1, V1).hashCode()) + .addEqualityGroup(Tag.create(K2, V1).hashCode()) + .addEqualityGroup(Tag.create(K1, V2).hashCode()) + .testEquals(); + } + + @Test + public void testToString() { + assertThat(Tag.create(K1, V1).toString()).isEqualTo("Tag"); + assertThat(Tag.create(K2, V1).toString()).isEqualTo("Tag"); + assertThat(Tag.create(K1, V2).toString()).isEqualTo("Tag"); + } + + private static final TagKey K1 = TagKey.create("k1"); + private static final TagKey K2 = TagKey.create("k2"); + private static final TagValue V1 = TagValue.create("v1"); + private static final TagValue V2 = TagValue.create("v2"); +} diff --git a/core/src/test/java/com/google/instrumentation/stats/TagValueTest.java b/core/src/test/java/com/google/instrumentation/stats/TagValueTest.java new file mode 100644 index 00000000..63b98bf5 --- /dev/null +++ b/core/src/test/java/com/google/instrumentation/stats/TagValueTest.java @@ -0,0 +1,59 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.common.testing.EqualsTester; +import java.util.Arrays; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Tests for {@link TagValue} + */ +@RunWith(JUnit4.class) +public final class TagValueTest { + @Test + public void testValueMaxLength() { + char[] value = new char[TagValue.MAX_LENGTH]; + char[] truncValue = new char[TagValue.MAX_LENGTH + 10]; + Arrays.fill(value, 'v'); + Arrays.fill(truncValue, 'v'); + assertThat(TagValue.create(new String(value)).toString()) + .isEqualTo(TagValue.create(new String(truncValue)).toString()); + } + + @Test + public void testValueBadChar() { + String value = "\2ab\3cd"; + assertThat(TagValue.create(value).toString()) + .isEqualTo(StringUtil.UNPRINTABLE_CHAR_SUBSTITUTE + "ab" + + StringUtil.UNPRINTABLE_CHAR_SUBSTITUTE + "cd"); + } + + @Test + public void testEquals() { + new EqualsTester() + .addEqualityGroup(TagValue.create("foo"), TagValue.create("foo")) + .addEqualityGroup(TagValue.create("bar")) + .testEquals(); + } + + @Test + public void testToString() { + assertThat(TagValue.create("foo").toString()).isEqualTo("foo"); + } +} diff --git a/core/src/test/java/com/google/instrumentation/stats/ViewDescriptorTest.java b/core/src/test/java/com/google/instrumentation/stats/ViewDescriptorTest.java new file mode 100644 index 00000000..f0781bf6 --- /dev/null +++ b/core/src/test/java/com/google/instrumentation/stats/ViewDescriptorTest.java @@ -0,0 +1,98 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import static com.google.common.truth.Truth.assertThat; + +import static org.junit.Assert.assertTrue; + +import com.google.instrumentation.common.Duration; +import com.google.instrumentation.common.Function; +import com.google.instrumentation.stats.MeasurementDescriptor.BasicUnit; +import com.google.instrumentation.stats.MeasurementDescriptor.MeasurementUnit; +import com.google.instrumentation.stats.ViewDescriptor.DistributionViewDescriptor; +import com.google.instrumentation.stats.ViewDescriptor.IntervalViewDescriptor; + +import java.util.Arrays; +import java.util.List; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Tests for {@link ViewDescriptor} + */ +@RunWith(JUnit4.class) +public final class ViewDescriptorTest { + @Test + public void testDistributionViewDescriptor() { + DistributionAggregationDescriptor dAggrDescriptor = DistributionAggregationDescriptor.create(); + final ViewDescriptor viewDescriptor = DistributionViewDescriptor.create( + name, description, measurementDescriptor, dAggrDescriptor, keys); + + assertThat(viewDescriptor.getName()).isEqualTo(name); + assertThat(viewDescriptor.getDescription()).isEqualTo(description); + assertThat(viewDescriptor.getMeasurementDescriptor().getName()) + .isEqualTo(measurementDescriptor.getName()); + assertThat(viewDescriptor.getTagKeys()).hasSize(2); + assertThat(viewDescriptor.getTagKeys().get(0).toString()).isEqualTo("foo"); + assertThat(viewDescriptor.getTagKeys().get(1).toString()).isEqualTo("bar"); + assertTrue(viewDescriptor.match( + new Function () { + @Override public Boolean apply(DistributionViewDescriptor dViewDescriptor) { + return dViewDescriptor == viewDescriptor; + } + }, + new Function () { + @Override public Boolean apply(IntervalViewDescriptor iViewDescriptor) { + return false; + } + })); + } + + @Test + public void testIntervalViewDescriptor() { + IntervalAggregationDescriptor iAggrDescriptor = IntervalAggregationDescriptor.create( + Arrays.asList(Duration.fromMillis(1), Duration.fromMillis(22), Duration.fromMillis(333))); + final ViewDescriptor viewDescriptor = IntervalViewDescriptor.create( + name, description, measurementDescriptor, iAggrDescriptor, keys); + + assertThat(viewDescriptor.getName()).isEqualTo(name); + assertThat(viewDescriptor.getDescription()).isEqualTo(description); + assertThat(viewDescriptor.getMeasurementDescriptor().getName()) + .isEqualTo(measurementDescriptor.getName()); + assertThat(viewDescriptor.getTagKeys()).hasSize(2); + assertThat(viewDescriptor.getTagKeys().get(0).toString()).isEqualTo("foo"); + assertThat(viewDescriptor.getTagKeys().get(1).toString()).isEqualTo("bar"); + assertTrue(viewDescriptor.match( + new Function () { + @Override public Boolean apply(DistributionViewDescriptor dViewDescriptor) { + return false; + } + }, + new Function () { + @Override public Boolean apply(IntervalViewDescriptor iViewDescriptor) { + return iViewDescriptor == viewDescriptor; + } + })); + } + + private final String name = "test-view-name"; + private final String description = "test-view-name description"; + private final MeasurementDescriptor measurementDescriptor = MeasurementDescriptor.create( + "measurement", + "measurement description", + MeasurementUnit.create(1, Arrays.asList(BasicUnit.SCALAR))); + private final List keys = Arrays.asList(TagKey.create("foo"), TagKey.create("bar")); +} diff --git a/core/src/test/java/com/google/instrumentation/stats/ViewTest.java b/core/src/test/java/com/google/instrumentation/stats/ViewTest.java new file mode 100644 index 00000000..5a057918 --- /dev/null +++ b/core/src/test/java/com/google/instrumentation/stats/ViewTest.java @@ -0,0 +1,143 @@ +/* + * Copyright 2016, Google Inc. + * 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 com.google.instrumentation.stats; + +import static com.google.common.truth.Truth.assertThat; + +import static org.junit.Assert.assertTrue; + +import com.google.instrumentation.common.Duration; +import com.google.instrumentation.common.Function; +import com.google.instrumentation.common.Timestamp; +import com.google.instrumentation.stats.DistributionAggregation.Range; +import com.google.instrumentation.stats.IntervalAggregation.Interval; +import com.google.instrumentation.stats.MeasurementDescriptor.BasicUnit; +import com.google.instrumentation.stats.MeasurementDescriptor.MeasurementUnit; +import com.google.instrumentation.stats.View.DistributionView; +import com.google.instrumentation.stats.View.IntervalView; +import com.google.instrumentation.stats.ViewDescriptor.DistributionViewDescriptor; +import com.google.instrumentation.stats.ViewDescriptor.IntervalViewDescriptor; + +import java.util.Arrays; +import java.util.List; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Tests for class {@link View}. + */ +@RunWith(JUnit4.class) +public final class ViewTest { + @Test + public void testDistributionView() { + DistributionAggregationDescriptor aggregationDescriptor = + DistributionAggregationDescriptor.create(Arrays.asList(10.0, 20.0, 30.0, 40.0)); + final DistributionViewDescriptor viewDescriptor = + DistributionViewDescriptor.create( + name, description, measurementDescriptor, aggregationDescriptor, tagKeys); + List aggregations = Arrays.asList( + DistributionAggregation.create(5, 5.0, 15.0, Range.create(1.0, 5.0), tags1, + Arrays.asList(1L, 1L, 1L, 1L, 1L)), + DistributionAggregation.create(10, 5.0, 30.0, Range.create(1.0, 5.0), tags2, + Arrays.asList(2L, 2L, 2L, 2L, 2L))); + final Timestamp start = Timestamp.fromMillis(1000); + final Timestamp end = Timestamp.fromMillis(2000); + final View view = DistributionView.create(viewDescriptor, aggregations, start, end); + + assertThat(view.getViewDescriptor()).isEqualTo(viewDescriptor); + assertTrue(view.match( + new Function () { + @Override public Boolean apply(DistributionView dView) { + return dView == view + && dView.getViewDescriptor().equals(viewDescriptor) + && shallowListEquals(dView.getDistributionAggregations(), aggregations) + && dView.getStart().equals(start) + && dView.getEnd().equals(end); + } + }, + new Function () { + @Override public Boolean apply(IntervalView iView) { + return false; + } + })); + } + + @Test + public void testIntervalView() { + IntervalAggregationDescriptor aggregationDescriptor = + IntervalAggregationDescriptor.create(Arrays.asList(Duration.fromMillis(111))); + final IntervalViewDescriptor viewDescriptor = + IntervalViewDescriptor.create( + name, description, measurementDescriptor, aggregationDescriptor, tagKeys); + final List aggregations = Arrays.asList( + IntervalAggregation.create(tags1, Arrays.asList( + Interval.create(Duration.fromMillis(111), 10, 100))), + IntervalAggregation.create(tags2, Arrays.asList( + Interval.create(Duration.fromMillis(111), 10, 100)))); + + final View view = IntervalView.create(viewDescriptor, aggregations); + assertThat(view.getViewDescriptor()).isEqualTo(viewDescriptor); + assertTrue(view.match( + new Function () { + @Override public Boolean apply(DistributionView dView) { + return false; + } + }, + new Function () { + @Override public Boolean apply(IntervalView iView) { + return iView == view + && iView.getViewDescriptor().equals(viewDescriptor) + && shallowListEquals(iView.getIntervalAggregations(), aggregations); + } + })); + } + + // tag keys + private static final TagKey K1 = TagKey.create("k1"); + private static final TagKey K2 = TagKey.create("k2"); + private final List tagKeys = Arrays.asList(K1, K2); + + // tag values + private static final TagValue V1 = TagValue.create("v1"); + private static final TagValue V2 = TagValue.create("v2"); + private static final TagValue V10 = TagValue.create("v10"); + private static final TagValue V20 = TagValue.create("v20"); + + // tags + List tags1 = Arrays.asList(Tag.create(K1, V1), Tag.create(K2, V2)); + List tags2 = Arrays.asList(Tag.create(K1, V10), Tag.create(K2, V20)); + + // name + private final String name = "test-view-descriptor"; + // description + private final String description = "test-view-descriptor description"; + // measurement descriptor + private final MeasurementDescriptor measurementDescriptor = MeasurementDescriptor.create( + "measurement-descriptor", + "measurement-descriptor description", + MeasurementUnit.create(1, Arrays.asList(BasicUnit.SCALAR))); + + private static final boolean shallowListEquals(List l1, List l2) { + if (l1.size() != l2.size()) { + return false; + } + for (int i = 0; i < l1.size(); i++) { + if (l1.get(i) != l2.get(i)) { + return false; + } + } + return true; + } +} -- cgit v1.2.3