Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pdatagen] Generate Metric type accessors #4811

Merged
merged 1 commit into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 85 additions & 5 deletions model/internal/cmd/pdatagen/internal/base_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
42 changes: 42 additions & 0 deletions model/pdata/generated_metrics.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions model/pdata/generated_metrics_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 0 additions & 42 deletions model/pdata/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
39 changes: 0 additions & 39 deletions model/pdata/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,52 +43,13 @@ 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())
assert.Equal(t, "Double", MetricValueTypeDouble.String())
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
Expand Down