Skip to content

Commit

Permalink
Simplify implementation for trace ids, no public API changes (#5968)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan <[email protected]>

Signed-off-by: Bogdan <[email protected]>
  • Loading branch information
bogdandrutu authored Aug 29, 2022
1 parent 27f852a commit a3c63bb
Show file tree
Hide file tree
Showing 18 changed files with 163 additions and 259 deletions.
8 changes: 4 additions & 4 deletions pdata/internal/cmd/pdatagen/internal/base_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,18 @@ const accessorsPrimitiveTypedTestTemplate = `func Test${structName}_${fieldName}

const accessorsPrimitiveStructTemplate = `// ${fieldName} returns the ${lowerFieldName} associated with this ${structName}.
func (ms ${structName}) ${fieldName}() ${packageName}${returnType} {
return ${packageName}${returnType}(internal.New${returnType}(ms.getOrig().${originFieldName}))
return ${packageName}New${returnType}(ms.getOrig().${originFieldName})
}
// Set${fieldName} replaces the ${lowerFieldName} associated with this ${structName}.
func (ms ${structName}) Set${fieldName}(v ${packageName}${returnType}) {
ms.getOrig().${originFieldName} = internal.GetOrig${returnType}(internal.${returnType}(v))
ms.getOrig().${originFieldName} = v.Bytes()
}`

const accessorsPrimitiveStructTestTemplate = `func Test${structName}_${fieldName}(t *testing.T) {
ms := New${structName}()
assert.Equal(t, ${packageName}${returnType}(internal.New${returnType}(${defaultVal})), ms.${fieldName}())
testVal${fieldName} := ${packageName}${returnType}(internal.New${returnType}(${testValue}))
assert.Equal(t, ${packageName}New${returnType}(${defaultVal}), ms.${fieldName}())
testVal${fieldName} := ${packageName}New${returnType}(${testValue})
ms.Set${fieldName}(testVal${fieldName})
assert.Equal(t, testVal${fieldName}, ms.${fieldName}())
}`
Expand Down
4 changes: 0 additions & 4 deletions pdata/internal/data/bytesid.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ import (
// marshalJSON converts trace id into a hex string enclosed in quotes.
// Called by Protobuf JSON deserialization.
func marshalJSON(id []byte) ([]byte, error) {
if len(id) == 0 {
return []byte(`""`), nil
}

// 2 chars per byte plus 2 quote chars at the start and end.
hexLen := 2*len(id) + 2

Expand Down
49 changes: 16 additions & 33 deletions pdata/internal/data/spanid.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
package data // import "go.opentelemetry.io/collector/pdata/internal/data"

import (
"encoding/hex"
"errors"

"github.com/gogo/protobuf/proto"
)

const spanIDSize = 8
Expand All @@ -25,66 +26,48 @@ var errInvalidSpanIDSize = errors.New("invalid length for SpanID")

// SpanID is a custom data type that is used for all span_id fields in OTLP
// Protobuf messages.
type SpanID struct {
id [spanIDSize]byte
}
type SpanID [spanIDSize]byte

// NewSpanID creates a SpanID from a byte slice.
func NewSpanID(bytes [8]byte) SpanID {
return SpanID{id: bytes}
}
var _ proto.Sizer = (*SpanID)(nil)

// HexString returns hex representation of the ID.
func (sid SpanID) HexString() string {
if sid.IsEmpty() {
return ""
}
return hex.EncodeToString(sid.id[:])
// NewSpanID creates a SpanID from a byte slice.
func NewSpanID(bytes [spanIDSize]byte) SpanID {
return bytes
}

// Size returns the size of the data to serialize.
func (sid *SpanID) Size() int {
func (sid SpanID) Size() int {
if sid.IsEmpty() {
return 0
}
return spanIDSize
}

// Equal returns true if ids are equal.
func (sid SpanID) Equal(that SpanID) bool {
return sid.id == that.id
}

// IsEmpty returns true if id contains at least one non-zero byte.
func (sid SpanID) IsEmpty() bool {
return sid.id == [8]byte{}
}

// Bytes returns the byte array representation of the SpanID.
func (sid SpanID) Bytes() [8]byte {
return sid.id
return sid == [spanIDSize]byte{}
}

// MarshalTo converts trace ID into a binary representation. Called by Protobuf serialization.
func (sid *SpanID) MarshalTo(data []byte) (n int, err error) {
func (sid SpanID) MarshalTo(data []byte) (n int, err error) {
if sid.IsEmpty() {
return 0, nil
}
return marshalBytes(data, sid.id[:])
return marshalBytes(data, sid[:])
}

// Unmarshal inflates this trace ID from binary representation. Called by Protobuf serialization.
func (sid *SpanID) Unmarshal(data []byte) error {
if len(data) == 0 {
sid.id = [8]byte{}
*sid = [spanIDSize]byte{}
return nil
}

if len(data) != spanIDSize {
return errInvalidSpanIDSize
}

copy(sid.id[:], data)
copy(sid[:], data)
return nil
}

Expand All @@ -93,12 +76,12 @@ func (sid SpanID) MarshalJSON() ([]byte, error) {
if sid.IsEmpty() {
return []byte(`""`), nil
}
return marshalJSON(sid.id[:])
return marshalJSON(sid[:])
}

// UnmarshalJSON decodes SpanID from hex string, possibly enclosed in quotes.
// Called by Protobuf JSON deserialization.
func (sid *SpanID) UnmarshalJSON(data []byte) error {
sid.id = [8]byte{}
return unmarshalJSON(sid.id[:], data)
*sid = [spanIDSize]byte{}
return unmarshalJSON(sid[:], data)
}
34 changes: 8 additions & 26 deletions pdata/internal/data/spanid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,15 @@ import (

func TestNewSpanID(t *testing.T) {
sid := NewSpanID([8]byte{})
assert.EqualValues(t, [8]byte{}, sid.id)
assert.EqualValues(t, [8]byte{}, sid)
assert.EqualValues(t, 0, sid.Size())

b := [8]byte{1, 2, 3, 4, 5, 6, 7, 8}
sid = NewSpanID(b)
assert.EqualValues(t, b, sid.id)
assert.EqualValues(t, b, sid)
assert.EqualValues(t, 8, sid.Size())
}

func TestSpanIDHexString(t *testing.T) {
sid := NewSpanID([8]byte{})
assert.EqualValues(t, "", sid.HexString())

sid = NewSpanID([8]byte{0x12, 0x23, 0xAD, 0x12, 0x23, 0xAD, 0x12, 0x23})
assert.EqualValues(t, "1223ad1223ad1223", sid.HexString())
}

func TestSpanIDEqual(t *testing.T) {
sid := NewSpanID([8]byte{})
assert.True(t, sid.Equal(NewSpanID([8]byte{})))
assert.False(t, sid.Equal(NewSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})))

sid = NewSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})
assert.False(t, sid.Equal(NewSpanID([8]byte{})))
assert.True(t, sid.Equal(NewSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})))
}

func TestSpanIDMarshal(t *testing.T) {
buf := make([]byte, 10)

Expand Down Expand Up @@ -85,15 +67,15 @@ func TestSpanIDUnmarshal(t *testing.T) {
sid := SpanID{}
err := sid.Unmarshal(buf[0:8])
assert.NoError(t, err)
assert.EqualValues(t, [8]byte{0x12, 0x23, 0xAD, 0x12, 0x23, 0xAD, 0x12, 0x23}, sid.id)
assert.EqualValues(t, [8]byte{0x12, 0x23, 0xAD, 0x12, 0x23, 0xAD, 0x12, 0x23}, sid)

err = sid.Unmarshal(buf[0:0])
assert.NoError(t, err)
assert.EqualValues(t, [8]byte{}, sid.id)
assert.EqualValues(t, [8]byte{}, sid)

err = sid.Unmarshal(nil)
assert.NoError(t, err)
assert.EqualValues(t, [8]byte{}, sid.id)
assert.EqualValues(t, [8]byte{}, sid)

err = sid.Unmarshal(buf[0:3])
assert.Error(t, err)
Expand All @@ -103,15 +85,15 @@ func TestSpanIDUnmarshalJSON(t *testing.T) {
sid := SpanID{}
err := sid.UnmarshalJSON([]byte(`""`))
assert.NoError(t, err)
assert.EqualValues(t, [8]byte{}, sid.id)
assert.EqualValues(t, [8]byte{}, sid)

err = sid.UnmarshalJSON([]byte(`"1234567812345678"`))
assert.NoError(t, err)
assert.EqualValues(t, [8]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78}, sid.id)
assert.EqualValues(t, [8]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78}, sid)

err = sid.UnmarshalJSON([]byte(`1234567812345678`))
assert.NoError(t, err)
assert.EqualValues(t, [8]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78}, sid.id)
assert.EqualValues(t, [8]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78}, sid)

err = sid.UnmarshalJSON([]byte(`"nothex"`))
assert.Error(t, err)
Expand Down
49 changes: 15 additions & 34 deletions pdata/internal/data/traceid.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
package data // import "go.opentelemetry.io/collector/pdata/internal/data"

import (
"encoding/hex"
"errors"

"github.com/gogo/protobuf/proto"
)

const traceIDSize = 16
Expand All @@ -25,68 +26,48 @@ var errInvalidTraceIDSize = errors.New("invalid length for TraceID")

// TraceID is a custom data type that is used for all trace_id fields in OTLP
// Protobuf messages.
type TraceID struct {
id [traceIDSize]byte
}
type TraceID [traceIDSize]byte

var _ proto.Sizer = (*SpanID)(nil)

// NewTraceID creates a TraceID from a byte slice.
func NewTraceID(bytes [16]byte) TraceID {
return TraceID{
id: bytes,
}
}

// HexString returns hex representation of the ID.
func (tid TraceID) HexString() string {
if tid.IsEmpty() {
return ""
}
return hex.EncodeToString(tid.id[:])
return bytes
}

// Size returns the size of the data to serialize.
func (tid *TraceID) Size() int {
func (tid TraceID) Size() int {
if tid.IsEmpty() {
return 0
}
return traceIDSize
}

// Equal returns true if ids are equal.
func (tid TraceID) Equal(that TraceID) bool {
return tid.id == that.id
}

// IsEmpty returns true if id contains at leas one non-zero byte.
func (tid TraceID) IsEmpty() bool {
return tid.id == [16]byte{}
}

// Bytes returns the byte array representation of the TraceID.
func (tid TraceID) Bytes() [16]byte {
return tid.id
return tid == [traceIDSize]byte{}
}

// MarshalTo converts trace ID into a binary representation. Called by Protobuf serialization.
func (tid *TraceID) MarshalTo(data []byte) (n int, err error) {
func (tid TraceID) MarshalTo(data []byte) (n int, err error) {
if tid.IsEmpty() {
return 0, nil
}
return marshalBytes(data, tid.id[:])
return marshalBytes(data, tid[:])
}

// Unmarshal inflates this trace ID from binary representation. Called by Protobuf serialization.
func (tid *TraceID) Unmarshal(data []byte) error {
if len(data) == 0 {
tid.id = [16]byte{}
*tid = [traceIDSize]byte{}
return nil
}

if len(data) != traceIDSize {
return errInvalidTraceIDSize
}

copy(tid.id[:], data)
copy(tid[:], data)
return nil
}

Expand All @@ -95,12 +76,12 @@ func (tid TraceID) MarshalJSON() ([]byte, error) {
if tid.IsEmpty() {
return []byte(`""`), nil
}
return marshalJSON(tid.id[:])
return marshalJSON(tid[:])
}

// UnmarshalJSON inflates trace id from hex string, possibly enclosed in quotes.
// Called by Protobuf JSON deserialization.
func (tid *TraceID) UnmarshalJSON(data []byte) error {
tid.id = [16]byte{}
return unmarshalJSON(tid.id[:], data)
*tid = [traceIDSize]byte{}
return unmarshalJSON(tid[:], data)
}
34 changes: 8 additions & 26 deletions pdata/internal/data/traceid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,15 @@ import (

func TestNewTraceID(t *testing.T) {
tid := NewTraceID([16]byte{})
assert.EqualValues(t, [16]byte{}, tid.id)
assert.EqualValues(t, [16]byte{}, tid)
assert.EqualValues(t, 0, tid.Size())

b := [16]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78}
tid = NewTraceID(b)
assert.EqualValues(t, b, tid.id)
assert.EqualValues(t, b, tid)
assert.EqualValues(t, 16, tid.Size())
}

func TestTraceIDHexString(t *testing.T) {
tid := NewTraceID([16]byte{})
assert.EqualValues(t, "", tid.HexString())

tid = NewTraceID([16]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78})
assert.EqualValues(t, "12345678123456781234567812345678", tid.HexString())
}

func TestTraceIDEqual(t *testing.T) {
tid := NewTraceID([16]byte{})
assert.True(t, tid.Equal(NewTraceID([16]byte{})))
assert.False(t, tid.Equal(NewTraceID([16]byte{1})))

tid = NewTraceID([16]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78})
assert.True(t, tid.Equal(NewTraceID([16]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78})))
assert.False(t, tid.Equal(NewTraceID([16]byte{})))
}

func TestTraceIDMarshal(t *testing.T) {
buf := make([]byte, 20)

Expand Down Expand Up @@ -85,34 +67,34 @@ func TestTraceIDUnmarshal(t *testing.T) {
tid := TraceID{}
err := tid.Unmarshal(buf[0:16])
assert.NoError(t, err)
assert.EqualValues(t, buf, tid.id)
assert.EqualValues(t, buf, tid)

err = tid.Unmarshal(buf[0:0])
assert.NoError(t, err)
assert.EqualValues(t, [16]byte{}, tid.id)
assert.EqualValues(t, [16]byte{}, tid)

err = tid.Unmarshal(nil)
assert.NoError(t, err)
assert.EqualValues(t, [16]byte{}, tid.id)
assert.EqualValues(t, [16]byte{}, tid)
}

func TestTraceIDUnmarshalJSON(t *testing.T) {
tid := NewTraceID([16]byte{})
err := tid.UnmarshalJSON([]byte(`""`))
assert.NoError(t, err)
assert.EqualValues(t, [16]byte{}, tid.id)
assert.EqualValues(t, [16]byte{}, tid)

err = tid.UnmarshalJSON([]byte(`""""`))
assert.Error(t, err)

tidBytes := [16]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78}
err = tid.UnmarshalJSON([]byte(`"12345678123456781234567812345678"`))
assert.NoError(t, err)
assert.EqualValues(t, tidBytes, tid.id)
assert.EqualValues(t, tidBytes, tid)

err = tid.UnmarshalJSON([]byte(`12345678123456781234567812345678`))
assert.NoError(t, err)
assert.EqualValues(t, tidBytes, tid.id)
assert.EqualValues(t, tidBytes, tid)

err = tid.UnmarshalJSON([]byte(`"nothex"`))
assert.Error(t, err)
Expand Down
Loading

0 comments on commit a3c63bb

Please sign in to comment.