Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit b04ec36

Browse files
authored
Count metrics should have unit '1'. (#1942)
1 parent 967ad5a commit b04ec36

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

impl_core/src/main/java/io/opencensus/implcore/stats/MetricUtils.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.opencensus.metrics.export.MetricDescriptor;
2626
import io.opencensus.metrics.export.MetricDescriptor.Type;
2727
import io.opencensus.stats.Aggregation;
28+
import io.opencensus.stats.Aggregation.Count;
2829
import io.opencensus.stats.Measure;
2930
import io.opencensus.stats.View;
3031
import io.opencensus.tags.TagKey;
@@ -42,6 +43,8 @@
4243
// Utils to convert Stats data models to Metric data models.
4344
final class MetricUtils {
4445

46+
@VisibleForTesting static final String COUNT_UNIT = "1";
47+
4548
@javax.annotation.Nullable
4649
static MetricDescriptor viewToMetricDescriptor(View view) {
4750
if (view.getWindow() instanceof View.AggregationWindow.Interval) {
@@ -54,11 +57,12 @@ static MetricDescriptor viewToMetricDescriptor(View view) {
5457
labelKeys.add(LabelKey.create(tagKey.getName(), ""));
5558
}
5659
Measure measure = view.getMeasure();
60+
Aggregation aggregation = view.getAggregation();
5761
return MetricDescriptor.create(
5862
view.getName().asString(),
5963
view.getDescription(),
60-
measure.getUnit(),
61-
getType(measure, view.getAggregation()),
64+
getUnit(measure, aggregation),
65+
getType(measure, aggregation),
6266
labelKeys);
6367
}
6468

@@ -80,6 +84,13 @@ static Type getType(Measure measure, Aggregation aggregation) {
8084
AGGREGATION_TYPE_DEFAULT_FUNCTION);
8185
}
8286

87+
private static String getUnit(Measure measure, Aggregation aggregation) {
88+
if (aggregation instanceof Count) {
89+
return COUNT_UNIT;
90+
}
91+
return measure.getUnit();
92+
}
93+
8394
static List<LabelValue> tagValuesToLabelValues(List</*@Nullable*/ TagValue> tagValues) {
8495
List<LabelValue> labelValues = new ArrayList<LabelValue>();
8596
for (/*@Nullable*/ TagValue tagValue : tagValues) {

impl_core/src/test/java/io/opencensus/implcore/stats/MetricUtilsTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ public class MetricUtilsTest {
8181
MEAN,
8282
Collections.singletonList(KEY),
8383
INTERVAL);
84+
private static final View VIEW_3 =
85+
View.create(
86+
VIEW_NAME, VIEW_DESCRIPTION, MEASURE_DOUBLE, COUNT, Collections.singletonList(KEY));
8487

8588
@Test
8689
public void viewToMetricDescriptor() {
@@ -93,6 +96,17 @@ public void viewToMetricDescriptor() {
9396
assertThat(metricDescriptor.getLabelKeys()).containsExactly(LabelKey.create(KEY.getName(), ""));
9497
}
9598

99+
@Test
100+
public void viewToMetricDescriptor_Count() {
101+
MetricDescriptor metricDescriptor = MetricUtils.viewToMetricDescriptor(VIEW_3);
102+
assertThat(metricDescriptor).isNotNull();
103+
assertThat(metricDescriptor.getName()).isEqualTo(VIEW_NAME.asString());
104+
assertThat(metricDescriptor.getUnit()).isEqualTo(MetricUtils.COUNT_UNIT);
105+
assertThat(metricDescriptor.getType()).isEqualTo(Type.CUMULATIVE_INT64);
106+
assertThat(metricDescriptor.getDescription()).isEqualTo(VIEW_DESCRIPTION);
107+
assertThat(metricDescriptor.getLabelKeys()).containsExactly(LabelKey.create(KEY.getName(), ""));
108+
}
109+
96110
@Test
97111
public void viewToMetricDescriptor_NoIntervalViews() {
98112
MetricDescriptor metricDescriptor = MetricUtils.viewToMetricDescriptor(VIEW_2);

0 commit comments

Comments
 (0)