@@ -626,6 +626,140 @@ func TestSampleBuilder(t *testing.T) {
626626				},
627627			},
628628		},
629+ 		// Any counter metric with the _total suffix should be treated as normal if metadata 
630+ 		// can be found for the original metric name. 
631+ 		{
632+ 			series : seriesMap {
633+ 				1 : labels .FromStrings ("job" , "job1" , "instance" , "instance1" , "a" , "1" , "__name__" , "metric1_total" ),
634+ 			},
635+ 			targets : targetMap {
636+ 				"job1/instance1" : & targets.Target {
637+ 					Labels :           promlabels .FromStrings ("job" , "job1" , "instance" , "instance1" ),
638+ 					DiscoveredLabels : promlabels .FromStrings ("__resource_a" , "resource2_a" ),
639+ 				},
640+ 			},
641+ 			metadata : metadataMap {
642+ 				"job1/instance1/metric1_total" : & scrape.MetricMetadata {Type : textparse .MetricTypeCounter , Metric : "metric1_total" },
643+ 			},
644+ 			metricPrefix : "test.googleapis.com" ,
645+ 			input : []tsdb.RefSample {
646+ 				{Ref : 1 , T : 2000 , V : 5.5 },
647+ 				{Ref : 1 , T : 3000 , V : 8 },
648+ 			},
649+ 			result : []* monitoring_pb.TimeSeries {
650+ 				nil , // Skipped by reset timestamp handling. 
651+ 				{
652+ 					Resource : & monitoredres_pb.MonitoredResource {
653+ 						Type :   "resource2" ,
654+ 						Labels : map [string ]string {"resource_a" : "resource2_a" },
655+ 					},
656+ 					Metric : & metric_pb.Metric {
657+ 						Type :   "test.googleapis.com/metric1_total" ,
658+ 						Labels : map [string ]string {"a" : "1" },
659+ 					},
660+ 					MetricKind : metric_pb .MetricDescriptor_CUMULATIVE ,
661+ 					ValueType :  metric_pb .MetricDescriptor_DOUBLE ,
662+ 					Points : []* monitoring_pb.Point {{
663+ 						Interval : & monitoring_pb.TimeInterval {
664+ 							StartTime : & timestamp_pb.Timestamp {Seconds : 2 },
665+ 							EndTime :   & timestamp_pb.Timestamp {Seconds : 3 },
666+ 						},
667+ 						Value : & monitoring_pb.TypedValue {
668+ 							Value : & monitoring_pb.TypedValue_DoubleValue {2.5 },
669+ 						},
670+ 					}},
671+ 				},
672+ 			},
673+ 		},
674+ 		// Any counter metric with the _total suffix should fail over to the metadata for 
675+ 		// the metric with the _total suffix removed while reporting the metric with the 
676+ 		// _total suffix removed in the metric name as well. 
677+ 		{
678+ 			series : seriesMap {
679+ 				1 : labels .FromStrings ("job" , "job1" , "instance" , "instance1" , "a" , "1" , "__name__" , "metric1_total" ),
680+ 			},
681+ 			targets : targetMap {
682+ 				"job1/instance1" : & targets.Target {
683+ 					Labels :           promlabels .FromStrings ("job" , "job1" , "instance" , "instance1" ),
684+ 					DiscoveredLabels : promlabels .FromStrings ("__resource_a" , "resource2_a" ),
685+ 				},
686+ 			},
687+ 			metadata : metadataMap {
688+ 				"job1/instance1/metric1" : & scrape.MetricMetadata {Type : textparse .MetricTypeCounter , Metric : "metric1" },
689+ 			},
690+ 			metricPrefix : "test.googleapis.com" ,
691+ 			input : []tsdb.RefSample {
692+ 				{Ref : 1 , T : 2000 , V : 5.5 },
693+ 				{Ref : 1 , T : 3000 , V : 8 },
694+ 			},
695+ 			result : []* monitoring_pb.TimeSeries {
696+ 				nil , // Skipped by reset timestamp handling. 
697+ 				{
698+ 					Resource : & monitoredres_pb.MonitoredResource {
699+ 						Type :   "resource2" ,
700+ 						Labels : map [string ]string {"resource_a" : "resource2_a" },
701+ 					},
702+ 					Metric : & metric_pb.Metric {
703+ 						Type :   "test.googleapis.com/metric1" ,
704+ 						Labels : map [string ]string {"a" : "1" },
705+ 					},
706+ 					MetricKind : metric_pb .MetricDescriptor_CUMULATIVE ,
707+ 					ValueType :  metric_pb .MetricDescriptor_DOUBLE ,
708+ 					Points : []* monitoring_pb.Point {{
709+ 						Interval : & monitoring_pb.TimeInterval {
710+ 							StartTime : & timestamp_pb.Timestamp {Seconds : 2 },
711+ 							EndTime :   & timestamp_pb.Timestamp {Seconds : 3 },
712+ 						},
713+ 						Value : & monitoring_pb.TypedValue {
714+ 							Value : & monitoring_pb.TypedValue_DoubleValue {2.5 },
715+ 						},
716+ 					}},
717+ 				},
718+ 			},
719+ 		},
720+ 		// Any non-counter metric with the _total suffix should fail over to the metadata 
721+ 		// for the metric with the _total suffix removed while reporting the metric with 
722+ 		// the original name. 
723+ 		{
724+ 			series : seriesMap {
725+ 				1 : labels .FromStrings ("job" , "job1" , "instance" , "instance1" , "a" , "1" , "__name__" , "metric1_total" ),
726+ 			},
727+ 			targets : targetMap {
728+ 				"job1/instance1" : & targets.Target {
729+ 					Labels :           promlabels .FromStrings ("job" , "job1" , "instance" , "instance1" ),
730+ 					DiscoveredLabels : promlabels .FromStrings ("__resource_a" , "resource2_a" ),
731+ 				},
732+ 			},
733+ 			metadata : metadataMap {
734+ 				"job1/instance1/metric1" : & scrape.MetricMetadata {Type : textparse .MetricTypeGauge , Metric : "metric1" },
735+ 			},
736+ 			metricPrefix : "test.googleapis.com" ,
737+ 			input : []tsdb.RefSample {
738+ 				{Ref : 1 , T : 3000 , V : 8 },
739+ 			},
740+ 			result : []* monitoring_pb.TimeSeries {
741+ 				{
742+ 					Resource : & monitoredres_pb.MonitoredResource {
743+ 						Type :   "resource2" ,
744+ 						Labels : map [string ]string {"resource_a" : "resource2_a" },
745+ 					},
746+ 					Metric : & metric_pb.Metric {
747+ 						Type :   "test.googleapis.com/metric1_total" ,
748+ 						Labels : map [string ]string {"a" : "1" },
749+ 					},
750+ 					MetricKind : metric_pb .MetricDescriptor_GAUGE ,
751+ 					ValueType :  metric_pb .MetricDescriptor_DOUBLE ,
752+ 					Points : []* monitoring_pb.Point {{
753+ 						Interval : & monitoring_pb.TimeInterval {
754+ 							EndTime : & timestamp_pb.Timestamp {Seconds : 3 },
755+ 						},
756+ 						Value : & monitoring_pb.TypedValue {
757+ 							Value : & monitoring_pb.TypedValue_DoubleValue {8 },
758+ 						},
759+ 					}},
760+ 				},
761+ 			},
762+ 		},
629763	}
630764	ctx , cancel  :=  context .WithCancel (context .Background ())
631765	defer  cancel ()
0 commit comments