Skip to content

Commit

Permalink
[pdata] Make wrappers primitive slices mutable
Browse files Browse the repository at this point in the history
Change API for primitive slice wrappers to allow changing items as it appeared to be expensive to update that data as immutable wrappers: it requires making two copies to update a slice
  • Loading branch information
dmitryax committed Sep 1, 2022
1 parent 8e2bb85 commit 92458f8
Show file tree
Hide file tree
Showing 19 changed files with 627 additions and 559 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
- `processorhelper.New[Traces|Metrics|Logs]ProcessorWithCreateSettings`
- `component.NewExtensionFactoryWithStabilityLevel`

### 🚩 Deprecations 🚩

- Primitive slice wrapper are now mutable (#5971):
- `pcommon.ImmutableByteSlice` is deprecated in favor of `pcommon.ByteSlice`
- `pcommon.ImmutableFloat64Slice` is deprecated in favor of `pcommon.Float64Slice`
- `pcommon.ImmutableUInt64Slice` is deprecated in favor of `pcommon.UInt64Slice`
- Temporarily deprecate `pcommon.NewValueBytes` that will be replaced with `pcommon.NewValueBytesEmpty` in 0.60.0
- Deprecate `pcommon.Map.UpsertBytes` in favor of `pcommon.Map.UpsertEmptyBytes`
- Deprecate `pcommon.Value.SetBytesVal` in favor of `pcommon.Value.SetEmptyBytesVal`

## v0.59.0 Beta

### 🛑 Breaking changes 🛑
Expand Down
10 changes: 5 additions & 5 deletions internal/testdata/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,12 @@ func initHistogramMetric(hm pmetric.Metric) {
hdp1.SetSum(15)
hdp1.SetMin(15)
hdp1.SetMax(15)
hdp1.SetBucketCounts(pcommon.NewImmutableUInt64Slice([]uint64{0, 1}))
hdp1.BucketCounts().CopyFromRaw([]uint64{0, 1})
exemplar := hdp1.Exemplars().AppendEmpty()
exemplar.SetTimestamp(metricExemplarTimestamp)
exemplar.SetDoubleVal(15)
initMetricExemplarAttributes(exemplar.FilteredAttributes())
hdp1.SetExplicitBounds(pcommon.NewImmutableFloat64Slice([]float64{1}))
hdp1.ExplicitBounds().CopyFromRaw([]float64{1})
}

func initExponentialHistogramMetric(hm pmetric.Metric) {
Expand All @@ -220,10 +220,10 @@ func initExponentialHistogramMetric(hm pmetric.Metric) {

// positive index 1 and 2 are values sqrt(2), 2 at scale 1
hdp0.Positive().SetOffset(1)
hdp0.Positive().SetBucketCounts(pcommon.NewImmutableUInt64Slice([]uint64{1, 1}))
hdp0.Positive().BucketCounts().CopyFromRaw([]uint64{1, 1})
// negative index -1 and 0 are values -1/sqrt(2), -1 at scale 1
hdp0.Negative().SetOffset(-1)
hdp0.Negative().SetBucketCounts(pcommon.NewImmutableUInt64Slice([]uint64{1, 1}))
hdp0.Negative().BucketCounts().CopyFromRaw([]uint64{1, 1})

// The above will print:
// Bucket (-1.414214, -1.000000], Count: 1
Expand All @@ -245,7 +245,7 @@ func initExponentialHistogramMetric(hm pmetric.Metric) {

// index -1 and 0 are values 0.25, 1 at scale -1
hdp1.Positive().SetOffset(-1)
hdp1.Positive().SetBucketCounts(pcommon.NewImmutableUInt64Slice([]uint64{1, 1}))
hdp1.Positive().BucketCounts().CopyFromRaw([]uint64{1, 1})

// The above will print:
// Bucket [0, 0], Count: 1
Expand Down
14 changes: 11 additions & 3 deletions pdata/internal/cmd/pdatagen/internal/base_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,20 @@ const copyToPrimitiveSliceTestTemplate = ` if len(ms.getOrig().${originFieldName

const accessorsPrimitiveSliceTemplate = `// ${fieldName} returns the ${lowerFieldName} associated with this ${structName}.
func (ms ${structName}) ${fieldName}() ${packageName}${returnType} {
return ${packageName}${returnType}(internal.New${returnType}(ms.getOrig().${originFieldName}))
return ${packageName}${returnType}(internal.New${returnType}(&ms.getOrig().${originFieldName}))
}
// Set${fieldName} replaces the ${lowerFieldName} associated with this ${structName}.
// Deprecated: [0.59.0] Use ${fieldName}().CopyFromRaw() instead
func (ms ${structName}) Set${fieldName}(v ${packageName}${returnType}) {
ms.getOrig().${originFieldName} = internal.GetOrig${returnType}(internal.${returnType}(v))
ms.getOrig().${originFieldName} = *internal.GetOrig${returnType}(internal.${returnType}(v))
}`

const accessorsPrimitiveSliceTestTemplate = `func Test${structName}_${fieldName}(t *testing.T) {
ms := New${structName}()
assert.Equal(t, ${defaultVal}, ms.${fieldName}().AsRaw())
ms.${fieldName}().CopyFromRaw(${testValue})
assert.Equal(t, ${testValue}, ms.${fieldName}().AsRaw())
}`

const oneOfTypeAccessorHeaderTemplate = `// ${originFieldName}Type returns the type of the ${lowerOriginFieldName} for this ${structName}.
Expand Down Expand Up @@ -558,7 +566,7 @@ func (psf *primitiveSliceField) generateAccessors(ms baseStruct, sb *strings.Bui
}

func (psf *primitiveSliceField) generateAccessorsTest(ms baseStruct, sb *strings.Builder) {
sb.WriteString(os.Expand(accessorsPrimitiveStructTestTemplate, func(name string) string {
sb.WriteString(os.Expand(accessorsPrimitiveSliceTestTemplate, func(name string) string {
switch name {
case "structName":
return ms.getName()
Expand Down
2 changes: 1 addition & 1 deletion pdata/internal/cmd/pdatagen/internal/files.go

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

199 changes: 0 additions & 199 deletions pdata/internal/cmd/pdatagen/internal/immutable_slice_structs.go

This file was deleted.

4 changes: 2 additions & 2 deletions pdata/internal/cmd/pdatagen/internal/metrics_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ var valueFloat64Field = &primitiveField{
var bucketCountsField = &primitiveSliceField{
fieldName: "BucketCounts",
originFieldName: "BucketCounts",
returnType: "ImmutableUInt64Slice",
returnType: "UInt64Slice",
returnPackageName: "pcommon",
defaultVal: "[]uint64(nil)",
rawType: "[]uint64",
Expand All @@ -544,7 +544,7 @@ var bucketCountsField = &primitiveSliceField{
var explicitBoundsField = &primitiveSliceField{
fieldName: "ExplicitBounds",
originFieldName: "ExplicitBounds",
returnType: "ImmutableFloat64Slice",
returnType: "Float64Slice",
returnPackageName: "pcommon",
defaultVal: "[]float64(nil)",
rawType: "[]float64",
Expand Down
Loading

0 comments on commit 92458f8

Please sign in to comment.