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

Commit 29aa3ca

Browse files
authored
change unit to dimensionless when aggregation is count. (#1157)
1 parent b4a1468 commit 29aa3ca

2 files changed

Lines changed: 69 additions & 1 deletion

File tree

stats/view/view_to_metric.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,21 @@ func viewToMetricDescriptor(v *View) *metricdata.Descriptor {
8585
return &metricdata.Descriptor{
8686
Name: v.Name,
8787
Description: v.Description,
88-
Unit: getUnit(v.Measure.Unit()),
88+
Unit: convertUnit(v),
8989
Type: getType(v),
9090
LabelKeys: getLabelKeys(v),
9191
}
9292
}
9393

94+
func convertUnit(v *View) metricdata.Unit {
95+
switch v.Aggregation.Type {
96+
case AggTypeCount:
97+
return metricdata.UnitDimensionless
98+
default:
99+
return getUnit(v.Measure.Unit())
100+
}
101+
}
102+
94103
func toLabelValues(row *Row, expectedKeys []metricdata.LabelKey) []metricdata.LabelValue {
95104
labelValues := []metricdata.LabelValue{}
96105
tagMap := make(map[string]string)

stats/view/view_to_metric_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
460519
func serializeAsJSON(v interface{}) string {
461520
blob, _ := json.MarshalIndent(v, "", " ")
462521
return string(blob)

0 commit comments

Comments
 (0)