aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYang Song <songy23@users.noreply.github.com>2019-06-18 12:40:10 -0700
committerGitHub <noreply@github.com>2019-06-18 12:40:10 -0700
commitb04ec360d0663f3d54e109497e85f333d1592665 (patch)
tree3abfbe640b4574835bbd81d482b7d3ac3de80e16
parent967ad5a6b880f13acba83471c9ef404a710d8bd9 (diff)
downloadplatform_external_opencensus-java-b04ec360d0663f3d54e109497e85f333d1592665.tar.gz
platform_external_opencensus-java-b04ec360d0663f3d54e109497e85f333d1592665.tar.bz2
platform_external_opencensus-java-b04ec360d0663f3d54e109497e85f333d1592665.zip
Count metrics should have unit '1'. (#1942)
-rw-r--r--impl_core/src/main/java/io/opencensus/implcore/stats/MetricUtils.java15
-rw-r--r--impl_core/src/test/java/io/opencensus/implcore/stats/MetricUtilsTest.java14
2 files changed, 27 insertions, 2 deletions
diff --git a/impl_core/src/main/java/io/opencensus/implcore/stats/MetricUtils.java b/impl_core/src/main/java/io/opencensus/implcore/stats/MetricUtils.java
index 34b783e8..d4dc81de 100644
--- a/impl_core/src/main/java/io/opencensus/implcore/stats/MetricUtils.java
+++ b/impl_core/src/main/java/io/opencensus/implcore/stats/MetricUtils.java
@@ -25,6 +25,7 @@ import io.opencensus.metrics.data.AttachmentValue;
import io.opencensus.metrics.export.MetricDescriptor;
import io.opencensus.metrics.export.MetricDescriptor.Type;
import io.opencensus.stats.Aggregation;
+import io.opencensus.stats.Aggregation.Count;
import io.opencensus.stats.Measure;
import io.opencensus.stats.View;
import io.opencensus.tags.TagKey;
@@ -42,6 +43,8 @@ import org.checkerframework.checker.nullness.qual.Nullable;
// Utils to convert Stats data models to Metric data models.
final class MetricUtils {
+ @VisibleForTesting static final String COUNT_UNIT = "1";
+
@javax.annotation.Nullable
static MetricDescriptor viewToMetricDescriptor(View view) {
if (view.getWindow() instanceof View.AggregationWindow.Interval) {
@@ -54,11 +57,12 @@ final class MetricUtils {
labelKeys.add(LabelKey.create(tagKey.getName(), ""));
}
Measure measure = view.getMeasure();
+ Aggregation aggregation = view.getAggregation();
return MetricDescriptor.create(
view.getName().asString(),
view.getDescription(),
- measure.getUnit(),
- getType(measure, view.getAggregation()),
+ getUnit(measure, aggregation),
+ getType(measure, aggregation),
labelKeys);
}
@@ -80,6 +84,13 @@ final class MetricUtils {
AGGREGATION_TYPE_DEFAULT_FUNCTION);
}
+ private static String getUnit(Measure measure, Aggregation aggregation) {
+ if (aggregation instanceof Count) {
+ return COUNT_UNIT;
+ }
+ return measure.getUnit();
+ }
+
static List<LabelValue> tagValuesToLabelValues(List</*@Nullable*/ TagValue> tagValues) {
List<LabelValue> labelValues = new ArrayList<LabelValue>();
for (/*@Nullable*/ TagValue tagValue : tagValues) {
diff --git a/impl_core/src/test/java/io/opencensus/implcore/stats/MetricUtilsTest.java b/impl_core/src/test/java/io/opencensus/implcore/stats/MetricUtilsTest.java
index 302ddcd4..f5a35d75 100644
--- a/impl_core/src/test/java/io/opencensus/implcore/stats/MetricUtilsTest.java
+++ b/impl_core/src/test/java/io/opencensus/implcore/stats/MetricUtilsTest.java
@@ -81,6 +81,9 @@ public class MetricUtilsTest {
MEAN,
Collections.singletonList(KEY),
INTERVAL);
+ private static final View VIEW_3 =
+ View.create(
+ VIEW_NAME, VIEW_DESCRIPTION, MEASURE_DOUBLE, COUNT, Collections.singletonList(KEY));
@Test
public void viewToMetricDescriptor() {
@@ -94,6 +97,17 @@ public class MetricUtilsTest {
}
@Test
+ public void viewToMetricDescriptor_Count() {
+ MetricDescriptor metricDescriptor = MetricUtils.viewToMetricDescriptor(VIEW_3);
+ assertThat(metricDescriptor).isNotNull();
+ assertThat(metricDescriptor.getName()).isEqualTo(VIEW_NAME.asString());
+ assertThat(metricDescriptor.getUnit()).isEqualTo(MetricUtils.COUNT_UNIT);
+ assertThat(metricDescriptor.getType()).isEqualTo(Type.CUMULATIVE_INT64);
+ assertThat(metricDescriptor.getDescription()).isEqualTo(VIEW_DESCRIPTION);
+ assertThat(metricDescriptor.getLabelKeys()).containsExactly(LabelKey.create(KEY.getName(), ""));
+ }
+
+ @Test
public void viewToMetricDescriptor_NoIntervalViews() {
MetricDescriptor metricDescriptor = MetricUtils.viewToMetricDescriptor(VIEW_2);
assertThat(metricDescriptor).isNull();