@@ -21,6 +21,7 @@ import (
2121 "testing"
2222
2323 "cloud.google.com/go/monitoring/apiv3"
24+ resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
2425 "github.com/golang/protobuf/ptypes/timestamp"
2526 distributionpb "google.golang.org/genproto/googleapis/api/distribution"
2627 labelpb "google.golang.org/genproto/googleapis/api/label"
@@ -30,56 +31,11 @@ import (
3031
3132 commonpb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/common/v1"
3233 metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
33- resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
3434 "github.com/golang/protobuf/ptypes/wrappers"
3535 "github.com/google/go-cmp/cmp"
36+ "go.opencensus.io/resource/resourcekeys"
3637)
3738
38- func TestProtoResourceToMonitoringResource (t * testing.T ) {
39- tests := []struct {
40- in * resourcepb.Resource
41- want * monitoredrespb.MonitoredResource
42- }{
43- {in : nil , want : & monitoredrespb.MonitoredResource {Type : "global" }},
44- {in : & resourcepb.Resource {}, want : & monitoredrespb.MonitoredResource {Type : "global" }},
45- {
46- in : & resourcepb.Resource {
47- Type : "foo" ,
48- },
49- want : & monitoredrespb.MonitoredResource {
50- Type : "foo" ,
51- },
52- },
53- {
54- in : & resourcepb.Resource {
55- Type : "foo" ,
56- Labels : map [string ]string {},
57- },
58- want : & monitoredrespb.MonitoredResource {
59- Type : "foo" ,
60- Labels : map [string ]string {},
61- },
62- },
63- {
64- in : & resourcepb.Resource {
65- Type : "foo" ,
66- Labels : map [string ]string {"a" : "A" },
67- },
68- want : & monitoredrespb.MonitoredResource {
69- Type : "foo" ,
70- Labels : map [string ]string {"a" : "A" },
71- },
72- },
73- }
74-
75- for i , tt := range tests {
76- got := protoResourceToMonitoredResource (tt .in )
77- if diff := cmpResource (got , tt .want ); diff != "" {
78- t .Fatalf ("Test %d failed. Unexpected Resource -got +want: %s" , i , diff )
79- }
80- }
81- }
82-
8339func TestProtoMetricToCreateTimeSeriesRequest (t * testing.T ) {
8440 startTimestamp := & timestamp.Timestamp {
8541 Seconds : 1543160298 ,
@@ -133,7 +89,7 @@ func TestProtoMetricToCreateTimeSeriesRequest(t *testing.T) {
13389 },
13490 },
13591 statsExporter : & statsExporter {
136- o : Options {ProjectID : "foo" },
92+ o : Options {ProjectID : "foo" , MapResource : defaultMapResource },
13793 },
13894 want : []* monitoringpb.CreateTimeSeriesRequest {
13995 {
@@ -205,6 +161,187 @@ func TestProtoMetricToCreateTimeSeriesRequest(t *testing.T) {
205161 }
206162}
207163
164+ func TestProtoMetricWithDifferentResource (t * testing.T ) {
165+ startTimestamp := & timestamp.Timestamp {
166+ Seconds : 1543160298 ,
167+ Nanos : 100000090 ,
168+ }
169+ endTimestamp := & timestamp.Timestamp {
170+ Seconds : 1543160298 ,
171+ Nanos : 100000997 ,
172+ }
173+
174+ tests := []struct {
175+ in * metricspb.Metric
176+ want []* monitoringpb.CreateTimeSeriesRequest
177+ wantErr string
178+ statsExporter * statsExporter
179+ }{
180+ {
181+ in : & metricspb.Metric {
182+ MetricDescriptor : & metricspb.MetricDescriptor {
183+ Name : "with_k8s_resource" ,
184+ Description : "This is a test" ,
185+ Unit : "By" ,
186+ },
187+ Resource : & resourcepb.Resource {
188+ Type : resourcekeys .K8SType ,
189+ Labels : map [string ]string {
190+ resourcekeys .K8SKeyClusterName : "cluster1" ,
191+ resourcekeys .K8SKeyPodName : "pod1" ,
192+ resourcekeys .K8SKeyNamespaceName : "namespace1" ,
193+ resourcekeys .ContainerKeyName : "container-name1" ,
194+ resourcekeys .CloudKeyZone : "zone1" ,
195+ },
196+ },
197+ Timeseries : []* metricspb.TimeSeries {
198+ {
199+ StartTimestamp : startTimestamp ,
200+ Points : []* metricspb.Point {
201+ {
202+ Timestamp : endTimestamp ,
203+ Value : & metricspb.Point_Int64Value {
204+ Int64Value : 1 ,
205+ },
206+ },
207+ },
208+ },
209+ },
210+ },
211+ statsExporter : & statsExporter {
212+ o : Options {ProjectID : "foo" , MapResource : defaultMapResource },
213+ },
214+ want : []* monitoringpb.CreateTimeSeriesRequest {
215+ {
216+ Name : "projects/foo" ,
217+ TimeSeries : []* monitoringpb.TimeSeries {
218+ {
219+ Metric : & googlemetricpb.Metric {
220+ Type : "custom.googleapis.com/opencensus/with_k8s_resource" ,
221+ Labels : map [string ]string {},
222+ },
223+ Resource : & monitoredrespb.MonitoredResource {
224+ Type : "k8s_container" ,
225+ Labels : map [string ]string {
226+ "location" : "zone1" ,
227+ "cluster_name" : "cluster1" ,
228+ "namespace_name" : "namespace1" ,
229+ "pod_name" : "pod1" ,
230+ "container_name" : "container-name1" ,
231+ },
232+ },
233+ Points : []* monitoringpb.Point {
234+ {
235+ Interval : & monitoringpb.TimeInterval {
236+ StartTime : startTimestamp ,
237+ EndTime : endTimestamp ,
238+ },
239+ Value : & monitoringpb.TypedValue {
240+ Value : & monitoringpb.TypedValue_Int64Value {
241+ Int64Value : 1 ,
242+ },
243+ },
244+ },
245+ },
246+ },
247+ },
248+ },
249+ },
250+ },
251+ {
252+ in : & metricspb.Metric {
253+ MetricDescriptor : & metricspb.MetricDescriptor {
254+ Name : "with_gce_resource" ,
255+ Description : "This is a test" ,
256+ Unit : "By" ,
257+ },
258+ Resource : & resourcepb.Resource {
259+ Type : resourcekeys .CloudType ,
260+ Labels : map [string ]string {
261+ resourcekeys .CloudKeyProvider : resourcekeys .CloudProviderGCP ,
262+ resourcekeys .HostKeyID : "inst1" ,
263+ resourcekeys .CloudKeyZone : "zone1" ,
264+ },
265+ },
266+ Timeseries : []* metricspb.TimeSeries {
267+ {
268+ StartTimestamp : startTimestamp ,
269+ Points : []* metricspb.Point {
270+ {
271+ Timestamp : endTimestamp ,
272+ Value : & metricspb.Point_Int64Value {
273+ Int64Value : 1 ,
274+ },
275+ },
276+ },
277+ },
278+ },
279+ },
280+ statsExporter : & statsExporter {
281+ o : Options {ProjectID : "foo" , MapResource : defaultMapResource },
282+ },
283+ want : []* monitoringpb.CreateTimeSeriesRequest {
284+ {
285+ Name : "projects/foo" ,
286+ TimeSeries : []* monitoringpb.TimeSeries {
287+ {
288+ Metric : & googlemetricpb.Metric {
289+ Type : "custom.googleapis.com/opencensus/with_gce_resource" ,
290+ Labels : map [string ]string {},
291+ },
292+ Resource : & monitoredrespb.MonitoredResource {
293+ Type : "gce_instance" ,
294+ Labels : map [string ]string {
295+ "instance_id" : "inst1" ,
296+ "zone" : "zone1" ,
297+ },
298+ },
299+ Points : []* monitoringpb.Point {
300+ {
301+ Interval : & monitoringpb.TimeInterval {
302+ StartTime : startTimestamp ,
303+ EndTime : endTimestamp ,
304+ },
305+ Value : & monitoringpb.TypedValue {
306+ Value : & monitoringpb.TypedValue_Int64Value {
307+ Int64Value : 1 ,
308+ },
309+ },
310+ },
311+ },
312+ },
313+ },
314+ },
315+ },
316+ },
317+ }
318+
319+ for i , tt := range tests {
320+ se := tt .statsExporter
321+ if se == nil {
322+ se = new (statsExporter )
323+ }
324+ tsl , err := se .protoMetricToTimeSeries (context .Background (), nil , nil , tt .in , nil )
325+ if tt .wantErr != "" {
326+ if err == nil || ! strings .Contains (err .Error (), tt .wantErr ) {
327+ t .Errorf ("#%d: unmatched error. Got\n \t %v\n Want\n \t %v" , i , err , tt .wantErr )
328+ }
329+ continue
330+ }
331+ if err != nil {
332+ t .Errorf ("#%d: unexpected error: %v" , i , err )
333+ continue
334+ }
335+
336+ got := se .combineTimeSeriesToCreateTimeSeriesRequest (tsl )
337+ // Our saving grace is serialization equality since some
338+ // unexported fields could be present in the various values.
339+ if diff := cmpTSReqs (got , tt .want ); diff != "" {
340+ t .Fatalf ("Test %d failed. Unexpected CreateTimeSeriesRequests -got +want: %s" , i , diff )
341+ }
342+ }
343+ }
344+
208345func TestProtoToMonitoringMetricDescriptor (t * testing.T ) {
209346 tests := []struct {
210347 in * metricspb.Metric
0 commit comments