2525import io .opencensus .metrics .export .MetricDescriptor ;
2626import io .opencensus .metrics .export .MetricDescriptor .Type ;
2727import io .opencensus .stats .Aggregation ;
28+ import io .opencensus .stats .Aggregation .Count ;
2829import io .opencensus .stats .Measure ;
2930import io .opencensus .stats .View ;
3031import io .opencensus .tags .TagKey ;
4243// Utils to convert Stats data models to Metric data models.
4344final 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 ) {
0 commit comments