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

Commit

Permalink
metrics: Proto Resource to monitoredresource.Resource
Browse files Browse the repository at this point in the history
A converter for:
    OpenCensus Proto Resource
to
    monitoredresource.Resource

Updates #64.
  • Loading branch information
odeke-em committed Dec 11, 2018
1 parent ea69b46 commit 2812444
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
18 changes: 18 additions & 0 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
50 changes: 50 additions & 0 deletions metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2812444

Please sign in to comment.