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

Commit 2812444

Browse files
committed
metrics: Proto Resource to monitoredresource.Resource
A converter for: OpenCensus Proto Resource to monitoredresource.Resource Updates #64.
1 parent ea69b46 commit 2812444

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

metrics.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ import (
2929
distributionpb "google.golang.org/genproto/googleapis/api/distribution"
3030
labelpb "google.golang.org/genproto/googleapis/api/label"
3131
googlemetricpb "google.golang.org/genproto/googleapis/api/metric"
32+
monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
3233
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
3334

3435
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
36+
resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
3537
)
3638

3739
var errNilMetric = errors.New("expecting a non-nil metric")
@@ -238,3 +240,19 @@ func protoMetricDescriptorTypeToMetricKind(m *metricspb.Metric) (googlemetricpb.
238240
return googlemetricpb.MetricDescriptor_METRIC_KIND_UNSPECIFIED, googlemetricpb.MetricDescriptor_VALUE_TYPE_UNSPECIFIED
239241
}
240242
}
243+
244+
func protoResourceToMonitoredResource(rsp *resourcepb.Resource) *monitoredrespb.MonitoredResource {
245+
if rsp == nil {
246+
return nil
247+
}
248+
mrsp := &monitoredrespb.MonitoredResource{
249+
Type: rsp.Type,
250+
}
251+
if rsp.Labels != nil {
252+
mrsp.Labels = make(map[string]string, len(rsp.Labels))
253+
for k, v := range rsp.Labels {
254+
mrsp.Labels[k] = v
255+
}
256+
}
257+
return mrsp
258+
}

metrics_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,61 @@ import (
2323
"github.com/golang/protobuf/ptypes/timestamp"
2424
distributionpb "google.golang.org/genproto/googleapis/api/distribution"
2525
googlemetricpb "google.golang.org/genproto/googleapis/api/metric"
26+
monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
2627
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
2728

2829
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
30+
resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
2931
)
3032

33+
func TestProtoResourceToMonitoringResource(t *testing.T) {
34+
tests := []struct {
35+
in *resourcepb.Resource
36+
want *monitoredrespb.MonitoredResource
37+
}{
38+
{in: nil, want: nil},
39+
{in: &resourcepb.Resource{}, want: &monitoredrespb.MonitoredResource{}},
40+
{
41+
in: &resourcepb.Resource{
42+
Type: "foo",
43+
},
44+
want: &monitoredrespb.MonitoredResource{
45+
Type: "foo",
46+
},
47+
},
48+
{
49+
in: &resourcepb.Resource{
50+
Type: "foo",
51+
Labels: map[string]string{},
52+
},
53+
want: &monitoredrespb.MonitoredResource{
54+
Type: "foo",
55+
Labels: map[string]string{},
56+
},
57+
},
58+
{
59+
in: &resourcepb.Resource{
60+
Type: "foo",
61+
Labels: map[string]string{"a": "A"},
62+
},
63+
want: &monitoredrespb.MonitoredResource{
64+
Type: "foo",
65+
Labels: map[string]string{"a": "A"},
66+
},
67+
},
68+
}
69+
70+
for i, tt := range tests {
71+
got := protoResourceToMonitoredResource(tt.in)
72+
if !reflect.DeepEqual(got, tt.want) {
73+
gj, wj := serializeAsJSON(got), serializeAsJSON(tt.want)
74+
if gj != wj {
75+
t.Errorf("#%d: Unmatched JSON\nGot:\n\t%s\nWant:\n\t%s", i, gj, wj)
76+
}
77+
}
78+
}
79+
}
80+
3181
func TestProtoToMonitoringMetricDescriptor(t *testing.T) {
3282
tests := []struct {
3383
in *metricspb.Metric

0 commit comments

Comments
 (0)