@@ -562,6 +562,9 @@ func TestSample(t *testing.T) {
562
562
func TestExtract (t * testing.T ) {
563
563
ss := newTestSink (0 * time .Second , nil )
564
564
ss .extractor = func (key []string , kind string ) ([]string , []metrics.Label , error ) {
565
+ if len (key ) < 2 {
566
+ return nil , nil , errors .New ("bad extraction" )
567
+ }
565
568
return key [:1 ], []metrics.Label {
566
569
{
567
570
Name : "method" ,
@@ -732,6 +735,159 @@ func TestExtract(t *testing.T) {
732
735
}
733
736
},
734
737
},
738
+ {
739
+ name : "histogram error, no requests" ,
740
+ collect : func () {
741
+ ss .AddSample ([]string {"foo" }, 5.0 )
742
+ },
743
+ createFn : func (t * testing.T ) func (context.Context , * monitoringpb.CreateTimeSeriesRequest ) (* emptypb.Empty , error ) {
744
+ return func (_ context.Context , req * monitoringpb.CreateTimeSeriesRequest ) (* emptypb.Empty , error ) {
745
+ t .Errorf ("unexpected CreateTimeSeriesRequest" )
746
+ return & emptypb.Empty {}, nil
747
+ }
748
+ },
749
+ },
750
+ {
751
+ name : "histogram error, metric skipped" ,
752
+ collect : func () {
753
+ ss .AddSample ([]string {"foo" }, 5.0 )
754
+ ss .AddSample ([]string {"bar" , "baz" }, 5.0 )
755
+ },
756
+ createFn : func (t * testing.T ) func (context.Context , * monitoringpb.CreateTimeSeriesRequest ) (* emptypb.Empty , error ) {
757
+ return func (_ context.Context , req * monitoringpb.CreateTimeSeriesRequest ) (* emptypb.Empty , error ) {
758
+ want := & monitoringpb.CreateTimeSeriesRequest {
759
+ Name : "projects/foo" ,
760
+ TimeSeries : []* monitoringpb.TimeSeries {
761
+ & monitoringpb.TimeSeries {
762
+ Metric : & metricpb.Metric {
763
+ Type : "custom.googleapis.com/go-metrics/bar" ,
764
+ Labels : map [string ]string {
765
+ "method" : "baz" ,
766
+ },
767
+ },
768
+ MetricKind : metric .MetricDescriptor_CUMULATIVE ,
769
+ Points : []* monitoringpb.Point {
770
+ & monitoringpb.Point {
771
+ Value : & monitoringpb.TypedValue {
772
+ Value : & monitoringpb.TypedValue_DistributionValue {
773
+ DistributionValue : & distributionpb.Distribution {
774
+ BucketCounts : []int64 {1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 },
775
+ Count : 1 ,
776
+ },
777
+ },
778
+ },
779
+ },
780
+ },
781
+ },
782
+ },
783
+ }
784
+ if diff := diffCreateMsg (want , req ); diff != "" {
785
+ t .Errorf ("unexpected CreateTimeSeriesRequest (-want +got):\n %s" , diff )
786
+ }
787
+ return & emptypb.Empty {}, nil
788
+ }
789
+ },
790
+ },
791
+ {
792
+ name : "counter error, no requests" ,
793
+ collect : func () {
794
+ ss .IncrCounter ([]string {"foo" }, 5.0 )
795
+ },
796
+ createFn : func (t * testing.T ) func (context.Context , * monitoringpb.CreateTimeSeriesRequest ) (* emptypb.Empty , error ) {
797
+ return func (_ context.Context , req * monitoringpb.CreateTimeSeriesRequest ) (* emptypb.Empty , error ) {
798
+ t .Errorf ("unexpected CreateTimeSeriesRequest" )
799
+ return & emptypb.Empty {}, nil
800
+ }
801
+ },
802
+ },
803
+ {
804
+ name : "counter error, metric skipped" ,
805
+ collect : func () {
806
+ ss .IncrCounter ([]string {"foo" }, 5.0 )
807
+ ss .IncrCounter ([]string {"bar" , "baz" }, 5.0 )
808
+ },
809
+ createFn : func (t * testing.T ) func (context.Context , * monitoringpb.CreateTimeSeriesRequest ) (* emptypb.Empty , error ) {
810
+ return func (_ context.Context , req * monitoringpb.CreateTimeSeriesRequest ) (* emptypb.Empty , error ) {
811
+ want := & monitoringpb.CreateTimeSeriesRequest {
812
+ Name : "projects/foo" ,
813
+ TimeSeries : []* monitoringpb.TimeSeries {
814
+ & monitoringpb.TimeSeries {
815
+ Metric : & metricpb.Metric {
816
+ Type : "custom.googleapis.com/go-metrics/bar" ,
817
+ Labels : map [string ]string {
818
+ "method" : "baz" ,
819
+ },
820
+ },
821
+ MetricKind : metric .MetricDescriptor_GAUGE ,
822
+ Points : []* monitoringpb.Point {
823
+ & monitoringpb.Point {
824
+ Value : & monitoringpb.TypedValue {
825
+ Value : & monitoringpb.TypedValue_DoubleValue {
826
+ DoubleValue : 5.0 ,
827
+ },
828
+ },
829
+ },
830
+ },
831
+ },
832
+ },
833
+ }
834
+ if diff := diffCreateMsg (want , req ); diff != "" {
835
+ t .Errorf ("unexpected CreateTimeSeriesRequest (-want +got):\n %s" , diff )
836
+ }
837
+ return & emptypb.Empty {}, nil
838
+ }
839
+ },
840
+ },
841
+ {
842
+ name : "gauge error, no requests" ,
843
+ collect : func () {
844
+ ss .SetGauge ([]string {"foo" }, 5.0 )
845
+ },
846
+ createFn : func (t * testing.T ) func (context.Context , * monitoringpb.CreateTimeSeriesRequest ) (* emptypb.Empty , error ) {
847
+ return func (_ context.Context , req * monitoringpb.CreateTimeSeriesRequest ) (* emptypb.Empty , error ) {
848
+ t .Errorf ("unexpected CreateTimeSeriesRequest" )
849
+ return & emptypb.Empty {}, nil
850
+ }
851
+ },
852
+ },
853
+ {
854
+ name : "gauge error, metric skipped" ,
855
+ collect : func () {
856
+ ss .SetGauge ([]string {"foo" }, 5.0 )
857
+ ss .SetGauge ([]string {"bar" , "baz" }, 5.0 )
858
+ },
859
+ createFn : func (t * testing.T ) func (context.Context , * monitoringpb.CreateTimeSeriesRequest ) (* emptypb.Empty , error ) {
860
+ return func (_ context.Context , req * monitoringpb.CreateTimeSeriesRequest ) (* emptypb.Empty , error ) {
861
+ want := & monitoringpb.CreateTimeSeriesRequest {
862
+ Name : "projects/foo" ,
863
+ TimeSeries : []* monitoringpb.TimeSeries {
864
+ & monitoringpb.TimeSeries {
865
+ Metric : & metricpb.Metric {
866
+ Type : "custom.googleapis.com/go-metrics/bar" ,
867
+ Labels : map [string ]string {
868
+ "method" : "baz" ,
869
+ },
870
+ },
871
+ MetricKind : metric .MetricDescriptor_GAUGE ,
872
+ Points : []* monitoringpb.Point {
873
+ & monitoringpb.Point {
874
+ Value : & monitoringpb.TypedValue {
875
+ Value : & monitoringpb.TypedValue_DoubleValue {
876
+ DoubleValue : 5.0 ,
877
+ },
878
+ },
879
+ },
880
+ },
881
+ },
882
+ },
883
+ }
884
+ if diff := diffCreateMsg (want , req ); diff != "" {
885
+ t .Errorf ("unexpected CreateTimeSeriesRequest (-want +got):\n %s" , diff )
886
+ }
887
+ return & emptypb.Empty {}, nil
888
+ }
889
+ },
890
+ },
735
891
}
736
892
737
893
for _ , tc := range tests {
0 commit comments