Skip to content

Commit 95e5ac6

Browse files
authored
[chore] Refactor data.*ID to mathch the new jsoniter helpers (#13495)
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
1 parent b5f3b1d commit 95e5ac6

File tree

17 files changed

+161
-271
lines changed

17 files changed

+161
-271
lines changed

pdata/internal/data/bytesid.go

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,26 @@ package data // import "go.opentelemetry.io/collector/pdata/internal/data"
55

66
import (
77
"encoding/hex"
8-
"errors"
9-
"fmt"
10-
)
11-
12-
// marshalJSON converts trace id into a hex string enclosed in quotes.
13-
// Called by Protobuf JSON deserialization.
14-
func marshalJSON(id []byte) ([]byte, error) {
15-
// Plus 2 quote chars at the start and end.
16-
hexLen := hex.EncodedLen(len(id)) + 2
17-
18-
b := make([]byte, hexLen)
19-
hex.Encode(b[1:hexLen-1], id)
20-
b[0], b[hexLen-1] = '"', '"'
218

22-
return b, nil
23-
}
9+
jsoniter "github.com/json-iterator/go"
10+
)
2411

2512
// unmarshalJSON inflates trace id from hex string, possibly enclosed in quotes.
2613
// Called by Protobuf JSON deserialization.
27-
func unmarshalJSON(dst []byte, src []byte) error {
28-
if l := len(src); l >= 2 && src[0] == '"' && src[l-1] == '"' {
29-
src = src[1 : l-1]
30-
}
31-
nLen := len(src)
32-
if nLen == 0 {
33-
return nil
14+
func unmarshalJSON(dst []byte, iter *jsoniter.Iterator) {
15+
src := iter.ReadStringAsSlice()
16+
if len(src) == 0 {
17+
return
3418
}
3519

36-
if len(dst) != hex.DecodedLen(nLen) {
37-
return errors.New("invalid length for ID")
20+
if len(dst) != hex.DecodedLen(len(src)) {
21+
iter.ReportError("ID.UnmarshalJSONIter", "length mismatch")
22+
return
3823
}
3924

4025
_, err := hex.Decode(dst, src)
4126
if err != nil {
42-
return fmt.Errorf("cannot unmarshal ID from string '%s': %w", string(src), err)
27+
iter.ReportError("ID.UnmarshalJSONIter", err.Error())
28+
return
4329
}
44-
return nil
4530
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package data
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
12+
"go.opentelemetry.io/collector/pdata/internal/json"
13+
)
14+
15+
func TestUnmarshalJSON(t *testing.T) {
16+
iter := json.BorrowIterator(nil)
17+
defer json.ReturnIterator(iter)
18+
19+
id := [16]byte{}
20+
unmarshalJSON(id[:], iter.ResetBytes([]byte(`""`)))
21+
require.NoError(t, iter.Error)
22+
assert.Equal(t, [16]byte{}, id)
23+
24+
idBytes := [16]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78}
25+
unmarshalJSON(id[:], iter.ResetBytes([]byte(`"12345678123456781234567812345678"`)))
26+
require.NoError(t, iter.Error)
27+
assert.Equal(t, idBytes, id)
28+
29+
unmarshalJSON(id[:], iter.ResetBytes([]byte(`"nothex"`)))
30+
require.Error(t, iter.Error)
31+
32+
unmarshalJSON(id[:], iter.ResetBytes([]byte(`"1"`)))
33+
require.Error(t, iter.Error)
34+
35+
unmarshalJSON(id[:], iter.ResetBytes([]byte(`"123"`)))
36+
require.Error(t, iter.Error)
37+
38+
unmarshalJSON(id[:], iter.ResetBytes([]byte(`"`)))
39+
require.Error(t, iter.Error)
40+
}

pdata/internal/data/profileid.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"errors"
99

1010
"github.com/gogo/protobuf/proto"
11+
jsoniter "github.com/json-iterator/go"
1112

1213
"go.opentelemetry.io/collector/pdata/internal/json"
1314
)
@@ -23,7 +24,7 @@ var (
2324
// Protobuf messages.
2425
type ProfileID [profileIDSize]byte
2526

26-
var _ proto.Sizer = (*SpanID)(nil)
27+
var _ proto.Sizer = (*ProfileID)(nil)
2728

2829
// Size returns the size of the data to serialize.
2930
func (tid ProfileID) Size() int {
@@ -66,26 +67,13 @@ func (tid *ProfileID) Unmarshal(data []byte) error {
6667
return nil
6768
}
6869

69-
// MarshalJSON converts profile id into a hex string enclosed in quotes.
70-
func (tid ProfileID) MarshalJSON() ([]byte, error) {
71-
if tid.IsEmpty() {
72-
return []byte(`""`), nil
73-
}
74-
return marshalJSON(tid[:])
75-
}
76-
77-
// MarshalJSONStream converts trace id into a hex string enclosed in quotes.
70+
// MarshalJSONStream converts ProfileID into a hex string.
7871
func (tid ProfileID) MarshalJSONStream(dest *json.Stream) {
79-
if tid.IsEmpty() {
80-
dest.WriteString("")
81-
return
82-
}
8372
dest.WriteString(hex.EncodeToString(tid[:]))
8473
}
8574

86-
// UnmarshalJSON inflates profile id from hex string, possibly enclosed in quotes.
87-
// Called by Protobuf JSON deserialization.
88-
func (tid *ProfileID) UnmarshalJSON(data []byte) error {
75+
// UnmarshalJSONIter decodes ProfileID from hex string.
76+
func (tid *ProfileID) UnmarshalJSONIter(iter *jsoniter.Iterator) {
8977
*tid = [profileIDSize]byte{}
90-
return unmarshalJSON(tid[:], data)
78+
unmarshalJSON(tid[:], iter)
9179
}

pdata/internal/data/profileid_test.go

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88

99
"github.com/stretchr/testify/assert"
1010
"github.com/stretchr/testify/require"
11+
12+
"go.opentelemetry.io/collector/pdata/internal/json"
1113
)
1214

1315
func TestProfileID(t *testing.T) {
@@ -39,18 +41,6 @@ func TestProfileIDMarshal(t *testing.T) {
3941
assert.Error(t, err)
4042
}
4143

42-
func TestProfileIDMarshalJSON(t *testing.T) {
43-
tid := ProfileID([16]byte{})
44-
json, err := tid.MarshalJSON()
45-
require.NoError(t, err)
46-
assert.JSONEq(t, `""`, string(json))
47-
48-
tid = [16]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78}
49-
json, err = tid.MarshalJSON()
50-
require.NoError(t, err)
51-
assert.JSONEq(t, `"12345678123456781234567812345678"`, string(json))
52-
}
53-
5444
func TestProfileIDUnmarshal(t *testing.T) {
5545
buf := [16]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78}
5646

@@ -68,33 +58,18 @@ func TestProfileIDUnmarshal(t *testing.T) {
6858
assert.EqualValues(t, [16]byte{}, tid)
6959
}
7060

71-
func TestProfileIDUnmarshalJSON(t *testing.T) {
72-
tid := ProfileID([16]byte{})
73-
err := tid.UnmarshalJSON([]byte(`""`))
74-
require.NoError(t, err)
75-
assert.EqualValues(t, [16]byte{}, tid)
76-
77-
err = tid.UnmarshalJSON([]byte(`""""`))
78-
require.Error(t, err)
61+
func TestProfileIDMarshalAndUnmarshalJSON(t *testing.T) {
62+
stream := json.BorrowStream(nil)
63+
defer json.ReturnStream(stream)
64+
src := ProfileID([16]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78})
65+
src.MarshalJSONStream(stream)
66+
require.NoError(t, stream.Error)
7967

80-
tidBytes := [16]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78}
81-
err = tid.UnmarshalJSON([]byte(`"12345678123456781234567812345678"`))
82-
require.NoError(t, err)
83-
assert.EqualValues(t, tidBytes, tid)
84-
85-
err = tid.UnmarshalJSON([]byte(`12345678123456781234567812345678`))
86-
require.NoError(t, err)
87-
assert.EqualValues(t, tidBytes, tid)
68+
iter := json.BorrowIterator(stream.Buffer())
69+
defer json.ReturnIterator(iter)
70+
dest := ProfileID{}
71+
dest.UnmarshalJSONIter(iter)
72+
require.NoError(t, iter.Error)
8873

89-
err = tid.UnmarshalJSON([]byte(`"nothex"`))
90-
require.Error(t, err)
91-
92-
err = tid.UnmarshalJSON([]byte(`"1"`))
93-
require.Error(t, err)
94-
95-
err = tid.UnmarshalJSON([]byte(`"123"`))
96-
require.Error(t, err)
97-
98-
err = tid.UnmarshalJSON([]byte(`"`))
99-
assert.Error(t, err)
74+
assert.Equal(t, src, dest)
10075
}

pdata/internal/data/spanid.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"errors"
99

1010
"github.com/gogo/protobuf/proto"
11+
jsoniter "github.com/json-iterator/go"
1112

1213
"go.opentelemetry.io/collector/pdata/internal/json"
1314
)
@@ -66,26 +67,13 @@ func (sid *SpanID) Unmarshal(data []byte) error {
6667
return nil
6768
}
6869

69-
// MarshalJSON converts SpanID into a hex string enclosed in quotes.
70-
func (sid SpanID) MarshalJSON() ([]byte, error) {
71-
if sid.IsEmpty() {
72-
return []byte(`""`), nil
73-
}
74-
return marshalJSON(sid[:])
75-
}
76-
77-
// MarshalJSONStream converts trace id into a hex string enclosed in quotes.
70+
// MarshalJSONStream converts SpanID into a hex string.
7871
func (sid SpanID) MarshalJSONStream(dest *json.Stream) {
79-
if sid.IsEmpty() {
80-
dest.WriteString("")
81-
return
82-
}
8372
dest.WriteString(hex.EncodeToString(sid[:]))
8473
}
8574

86-
// UnmarshalJSON decodes SpanID from hex string, possibly enclosed in quotes.
87-
// Called by Protobuf JSON deserialization.
88-
func (sid *SpanID) UnmarshalJSON(data []byte) error {
75+
// UnmarshalJSONIter decodes SpanID from hex string.
76+
func (sid *SpanID) UnmarshalJSONIter(iter *jsoniter.Iterator) {
8977
*sid = [spanIDSize]byte{}
90-
return unmarshalJSON(sid[:], data)
78+
unmarshalJSON(sid[:], iter)
9179
}

pdata/internal/data/spanid_test.go

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88

99
"github.com/stretchr/testify/assert"
1010
"github.com/stretchr/testify/require"
11+
12+
"go.opentelemetry.io/collector/pdata/internal/json"
1113
)
1214

1315
func TestSpanID(t *testing.T) {
@@ -39,18 +41,6 @@ func TestSpanIDMarshal(t *testing.T) {
3941
assert.Error(t, err)
4042
}
4143

42-
func TestSpanIDMarshalJSON(t *testing.T) {
43-
sid := SpanID([8]byte{})
44-
json, err := sid.MarshalJSON()
45-
require.NoError(t, err)
46-
assert.JSONEq(t, `""`, string(json))
47-
48-
sid = [8]byte{0x12, 0x23, 0xAD, 0x12, 0x23, 0xAD, 0x12, 0x23}
49-
json, err = sid.MarshalJSON()
50-
require.NoError(t, err)
51-
assert.JSONEq(t, `"1223ad1223ad1223"`, string(json))
52-
}
53-
5444
func TestSpanIDUnmarshal(t *testing.T) {
5545
buf := []byte{0x12, 0x23, 0xAD, 0x12, 0x23, 0xAD, 0x12, 0x23}
5646

@@ -71,29 +61,18 @@ func TestSpanIDUnmarshal(t *testing.T) {
7161
assert.Error(t, err)
7262
}
7363

74-
func TestSpanIDUnmarshalJSON(t *testing.T) {
75-
sid := SpanID{}
76-
err := sid.UnmarshalJSON([]byte(`""`))
77-
require.NoError(t, err)
78-
assert.EqualValues(t, [8]byte{}, sid)
79-
80-
err = sid.UnmarshalJSON([]byte(`"1234567812345678"`))
81-
require.NoError(t, err)
82-
assert.EqualValues(t, [8]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78}, sid)
83-
84-
err = sid.UnmarshalJSON([]byte(`1234567812345678`))
85-
require.NoError(t, err)
86-
assert.EqualValues(t, [8]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78}, sid)
87-
88-
err = sid.UnmarshalJSON([]byte(`"nothex"`))
89-
require.Error(t, err)
64+
func TestSpanIDMarshalAndUnmarshalJSON(t *testing.T) {
65+
stream := json.BorrowStream(nil)
66+
defer json.ReturnStream(stream)
67+
src := SpanID([8]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78})
68+
src.MarshalJSONStream(stream)
69+
require.NoError(t, stream.Error)
9070

91-
err = sid.UnmarshalJSON([]byte(`"1"`))
92-
require.Error(t, err)
71+
iter := json.BorrowIterator(stream.Buffer())
72+
defer json.ReturnIterator(iter)
73+
dest := SpanID{}
74+
dest.UnmarshalJSONIter(iter)
75+
require.NoError(t, iter.Error)
9376

94-
err = sid.UnmarshalJSON([]byte(`"123"`))
95-
require.Error(t, err)
96-
97-
err = sid.UnmarshalJSON([]byte(`"`))
98-
assert.Error(t, err)
77+
assert.Equal(t, src, dest)
9978
}

pdata/internal/data/traceid.go

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"errors"
99

1010
"github.com/gogo/protobuf/proto"
11+
jsoniter "github.com/json-iterator/go"
1112

1213
"go.opentelemetry.io/collector/pdata/internal/json"
1314
)
@@ -23,7 +24,7 @@ var (
2324
// Protobuf messages.
2425
type TraceID [traceIDSize]byte
2526

26-
var _ proto.Sizer = (*SpanID)(nil)
27+
var _ proto.Sizer = (*TraceID)(nil)
2728

2829
// Size returns the size of the data to serialize.
2930
func (tid TraceID) Size() int {
@@ -66,26 +67,13 @@ func (tid *TraceID) Unmarshal(data []byte) error {
6667
return nil
6768
}
6869

69-
// MarshalJSON converts trace id into a hex string enclosed in quotes.
70-
func (tid TraceID) MarshalJSON() ([]byte, error) {
71-
if tid.IsEmpty() {
72-
return []byte(`""`), nil
73-
}
74-
return marshalJSON(tid[:])
75-
}
76-
77-
// MarshalJSONStream converts trace id into a hex string enclosed in quotes.
70+
// MarshalJSONStream converts TraceID into a hex string.
7871
func (tid TraceID) MarshalJSONStream(dest *json.Stream) {
79-
if tid.IsEmpty() {
80-
dest.WriteString("")
81-
return
82-
}
8372
dest.WriteString(hex.EncodeToString(tid[:]))
8473
}
8574

86-
// UnmarshalJSON inflates trace id from hex string, possibly enclosed in quotes.
87-
// Called by Protobuf JSON deserialization.
88-
func (tid *TraceID) UnmarshalJSON(data []byte) error {
89-
*tid = [traceIDSize]byte{}
90-
return unmarshalJSON(tid[:], data)
75+
// UnmarshalJSONIter decodes TraceID from hex string.
76+
func (tid *TraceID) UnmarshalJSONIter(iter *jsoniter.Iterator) {
77+
*tid = [profileIDSize]byte{}
78+
unmarshalJSON(tid[:], iter)
9179
}

0 commit comments

Comments
 (0)