Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 433871b

Browse files
authored
Fix resource assignment in GKE and GCE environment when using metrics exporter. (#139)
* Fix resource assignment in GKE ancd GCE environment. * update with exporter resource only if metric doesn't have resource. * update metric_test.go
1 parent b39028f commit 433871b

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

metrics.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func (se *statsExporter) metricToMpbTs(ctx context.Context, metric *metricdata.M
122122
return nil, errNilMetric
123123
}
124124

125-
resource := metricRscToMpbRsc(metric.Resource)
125+
resource := se.metricRscToMpbRsc(metric.Resource)
126126

127127
metricName := metric.Descriptor.Name
128128
metricType, _ := se.metricTypeFromProto(metricName)
@@ -303,11 +303,15 @@ func metricDescriptorTypeToMetricKind(m *metricdata.Metric) (googlemetricpb.Metr
303303
}
304304
}
305305

306-
func metricRscToMpbRsc(rs *resource.Resource) *monitoredrespb.MonitoredResource {
306+
func (se *statsExporter) metricRscToMpbRsc(rs *resource.Resource) *monitoredrespb.MonitoredResource {
307307
if rs == nil {
308-
return &monitoredrespb.MonitoredResource{
309-
Type: "global",
308+
resource := se.o.Resource
309+
if resource == nil {
310+
resource = &monitoredrespb.MonitoredResource{
311+
Type: "global",
312+
}
310313
}
314+
return resource
311315
}
312316
typ := rs.Type
313317
if typ == "" {

metrics_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestMetricResourceToMonitoringResource(t *testing.T) {
7373
}
7474

7575
for i, tt := range tests {
76-
got := metricRscToMpbRsc(tt.in)
76+
got := se.metricRscToMpbRsc(tt.in)
7777
if diff := cmpResource(got, tt.want); diff != "" {
7878
t.Fatalf("Test %d failed. Unexpected Resource -got +want: %s", i, diff)
7979
}

0 commit comments

Comments
 (0)