Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .chloggen/fix-anyvalue-json.yaml
Original file line number Diff line number Diff line change
@@ -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: '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`."

# 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: []
10 changes: 5 additions & 5 deletions pdata/xpdata/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 7 additions & 5 deletions pdata/xpdata/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) MarshalValue(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()
}
Expand All @@ -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) UnmarshalValue(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
}
83 changes: 57 additions & 26 deletions pdata/xpdata/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,81 @@ 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) {
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)
})
}
}

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, 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 genTestEncodingValues() 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
}(),
}
}
Loading