From 566b23b9d0d7c9ef923a1ef5f892cdaaa88b1051 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 3 Feb 2022 09:05:31 -0800 Subject: [PATCH] [pdatagen] Generate Metric type accessors --- .../cmd/pdatagen/internal/base_fields.go | 90 +++++++++++++++++-- model/pdata/generated_metrics.go | 42 +++++++++ model/pdata/generated_metrics_test.go | 41 +++++++++ model/pdata/metrics.go | 42 --------- model/pdata/metrics_test.go | 39 -------- 5 files changed, 168 insertions(+), 86 deletions(-) diff --git a/model/internal/cmd/pdatagen/internal/base_fields.go b/model/internal/cmd/pdatagen/internal/base_fields.go index 419c9724d15..f63c7305004 100644 --- a/model/internal/cmd/pdatagen/internal/base_fields.go +++ b/model/internal/cmd/pdatagen/internal/base_fields.go @@ -53,6 +53,16 @@ func (ms ${structName}) Set${fieldName}(v ${returnType}) { (*ms.orig).${originFieldName} = v }` +const oneOfTypeAccessorHeaderTemplate = `// ${typeAccessor} returns the type of the ${lowerOriginFieldName} for this ${structName}. +// Calling this function on zero-initialized ${structName} will cause a panic. +func (ms ${structName}) ${typeAccessor}() ${typeName} { + switch ms.orig.${originFieldName}.(type) {` + +const oneOfTypeAccessorHeaderTestTemplate = `func Test${structName}${typeAccessor}(t *testing.T) { + tv := New${structName}() + assert.Equal(t, ${typeName}None, tv.${typeAccessor}()) + assert.Equal(t, "", ${typeName}(1000).String())` + const accessorsOneOfMessageTemplate = `// ${fieldName} returns the ${lowerFieldName} associated with this ${structName}. // Calling this function when ${typeAccessor}() != ${typeName} will cause a panic. // Calling this function on zero-initialized ${structName} will cause a panic. @@ -401,27 +411,77 @@ var _ baseField = (*primitiveStructField)(nil) type oneOfField struct { originTypePrefix string originFieldName string - // TODO: Generate type accessors. - typeAccessor string - typeName string - testValueIdx int - values []oneOfValue + typeAccessor string + typeName string + testValueIdx int + values []oneOfValue } func (of *oneOfField) generateAccessors(ms baseStruct, sb *strings.Builder) { + of.generateTypeAccessors(ms, sb) + sb.WriteString("\n") for _, v := range of.values { v.generateAccessors(ms, of, sb) sb.WriteString("\n") } } +func (of *oneOfField) generateTypeAccessors(ms baseStruct, sb *strings.Builder) { + sb.WriteString(os.Expand(oneOfTypeAccessorHeaderTemplate, func(name string) string { + switch name { + case "lowerOriginFieldName": + return strings.ToLower(of.originFieldName) + case "originFieldName": + return of.originFieldName + case "structName": + return ms.getName() + case "typeAccessor": + return of.typeAccessor + case "typeName": + return of.typeName + default: + panic(name) + } + })) + sb.WriteString("\n") + for _, v := range of.values { + v.generateTypeSwitchCase(of, sb) + } + sb.WriteString("\t}\n") + sb.WriteString("\treturn " + of.typeName + "None\n") + sb.WriteString("}\n") +} + func (of *oneOfField) generateAccessorsTest(ms baseStruct, sb *strings.Builder) { + of.generateTypeAccessorsTest(ms, sb) + sb.WriteString("\n") for _, v := range of.values { v.generateTests(ms.(*messageValueStruct), of, sb) sb.WriteString("\n") } } +func (of *oneOfField) generateTypeAccessorsTest(ms baseStruct, sb *strings.Builder) { + sb.WriteString(os.Expand(oneOfTypeAccessorHeaderTestTemplate, func(name string) string { + switch name { + case "structName": + return ms.getName() + case "typeAccessor": + return of.typeAccessor + case "typeName": + return of.typeName + default: + panic(name) + } + })) + sb.WriteString("\n") + for _, v := range of.values { + v.generateSetWithTestValue(of, sb) + sb.WriteString("\n\tassert.Equal(t, " + of.typeName + v.getFieldType() + ", tv." + of.typeAccessor + "())\n") + } + sb.WriteString("}\n") +} + func (of *oneOfField) generateSetWithTestValue(sb *strings.Builder) { of.values[of.testValueIdx].generateSetWithTestValue(of, sb) } @@ -437,10 +497,12 @@ func (of *oneOfField) generateCopyToValue(sb *strings.Builder) { var _ baseField = (*oneOfField)(nil) type oneOfValue interface { + getFieldType() string generateAccessors(ms baseStruct, of *oneOfField, sb *strings.Builder) generateTests(ms baseStruct, of *oneOfField, sb *strings.Builder) generateSetWithTestValue(of *oneOfField, sb *strings.Builder) generateCopyToValue(of *oneOfField, sb *strings.Builder) + generateTypeSwitchCase(of *oneOfField, sb *strings.Builder) } type oneOfPrimitiveValue struct { @@ -452,6 +514,10 @@ type oneOfPrimitiveValue struct { originFieldName string } +func (opv *oneOfPrimitiveValue) getFieldType() string { + return opv.fieldType +} + func (opv *oneOfPrimitiveValue) generateAccessors(ms baseStruct, of *oneOfField, sb *strings.Builder) { sb.WriteString(os.Expand(accessorsOneOfPrimitiveTemplate, func(name string) string { switch name { @@ -503,6 +569,11 @@ func (opv *oneOfPrimitiveValue) generateCopyToValue(of *oneOfField, sb *strings. sb.WriteString("\t dest.Set" + opv.fieldName + "(ms." + opv.fieldName + "())\n") } +func (opv *oneOfPrimitiveValue) generateTypeSwitchCase(of *oneOfField, sb *strings.Builder) { + sb.WriteString("\tcase *" + of.originTypePrefix + opv.originFieldName + ":\n") + sb.WriteString("\t\treturn " + of.typeName + opv.fieldType + "\n") +} + var _ oneOfValue = (*oneOfPrimitiveValue)(nil) type oneOfMessageValue struct { @@ -511,6 +582,10 @@ type oneOfMessageValue struct { returnMessage *messageValueStruct } +func (omv *oneOfMessageValue) getFieldType() string { + return omv.fieldName +} + func (omv *oneOfMessageValue) generateAccessors(ms baseStruct, of *oneOfField, sb *strings.Builder) { sb.WriteString(os.Expand(accessorsOneOfMessageTemplate, func(name string) string { switch name { @@ -580,4 +655,9 @@ func (omv *oneOfMessageValue) generateCopyToValue(of *oneOfField, sb *strings.Bu sb.WriteString("\n") } +func (omv *oneOfMessageValue) generateTypeSwitchCase(of *oneOfField, sb *strings.Builder) { + sb.WriteString("\tcase *" + of.originTypePrefix + omv.originFieldName + ":\n") + sb.WriteString("\t\treturn " + of.typeName + omv.fieldName + "\n") +} + var _ oneOfValue = (*oneOfMessageValue)(nil) diff --git a/model/pdata/generated_metrics.go b/model/pdata/generated_metrics.go index 5db1760350d..691f01e1c97 100644 --- a/model/pdata/generated_metrics.go +++ b/model/pdata/generated_metrics.go @@ -609,6 +609,24 @@ func (ms Metric) SetUnit(v string) { (*ms.orig).Unit = v } +// DataType returns the type of the data for this Metric. +// Calling this function on zero-initialized Metric will cause a panic. +func (ms Metric) DataType() MetricDataType { + switch ms.orig.Data.(type) { + case *otlpmetrics.Metric_Gauge: + return MetricDataTypeGauge + case *otlpmetrics.Metric_Sum: + return MetricDataTypeSum + case *otlpmetrics.Metric_Histogram: + return MetricDataTypeHistogram + case *otlpmetrics.Metric_ExponentialHistogram: + return MetricDataTypeExponentialHistogram + case *otlpmetrics.Metric_Summary: + return MetricDataTypeSummary + } + return MetricDataTypeNone +} + // Gauge returns the gauge associated with this Metric. // Calling this function when DataType() != MetricDataTypeGauge will cause a panic. // Calling this function on zero-initialized Metric will cause a panic. @@ -1106,6 +1124,18 @@ func (ms NumberDataPoint) SetTimestamp(v Timestamp) { (*ms.orig).TimeUnixNano = uint64(v) } +// Type returns the type of the value for this NumberDataPoint. +// Calling this function on zero-initialized NumberDataPoint will cause a panic. +func (ms NumberDataPoint) Type() MetricValueType { + switch ms.orig.Value.(type) { + case *otlpmetrics.NumberDataPoint_AsDouble: + return MetricValueTypeDouble + case *otlpmetrics.NumberDataPoint_AsInt: + return MetricValueTypeInt + } + return MetricValueTypeNone +} + // DoubleVal returns the doubleval associated with this NumberDataPoint. func (ms NumberDataPoint) DoubleVal() float64 { return (*ms.orig).GetAsDouble() @@ -2344,6 +2374,18 @@ func (ms Exemplar) SetTimestamp(v Timestamp) { (*ms.orig).TimeUnixNano = uint64(v) } +// Type returns the type of the value for this Exemplar. +// Calling this function on zero-initialized Exemplar will cause a panic. +func (ms Exemplar) Type() MetricValueType { + switch ms.orig.Value.(type) { + case *otlpmetrics.Exemplar_AsDouble: + return MetricValueTypeDouble + case *otlpmetrics.Exemplar_AsInt: + return MetricValueTypeInt + } + return MetricValueTypeNone +} + // DoubleVal returns the doubleval associated with this Exemplar. func (ms Exemplar) DoubleVal() float64 { return (*ms.orig).GetAsDouble() diff --git a/model/pdata/generated_metrics_test.go b/model/pdata/generated_metrics_test.go index 57162a8c4b5..1c3291e0491 100644 --- a/model/pdata/generated_metrics_test.go +++ b/model/pdata/generated_metrics_test.go @@ -465,6 +465,27 @@ func TestMetric_Unit(t *testing.T) { assert.EqualValues(t, testValUnit, ms.Unit()) } +func TestMetricDataType(t *testing.T) { + tv := NewMetric() + assert.Equal(t, MetricDataTypeNone, tv.DataType()) + assert.Equal(t, "", MetricDataType(1000).String()) + tv.SetDataType(MetricDataTypeGauge) + fillTestGauge(tv.Gauge()) + assert.Equal(t, MetricDataTypeGauge, tv.DataType()) + tv.SetDataType(MetricDataTypeSum) + fillTestSum(tv.Sum()) + assert.Equal(t, MetricDataTypeSum, tv.DataType()) + tv.SetDataType(MetricDataTypeHistogram) + fillTestHistogram(tv.Histogram()) + assert.Equal(t, MetricDataTypeHistogram, tv.DataType()) + tv.SetDataType(MetricDataTypeExponentialHistogram) + fillTestExponentialHistogram(tv.ExponentialHistogram()) + assert.Equal(t, MetricDataTypeExponentialHistogram, tv.DataType()) + tv.SetDataType(MetricDataTypeSummary) + fillTestSummary(tv.Summary()) + assert.Equal(t, MetricDataTypeSummary, tv.DataType()) +} + func TestMetric_Gauge(t *testing.T) { ms := NewMetric() ms.SetDataType(MetricDataTypeGauge) @@ -835,6 +856,16 @@ func TestNumberDataPoint_Timestamp(t *testing.T) { assert.EqualValues(t, testValTimestamp, ms.Timestamp()) } +func TestNumberDataPointType(t *testing.T) { + tv := NewNumberDataPoint() + assert.Equal(t, MetricValueTypeNone, tv.Type()) + assert.Equal(t, "", MetricValueType(1000).String()) + tv.SetDoubleVal(float64(17.13)) + assert.Equal(t, MetricValueTypeDouble, tv.Type()) + tv.SetIntVal(int64(17)) + assert.Equal(t, MetricValueTypeInt, tv.Type()) +} + func TestNumberDataPoint_DoubleVal(t *testing.T) { ms := NewNumberDataPoint() assert.EqualValues(t, float64(0.0), ms.DoubleVal()) @@ -1744,6 +1775,16 @@ func TestExemplar_Timestamp(t *testing.T) { assert.EqualValues(t, testValTimestamp, ms.Timestamp()) } +func TestExemplarType(t *testing.T) { + tv := NewExemplar() + assert.Equal(t, MetricValueTypeNone, tv.Type()) + assert.Equal(t, "", MetricValueType(1000).String()) + tv.SetDoubleVal(float64(17.13)) + assert.Equal(t, MetricValueTypeDouble, tv.Type()) + tv.SetIntVal(int64(17)) + assert.Equal(t, MetricValueTypeInt, tv.Type()) +} + func TestExemplar_DoubleVal(t *testing.T) { ms := NewExemplar() assert.EqualValues(t, float64(0.0), ms.DoubleVal()) diff --git a/model/pdata/metrics.go b/model/pdata/metrics.go index 1c8646bc23c..fd69cc57521 100644 --- a/model/pdata/metrics.go +++ b/model/pdata/metrics.go @@ -153,24 +153,6 @@ func (mdt MetricDataType) String() string { return "" } -// DataType returns the type of the data for this Metric. -// Calling this function on zero-initialized Metric will cause a panic. -func (ms Metric) DataType() MetricDataType { - switch ms.orig.Data.(type) { - case *otlpmetrics.Metric_Gauge: - return MetricDataTypeGauge - case *otlpmetrics.Metric_Sum: - return MetricDataTypeSum - case *otlpmetrics.Metric_Histogram: - return MetricDataTypeHistogram - case *otlpmetrics.Metric_ExponentialHistogram: - return MetricDataTypeExponentialHistogram - case *otlpmetrics.Metric_Summary: - return MetricDataTypeSummary - } - return MetricDataTypeNone -} - // SetDataType clears any existing data and initialize it with an empty data of the given type. // Calling this function on zero-initialized Metric will cause a panic. func (ms Metric) SetDataType(ty MetricDataType) { @@ -266,27 +248,3 @@ func (mdt MetricValueType) String() string { } return "" } - -// Type returns the type of the value for this NumberDataPoint. -// Calling this function on zero-initialized NumberDataPoint will cause a panic. -func (ms NumberDataPoint) Type() MetricValueType { - switch ms.orig.Value.(type) { - case *otlpmetrics.NumberDataPoint_AsDouble: - return MetricValueTypeDouble - case *otlpmetrics.NumberDataPoint_AsInt: - return MetricValueTypeInt - } - return MetricValueTypeNone -} - -// Type returns the type of the value for this Exemplar. -// Calling this function on zero-initialized Exemplar will cause a panic. -func (ms Exemplar) Type() MetricValueType { - switch ms.orig.Value.(type) { - case *otlpmetrics.Exemplar_AsDouble: - return MetricValueTypeDouble - case *otlpmetrics.Exemplar_AsInt: - return MetricValueTypeInt - } - return MetricValueTypeNone -} diff --git a/model/pdata/metrics_test.go b/model/pdata/metrics_test.go index 55fc191b97d..0c7268dc9cc 100644 --- a/model/pdata/metrics_test.go +++ b/model/pdata/metrics_test.go @@ -43,27 +43,6 @@ func TestMetricDataTypeString(t *testing.T) { assert.Equal(t, "", (MetricDataTypeSummary + 1).String()) } -func TestMetricDataType(t *testing.T) { - m := NewMetric() - assert.Equal(t, "None", MetricDataTypeNone.String()) - assert.Equal(t, MetricDataTypeNone, m.DataType()) - m.SetDataType(MetricDataTypeGauge) - assert.Equal(t, "Gauge", MetricDataTypeGauge.String()) - assert.Equal(t, MetricDataTypeGauge, m.DataType()) - m.SetDataType(MetricDataTypeSum) - assert.Equal(t, "Sum", MetricDataTypeSum.String()) - assert.Equal(t, MetricDataTypeSum, m.DataType()) - m.SetDataType(MetricDataTypeHistogram) - assert.Equal(t, "Histogram", MetricDataTypeHistogram.String()) - assert.Equal(t, MetricDataTypeHistogram, m.DataType()) - m.SetDataType(MetricDataTypeExponentialHistogram) - assert.Equal(t, "ExponentialHistogram", MetricDataTypeExponentialHistogram.String()) - assert.Equal(t, MetricDataTypeExponentialHistogram, m.DataType()) - m.SetDataType(MetricDataTypeSummary) - assert.Equal(t, "Summary", MetricDataTypeSummary.String()) - assert.Equal(t, MetricDataTypeSummary, m.DataType()) -} - func TestPointMetricValueTypeString(t *testing.T) { assert.Equal(t, "None", MetricValueTypeNone.String()) assert.Equal(t, "Int", MetricValueTypeInt.String()) @@ -71,24 +50,6 @@ func TestPointMetricValueTypeString(t *testing.T) { assert.Equal(t, "", (MetricValueTypeDouble + 1).String()) } -func TestPointMetricValueType(t *testing.T) { - m := NewNumberDataPoint() - assert.Equal(t, MetricValueTypeNone, m.Type()) - m.SetIntVal(10) - assert.Equal(t, MetricValueTypeInt, m.Type()) - m.SetDoubleVal(10.0) - assert.Equal(t, MetricValueTypeDouble, m.Type()) -} - -func TestExemplarMetricValueType(t *testing.T) { - m := NewExemplar() - assert.Equal(t, MetricValueTypeNone, m.Type()) - m.SetIntVal(10) - assert.Equal(t, MetricValueTypeInt, m.Type()) - m.SetDoubleVal(10.0) - assert.Equal(t, MetricValueTypeDouble, m.Type()) -} - func TestResourceMetricsWireCompatibility(t *testing.T) { // This test verifies that OTLP ProtoBufs generated using goproto lib in // opentelemetry-proto repository OTLP ProtoBufs generated using gogoproto lib in