Skip to content

Commit

Permalink
[pdata] Change outcome of Metric data accessors misuse (open-telemetr…
Browse files Browse the repository at this point in the history
…y#5035)

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
  • Loading branch information
dmitryax authored and Nicholaswang committed Jun 7, 2022
1 parent 6165486 commit 8ea1858
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
- `pdata.NewAttributeValue...` funcs are deprecated in favor of `pdata.NewValue...`
- Deprecate LogRecord.Name(), it was deprecated in the data model (#5054)

### 💡 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 @@ -478,6 +485,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.

0 comments on commit 8ea1858

Please sign in to comment.