From 338de8893056031dfdaeb17de2f76e4128e2e497 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Tue, 7 Oct 2025 17:00:35 -0400 Subject: [PATCH 1/6] Change API To Take pcommon.Value Signed-off-by: Mahad Zaryab --- pdata/xpdata/json.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pdata/xpdata/json.go b/pdata/xpdata/json.go index 247e81186b0..de2c52b6593 100644 --- a/pdata/xpdata/json.go +++ b/pdata/xpdata/json.go @@ -9,14 +9,16 @@ import ( "go.opentelemetry.io/collector/pdata/internal" otlpcommon "go.opentelemetry.io/collector/pdata/internal/data/protogen/common/v1" "go.opentelemetry.io/collector/pdata/internal/json" + "go.opentelemetry.io/collector/pdata/pcommon" ) type JSONMarshaler struct{} -func (*JSONMarshaler) MarshalAnyValue(value *otlpcommon.AnyValue) ([]byte, error) { +func (*JSONMarshaler) MarshalAnyValue(value pcommon.Value) ([]byte, error) { + av := internal.GetOrigValue(internal.Value(value)) dest := json.BorrowStream(nil) defer json.ReturnStream(dest) - internal.MarshalJSONOrigAnyValue(value, dest) + internal.MarshalJSONOrigAnyValue(av, dest) if dest.Error() != nil { return nil, dest.Error() } @@ -25,13 +27,13 @@ func (*JSONMarshaler) MarshalAnyValue(value *otlpcommon.AnyValue) ([]byte, error type JSONUnmarshaler struct{} -func (*JSONUnmarshaler) UnmarshalAnyValue(buf []byte) (*otlpcommon.AnyValue, error) { +func (*JSONUnmarshaler) UnmarshalAnyValue(buf []byte) (pcommon.Value, error) { iter := json.BorrowIterator(buf) defer json.ReturnIterator(iter) value := &otlpcommon.AnyValue{} internal.UnmarshalJSONOrigAnyValue(value, iter) if iter.Error() != nil { - return nil, iter.Error() + return pcommon.NewValueEmpty(), iter.Error() } - return value, nil + return pcommon.Value(internal.NewValue(value, internal.NewState())), nil } From c1493a8c42673d1125988749286e4b15dee8b8d3 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Tue, 7 Oct 2025 17:01:09 -0400 Subject: [PATCH 2/6] Update Tests Signed-off-by: Mahad Zaryab --- pdata/xpdata/json_test.go | 71 ++++++++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 20 deletions(-) diff --git a/pdata/xpdata/json_test.go b/pdata/xpdata/json_test.go index ce6ce7a7735..d3df8ad0cec 100644 --- a/pdata/xpdata/json_test.go +++ b/pdata/xpdata/json_test.go @@ -9,8 +9,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.opentelemetry.io/collector/pdata/internal" - otlpcommon "go.opentelemetry.io/collector/pdata/internal/data/protogen/common/v1" + "go.opentelemetry.io/collector/pdata/pcommon" ) func TestMarshalAndUnmarshalAnyValue(t *testing.T) { @@ -34,25 +33,57 @@ func TestUnmarshalAnyValueUnknown(t *testing.T) { b, err := m.UnmarshalAnyValue([]byte(`{"unknown": "string"}`)) require.NoError(t, err) - assert.Equal(t, internal.NewOrigAnyValue(), b) + assert.Equal(t, pcommon.NewValueEmpty(), b) } -func genTestEncodingValuesAnyValue() map[string]*otlpcommon.AnyValue { - return map[string]*otlpcommon.AnyValue{ - "empty": internal.NewOrigAnyValue(), - "StringValue/default": {Value: &otlpcommon.AnyValue_StringValue{StringValue: ""}}, - "StringValue/test": {Value: &otlpcommon.AnyValue_StringValue{StringValue: "test_stringvalue"}}, - "BoolValue/default": {Value: &otlpcommon.AnyValue_BoolValue{BoolValue: false}}, - "BoolValue/test": {Value: &otlpcommon.AnyValue_BoolValue{BoolValue: true}}, - "IntValue/default": {Value: &otlpcommon.AnyValue_IntValue{IntValue: int64(0)}}, - "IntValue/test": {Value: &otlpcommon.AnyValue_IntValue{IntValue: int64(13)}}, - "DoubleValue/default": {Value: &otlpcommon.AnyValue_DoubleValue{DoubleValue: float64(0)}}, - "DoubleValue/test": {Value: &otlpcommon.AnyValue_DoubleValue{DoubleValue: float64(3.1415926)}}, - "ArrayValue/default": {Value: &otlpcommon.AnyValue_ArrayValue{ArrayValue: &otlpcommon.ArrayValue{}}}, - "ArrayValue/test": {Value: &otlpcommon.AnyValue_ArrayValue{ArrayValue: internal.GenTestOrigArrayValue()}}, - "KvlistValue/default": {Value: &otlpcommon.AnyValue_KvlistValue{KvlistValue: &otlpcommon.KeyValueList{}}}, - "KvlistValue/test": {Value: &otlpcommon.AnyValue_KvlistValue{KvlistValue: internal.GenTestOrigKeyValueList()}}, - "BytesValue/default": {Value: &otlpcommon.AnyValue_BytesValue{BytesValue: nil}}, - "BytesValue/test": {Value: &otlpcommon.AnyValue_BytesValue{BytesValue: []byte{1, 2, 3}}}, +func genTestEncodingValuesAnyValue() map[string]pcommon.Value { + return map[string]pcommon.Value{ + "empty": pcommon.NewValueEmpty(), + "StringValue/default": pcommon.NewValueStr(""), + "StringValue/test": pcommon.NewValueStr("test_stringvalue"), + "BoolValue/default": pcommon.NewValueBool(false), + "BoolValue/test": pcommon.NewValueBool(true), + "IntValue/default": pcommon.NewValueInt(0), + "IntValue/test": pcommon.NewValueInt(13), + "DoubleValue/default": pcommon.NewValueDouble(0), + "DoubleValue/test": pcommon.NewValueDouble(3.1415926), + "ArrayValue/default": pcommon.NewValueSlice(), + "ArrayValue/test": func() pcommon.Value { + s := pcommon.NewValueSlice() + s.Slice().AppendEmpty().SetStr("test1") + s.Slice().AppendEmpty().SetInt(13) + s.Slice().AppendEmpty().SetBool(true) + s.Slice().AppendEmpty().SetDouble(3.1415926) + + s1 := s.Slice().AppendEmpty().SetEmptySlice() + s1.AppendEmpty().SetStr("nested") + + m := s.Slice().AppendEmpty().SetEmptyMap() + m.PutStr("key1", "value1") + + return s + }(), + "KvlistValue/default": pcommon.NewValueMap(), + "KvlistValue/test": func() pcommon.Value { + m := pcommon.NewValueMap() + m.Map().PutStr("key1", "value1") + m.Map().PutInt("key2", 13) + m.Map().PutBool("key3", true) + m.Map().PutDouble("key4", 3.1415926) + m.Map().PutEmpty("key5").SetStr("value5") + + s := m.Map().PutEmptySlice("key6") + s.AppendEmpty().SetStr("nested1") + + m1 := m.Map().PutEmptyMap("key6") + m1.PutStr("nestedKey1", "nestedValue1") + + return m + }(), + "BytesValue/test": func() pcommon.Value { + v := pcommon.NewValueBytes() + v.Bytes().FromRaw([]byte{1, 2, 3}) + return v + }(), } } From fb146b6941d95e47f176042dd60cc7a20759c5c3 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Tue, 7 Oct 2025 17:05:42 -0400 Subject: [PATCH 3/6] Fix Naming Signed-off-by: Mahad Zaryab --- pdata/xpdata/fuzz_test.go | 10 +++++----- pdata/xpdata/json.go | 4 ++-- pdata/xpdata/json_test.go | 14 +++++++------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/pdata/xpdata/fuzz_test.go b/pdata/xpdata/fuzz_test.go index aa1131dad06..24bae44cbb0 100644 --- a/pdata/xpdata/fuzz_test.go +++ b/pdata/xpdata/fuzz_test.go @@ -12,22 +12,22 @@ import ( var unexpectedBytes = "expected the same bytes from unmarshaling and marshaling." -func FuzzUnmarshalJSONAnyValue(f *testing.F) { +func FuzzUnmarshalJSONValue(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { u1 := &JSONUnmarshaler{} - ld1, err := u1.UnmarshalAnyValue(data) + ld1, err := u1.UnmarshalValue(data) if err != nil { return } m1 := &JSONMarshaler{} - b1, err := m1.MarshalAnyValue(ld1) + b1, err := m1.MarshalValue(ld1) require.NoError(t, err, "failed to marshal valid struct") u2 := &JSONUnmarshaler{} - ld2, err := u2.UnmarshalAnyValue(b1) + ld2, err := u2.UnmarshalValue(b1) require.NoError(t, err, "failed to unmarshal valid bytes") m2 := &JSONMarshaler{} - b2, err := m2.MarshalAnyValue(ld2) + b2, err := m2.MarshalValue(ld2) require.NoError(t, err, "failed to marshal valid struct") require.True(t, bytes.Equal(b1, b2), "%s. \nexpected %d but got %d\n", unexpectedBytes, b1, b2) diff --git a/pdata/xpdata/json.go b/pdata/xpdata/json.go index de2c52b6593..e40eabe8a77 100644 --- a/pdata/xpdata/json.go +++ b/pdata/xpdata/json.go @@ -14,7 +14,7 @@ import ( type JSONMarshaler struct{} -func (*JSONMarshaler) MarshalAnyValue(value pcommon.Value) ([]byte, error) { +func (*JSONMarshaler) MarshalValue(value pcommon.Value) ([]byte, error) { av := internal.GetOrigValue(internal.Value(value)) dest := json.BorrowStream(nil) defer json.ReturnStream(dest) @@ -27,7 +27,7 @@ func (*JSONMarshaler) MarshalAnyValue(value pcommon.Value) ([]byte, error) { type JSONUnmarshaler struct{} -func (*JSONUnmarshaler) UnmarshalAnyValue(buf []byte) (pcommon.Value, error) { +func (*JSONUnmarshaler) UnmarshalValue(buf []byte) (pcommon.Value, error) { iter := json.BorrowIterator(buf) defer json.ReturnIterator(iter) value := &otlpcommon.AnyValue{} diff --git a/pdata/xpdata/json_test.go b/pdata/xpdata/json_test.go index d3df8ad0cec..be5b8232c5f 100644 --- a/pdata/xpdata/json_test.go +++ b/pdata/xpdata/json_test.go @@ -12,15 +12,15 @@ import ( "go.opentelemetry.io/collector/pdata/pcommon" ) -func TestMarshalAndUnmarshalAnyValue(t *testing.T) { - for name, src := range genTestEncodingValuesAnyValue() { +func TestMarshalAndUnmarshalValue(t *testing.T) { + for name, src := range genTestEncodingValues() { t.Run(name, func(t *testing.T) { m := &JSONMarshaler{} - b, err := m.MarshalAnyValue(src) + b, err := m.MarshalValue(src) require.NoError(t, err) u := &JSONUnmarshaler{} - dest, err := u.UnmarshalAnyValue(b) + dest, err := u.UnmarshalValue(b) require.NoError(t, err) require.Equal(t, src, dest) @@ -28,15 +28,15 @@ func TestMarshalAndUnmarshalAnyValue(t *testing.T) { } } -func TestUnmarshalAnyValueUnknown(t *testing.T) { +func TestUnmarshalValueUnknown(t *testing.T) { m := &JSONUnmarshaler{} - b, err := m.UnmarshalAnyValue([]byte(`{"unknown": "string"}`)) + b, err := m.UnmarshalValue([]byte(`{"unknown": "string"}`)) require.NoError(t, err) assert.Equal(t, pcommon.NewValueEmpty(), b) } -func genTestEncodingValuesAnyValue() map[string]pcommon.Value { +func genTestEncodingValues() map[string]pcommon.Value { return map[string]pcommon.Value{ "empty": pcommon.NewValueEmpty(), "StringValue/default": pcommon.NewValueStr(""), From 441509178d25f5454f8569ab4214124cf29bbdda Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Thu, 9 Oct 2025 17:58:16 -0400 Subject: [PATCH 4/6] Add Changelog Entry Signed-off-by: Mahad Zaryab --- .chloggen/fix-anyvalue-json.yaml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .chloggen/fix-anyvalue-json.yaml diff --git a/.chloggen/fix-anyvalue-json.yaml b/.chloggen/fix-anyvalue-json.yaml new file mode 100644 index 00000000000..bd0bb438e8e --- /dev/null +++ b/.chloggen/fix-anyvalue-json.yaml @@ -0,0 +1,25 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: 'enhancement' + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: 'xpdata' + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: "Refactor JSON marshaling and unmarshaling to use `Value` instead of `AnyValue`." + +# One or more tracking issues or pull requests related to the change +issues: [13837] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] From c47991f748c22d8a169a0ea9abb4bdf5e4634fdc Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Thu, 9 Oct 2025 17:59:55 -0400 Subject: [PATCH 5/6] Fix Signed-off-by: Mahad Zaryab --- .chloggen/fix-anyvalue-json.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.chloggen/fix-anyvalue-json.yaml b/.chloggen/fix-anyvalue-json.yaml index bd0bb438e8e..7a62e7bf9cc 100644 --- a/.chloggen/fix-anyvalue-json.yaml +++ b/.chloggen/fix-anyvalue-json.yaml @@ -7,7 +7,7 @@ change_type: 'enhancement' component: 'xpdata' # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). -note: "Refactor JSON marshaling and unmarshaling to use `Value` instead of `AnyValue`." +note: "Refactor JSON marshaling and unmarshaling to use `pcommon.Value` instead of `AnyValue`." # One or more tracking issues or pull requests related to the change issues: [13837] From 81fa0699058c1fc520d79bf6fd72f333ed813370 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Sat, 11 Oct 2025 10:04:51 -0400 Subject: [PATCH 6/6] Fix Signed-off-by: Mahad Zaryab --- .chloggen/fix-anyvalue-json.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.chloggen/fix-anyvalue-json.yaml b/.chloggen/fix-anyvalue-json.yaml index 7a62e7bf9cc..37f1af99e78 100644 --- a/.chloggen/fix-anyvalue-json.yaml +++ b/.chloggen/fix-anyvalue-json.yaml @@ -4,7 +4,7 @@ change_type: 'enhancement' # The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) -component: 'xpdata' +component: 'pdata/xpdata' # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). note: "Refactor JSON marshaling and unmarshaling to use `pcommon.Value` instead of `AnyValue`."