Skip to content

Commit

Permalink
[pdata] Rename NumberDataPoint/Exemplar.Type methods to ValueType
Browse files Browse the repository at this point in the history
Deprecate `NumberDataPoint.Type()` and `Exemplar.Type()` in favor of `NumberDataPoint.ValueType()` and `Exemplar.ValueType()` instead. Do not use accessorType in the pdatagen, always rely on proto field type for consistency.
  • Loading branch information
dmitryax committed Feb 11, 2022
1 parent 8646986 commit ab9994e
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 40 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
- Reject invalid queue size exporterhelper (#4799)
- Transform configmapprovider.Retrieved interface to a struct (#4789)

## 🚩 Deprecations 🚩

- Deprecate `pdata.NumberDataPoint.Type()` and `pdata.Exemplar.Type()` in favor of `NumberDataPoint.ValueType()` and
`Exemplar.ValueType()` (#4850)

## v0.44.0 Beta

## 🛑 Breaking changes 🛑
Expand Down
2 changes: 1 addition & 1 deletion internal/otlptext/databuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (b *dataBuffer) logNumberDataPoints(ps pdata.NumberDataPointSlice) {

b.logEntry("StartTimestamp: %s", p.StartTimestamp())
b.logEntry("Timestamp: %s", p.Timestamp())
switch p.Type() {
switch p.ValueType() {
case pdata.MetricValueTypeInt:
b.logEntry("Value: %d", p.IntVal())
case pdata.MetricValueTypeDouble:
Expand Down
40 changes: 18 additions & 22 deletions model/internal/cmd/pdatagen/internal/base_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,41 +53,41 @@ func (ms ${structName}) Set${fieldName}(v ${returnType}) {
(*ms.orig).${originFieldName} = v
}`

const oneOfTypeAccessorHeaderTemplate = `// ${typeAccessor} returns the type of the ${lowerOriginFieldName} for this ${structName}.
const oneOfTypeAccessorHeaderTemplate = `// ${originFieldName}Type 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} {
func (ms ${structName}) ${originFieldName}Type() ${typeName} {
switch ms.orig.${originFieldName}.(type) {`

const oneOfTypeAccessorHeaderTestTemplate = `func Test${structName}${typeAccessor}(t *testing.T) {
const oneOfTypeAccessorHeaderTestTemplate = `func Test${structName}${originFieldName}Type(t *testing.T) {
tv := New${structName}()
assert.Equal(t, ${typeName}None, tv.${typeAccessor}())
assert.Equal(t, ${typeName}None, tv.${originFieldName}Type())
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 when ${originOneOfFieldName}Type() != ${typeName} will cause a panic.
// Calling this function on zero-initialized ${structName} will cause a panic.
func (ms ${structName}) ${fieldName}() ${returnType} {
return new${returnType}((*ms.orig).${originOneOfFieldName}.(*${originStructType}).${originFieldName})
}`

const accessorsOneOfMessageTestTemplate = `func Test${structName}_${fieldName}(t *testing.T) {
ms := New${structName}()
ms.Set${typeAccessor}(${typeName})
ms.Set${originOneOfFieldName}Type(${typeName})
fillTest${returnType}(ms.${fieldName}())
assert.EqualValues(t, generateTest${returnType}(), ms.${fieldName}())
}
func Test${structName}_CopyTo_${fieldName}(t *testing.T) {
ms := New${structName}()
ms.Set${typeAccessor}(${typeName})
ms.Set${originOneOfFieldName}Type(${typeName})
fillTest${returnType}(ms.${fieldName}())
dest := New${structName}()
ms.CopyTo(dest)
assert.EqualValues(t, ms, dest)
}`

const copyToValueOneOfMessageTemplate = ` case ${typeName}:
dest.Set${typeAccessor}(${typeName})
dest.Set${originOneOfFieldName}Type(${typeName})
ms.${fieldName}().CopyTo(dest.${fieldName}())`

const accessorsOneOfPrimitiveTemplate = `// ${fieldName} returns the ${lowerFieldName} associated with this ${structName}.
Expand Down Expand Up @@ -411,7 +411,6 @@ var _ baseField = (*primitiveStructField)(nil)
type oneOfField struct {
originTypePrefix string
originFieldName string
typeAccessor string
typeName string
testValueIdx int
values []oneOfValue
Expand All @@ -435,8 +434,6 @@ func (of *oneOfField) generateTypeAccessors(ms baseStruct, sb *strings.Builder)
return of.originFieldName
case "structName":
return ms.getName()
case "typeAccessor":
return of.typeAccessor
case "typeName":
return of.typeName
default:
Expand Down Expand Up @@ -466,8 +463,8 @@ func (of *oneOfField) generateTypeAccessorsTest(ms baseStruct, sb *strings.Build
switch name {
case "structName":
return ms.getName()
case "typeAccessor":
return of.typeAccessor
case "originFieldName":
return of.originFieldName
case "typeName":
return of.typeName
default:
Expand All @@ -477,7 +474,8 @@ func (of *oneOfField) generateTypeAccessorsTest(ms baseStruct, sb *strings.Build
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\tassert.Equal(t, " + of.typeName + v.getFieldType() + ", " +
"tv." + of.originFieldName + "Type())\n")
}
sb.WriteString("}\n")
}
Expand All @@ -487,7 +485,7 @@ func (of *oneOfField) generateSetWithTestValue(sb *strings.Builder) {
}

func (of *oneOfField) generateCopyToValue(sb *strings.Builder) {
sb.WriteString("\tswitch ms." + of.typeAccessor + "() {\n")
sb.WriteString("\tswitch ms." + of.originFieldName + "Type() {\n")
for _, v := range of.values {
v.generateCopyToValue(of, sb)
}
Expand Down Expand Up @@ -603,8 +601,6 @@ func (omv *oneOfMessageValue) generateAccessors(ms baseStruct, of *oneOfField, s
return omv.returnMessage.structName
case "structName":
return ms.getName()
case "typeAccessor":
return of.typeAccessor
case "typeName":
return of.typeName + omv.returnMessage.structName
default:
Expand All @@ -623,8 +619,8 @@ func (omv *oneOfMessageValue) generateTests(ms baseStruct, of *oneOfField, sb *s
return omv.fieldName
case "returnType":
return omv.returnMessage.structName
case "typeAccessor":
return of.typeAccessor
case "originOneOfFieldName":
return of.originFieldName
case "typeName":
return of.typeName + omv.returnMessage.structName
default:
Expand All @@ -635,7 +631,7 @@ func (omv *oneOfMessageValue) generateTests(ms baseStruct, of *oneOfField, sb *s
}

func (omv *oneOfMessageValue) generateSetWithTestValue(of *oneOfField, sb *strings.Builder) {
sb.WriteString("tv.Set" + of.typeAccessor + "(" + of.typeName + omv.returnMessage.structName + ")\n")
sb.WriteString("tv.Set" + of.originFieldName + "Type(" + of.typeName + omv.returnMessage.structName + ")\n")
sb.WriteString("fillTest" + omv.returnMessage.structName + "(tv." + omv.fieldName + "())")
}

Expand All @@ -644,8 +640,8 @@ func (omv *oneOfMessageValue) generateCopyToValue(of *oneOfField, sb *strings.Bu
switch name {
case "fieldName":
return omv.fieldName
case "typeAccessor":
return of.typeAccessor
case "originOneOfFieldName":
return of.originFieldName
case "typeName":
return of.typeName + omv.fieldName
default:
Expand Down
3 changes: 0 additions & 3 deletions model/internal/cmd/pdatagen/internal/metrics_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ var metric = &messageValueStruct{
testVal: `"1"`,
},
&oneOfField{
typeAccessor: "DataType",
typeName: "MetricDataType",
originFieldName: "Data",
originTypePrefix: "otlpmetrics.Metric_",
Expand Down Expand Up @@ -243,7 +242,6 @@ var numberDataPoint = &messageValueStruct{
startTimeField,
timeField,
&oneOfField{
typeAccessor: "Type",
typeName: "MetricValueType",
originFieldName: "Value",
originTypePrefix: "otlpmetrics.NumberDataPoint_",
Expand Down Expand Up @@ -414,7 +412,6 @@ var exemplar = &messageValueStruct{
fields: []baseField{
timeField,
&oneOfField{
typeAccessor: "Type",
typeName: "MetricValueType",
originFieldName: "Value",
originTypePrefix: "otlpmetrics.Exemplar_",
Expand Down
12 changes: 6 additions & 6 deletions model/pdata/generated_metrics.go

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

16 changes: 8 additions & 8 deletions model/pdata/generated_metrics_test.go

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

10 changes: 10 additions & 0 deletions model/pdata/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,13 @@ func (mdt MetricValueType) String() string {
}
return ""
}

// Deprecated: [v0.45.0] Use ValueType instead.
func (ms NumberDataPoint) Type() MetricValueType {
return ms.ValueType()
}

// Deprecated: [v0.45.0] Use ValueType instead.
func (ms Exemplar) Type() MetricValueType {
return ms.ValueType()
}
12 changes: 12 additions & 0 deletions model/pdata/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,18 @@ func TestMetricsDataPointFlags(t *testing.T) {
assert.Equal(t, "FLAG_NO_RECORDED_VALUE", gauge.DataPoints().At(0).Flags().String())
}

// TestNumberDataPointType tests deprecated NumberDataPoint.Type
func TestNumberDataPointType(t *testing.T) {
tv := NewNumberDataPoint()
assert.Equal(t, tv.ValueType(), tv.Type())
}

// TestExemplarType tests deprecated Exemplar.Type
func TestExemplarType(t *testing.T) {
tv := NewExemplar()
assert.Equal(t, tv.ValueType(), tv.Type())
}

func BenchmarkMetricsClone(b *testing.B) {
metrics := NewMetrics()
fillTestResourceMetricsSlice(metrics.ResourceMetrics())
Expand Down

0 comments on commit ab9994e

Please sign in to comment.