From f47a9e7f581fa1d192d5706ab8d1c22a51028d83 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 26 Apr 2022 18:13:16 -0700 Subject: [PATCH] [pdata] Create real copy of Value with ValueTypeBytes type --- CHANGELOG.md | 1 + pdata/internal/common.go | 8 ++++++++ pdata/internal/common_test.go | 12 ++++++++++++ 3 files changed, 21 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 20b61830f92..4603708aa84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ ### 🧰 Bug fixes 🧰 - Fix translation from otlp.Request to pdata representation, changes to the returned pdata not all reflected to the otlp.Request (#5197) - `exporterhelper` now properly consumes any remaining items on stop (#5203) +- `pdata`: Fix copying of `Value` with `ValueTypeBytes` type (#5267) ## v0.49.0 Beta diff --git a/pdata/internal/common.go b/pdata/internal/common.go index 5c0964df2a2..fb93553ab17 100644 --- a/pdata/internal/common.go +++ b/pdata/internal/common.go @@ -330,6 +330,14 @@ func (v Value) copyTo(dest *otlpcommon.AnyValue) { } // Deep copy to dest. newSlice(&ov.ArrayValue.Values).CopyTo(newSlice(&av.ArrayValue.Values)) + case *otlpcommon.AnyValue_BytesValue: + bv, ok := dest.Value.(*otlpcommon.AnyValue_BytesValue) + if !ok { + bv = &otlpcommon.AnyValue_BytesValue{} + dest.Value = bv + } + bv.BytesValue = make([]byte, len(ov.BytesValue)) + copy(bv.BytesValue, ov.BytesValue) default: // Primitive immutable type, no need for deep copy. dest.Value = v.orig.Value diff --git a/pdata/internal/common_test.go b/pdata/internal/common_test.go index e4334f1a8dd..45713c620f2 100644 --- a/pdata/internal/common_test.go +++ b/pdata/internal/common_test.go @@ -663,6 +663,18 @@ func TestAttributeValue_copyTo(t *testing.T) { assert.EqualValues(t, nil, destVal.Value) } +func TestValueBytes_CopyTo(t *testing.T) { + orig := NewValueBytes([]byte{1, 2, 3}) + dest := NewValueEmpty() + orig.CopyTo(dest) + assert.Equal(t, orig, dest) + + orig.BytesVal()[0] = 10 + assert.NotEqual(t, orig, dest) + assert.Equal(t, []byte{1, 2, 3}, dest.BytesVal()) + assert.Equal(t, []byte{10, 2, 3}, orig.BytesVal()) +} + func TestMap_Update(t *testing.T) { origWithNil := []otlpcommon.KeyValue{ {