@@ -24,6 +24,7 @@ import (
2424
2525 "github.com/google/go-cmp/cmp"
2626 "go.opencensus.io/metric/metricdata"
27+ "go.opencensus.io/metric/metricexport"
2728 "go.opencensus.io/stats"
2829 "go.opencensus.io/tag"
2930)
@@ -516,6 +517,139 @@ func TestUnitConversionForAggCount(t *testing.T) {
516517 }
517518}
518519
520+ type mockExp struct {
521+ metrics []* metricdata.Metric
522+ }
523+
524+ func (me * mockExp ) ExportMetrics (ctx context.Context , metrics []* metricdata.Metric ) error {
525+ me .metrics = append (me .metrics , metrics ... )
526+ return nil
527+ }
528+
529+ var _ metricexport.Exporter = (* mockExp )(nil )
530+
531+ func TestViewToMetric_OutOfOrderWithZeroBuckets (t * testing.T ) {
532+ m := stats .Int64 ("OutOfOrderWithZeroBuckets" , "" , "" )
533+ now := time .Now ()
534+ tts := []struct {
535+ v * View
536+ m * metricdata.Metric
537+ }{
538+ {
539+ v : & View {
540+ Name : m .Name () + "_order1" ,
541+ Measure : m ,
542+ Aggregation : Distribution (10 , 0 , 2 ),
543+ },
544+ m : & metricdata.Metric {
545+ Descriptor : metricdata.Descriptor {
546+ Name : "OutOfOrderWithZeroBuckets_order1" ,
547+ Unit : metricdata .UnitDimensionless ,
548+ Type : metricdata .TypeCumulativeDistribution ,
549+ LabelKeys : []metricdata.LabelKey {},
550+ },
551+ TimeSeries : []* metricdata.TimeSeries {
552+ {Points : []metricdata.Point {
553+ {Value : & metricdata.Distribution {
554+ Count : 3 ,
555+ Sum : 9.0 ,
556+ SumOfSquaredDeviation : 8 ,
557+ BucketOptions : & metricdata.BucketOptions {
558+ Bounds : []float64 {2 , 10 },
559+ },
560+ Buckets : []metricdata.Bucket {
561+ {Count : 1 , Exemplar : nil },
562+ {Count : 2 , Exemplar : nil },
563+ {Count : 0 , Exemplar : nil },
564+ },
565+ },
566+ Time : now ,
567+ },
568+ },
569+ StartTime : now ,
570+ LabelValues : []metricdata.LabelValue {},
571+ },
572+ },
573+ },
574+ },
575+ {
576+ v : & View {
577+ Name : m .Name () + "_order2" ,
578+ Measure : m ,
579+ Aggregation : Distribution (0 , 5 , 10 ),
580+ },
581+ m : & metricdata.Metric {
582+ Descriptor : metricdata.Descriptor {
583+ Name : "OutOfOrderWithZeroBuckets_order2" ,
584+ Unit : metricdata .UnitDimensionless ,
585+ Type : metricdata .TypeCumulativeDistribution ,
586+ LabelKeys : []metricdata.LabelKey {},
587+ },
588+ TimeSeries : []* metricdata.TimeSeries {
589+ {Points : []metricdata.Point {
590+ {Value : & metricdata.Distribution {
591+ Count : 3 ,
592+ Sum : 9.0 ,
593+ SumOfSquaredDeviation : 8 ,
594+ BucketOptions : & metricdata.BucketOptions {
595+ Bounds : []float64 {5 , 10 },
596+ },
597+ Buckets : []metricdata.Bucket {
598+ {Count : 2 , Exemplar : nil },
599+ {Count : 1 , Exemplar : nil },
600+ {Count : 0 , Exemplar : nil },
601+ },
602+ },
603+ Time : now ,
604+ },
605+ },
606+ StartTime : now ,
607+ LabelValues : []metricdata.LabelValue {},
608+ },
609+ },
610+ },
611+ },
612+ }
613+ for _ , tt := range tts {
614+ err := Register (tt .v )
615+ if err != nil {
616+ t .Fatalf ("error registering view %v, err: %v" , tt .v , err )
617+ }
618+
619+ }
620+
621+ stats .Record (context .Background (), m .M (5 ), m .M (1 ), m .M (3 ))
622+ time .Sleep (1 * time .Second )
623+
624+ me := & mockExp {}
625+ reader := metricexport .NewReader ()
626+ reader .ReadAndExport (me )
627+
628+ var got * metricdata.Metric
629+ lookup := func (vname string , metrics []* metricdata.Metric ) * metricdata.Metric {
630+ for _ , m := range metrics {
631+ if m .Descriptor .Name == vname {
632+ return m
633+ }
634+ }
635+ return nil
636+ }
637+
638+ for _ , tt := range tts {
639+ got = lookup (tt .v .Name , me .metrics )
640+ if got == nil {
641+ t .Fatalf ("metric %s not found in %v\n " , tt .v .Name , me .metrics )
642+ }
643+ got .TimeSeries [0 ].Points [0 ].Time = now
644+ got .TimeSeries [0 ].StartTime = now
645+
646+ want := tt .m
647+ if diff := cmp .Diff (got , want ); diff != "" {
648+ t .Errorf ("buckets differ -got +want: %s \n Serialized got %v\n , Serialized want %v\n " , diff , serializeAsJSON (got ), serializeAsJSON (want ))
649+ }
650+ }
651+ }
652+
519653func serializeAsJSON (v interface {}) string {
520654 blob , _ := json .MarshalIndent (v , "" , " " )
521655 return string (blob )
0 commit comments