metricsEnabledDimensions) {
+ this.metricsLevel = metricsLevel;
+ this.metricsEnabledDimensions = metricsEnabledDimensions;
+ this.metricsEnabledDimensionsAll = (metricsEnabledDimensions != null &&
+ metricsEnabledDimensions.contains(METRICS_DIMENSIONS_ALL));
+ }
+
+ /**
+ * Adds the data to the metrics scope at lowest metrics level.
+ * @param name Metrics data name.
+ * @param value Value of the metrics.
+ * @param unit Unit of the metrics.
+ */
+ @Override
+ public void addData(String name, double value, StandardUnit unit) {
+ addData(name, value, unit, MetricsLevel.DETAILED);
+ }
+
+ /**
+ * Adds the data to the metrics scope if the given level is equal to above the enabled metrics
+ * level.
+ * @param name Metrics data name.
+ * @param value Value of the metrics.
+ * @param unit Unit of the metrics.
+ * @param level Metrics level for the data.
+ */
+ @Override
+ public void addData(String name, double value, StandardUnit unit, MetricsLevel level) {
+ if (level.getValue() < metricsLevel.getValue()) {
+ // Drop the data.
+ return;
+ }
+ super.addData(name, value, unit);
+ }
+
+ /**
+ * Adds given dimension with value if allowed dimensions list contains this dimension's name.
+ * @param name Name of the dimension.
+ * @param value Value for the dimension.
+ */
+ @Override
+ public void addDimension(String name, String value) {
+ if (!metricsEnabledDimensionsAll &&
+ (metricsEnabledDimensions == null || !metricsEnabledDimensions.contains(name))) {
+ // Drop dimension.
+ return;
+ }
+ super.addDimension(name, value);
+ }
+}
diff --git a/src/main/java/com/amazonaws/services/kinesis/metrics/impl/InterceptingMetricsFactory.java b/src/main/java/com/amazonaws/services/kinesis/metrics/impl/InterceptingMetricsFactory.java
index 3e30c139..f89f9550 100644
--- a/src/main/java/com/amazonaws/services/kinesis/metrics/impl/InterceptingMetricsFactory.java
+++ b/src/main/java/com/amazonaws/services/kinesis/metrics/impl/InterceptingMetricsFactory.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2012-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
+ * Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Amazon Software License (the "License").
* You may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@ package com.amazonaws.services.kinesis.metrics.impl;
import com.amazonaws.services.cloudwatch.model.StandardUnit;
import com.amazonaws.services.kinesis.metrics.interfaces.IMetricsFactory;
import com.amazonaws.services.kinesis.metrics.interfaces.IMetricsScope;
+import com.amazonaws.services.kinesis.metrics.interfaces.MetricsLevel;
public abstract class InterceptingMetricsFactory implements IMetricsFactory {
@@ -41,6 +42,10 @@ public abstract class InterceptingMetricsFactory implements IMetricsFactory {
scope.addData(name, value, unit);
}
+ protected void interceptAddData(String name, double value, StandardUnit unit, MetricsLevel level, IMetricsScope scope) {
+ scope.addData(name, value, unit, level);
+ }
+
protected void interceptAddDimension(String name, String value, IMetricsScope scope) {
scope.addDimension(name, value);
}
@@ -62,6 +67,11 @@ public abstract class InterceptingMetricsFactory implements IMetricsFactory {
interceptAddData(name, value, unit, other);
}
+ @Override
+ public void addData(String name, double value, StandardUnit unit, MetricsLevel level) {
+ interceptAddData(name, value, unit, level, other);
+ }
+
@Override
public void addDimension(String name, String value) {
interceptAddDimension(name, value, other);
diff --git a/src/main/java/com/amazonaws/services/kinesis/metrics/impl/MetricsHelper.java b/src/main/java/com/amazonaws/services/kinesis/metrics/impl/MetricsHelper.java
index a4d9d9d2..fe5982fa 100644
--- a/src/main/java/com/amazonaws/services/kinesis/metrics/impl/MetricsHelper.java
+++ b/src/main/java/com/amazonaws/services/kinesis/metrics/impl/MetricsHelper.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2012-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
+ * Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Amazon Software License (the "License").
* You may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import org.apache.commons.logging.LogFactory;
import com.amazonaws.services.cloudwatch.model.StandardUnit;
import com.amazonaws.services.kinesis.metrics.interfaces.IMetricsFactory;
import com.amazonaws.services.kinesis.metrics.interfaces.IMetricsScope;
+import com.amazonaws.services.kinesis.metrics.interfaces.MetricsLevel;
/**
* MetricsHelper assists with common metrics operations, most notably the storage of IMetricsScopes objects in a
@@ -37,6 +38,7 @@ public class MetricsHelper {
* Constants used to publish metrics.
*/
public static final String OPERATION_DIMENSION_NAME = "Operation";
+ public static final String SHARD_ID_DIMENSION_NAME = "ShardId";
public static final String TIME = "Time";
public static final String SUCCESS = "Success";
private static final String SEP = ".";
@@ -73,31 +75,53 @@ public class MetricsHelper {
}
}
- public static void addSuccessAndLatency(long startTimeMillis, boolean success) {
- addSuccessAndLatency(null, startTimeMillis, success);
+ public static void addSuccessAndLatency(long startTimeMillis, boolean success, MetricsLevel level) {
+ addSuccessAndLatency(null, startTimeMillis, success, level);
}
- public static void addSuccessAndLatency(String prefix, long startTimeMillis, boolean success) {
- addSuccessAndLatencyPerShard(null, prefix, startTimeMillis, success);
+ public static void addSuccessAndLatency(
+ String prefix, long startTimeMillis, boolean success, MetricsLevel level) {
+ addSuccessAndLatencyPerShard(null, prefix, startTimeMillis, success, level);
}
public static void addSuccessAndLatencyPerShard (
String shardId,
String prefix,
long startTimeMillis,
- boolean success) {
+ boolean success,
+ MetricsLevel level) {
+ addSuccessAndLatency(shardId, prefix, startTimeMillis, success, level, true, true);
+ }
+
+ public static void addLatency(long startTimeMillis, MetricsLevel level) {
+ addLatency(null, startTimeMillis, level);
+ }
+
+ public static void addLatency(String prefix, long startTimeMillis, MetricsLevel level) {
+ addLatencyPerShard(null, prefix, startTimeMillis, level);
+ }
+
+ public static void addLatencyPerShard(String shardId, String prefix, long startTimeMillis, MetricsLevel level) {
+ addSuccessAndLatency(shardId, prefix, startTimeMillis, false, level, false, true);
+ }
+
+ private static void addSuccessAndLatency(
+ String shardId, String prefix, long startTimeMillis, boolean success, MetricsLevel level,
+ boolean includeSuccess, boolean includeLatency) {
IMetricsScope scope = getMetricsScope();
String realPrefix = prefix == null ? "" : prefix + SEP;
if (shardId != null) {
- scope.addDimension("ShardId", shardId);
+ scope.addDimension(SHARD_ID_DIMENSION_NAME, shardId);
+ }
+ if (includeSuccess) {
+ scope.addData(realPrefix + MetricsHelper.SUCCESS, success ? 1 : 0, StandardUnit.Count, level);
+ }
+ if (includeLatency) {
+ scope.addData(realPrefix + MetricsHelper.TIME,
+ System.currentTimeMillis() - startTimeMillis, StandardUnit.Milliseconds, level);
}
-
- scope.addData(realPrefix + MetricsHelper.SUCCESS, success ? 1 : 0, StandardUnit.Count);
- scope.addData(realPrefix + MetricsHelper.TIME,
- System.currentTimeMillis() - startTimeMillis,
- StandardUnit.Milliseconds);
}
public static void endScope() {
diff --git a/src/main/java/com/amazonaws/services/kinesis/metrics/impl/NullMetricsScope.java b/src/main/java/com/amazonaws/services/kinesis/metrics/impl/NullMetricsScope.java
index ff2e32a4..7d66dffc 100644
--- a/src/main/java/com/amazonaws/services/kinesis/metrics/impl/NullMetricsScope.java
+++ b/src/main/java/com/amazonaws/services/kinesis/metrics/impl/NullMetricsScope.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2012-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
+ * Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Amazon Software License (the "License").
* You may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@ package com.amazonaws.services.kinesis.metrics.impl;
import com.amazonaws.services.cloudwatch.model.StandardUnit;
import com.amazonaws.services.kinesis.metrics.interfaces.IMetricsScope;
+import com.amazonaws.services.kinesis.metrics.interfaces.MetricsLevel;
public class NullMetricsScope implements IMetricsScope {
@@ -24,6 +25,11 @@ public class NullMetricsScope implements IMetricsScope {
}
+ @Override
+ public void addData(String name, double value, StandardUnit unit, MetricsLevel level) {
+
+ }
+
@Override
public void addDimension(String name, String value) {
@@ -33,5 +39,4 @@ public class NullMetricsScope implements IMetricsScope {
public void end() {
}
-
}
diff --git a/src/main/java/com/amazonaws/services/kinesis/metrics/interfaces/IMetricsScope.java b/src/main/java/com/amazonaws/services/kinesis/metrics/interfaces/IMetricsScope.java
index 4d9edbf7..5683b345 100644
--- a/src/main/java/com/amazonaws/services/kinesis/metrics/interfaces/IMetricsScope.java
+++ b/src/main/java/com/amazonaws/services/kinesis/metrics/interfaces/IMetricsScope.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2012-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
+ * Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Amazon Software License (the "License").
* You may not use this file except in compliance with the License.
@@ -22,6 +22,11 @@ import com.amazonaws.services.cloudwatch.model.StandardUnit;
*/
public interface IMetricsScope {
+ /**
+ * Value that signifies that all dimensions are allowed for the metrics scope.
+ */
+ public static final String METRICS_DIMENSIONS_ALL = "ALL";
+
/**
* Adds a data point to this IMetricsScope. Multiple calls against the same IMetricsScope with the same name
* parameter will result in accumulation.
@@ -32,6 +37,17 @@ public interface IMetricsScope {
*/
public void addData(String name, double value, StandardUnit unit);
+ /**
+ * Adds a data point to this IMetricsScope if given metrics level is enabled. Multiple calls against the same
+ * IMetricsScope with the same name parameter will result in accumulation.
+ *
+ * @param name data point name
+ * @param value data point value
+ * @param unit unit of data point
+ * @param level metrics level of this data point
+ */
+ public void addData(String name, double value, StandardUnit unit, MetricsLevel level);
+
/**
* Adds a dimension that applies to all metrics in this IMetricsScope.
*
diff --git a/src/main/java/com/amazonaws/services/kinesis/metrics/interfaces/MetricsLevel.java b/src/main/java/com/amazonaws/services/kinesis/metrics/interfaces/MetricsLevel.java
new file mode 100644
index 00000000..5ad9ed48
--- /dev/null
+++ b/src/main/java/com/amazonaws/services/kinesis/metrics/interfaces/MetricsLevel.java
@@ -0,0 +1,93 @@
+/*
+ * Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Amazon Software License (the "License").
+ * You may not use this file except in compliance with the License.
+ * A copy of the License is located at
+ *
+ * http://aws.amazon.com/asl/
+ *
+ * or in the "license" file accompanying this file. This file 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.amazonaws.services.kinesis.metrics.interfaces;
+
+/**
+ * This class defines a set of standard metrics levels that can be used to control which metrics get emitted.
+ * {@code MetricsLevel} objects are ordered and are specified by ordered integers. Enabling metrics at a given
+ * level also enables the metrics at all higher levels.
+ *
+ * Metrics levels in descending order are:
+ *
+ * - SUMMARY
+ * - DETAILED
+ *
+ * In addition, NONE level can be used to turn off all metrics.
+ */
+public enum MetricsLevel {
+
+ /**
+ * NONE metrics level can be used to turn off metrics.
+ */
+ NONE("NONE", Integer.MAX_VALUE),
+
+ /**
+ * SUMMARY metrics level can be used to emit only the most significant metrics.
+ */
+ SUMMARY("SUMMARY", 10000),
+
+ /**
+ * DETAILED metrics level can be used to emit all metrics.
+ */
+ DETAILED("DETAILED", 9000);
+
+ /**
+ * Name of the metrics level.
+ */
+ private final String name;
+
+ /**
+ * Integer value of the metrics level.
+ */
+ private final int value;
+
+ /**
+ * Creates metrics level with given name and value.
+ * @param name Metrics level name
+ * @param value Metrics level value
+ */
+ private MetricsLevel(String name, int value) {
+ this.name = name;
+ this.value = value;
+ }
+
+ /**
+ * Returns the name for this metrics level.
+ * @return Returns the name for this metrics level.
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * Returns the value for this metrics level.
+ * @return Returns the value for this metrics level.
+ */
+ public int getValue() {
+ return value;
+ }
+
+ /**
+ * Returns metrics level associated with the given name.
+ * @param name Name of the metrics level.
+ * @return Returns metrics level associated with the given name.
+ */
+ public static MetricsLevel fromName(String name) {
+ if (name != null) {
+ name = name.toUpperCase();
+ }
+ return valueOf(name);
+ }
+}