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

[pdata] Change outcome of Metric data accessors misuse #5035

Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
- `pdata.AttributeValueType...` constants are deprecated in favor of `pdata.ValueType...`
- `pdata.NewAttributeValue...` funcs are deprecated in favor of `pdata.NewValue...`

### 💡 Enhancements 💡

- Change outcome of `pdata.Metric.<Gauge|Sum|Histogram|ExponentialHistogram>()` functions misuse.
In case of type mismatch, they don't panic right away but return an invalid zero-initialized
instance for consistency with other OneOf field accessors (#5034)

## v0.47.0 Beta

### 🛑 Breaking changes 🛑
Expand Down
16 changes: 14 additions & 2 deletions model/internal/cmd/pdatagen/internal/base_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,17 @@ const oneOfTypeAccessorHeaderTestTemplate = `func Test${structName}${originField
assert.Equal(t, "", ${typeName}(1000).String())`

const accessorsOneOfMessageTemplate = `// ${fieldName} returns the ${lowerFieldName} associated with this ${structName}.
// Calling this function when ${originOneOfFieldName}Type() != ${typeName} will cause a panic.
//
// Calling this function when ${originOneOfFieldName}Type() != ${typeName} returns an invalid
// zero-initialized instance of ${returnType}. Note that using such ${returnType} instance can cause 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})
v, ok := ms.orig.Get${originOneOfFieldName}().(*${originStructType})
if !ok {
return ${returnType}{}
}
return new${returnType}(v.${originFieldName})
}`

const accessorsOneOfMessageTestTemplate = `func Test${structName}_${fieldName}(t *testing.T) {
Expand Down Expand Up @@ -472,6 +479,11 @@ func (of *oneOfField) generateTypeAccessorsTest(ms baseStruct, sb *strings.Build
}
}))
sb.WriteString("\n")
for _, v := range of.values {
if mv, ok := v.(*oneOfMessageValue); ok {
sb.WriteString("\tassert.Equal(t, " + mv.fieldName + "{}, tv." + mv.fieldName + "())\n")
}
}
for _, v := range of.values {
v.generateSetWithTestValue(of, sb)
sb.WriteString("\n\tassert.Equal(t, " + of.typeName + v.getFieldType() + ", " +
Expand Down
55 changes: 45 additions & 10 deletions model/internal/pdata/generated_metrics.go

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

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

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