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

Commit eb72365

Browse files
authored
Fix metric descriptor convertion. (#108)
Fixes #103.
1 parent 199f933 commit eb72365

3 files changed

Lines changed: 17 additions & 26 deletions

File tree

equivalence_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ import (
3939
)
4040

4141
func TestStatsAndMetricsEquivalence(t *testing.T) {
42-
t.Skip("Metric schema has changed. Fix this test later.")
43-
4442
ma, addr, stop := createMockAgent(t)
4543
defer stop()
4644

@@ -129,8 +127,6 @@ func TestStatsAndMetricsEquivalence(t *testing.T) {
129127
// This test ensures that the final responses sent by direct stats(view.Data) exporting
130128
// are exactly equal to those from view.Data-->OpenCensus-Proto.Metrics exporting.
131129
func TestEquivalenceStatsVsMetricsUploads(t *testing.T) {
132-
t.Skip("Metric schema has changed. Fix this test later.")
133-
134130
ma, addr, doneFn := createMockAgent(t)
135131
defer doneFn()
136132

metrics_proto.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
)
4343

4444
var errNilMetric = errors.New("expecting a non-nil metric")
45+
var errNilMetricDescriptor = errors.New("expecting a non-nil metric descriptor")
4546

4647
type metricProtoPayload struct {
4748
node *commonpb.Node
@@ -189,7 +190,10 @@ func (se *statsExporter) protoMetricToTimeSeries(ctx context.Context, node *comm
189190
resource = metric.Resource
190191
}
191192

192-
metricName, _, _, _ := metricProseFromProto(metric)
193+
metricName, _, _, err := metricProseFromProto(metric)
194+
if err != nil {
195+
return nil, err
196+
}
193197
metricType, _ := se.metricTypeFromProto(metricName)
194198
metricLabelKeys := metric.GetMetricDescriptor().GetLabelKeys()
195199
metricKind, _ := protoMetricDescriptorTypeToMetricKind(metric)
@@ -322,7 +326,10 @@ func (se *statsExporter) protoToMonitoringMetricDescriptor(metric *metricspb.Met
322326
return nil, errNilMetric
323327
}
324328

325-
metricName, description, unit, _ := metricProseFromProto(metric)
329+
metricName, description, unit, err := metricProseFromProto(metric)
330+
if err != nil {
331+
return nil, err
332+
}
326333
metricType, _ := se.metricTypeFromProto(metricName)
327334
displayName := se.displayName(metricName)
328335
metricKind, valueType := protoMetricDescriptorTypeToMetricKind(metric)
@@ -364,26 +371,23 @@ func labelDescriptorsFromProto(defaults map[string]labelValue, protoLabelKeys []
364371
return labelDescriptors
365372
}
366373

367-
func metricProseFromProto(metric *metricspb.Metric) (name, description, unit string, ok bool) {
368-
mname := metric.GetMetricDescriptor().GetName()
369-
if mname != "" {
370-
name = mname
371-
return
372-
}
373-
374+
func metricProseFromProto(metric *metricspb.Metric) (name, description, unit string, err error) {
374375
md := metric.GetMetricDescriptor()
376+
if md == nil {
377+
return "", "", "", errNilMetricDescriptor
378+
}
375379

376380
name = md.GetName()
377381
unit = md.GetUnit()
378382
description = md.GetDescription()
379383

380-
if md != nil && md.Type == metricspb.MetricDescriptor_CUMULATIVE_INT64 {
384+
if md.Type == metricspb.MetricDescriptor_CUMULATIVE_INT64 {
381385
// If the aggregation type is count, which counts the number of recorded measurements, the unit must be "1",
382386
// because this view does not apply to the recorded values.
383387
unit = stats.UnitDimensionless
384388
}
385389

386-
return
390+
return name, description, unit, nil
387391
}
388392

389393
func (se *statsExporter) metricTypeFromProto(name string) (string, bool) {

metrics_proto_test.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,6 @@ func TestProtoMetricToCreateTimeSeriesRequest(t *testing.T) {
208208
}
209209

210210
func TestProtoToMonitoringMetricDescriptor(t *testing.T) {
211-
t.Skip("Metric schema has changed. Fix this test later.")
212-
213211
tests := []struct {
214212
in *metricspb.Metric
215213
want *googlemetricpb.MetricDescriptor
@@ -219,15 +217,8 @@ func TestProtoToMonitoringMetricDescriptor(t *testing.T) {
219217
}{
220218
{in: nil, wantErr: "non-nil metric"},
221219
{
222-
in: &metricspb.Metric{},
223-
statsExporter: &statsExporter{
224-
o: Options{ProjectID: "test"},
225-
},
226-
want: &googlemetricpb.MetricDescriptor{
227-
Name: "projects/test/metricDescriptors/custom.googleapis.com/opencensus",
228-
Type: "custom.googleapis.com/opencensus",
229-
DisplayName: "OpenCensus",
230-
},
220+
in: &metricspb.Metric{},
221+
wantErr: "non-nil metric descriptor",
231222
},
232223
{
233224
in: &metricspb.Metric{

0 commit comments

Comments
 (0)