@@ -457,6 +457,65 @@ func Test_ViewToMetric(t *testing.T) {
457457 }
458458}
459459
460+ // Test to verify that a metric converted from a view with Aggregation Count should always
461+ // have Dimensionless unit.
462+ func TestUnitConversionForAggCount (t * testing.T ) {
463+ startTime := time .Now ().Add (- time .Duration (60 * time .Second ))
464+ now := time .Now ()
465+ tests := []* struct {
466+ name string
467+ vi * viewInternal
468+ v * View
469+ wantUnit metricdata.Unit
470+ }{
471+ {
472+ name : "View with Count Aggregation on Latency measurement" ,
473+ v : & View {
474+ Name : "request_count1" ,
475+ Measure : stats .Int64 ("request_latency" , "" , stats .UnitMilliseconds ),
476+ Aggregation : aggCnt ,
477+ },
478+ wantUnit : metricdata .UnitDimensionless ,
479+ },
480+ {
481+ name : "View with Count Aggregation on bytes measurement" ,
482+ v : & View {
483+ Name : "request_count2" ,
484+ Measure : stats .Int64 ("request_bytes" , "" , stats .UnitBytes ),
485+ Aggregation : aggCnt ,
486+ },
487+ wantUnit : metricdata .UnitDimensionless ,
488+ },
489+ {
490+ name : "View with aggregation other than Count Aggregation on Latency measurement" ,
491+ v : & View {
492+ Name : "request_latency" ,
493+ Measure : stats .Int64 ("request_latency" , "" , stats .UnitMilliseconds ),
494+ Aggregation : aggSum ,
495+ },
496+ wantUnit : metricdata .UnitMilliseconds ,
497+ },
498+ }
499+ var err error
500+ for _ , tc := range tests {
501+ tc .vi , err = defaultWorker .tryRegisterView (tc .v )
502+ if err != nil {
503+ t .Fatalf ("error registering view: %v, err: %v\n " , tc .v , err )
504+ }
505+ tc .vi .clearRows ()
506+ tc .vi .subscribe ()
507+ }
508+
509+ for _ , tc := range tests {
510+ tc .vi .addSample (tag .FromContext (context .Background ()), 5.0 , nil , now )
511+ gotMetric := viewToMetric (tc .vi , now , startTime )
512+ gotUnit := gotMetric .Descriptor .Unit
513+ if ! cmp .Equal (gotUnit , tc .wantUnit ) {
514+ t .Errorf ("Verify Unit: %s: Got:%v Want:%v" , tc .name , gotUnit , tc .wantUnit )
515+ }
516+ }
517+ }
518+
460519func serializeAsJSON (v interface {}) string {
461520 blob , _ := json .MarshalIndent (v , "" , " " )
462521 return string (blob )
0 commit comments