@@ -30,6 +30,7 @@ import (
3030 distributionpb "google.golang.org/genproto/googleapis/api/distribution"
3131 "google.golang.org/genproto/googleapis/api/metric"
3232 metricpb "google.golang.org/genproto/googleapis/api/metric"
33+ monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
3334 monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
3435 "google.golang.org/grpc"
3536 "google.golang.org/grpc/test/bufconn"
@@ -91,6 +92,12 @@ func BenchmarkReport10(b *testing.B) { benchmarkCopy(10, 10, 10, b) }
9192func BenchmarkReport50 (b * testing.B ) { benchmarkCopy (50 , 50 , 50 , b ) }
9293func BenchmarkReport100 (b * testing.B ) { benchmarkCopy (100 , 100 , 100 , b ) }
9394
95+ func diffTest (t * testing.T , title string , x , y interface {}) {
96+ if diff := cmp .Diff (x , y ); diff != "" {
97+ t .Errorf ("%s mismatch (-want +got):\n %s" , title , diff )
98+ }
99+ }
100+
94101func sPtr (s string ) * string {
95102 return & s
96103}
@@ -1091,3 +1098,68 @@ func diffCreateMsg(want, got *monitoringpb.CreateTimeSeriesRequest) string {
10911098 }
10921099 return out
10931100}
1101+
1102+ func monitoredResourceDiff (t * testing.T , s * Sink , labels map [string ]string ) {
1103+ diffTest (t , "Monitored Resource labels" , s .monitoredResource .GetLabels (), labels )
1104+ }
1105+
1106+ func TestCustomMonitorResource (t * testing.T ) {
1107+ labels := map [string ]string {
1108+ "project_id" : "project" ,
1109+ "location" : "zone" ,
1110+ "cluster_name" : "cluster" ,
1111+ "container_name" : "container_name" ,
1112+ "namespace_name" : "namespace_name" ,
1113+ "pod_name" : "pod_name" ,
1114+ }
1115+
1116+ sink := NewSink (nil , & Config {
1117+ ProjectID : "example_project" ,
1118+ Prefix : sPtr ("" ),
1119+ MonitoredResource : & monitoredrespb.MonitoredResource {
1120+ Labels : labels ,
1121+ Type : "k8s_container" ,
1122+ },
1123+ })
1124+
1125+ monitoredResourceDiff (t , sink , labels )
1126+ }
1127+
1128+ func TestCustomMonitorResourceWithDefaultLabels (t * testing.T ) {
1129+ sink := NewSink (nil , & Config {
1130+ ProjectID : "example_project" ,
1131+ Prefix : sPtr ("" ),
1132+ })
1133+
1134+ labels := defaultMonitoredResource (sink .taskInfo ).GetLabels ()
1135+
1136+ monitoredResourceDiff (t , sink , labels )
1137+ }
1138+
1139+ func TestCustomMonitorResourceWithInvalidLabels (t * testing.T ) {
1140+ labels := map [string ]string {
1141+ "project_id" : "project" ,
1142+ "location" : "zone" ,
1143+ "cluster_name" : "cluster" ,
1144+ "container_name" : "container_name" ,
1145+ "namespace_name" : "namespace_name" ,
1146+ "pod_name" : "pod_name" ,
1147+ }
1148+
1149+ invalidLabels := map [string ]string {
1150+ "project_id" : "project" ,
1151+ }
1152+
1153+ sink := NewSink (nil , & Config {
1154+ ProjectID : "example_project" ,
1155+ Prefix : sPtr ("" ),
1156+ MonitoredResource : & monitoredrespb.MonitoredResource {
1157+ Labels : labels ,
1158+ Type : "k8s_container" ,
1159+ },
1160+ })
1161+
1162+ if diff := cmp .Diff (sink .monitoredResource .GetLabels (), invalidLabels ); diff == "" {
1163+ t .Error ("Monitored Resource labels should not be equal" )
1164+ }
1165+ }
0 commit comments