From 281244432d1ddf7f0566cfd0964d79c2b6cea7c2 Mon Sep 17 00:00:00 2001 From: Emmanuel T Odeke Date: Tue, 11 Dec 2018 06:55:59 -0800 Subject: [PATCH] metrics: Proto Resource to monitoredresource.Resource A converter for: OpenCensus Proto Resource to monitoredresource.Resource Updates #64. --- metrics.go | 18 ++++++++++++++++++ metrics_test.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/metrics.go b/metrics.go index d1f3386..e1eed8b 100644 --- a/metrics.go +++ b/metrics.go @@ -29,9 +29,11 @@ import ( distributionpb "google.golang.org/genproto/googleapis/api/distribution" labelpb "google.golang.org/genproto/googleapis/api/label" googlemetricpb "google.golang.org/genproto/googleapis/api/metric" + monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres" monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3" metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1" + resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1" ) var errNilMetric = errors.New("expecting a non-nil metric") @@ -238,3 +240,19 @@ func protoMetricDescriptorTypeToMetricKind(m *metricspb.Metric) (googlemetricpb. return googlemetricpb.MetricDescriptor_METRIC_KIND_UNSPECIFIED, googlemetricpb.MetricDescriptor_VALUE_TYPE_UNSPECIFIED } } + +func protoResourceToMonitoredResource(rsp *resourcepb.Resource) *monitoredrespb.MonitoredResource { + if rsp == nil { + return nil + } + mrsp := &monitoredrespb.MonitoredResource{ + Type: rsp.Type, + } + if rsp.Labels != nil { + mrsp.Labels = make(map[string]string, len(rsp.Labels)) + for k, v := range rsp.Labels { + mrsp.Labels[k] = v + } + } + return mrsp +} diff --git a/metrics_test.go b/metrics_test.go index 4e35f64..20f35d0 100644 --- a/metrics_test.go +++ b/metrics_test.go @@ -23,11 +23,61 @@ import ( "github.com/golang/protobuf/ptypes/timestamp" distributionpb "google.golang.org/genproto/googleapis/api/distribution" googlemetricpb "google.golang.org/genproto/googleapis/api/metric" + monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres" monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3" metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1" + resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1" ) +func TestProtoResourceToMonitoringResource(t *testing.T) { + tests := []struct { + in *resourcepb.Resource + want *monitoredrespb.MonitoredResource + }{ + {in: nil, want: nil}, + {in: &resourcepb.Resource{}, want: &monitoredrespb.MonitoredResource{}}, + { + in: &resourcepb.Resource{ + Type: "foo", + }, + want: &monitoredrespb.MonitoredResource{ + Type: "foo", + }, + }, + { + in: &resourcepb.Resource{ + Type: "foo", + Labels: map[string]string{}, + }, + want: &monitoredrespb.MonitoredResource{ + Type: "foo", + Labels: map[string]string{}, + }, + }, + { + in: &resourcepb.Resource{ + Type: "foo", + Labels: map[string]string{"a": "A"}, + }, + want: &monitoredrespb.MonitoredResource{ + Type: "foo", + Labels: map[string]string{"a": "A"}, + }, + }, + } + + for i, tt := range tests { + got := protoResourceToMonitoredResource(tt.in) + if !reflect.DeepEqual(got, tt.want) { + gj, wj := serializeAsJSON(got), serializeAsJSON(tt.want) + if gj != wj { + t.Errorf("#%d: Unmatched JSON\nGot:\n\t%s\nWant:\n\t%s", i, gj, wj) + } + } + } +} + func TestProtoToMonitoringMetricDescriptor(t *testing.T) { tests := []struct { in *metricspb.Metric