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 7, 2022
1 parent f4e6175 commit 30c0388
Show file tree
Hide file tree
Showing 19 changed files with 634 additions and 562 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
- Deprecate `MetricDataPointFlagsImmutable` type. (#6017)
- Deprecate `*DataPoint.[Set]FlagsImmutable()` funcs in favor of `*DataPoint.[Set]Flags()`. (#6017)
- Deprecate `LogRecord.FlagsStruct()` and `LogRecord.SetFlagsStruct()` in favor of `LogRecord.Flags()` and `LogRecord.SetFlags()`. (#6007)
- 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`

### 💡 Enhancements 💡

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().FromRaw([]uint64{0, 1})
exemplar := hdp1.Exemplars().AppendEmpty()
exemplar.SetTimestamp(metricExemplarTimestamp)
exemplar.SetDoubleVal(15)
initMetricExemplarAttributes(exemplar.FilteredAttributes())
hdp1.SetExplicitBounds(pcommon.NewImmutableFloat64Slice([]float64{1}))
hdp1.ExplicitBounds().FromRaw([]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().FromRaw([]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().FromRaw([]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().FromRaw([]uint64{1, 1})

// The above will print:
// Bucket [0, 0], Count: 1
Expand Down
12 changes: 6 additions & 6 deletions pdata/internal/cmd/pdatagen/internal/base_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ 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.60.0] Use ${fieldName}().FromRaw() 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 oneOfTypeAccessorHeaderTemplate = `// ${originFieldName}Type returns the type of the ${lowerOriginFieldName} for this ${structName}.
Expand Down Expand Up @@ -163,10 +164,9 @@ const accessorsPrimitiveTypedTestTemplate = `func Test${structName}_${fieldName}

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

const accessorsOptionalPrimitiveValueTemplate = `// ${fieldName} returns the ${lowerFieldName} associated with this ${structName}.
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 30c0388

Please sign in to comment.