Skip to content

Commit

Permalink
[pdata] Rename methods dealing with slices of primitive type items (#…
Browse files Browse the repository at this point in the history
…5344)

* [pdata] Rename methods dealing with slices of primitive type items

- In preparation of migration to immutable slices for primitive type items the following methods are renamed:
  - `Value.BytesVal` func is deprecated in favor of `Value.MBytesVal`.
  - `Value.SetBytesVal` func is deprecated in favor of `Value.SetMBytesVal`.
  - `Value.UpdateBytes` func is deprecated in favor of `Value.UpdateMBytes`.
  - `Value.InsertBytes` func is deprecated in favor of `Value.InsertMBytes`.
  - `Value.UpsertBytes` func is deprecated in favor of `Value.UpsertMBytes`.
  - `<HistogramDataPoint|Buckets>.BucketCounts` funcs are deprecated in favor of
    `<HistogramDataPoint|Buckets>.MBucketCounts`.
  - `<HistogramDataPoint|Buckets>.SetBucketCounts` funcs are deprecated in favor of
    `<HistogramDataPoint|Buckets>.SetMBucketCounts`.
  - `HistogramDataPoint.ExplicitBounds` func is deprecated in favor of `HistogramDataPoint.MExplicitBounds`.
  - `HistogramDataPoint.SetExplicitBounds` func is deprecated in favor of `HistogramDataPoint.SetMExplicitBounds`.

* Update CHANGELOG.md
  • Loading branch information
dmitryax committed May 11, 2022
1 parent 7f66c9c commit 6cde802
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 68 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@

### 🚩 Deprecations 🚩

- In preparation of migration to immutable slices for primitive type items, the following methods are renamed (#5344)
- `Value.BytesVal` func is deprecated in favor of `Value.MBytesVal`.
- `Value.SetBytesVal` func is deprecated in favor of `Value.SetMBytesVal`.
- `Value.UpdateBytes` func is deprecated in favor of `Value.UpdateMBytes`.
- `Value.InsertBytes` func is deprecated in favor of `Value.InsertMBytes`.
- `Value.UpsertBytes` func is deprecated in favor of `Value.UpsertMBytes`.
- `<HistogramDataPoint|Buckets>.BucketCounts` funcs are deprecated in favor of
`<HistogramDataPoint|Buckets>.MBucketCounts`.
- `<HistogramDataPoint|Buckets>.SetBucketCounts` funcs are deprecated in favor of
`<HistogramDataPoint|Buckets>.SetMBucketCounts`.
- `HistogramDataPoint.ExplicitBounds` func is deprecated in favor of `HistogramDataPoint.MExplicitBounds`.
- `HistogramDataPoint.SetExplicitBounds` func is deprecated in favor of `HistogramDataPoint.SetMExplicitBounds`.

### 💡 Enhancements 💡

- `pdata`: Expose `pcommon.NewSliceFromRay` and `pcommon.Slice.AsRaw` functions (#5298)
Expand Down
12 changes: 6 additions & 6 deletions internal/otlptext/databuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ func (b *dataBuffer) logDoubleHistogramDataPoints(ps pmetric.HistogramDataPointS
b.logEntry("Count: %d", p.Count())
b.logEntry("Sum: %f", p.Sum())

bounds := p.ExplicitBounds()
bounds := p.MExplicitBounds()
if len(bounds) != 0 {
for i, bound := range bounds {
b.logEntry("ExplicitBounds #%d: %f", i, bound)
}
}

buckets := p.BucketCounts()
buckets := p.MBucketCounts()
if len(buckets) != 0 {
for j, bucket := range buckets {
b.logEntry("Buckets #%d, Count: %d", j, bucket)
Expand Down Expand Up @@ -157,13 +157,13 @@ func (b *dataBuffer) logExponentialHistogramDataPoints(ps pmetric.ExponentialHis
// uses a lookup table for the last finite boundary, which can be
// easily computed using `math/big` (for scales up to 20).

negB := p.Negative().BucketCounts()
posB := p.Positive().BucketCounts()
negB := p.Negative().MBucketCounts()
posB := p.Positive().MBucketCounts()

for i := 0; i < len(negB); i++ {
pos := len(negB) - i - 1
index := p.Negative().Offset() + int32(pos)
count := p.Negative().BucketCounts()[pos]
count := p.Negative().MBucketCounts()[pos]
lower := math.Exp(float64(index) * factor)
upper := math.Exp(float64(index+1) * factor)
b.logEntry("Bucket (%f, %f], Count: %d", -upper, -lower, count)
Expand All @@ -175,7 +175,7 @@ func (b *dataBuffer) logExponentialHistogramDataPoints(ps pmetric.ExponentialHis

for pos := 0; pos < len(posB); pos++ {
index := p.Positive().Offset() + int32(pos)
count := p.Positive().BucketCounts()[pos]
count := p.Positive().MBucketCounts()[pos]
lower := math.Exp(float64(index) * factor)
upper := math.Exp(float64(index+1) * factor)
b.logEntry("Bucket [%f, %f), Count: %d", lower, upper, count)
Expand Down
10 changes: 5 additions & 5 deletions internal/testdata/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,12 @@ func initHistogramMetric(hm pmetric.Metric) {
hdp1.SetTimestamp(TestMetricTimestamp)
hdp1.SetCount(1)
hdp1.SetSum(15)
hdp1.SetBucketCounts([]uint64{0, 1})
hdp1.SetMBucketCounts([]uint64{0, 1})
exemplar := hdp1.Exemplars().AppendEmpty()
exemplar.SetTimestamp(TestMetricExemplarTimestamp)
exemplar.SetDoubleVal(15)
initMetricAttachment(exemplar.FilteredAttributes())
hdp1.SetExplicitBounds([]float64{1})
hdp1.SetMExplicitBounds([]float64{1})
}

func initExponentialHistogramMetric(hm pmetric.Metric) {
Expand All @@ -229,10 +229,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([]uint64{1, 1})
hdp0.Positive().SetMBucketCounts([]uint64{1, 1})
// negative index -1 and 0 are values -1/sqrt(2), -1 at scale 1
hdp0.Negative().SetOffset(-1)
hdp0.Negative().SetBucketCounts([]uint64{1, 1})
hdp0.Negative().SetMBucketCounts([]uint64{1, 1})

// The above will print:
// Bucket (-1.414214, -1.000000], Count: 1
Expand All @@ -252,7 +252,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([]uint64{1, 1})
hdp1.Positive().SetMBucketCounts([]uint64{1, 1})

// The above will print:
// Bucket [0, 0], Count: 1
Expand Down
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 @@ -476,7 +476,7 @@ var valueFloat64Field = &primitiveField{
}

var bucketCountsField = &primitiveSliceField{
fieldName: "BucketCounts",
fieldName: "MBucketCounts",
originFieldName: "BucketCounts",
returnType: "[]uint64",
defaultVal: "[]uint64(nil)",
Expand All @@ -485,7 +485,7 @@ var bucketCountsField = &primitiveSliceField{
}

var explicitBoundsField = &primitiveSliceField{
fieldName: "ExplicitBounds",
fieldName: "MExplicitBounds",
originFieldName: "ExplicitBounds",
returnType: "[]float64",
defaultVal: "[]float64(nil)",
Expand Down
59 changes: 57 additions & 2 deletions pdata/internal/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,19 @@ func (v Value) SliceVal() Slice {
// If the Type() is not ValueTypeBytes then returns false.
// Calling this function on zero-initialized Value will cause a panic.
// Modifying the returned []byte in-place is forbidden.
// Deprecated: [0.51.0] Use MBytesVal instead.
func (v Value) BytesVal() []byte {
return v.orig.GetBytesValue()
}

// MBytesVal returns the []byte value associated with this Value.
// If the Type() is not ValueTypeBytes then returns false.
// Calling this function on zero-initialized Value will cause a panic.
// Modifying the returned []byte in-place is forbidden.
func (v Value) MBytesVal() []byte {
return v.orig.GetBytesValue()
}

// SetStringVal replaces the string value associated with this Value,
// it also changes the type to be ValueTypeString.
// Calling this function on zero-initialized Value will cause a panic.
Expand Down Expand Up @@ -299,10 +308,20 @@ func (v Value) SetBoolVal(bv bool) {
// Calling this function on zero-initialized Value will cause a panic.
// The caller must ensure the []byte passed in is not modified after the call is made, sharing the data
// across multiple attributes is forbidden.
// Deprecated: [0.51.0] Use SetMBytesVal instead.
func (v Value) SetBytesVal(bv []byte) {
v.orig.Value = &otlpcommon.AnyValue_BytesValue{BytesValue: bv}
}

// SetMBytesVal replaces the []byte value associated with this Value,
// it also changes the type to be ValueTypeBytes.
// Calling this function on zero-initialized Value will cause a panic.
// The caller must ensure the []byte passed in is not modified after the call is made, sharing the data
// across multiple attributes is forbidden.
func (v Value) SetMBytesVal(bv []byte) {
v.orig.Value = &otlpcommon.AnyValue_BytesValue{BytesValue: bv}
}

// copyTo copies the value to Value. Will panic if dest is nil.
func (v Value) copyTo(dest *otlpcommon.AnyValue) {
switch ov := v.orig.Value.(type) {
Expand Down Expand Up @@ -709,12 +728,23 @@ func (m Map) InsertBool(k string, v bool) {
// No action is applied to the map where the key already exists.
// The caller must ensure the []byte passed in is not modified after the call is made, sharing the data
// across multiple attributes is forbidden.
// Deprecated: [0.51.0] Use InsertMBytes instead.
func (m Map) InsertBytes(k string, v []byte) {
if _, existing := m.Get(k); !existing {
*m.orig = append(*m.orig, newAttributeKeyValueBytes(k, v))
}
}

// InsertMBytes adds the []byte Value to the map when the key does not exist.
// No action is applied to the map where the key already exists.
// The caller must ensure the []byte passed in is not modified after the call is made, sharing the data
// across multiple attributes is forbidden.
func (m Map) InsertMBytes(k string, v []byte) {
if _, existing := m.Get(k); !existing {
*m.orig = append(*m.orig, newAttributeKeyValueBytes(k, v))
}
}

// Update updates an existing Value with a value.
// No action is applied to the map where the key does not exist.
//
Expand Down Expand Up @@ -764,9 +794,20 @@ func (m Map) UpdateBool(k string, v bool) {
// No action is applied to the map where the key does not exist.
// The caller must ensure the []byte passed in is not modified after the call is made, sharing the data
// across multiple attributes is forbidden.
// Deprecated: [0.51.0] Use UpdateMBytes instead.
func (m Map) UpdateBytes(k string, v []byte) {
if av, existing := m.Get(k); existing {
av.SetBytesVal(v)
av.SetMBytesVal(v)
}
}

// UpdateMBytes updates an existing []byte Value with a value.
// No action is applied to the map where the key does not exist.
// The caller must ensure the []byte passed in is not modified after the call is made, sharing the data
// across multiple attributes is forbidden.
func (m Map) UpdateMBytes(k string, v []byte) {
if av, existing := m.Get(k); existing {
av.SetMBytesVal(v)
}
}

Expand Down Expand Up @@ -835,9 +876,23 @@ func (m Map) UpsertBool(k string, v bool) {
// updated to the map where the key already existed.
// The caller must ensure the []byte passed in is not modified after the call is made, sharing the data
// across multiple attributes is forbidden.
// Deprecated: [0.51.0] Use UpsertMBytes instead.
func (m Map) UpsertBytes(k string, v []byte) {
if av, existing := m.Get(k); existing {
av.SetBytesVal(v)
av.SetMBytesVal(v)
} else {
*m.orig = append(*m.orig, newAttributeKeyValueBytes(k, v))
}
}

// UpsertMBytes performs the Insert or Update action. The []byte Value is
// inserted to the map that did not originally have the key. The key/value is
// updated to the map where the key already existed.
// The caller must ensure the []byte passed in is not modified after the call is made, sharing the data
// across multiple attributes is forbidden.
func (m Map) UpsertMBytes(k string, v []byte) {
if av, existing := m.Get(k); existing {
av.SetMBytesVal(v)
} else {
*m.orig = append(*m.orig, newAttributeKeyValueBytes(k, v))
}
Expand Down
14 changes: 7 additions & 7 deletions pdata/internal/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func TestNilMap(t *testing.T) {
assert.EqualValues(t, generateTestBoolMap(), insertMapBool)

insertMapBytes := NewMap()
insertMapBytes.InsertBytes("k", []byte{1, 2, 3, 4, 5})
insertMapBytes.InsertMBytes("k", []byte{1, 2, 3, 4, 5})
assert.EqualValues(t, generateTestBytesMap(), insertMapBytes)

updateMap := NewMap()
Expand All @@ -322,7 +322,7 @@ func TestNilMap(t *testing.T) {
assert.EqualValues(t, NewMap(), updateMapBool)

updateMapBytes := NewMap()
updateMapBytes.UpdateBytes("k", []byte{1, 2, 3})
updateMapBytes.UpdateMBytes("k", []byte{1, 2, 3})
assert.EqualValues(t, NewMap(), updateMapBytes)

upsertMap := NewMap()
Expand All @@ -346,7 +346,7 @@ func TestNilMap(t *testing.T) {
assert.EqualValues(t, generateTestBoolMap(), upsertMapBool)

upsertMapBytes := NewMap()
upsertMapBytes.UpsertBytes("k", []byte{1, 2, 3, 4, 5})
upsertMapBytes.UpsertMBytes("k", []byte{1, 2, 3, 4, 5})
assert.EqualValues(t, generateTestBytesMap(), upsertMapBytes)

removeMap := NewMap()
Expand Down Expand Up @@ -412,7 +412,7 @@ func TestMapWithEmpty(t *testing.T) {
assert.EqualValues(t, ValueTypeBool, val.Type())
assert.True(t, val.BoolVal())

sm.InsertBytes("other_key_bytes", []byte{1, 2, 3})
sm.InsertMBytes("other_key_bytes", []byte{1, 2, 3})
val, exist = sm.Get("other_key_bytes")
assert.True(t, exist)
assert.EqualValues(t, ValueTypeBytes, val.Type())
Expand Down Expand Up @@ -448,7 +448,7 @@ func TestMapWithEmpty(t *testing.T) {
assert.EqualValues(t, ValueTypeBool, val.Type())
assert.False(t, val.BoolVal())

sm.UpdateBytes("other_key_bytes", []byte{4, 5, 6})
sm.UpdateMBytes("other_key_bytes", []byte{4, 5, 6})
val, exist = sm.Get("other_key_bytes")
assert.True(t, exist)
assert.EqualValues(t, ValueTypeBytes, val.Type())
Expand Down Expand Up @@ -484,7 +484,7 @@ func TestMapWithEmpty(t *testing.T) {
assert.EqualValues(t, ValueTypeBool, val.Type())
assert.True(t, val.BoolVal())

sm.UpsertBytes("other_key_bytes", []byte{7, 8, 9})
sm.UpsertMBytes("other_key_bytes", []byte{7, 8, 9})
val, exist = sm.Get("other_key_bytes")
assert.True(t, exist)
assert.EqualValues(t, ValueTypeBytes, val.Type())
Expand Down Expand Up @@ -520,7 +520,7 @@ func TestMapWithEmpty(t *testing.T) {
assert.EqualValues(t, ValueTypeBool, val.Type())
assert.False(t, val.BoolVal())

sm.UpsertBytes("yet_another_key_bytes", []byte{1})
sm.UpsertMBytes("yet_another_key_bytes", []byte{1})
val, exist = sm.Get("yet_another_key_bytes")
assert.True(t, exist)
assert.EqualValues(t, ValueTypeBytes, val.Type())
Expand Down
24 changes: 12 additions & 12 deletions pdata/internal/generated_pmetric.go

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

Loading

0 comments on commit 6cde802

Please sign in to comment.