Fix metric descriptor convertion. (#108)
Fixes #103.
diff --git a/equivalence_test.go b/equivalence_test.go
index 9994dd1..ba48dea 100644
--- a/equivalence_test.go
+++ b/equivalence_test.go
@@ -39,8 +39,6 @@
)
func TestStatsAndMetricsEquivalence(t *testing.T) {
- t.Skip("Metric schema has changed. Fix this test later.")
-
ma, addr, stop := createMockAgent(t)
defer stop()
@@ -129,8 +127,6 @@
// This test ensures that the final responses sent by direct stats(view.Data) exporting
// are exactly equal to those from view.Data-->OpenCensus-Proto.Metrics exporting.
func TestEquivalenceStatsVsMetricsUploads(t *testing.T) {
- t.Skip("Metric schema has changed. Fix this test later.")
-
ma, addr, doneFn := createMockAgent(t)
defer doneFn()
diff --git a/metrics_proto.go b/metrics_proto.go
index e897f70..3ee9004 100644
--- a/metrics_proto.go
+++ b/metrics_proto.go
@@ -42,6 +42,7 @@
)
var errNilMetric = errors.New("expecting a non-nil metric")
+var errNilMetricDescriptor = errors.New("expecting a non-nil metric descriptor")
type metricProtoPayload struct {
node *commonpb.Node
@@ -189,7 +190,10 @@
resource = metric.Resource
}
- metricName, _, _, _ := metricProseFromProto(metric)
+ metricName, _, _, err := metricProseFromProto(metric)
+ if err != nil {
+ return nil, err
+ }
metricType, _ := se.metricTypeFromProto(metricName)
metricLabelKeys := metric.GetMetricDescriptor().GetLabelKeys()
metricKind, _ := protoMetricDescriptorTypeToMetricKind(metric)
@@ -322,7 +326,10 @@
return nil, errNilMetric
}
- metricName, description, unit, _ := metricProseFromProto(metric)
+ metricName, description, unit, err := metricProseFromProto(metric)
+ if err != nil {
+ return nil, err
+ }
metricType, _ := se.metricTypeFromProto(metricName)
displayName := se.displayName(metricName)
metricKind, valueType := protoMetricDescriptorTypeToMetricKind(metric)
@@ -364,26 +371,23 @@
return labelDescriptors
}
-func metricProseFromProto(metric *metricspb.Metric) (name, description, unit string, ok bool) {
- mname := metric.GetMetricDescriptor().GetName()
- if mname != "" {
- name = mname
- return
- }
-
+func metricProseFromProto(metric *metricspb.Metric) (name, description, unit string, err error) {
md := metric.GetMetricDescriptor()
+ if md == nil {
+ return "", "", "", errNilMetricDescriptor
+ }
name = md.GetName()
unit = md.GetUnit()
description = md.GetDescription()
- if md != nil && md.Type == metricspb.MetricDescriptor_CUMULATIVE_INT64 {
+ if md.Type == metricspb.MetricDescriptor_CUMULATIVE_INT64 {
// If the aggregation type is count, which counts the number of recorded measurements, the unit must be "1",
// because this view does not apply to the recorded values.
unit = stats.UnitDimensionless
}
- return
+ return name, description, unit, nil
}
func (se *statsExporter) metricTypeFromProto(name string) (string, bool) {
diff --git a/metrics_proto_test.go b/metrics_proto_test.go
index 3561737..8fafd52 100644
--- a/metrics_proto_test.go
+++ b/metrics_proto_test.go
@@ -208,8 +208,6 @@
}
func TestProtoToMonitoringMetricDescriptor(t *testing.T) {
- t.Skip("Metric schema has changed. Fix this test later.")
-
tests := []struct {
in *metricspb.Metric
want *googlemetricpb.MetricDescriptor
@@ -219,15 +217,8 @@
}{
{in: nil, wantErr: "non-nil metric"},
{
- in: &metricspb.Metric{},
- statsExporter: &statsExporter{
- o: Options{ProjectID: "test"},
- },
- want: &googlemetricpb.MetricDescriptor{
- Name: "projects/test/metricDescriptors/custom.googleapis.com/opencensus",
- Type: "custom.googleapis.com/opencensus",
- DisplayName: "OpenCensus",
- },
+ in: &metricspb.Metric{},
+ wantErr: "non-nil metric descriptor",
},
{
in: &metricspb.Metric{