diff --git a/pdata/internal/cmd/pdatagen/internal/base_fields.go b/pdata/internal/cmd/pdatagen/internal/base_fields.go index c2e5f729c0a..14ea6cc33b6 100644 --- a/pdata/internal/cmd/pdatagen/internal/base_fields.go +++ b/pdata/internal/cmd/pdatagen/internal/base_fields.go @@ -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}()) }` diff --git a/pdata/internal/data/bytesid.go b/pdata/internal/data/bytesid.go index 4a207da1127..3b330454a4f 100644 --- a/pdata/internal/data/bytesid.go +++ b/pdata/internal/data/bytesid.go @@ -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 diff --git a/pdata/internal/data/spanid.go b/pdata/internal/data/spanid.go index 09063199c7a..7467a7423c6 100644 --- a/pdata/internal/data/spanid.go +++ b/pdata/internal/data/spanid.go @@ -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 @@ -25,58 +26,40 @@ 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 } @@ -84,7 +67,7 @@ func (sid *SpanID) Unmarshal(data []byte) error { return errInvalidSpanIDSize } - copy(sid.id[:], data) + copy(sid[:], data) return nil } @@ -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) } diff --git a/pdata/internal/data/spanid_test.go b/pdata/internal/data/spanid_test.go index 24e1c9e7acd..cac5d1a84b5 100644 --- a/pdata/internal/data/spanid_test.go +++ b/pdata/internal/data/spanid_test.go @@ -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) @@ -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) @@ -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) diff --git a/pdata/internal/data/traceid.go b/pdata/internal/data/traceid.go index b0730a0c4a4..fcb7f17f5a7 100644 --- a/pdata/internal/data/traceid.go +++ b/pdata/internal/data/traceid.go @@ -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 @@ -25,60 +26,40 @@ 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 } @@ -86,7 +67,7 @@ func (tid *TraceID) Unmarshal(data []byte) error { return errInvalidTraceIDSize } - copy(tid.id[:], data) + copy(tid[:], data) return nil } @@ -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) } diff --git a/pdata/internal/data/traceid_test.go b/pdata/internal/data/traceid_test.go index b57ab8defb7..3710e6e3a6c 100644 --- a/pdata/internal/data/traceid_test.go +++ b/pdata/internal/data/traceid_test.go @@ -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) @@ -85,22 +67,22 @@ 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) @@ -108,11 +90,11 @@ func TestTraceIDUnmarshalJSON(t *testing.T) { 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) diff --git a/pdata/internal/wrapper_span_id.go b/pdata/internal/wrapper_span_id.go deleted file mode 100644 index c8aaf2ae8b3..00000000000 --- a/pdata/internal/wrapper_span_id.go +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package internal // import "go.opentelemetry.io/collector/pdata/internal" - -import ( - "go.opentelemetry.io/collector/pdata/internal/data" -) - -type SpanID struct { - orig data.SpanID -} - -func GetOrigSpanID(ms SpanID) data.SpanID { - return ms.orig -} - -func NewSpanID(orig data.SpanID) SpanID { - return SpanID{orig: orig} -} diff --git a/pdata/internal/wrapper_trace_id.go b/pdata/internal/wrapper_trace_id.go deleted file mode 100644 index 5de30f675d7..00000000000 --- a/pdata/internal/wrapper_trace_id.go +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package internal // import "go.opentelemetry.io/collector/pdata/internal" - -import ( - "go.opentelemetry.io/collector/pdata/internal/data" -) - -type TraceID struct { - orig data.TraceID -} - -func GetOrigTraceID(ms TraceID) data.TraceID { - return ms.orig -} - -func NewTraceID(orig data.TraceID) TraceID { - return TraceID{orig: orig} -} diff --git a/pdata/pcommon/spanid.go b/pdata/pcommon/spanid.go index 4593a4a7c5d..8163646fc09 100644 --- a/pdata/pcommon/spanid.go +++ b/pdata/pcommon/spanid.go @@ -13,9 +13,9 @@ // limitations under the License. package pcommon // import "go.opentelemetry.io/collector/pdata/pcommon" - import ( - "go.opentelemetry.io/collector/pdata/internal" + "encoding/hex" + "go.opentelemetry.io/collector/pdata/internal/data" ) @@ -23,10 +23,8 @@ import ( var EmptySpanID = NewSpanID([8]byte{}) // SpanID is span identifier. -type SpanID internal.SpanID - -func (ms SpanID) getOrig() data.SpanID { - return internal.GetOrigSpanID(internal.SpanID(ms)) +type SpanID struct { + orig [8]byte } // Deprecated: [v0.59.0] use EmptySpanID. @@ -36,20 +34,20 @@ func InvalidSpanID() SpanID { // NewSpanID returns a new SpanID from the given byte array. func NewSpanID(bytes [8]byte) SpanID { - return SpanID(internal.NewSpanID(data.NewSpanID(bytes))) + return SpanID{orig: bytes} } // Bytes returns the byte array representation of the SpanID. func (ms SpanID) Bytes() [8]byte { - return ms.getOrig().Bytes() + return ms.orig } // HexString returns hex representation of the SpanID. func (ms SpanID) HexString() string { - return ms.getOrig().HexString() + return hex.EncodeToString(ms.orig[:]) } // IsEmpty returns true if id doesn't contain at least one non-zero byte. func (ms SpanID) IsEmpty() bool { - return ms.getOrig().IsEmpty() + return data.SpanID(ms.orig).IsEmpty() } diff --git a/pdata/pcommon/spanid_test.go b/pdata/pcommon/spanid_test.go index 72b8382207c..6a8b2d87ccb 100644 --- a/pdata/pcommon/spanid_test.go +++ b/pdata/pcommon/spanid_test.go @@ -20,14 +20,36 @@ import ( "github.com/stretchr/testify/assert" ) -func TestSpanID(t *testing.T) { +func TestEmptySpanID(t *testing.T) { sid := EmptySpanID - assert.EqualValues(t, [8]byte{}, sid.Bytes()) + assert.Equal(t, [8]byte{}, sid.Bytes()) assert.True(t, sid.IsEmpty()) - assert.Equal(t, "", sid.HexString()) +} - sid = NewSpanID([8]byte{1, 2, 3, 4, 4, 3, 2, 1}) - assert.EqualValues(t, [8]byte{1, 2, 3, 4, 4, 3, 2, 1}, sid.Bytes()) +func TestNewSpanID(t *testing.T) { + sid := NewSpanID([8]byte{1, 2, 3, 4, 4, 3, 2, 1}) + assert.Equal(t, [8]byte{1, 2, 3, 4, 4, 3, 2, 1}, sid.Bytes()) assert.False(t, sid.IsEmpty()) - assert.Equal(t, "0102030404030201", sid.HexString()) +} + +func TestSpanIDHexString(t *testing.T) { + sid := NewSpanID([8]byte{}) + assert.Equal(t, "0000000000000000", sid.HexString()) + + sid = NewSpanID([8]byte{0x12, 0x23, 0xAD, 0x12, 0x23, 0xAD, 0x12, 0x23}) + assert.Equal(t, "1223ad1223ad1223", sid.HexString()) +} + +func TestSpanIDImmutable(t *testing.T) { + initialBytes := [8]byte{0x12, 0x23, 0xAD, 0x12, 0x23, 0xAD, 0x12, 0x23} + sid := NewSpanID(initialBytes) + assert.Equal(t, initialBytes, sid.Bytes()) + + // Get the bytes and try to mutate. + bytes := sid.Bytes() + bytes[4] = 0x89 + + // Does not change the already created SpanID. + assert.NotEqual(t, bytes, sid.Bytes()) + assert.Equal(t, initialBytes, sid.Bytes()) } diff --git a/pdata/pcommon/traceid.go b/pdata/pcommon/traceid.go index 7f1ddb6bd05..4a12199893f 100644 --- a/pdata/pcommon/traceid.go +++ b/pdata/pcommon/traceid.go @@ -15,7 +15,8 @@ package pcommon // import "go.opentelemetry.io/collector/pdata/pcommon" import ( - "go.opentelemetry.io/collector/pdata/internal" + "encoding/hex" + "go.opentelemetry.io/collector/pdata/internal/data" ) @@ -23,10 +24,8 @@ import ( var EmptyTraceID = NewTraceID([16]byte{}) // TraceID is a trace identifier. -type TraceID internal.TraceID - -func (ms TraceID) getOrig() data.TraceID { - return internal.GetOrigTraceID(internal.TraceID(ms)) +type TraceID struct { + orig [16]byte } // Deprecated: [v0.59.0] use EmptyTraceID. @@ -36,20 +35,20 @@ func InvalidTraceID() TraceID { // NewTraceID returns a new TraceID from the given byte array. func NewTraceID(bytes [16]byte) TraceID { - return TraceID(internal.NewTraceID(data.NewTraceID(bytes))) + return TraceID{orig: bytes} } // Bytes returns the byte array representation of the TraceID. func (ms TraceID) Bytes() [16]byte { - return ms.getOrig().Bytes() + return ms.orig } // HexString returns hex representation of the TraceID. func (ms TraceID) HexString() string { - return ms.getOrig().HexString() + return hex.EncodeToString(ms.orig[:]) } // IsEmpty returns true if id doesn't contain at least one non-zero byte. func (ms TraceID) IsEmpty() bool { - return ms.getOrig().IsEmpty() + return data.TraceID(ms.orig).IsEmpty() } diff --git a/pdata/pcommon/traceid_test.go b/pdata/pcommon/traceid_test.go index dfadbd27dba..8159904abb4 100644 --- a/pdata/pcommon/traceid_test.go +++ b/pdata/pcommon/traceid_test.go @@ -20,14 +20,37 @@ import ( "github.com/stretchr/testify/assert" ) -func TestTraceID(t *testing.T) { +func TestEmptyTraceID(t *testing.T) { tid := EmptyTraceID assert.Equal(t, [16]byte{}, tid.Bytes()) assert.True(t, tid.IsEmpty()) - assert.Equal(t, "", tid.HexString()) +} - tid = NewTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1}) +func TestNewTraceID(t *testing.T) { + tid := NewTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1}) assert.Equal(t, [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1}, tid.Bytes()) assert.False(t, tid.IsEmpty()) assert.Equal(t, "01020304050607080807060504030201", tid.HexString()) } + +func TestTraceIDHexString(t *testing.T) { + tid := NewTraceID([16]byte{}) + assert.Equal(t, "00000000000000000000000000000000", tid.HexString()) + + tid = NewTraceID([16]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78}) + assert.Equal(t, "12345678123456781234567812345678", tid.HexString()) +} + +func TestTraceIDImmutable(t *testing.T) { + initialBytes := [16]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78} + tid := NewTraceID(initialBytes) + assert.Equal(t, initialBytes, tid.Bytes()) + + // Get the bytes and try to mutate. + bytes := tid.Bytes() + bytes[4] = 0x23 + + // Does not change the already created TraceID. + assert.NotEqual(t, bytes, tid.Bytes()) + assert.Equal(t, initialBytes, tid.Bytes()) +} diff --git a/pdata/plog/generated_logs.go b/pdata/plog/generated_logs.go index 1f6d930fedb..aae651ce169 100644 --- a/pdata/plog/generated_logs.go +++ b/pdata/plog/generated_logs.go @@ -621,22 +621,22 @@ func (ms LogRecord) SetTimestamp(v pcommon.Timestamp) { // TraceID returns the traceid associated with this LogRecord. func (ms LogRecord) TraceID() pcommon.TraceID { - return pcommon.TraceID(internal.NewTraceID(ms.getOrig().TraceId)) + return pcommon.NewTraceID(ms.getOrig().TraceId) } // SetTraceID replaces the traceid associated with this LogRecord. func (ms LogRecord) SetTraceID(v pcommon.TraceID) { - ms.getOrig().TraceId = internal.GetOrigTraceID(internal.TraceID(v)) + ms.getOrig().TraceId = v.Bytes() } // SpanID returns the spanid associated with this LogRecord. func (ms LogRecord) SpanID() pcommon.SpanID { - return pcommon.SpanID(internal.NewSpanID(ms.getOrig().SpanId)) + return pcommon.NewSpanID(ms.getOrig().SpanId) } // SetSpanID replaces the spanid associated with this LogRecord. func (ms LogRecord) SetSpanID(v pcommon.SpanID) { - ms.getOrig().SpanId = internal.GetOrigSpanID(internal.SpanID(v)) + ms.getOrig().SpanId = v.Bytes() } // FlagsStruct returns the flagsstruct associated with this LogRecord. diff --git a/pdata/plog/generated_logs_test.go b/pdata/plog/generated_logs_test.go index 9d0e449411c..0114d8cab4a 100644 --- a/pdata/plog/generated_logs_test.go +++ b/pdata/plog/generated_logs_test.go @@ -470,16 +470,16 @@ func TestLogRecord_Timestamp(t *testing.T) { func TestLogRecord_TraceID(t *testing.T) { ms := NewLogRecord() - assert.Equal(t, pcommon.TraceID(internal.NewTraceID(data.NewTraceID([16]byte{}))), ms.TraceID()) - testValTraceID := pcommon.TraceID(internal.NewTraceID(data.NewTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1}))) + assert.Equal(t, pcommon.NewTraceID(data.NewTraceID([16]byte{})), ms.TraceID()) + testValTraceID := pcommon.NewTraceID(data.NewTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1})) ms.SetTraceID(testValTraceID) assert.Equal(t, testValTraceID, ms.TraceID()) } func TestLogRecord_SpanID(t *testing.T) { ms := NewLogRecord() - assert.Equal(t, pcommon.SpanID(internal.NewSpanID(data.NewSpanID([8]byte{}))), ms.SpanID()) - testValSpanID := pcommon.SpanID(internal.NewSpanID(data.NewSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}))) + assert.Equal(t, pcommon.NewSpanID(data.NewSpanID([8]byte{})), ms.SpanID()) + testValSpanID := pcommon.NewSpanID(data.NewSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})) ms.SetSpanID(testValSpanID) assert.Equal(t, testValSpanID, ms.SpanID()) } diff --git a/pdata/pmetric/generated_metrics.go b/pdata/pmetric/generated_metrics.go index cfefd9189d6..4c7ba84bc07 100644 --- a/pdata/pmetric/generated_metrics.go +++ b/pdata/pmetric/generated_metrics.go @@ -2621,22 +2621,22 @@ func (ms Exemplar) FilteredAttributes() pcommon.Map { // TraceID returns the traceid associated with this Exemplar. func (ms Exemplar) TraceID() pcommon.TraceID { - return pcommon.TraceID(internal.NewTraceID(ms.getOrig().TraceId)) + return pcommon.NewTraceID(ms.getOrig().TraceId) } // SetTraceID replaces the traceid associated with this Exemplar. func (ms Exemplar) SetTraceID(v pcommon.TraceID) { - ms.getOrig().TraceId = internal.GetOrigTraceID(internal.TraceID(v)) + ms.getOrig().TraceId = v.Bytes() } // SpanID returns the spanid associated with this Exemplar. func (ms Exemplar) SpanID() pcommon.SpanID { - return pcommon.SpanID(internal.NewSpanID(ms.getOrig().SpanId)) + return pcommon.NewSpanID(ms.getOrig().SpanId) } // SetSpanID replaces the spanid associated with this Exemplar. func (ms Exemplar) SetSpanID(v pcommon.SpanID) { - ms.getOrig().SpanId = internal.GetOrigSpanID(internal.SpanID(v)) + ms.getOrig().SpanId = v.Bytes() } // CopyTo copies all properties from the current struct to the dest. diff --git a/pdata/pmetric/generated_metrics_test.go b/pdata/pmetric/generated_metrics_test.go index ba2c78fe2b8..297ae55c540 100644 --- a/pdata/pmetric/generated_metrics_test.go +++ b/pdata/pmetric/generated_metrics_test.go @@ -1071,16 +1071,16 @@ func TestHistogramDataPoint_Sum(t *testing.T) { func TestHistogramDataPoint_BucketCounts(t *testing.T) { ms := NewHistogramDataPoint() - assert.Equal(t, pcommon.ImmutableUInt64Slice(internal.NewImmutableUInt64Slice([]uint64(nil))), ms.BucketCounts()) - testValBucketCounts := pcommon.ImmutableUInt64Slice(internal.NewImmutableUInt64Slice([]uint64{1, 2, 3})) + assert.Equal(t, pcommon.NewImmutableUInt64Slice([]uint64(nil)), ms.BucketCounts()) + testValBucketCounts := pcommon.NewImmutableUInt64Slice([]uint64{1, 2, 3}) ms.SetBucketCounts(testValBucketCounts) assert.Equal(t, testValBucketCounts, ms.BucketCounts()) } func TestHistogramDataPoint_ExplicitBounds(t *testing.T) { ms := NewHistogramDataPoint() - assert.Equal(t, pcommon.ImmutableFloat64Slice(internal.NewImmutableFloat64Slice([]float64(nil))), ms.ExplicitBounds()) - testValExplicitBounds := pcommon.ImmutableFloat64Slice(internal.NewImmutableFloat64Slice([]float64{1, 2, 3})) + assert.Equal(t, pcommon.NewImmutableFloat64Slice([]float64(nil)), ms.ExplicitBounds()) + testValExplicitBounds := pcommon.NewImmutableFloat64Slice([]float64{1, 2, 3}) ms.SetExplicitBounds(testValExplicitBounds) assert.Equal(t, testValExplicitBounds, ms.ExplicitBounds()) } @@ -1357,8 +1357,8 @@ func TestBuckets_Offset(t *testing.T) { func TestBuckets_BucketCounts(t *testing.T) { ms := NewBuckets() - assert.Equal(t, pcommon.ImmutableUInt64Slice(internal.NewImmutableUInt64Slice([]uint64(nil))), ms.BucketCounts()) - testValBucketCounts := pcommon.ImmutableUInt64Slice(internal.NewImmutableUInt64Slice([]uint64{1, 2, 3})) + assert.Equal(t, pcommon.NewImmutableUInt64Slice([]uint64(nil)), ms.BucketCounts()) + testValBucketCounts := pcommon.NewImmutableUInt64Slice([]uint64{1, 2, 3}) ms.SetBucketCounts(testValBucketCounts) assert.Equal(t, testValBucketCounts, ms.BucketCounts()) } @@ -1840,16 +1840,16 @@ func TestExemplar_FilteredAttributes(t *testing.T) { func TestExemplar_TraceID(t *testing.T) { ms := NewExemplar() - assert.Equal(t, pcommon.TraceID(internal.NewTraceID(data.NewTraceID([16]byte{}))), ms.TraceID()) - testValTraceID := pcommon.TraceID(internal.NewTraceID(data.NewTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1}))) + assert.Equal(t, pcommon.NewTraceID(data.NewTraceID([16]byte{})), ms.TraceID()) + testValTraceID := pcommon.NewTraceID(data.NewTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1})) ms.SetTraceID(testValTraceID) assert.Equal(t, testValTraceID, ms.TraceID()) } func TestExemplar_SpanID(t *testing.T) { ms := NewExemplar() - assert.Equal(t, pcommon.SpanID(internal.NewSpanID(data.NewSpanID([8]byte{}))), ms.SpanID()) - testValSpanID := pcommon.SpanID(internal.NewSpanID(data.NewSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}))) + assert.Equal(t, pcommon.NewSpanID(data.NewSpanID([8]byte{})), ms.SpanID()) + testValSpanID := pcommon.NewSpanID(data.NewSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})) ms.SetSpanID(testValSpanID) assert.Equal(t, testValSpanID, ms.SpanID()) } diff --git a/pdata/ptrace/generated_traces.go b/pdata/ptrace/generated_traces.go index e243bcaaafa..6daf248368b 100644 --- a/pdata/ptrace/generated_traces.go +++ b/pdata/ptrace/generated_traces.go @@ -601,22 +601,22 @@ func (ms Span) MoveTo(dest Span) { // TraceID returns the traceid associated with this Span. func (ms Span) TraceID() pcommon.TraceID { - return pcommon.TraceID(internal.NewTraceID(ms.getOrig().TraceId)) + return pcommon.NewTraceID(ms.getOrig().TraceId) } // SetTraceID replaces the traceid associated with this Span. func (ms Span) SetTraceID(v pcommon.TraceID) { - ms.getOrig().TraceId = internal.GetOrigTraceID(internal.TraceID(v)) + ms.getOrig().TraceId = v.Bytes() } // SpanID returns the spanid associated with this Span. func (ms Span) SpanID() pcommon.SpanID { - return pcommon.SpanID(internal.NewSpanID(ms.getOrig().SpanId)) + return pcommon.NewSpanID(ms.getOrig().SpanId) } // SetSpanID replaces the spanid associated with this Span. func (ms Span) SetSpanID(v pcommon.SpanID) { - ms.getOrig().SpanId = internal.GetOrigSpanID(internal.SpanID(v)) + ms.getOrig().SpanId = v.Bytes() } // TraceState returns the tracestate associated with this Span. @@ -631,12 +631,12 @@ func (ms Span) SetTraceState(v TraceState) { // ParentSpanID returns the parentspanid associated with this Span. func (ms Span) ParentSpanID() pcommon.SpanID { - return pcommon.SpanID(internal.NewSpanID(ms.getOrig().ParentSpanId)) + return pcommon.NewSpanID(ms.getOrig().ParentSpanId) } // SetParentSpanID replaces the parentspanid associated with this Span. func (ms Span) SetParentSpanID(v pcommon.SpanID) { - ms.getOrig().ParentSpanId = internal.GetOrigSpanID(internal.SpanID(v)) + ms.getOrig().ParentSpanId = v.Bytes() } // Name returns the name associated with this Span. @@ -1142,22 +1142,22 @@ func (ms SpanLink) MoveTo(dest SpanLink) { // TraceID returns the traceid associated with this SpanLink. func (ms SpanLink) TraceID() pcommon.TraceID { - return pcommon.TraceID(internal.NewTraceID(ms.getOrig().TraceId)) + return pcommon.NewTraceID(ms.getOrig().TraceId) } // SetTraceID replaces the traceid associated with this SpanLink. func (ms SpanLink) SetTraceID(v pcommon.TraceID) { - ms.getOrig().TraceId = internal.GetOrigTraceID(internal.TraceID(v)) + ms.getOrig().TraceId = v.Bytes() } // SpanID returns the spanid associated with this SpanLink. func (ms SpanLink) SpanID() pcommon.SpanID { - return pcommon.SpanID(internal.NewSpanID(ms.getOrig().SpanId)) + return pcommon.NewSpanID(ms.getOrig().SpanId) } // SetSpanID replaces the spanid associated with this SpanLink. func (ms SpanLink) SetSpanID(v pcommon.SpanID) { - ms.getOrig().SpanId = internal.GetOrigSpanID(internal.SpanID(v)) + ms.getOrig().SpanId = v.Bytes() } // TraceState returns the tracestate associated with this SpanLink. diff --git a/pdata/ptrace/generated_traces_test.go b/pdata/ptrace/generated_traces_test.go index b25513200ca..31c7ceea4a7 100644 --- a/pdata/ptrace/generated_traces_test.go +++ b/pdata/ptrace/generated_traces_test.go @@ -454,16 +454,16 @@ func TestSpan_CopyTo(t *testing.T) { func TestSpan_TraceID(t *testing.T) { ms := NewSpan() - assert.Equal(t, pcommon.TraceID(internal.NewTraceID(data.NewTraceID([16]byte{}))), ms.TraceID()) - testValTraceID := pcommon.TraceID(internal.NewTraceID(data.NewTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1}))) + assert.Equal(t, pcommon.NewTraceID(data.NewTraceID([16]byte{})), ms.TraceID()) + testValTraceID := pcommon.NewTraceID(data.NewTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1})) ms.SetTraceID(testValTraceID) assert.Equal(t, testValTraceID, ms.TraceID()) } func TestSpan_SpanID(t *testing.T) { ms := NewSpan() - assert.Equal(t, pcommon.SpanID(internal.NewSpanID(data.NewSpanID([8]byte{}))), ms.SpanID()) - testValSpanID := pcommon.SpanID(internal.NewSpanID(data.NewSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}))) + assert.Equal(t, pcommon.NewSpanID(data.NewSpanID([8]byte{})), ms.SpanID()) + testValSpanID := pcommon.NewSpanID(data.NewSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})) ms.SetSpanID(testValSpanID) assert.Equal(t, testValSpanID, ms.SpanID()) } @@ -478,8 +478,8 @@ func TestSpan_TraceState(t *testing.T) { func TestSpan_ParentSpanID(t *testing.T) { ms := NewSpan() - assert.Equal(t, pcommon.SpanID(internal.NewSpanID(data.NewSpanID([8]byte{}))), ms.ParentSpanID()) - testValParentSpanID := pcommon.SpanID(internal.NewSpanID(data.NewSpanID([8]byte{8, 7, 6, 5, 4, 3, 2, 1}))) + assert.Equal(t, pcommon.NewSpanID(data.NewSpanID([8]byte{})), ms.ParentSpanID()) + testValParentSpanID := pcommon.NewSpanID(data.NewSpanID([8]byte{8, 7, 6, 5, 4, 3, 2, 1})) ms.SetParentSpanID(testValParentSpanID) assert.Equal(t, testValParentSpanID, ms.ParentSpanID()) } @@ -850,16 +850,16 @@ func TestSpanLink_CopyTo(t *testing.T) { func TestSpanLink_TraceID(t *testing.T) { ms := NewSpanLink() - assert.Equal(t, pcommon.TraceID(internal.NewTraceID(data.NewTraceID([16]byte{}))), ms.TraceID()) - testValTraceID := pcommon.TraceID(internal.NewTraceID(data.NewTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1}))) + assert.Equal(t, pcommon.NewTraceID(data.NewTraceID([16]byte{})), ms.TraceID()) + testValTraceID := pcommon.NewTraceID(data.NewTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1})) ms.SetTraceID(testValTraceID) assert.Equal(t, testValTraceID, ms.TraceID()) } func TestSpanLink_SpanID(t *testing.T) { ms := NewSpanLink() - assert.Equal(t, pcommon.SpanID(internal.NewSpanID(data.NewSpanID([8]byte{}))), ms.SpanID()) - testValSpanID := pcommon.SpanID(internal.NewSpanID(data.NewSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}))) + assert.Equal(t, pcommon.NewSpanID(data.NewSpanID([8]byte{})), ms.SpanID()) + testValSpanID := pcommon.NewSpanID(data.NewSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})) ms.SetSpanID(testValSpanID) assert.Equal(t, testValSpanID, ms.SpanID()) }