Skip to content

Commit

Permalink
[pdata] Deprecate HexString func on SpanID and TraceID (#6530)
Browse files Browse the repository at this point in the history
`[SpanID|TraceID].HexString` methods currently return an empty string for empty SpanID and TraceID instances. This behavior is not defined in the OTel spec and contradicts with w3c recommendations. We don't want to promote a behavior which possibly can be changed in future.

Additionally we introduce `[SpanID|TraceID].String` methods that have the same logic as `HexString` and implement `fmt.Stringer` interface to allow `SpanID` and `TraceID` instances to be used in log statements and avoid unnecessary allocation when log is not emitted. But the `String` method is not encouraged to be used as a replacement for `HexString` because of the concerns as described above.
  • Loading branch information
dmitryax authored Nov 12, 2022
1 parent 65152ee commit d55b152
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .chloggen/deprecate_hexstring.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
change_type: 'deprecation'
component: pdata
note: Deprecate `pcommon.[Span|Trace]ID.HexString` methods. Call `hex.EncodeToString` explicitly instead.
issues: [6514]
6 changes: 3 additions & 3 deletions exporter/loggingexporter/internal/otlptext/databuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (b *dataBuffer) logEntry(format string, a ...interface{}) {
b.buf.WriteString("\n")
}

func (b *dataBuffer) logAttr(attr string, value string) {
func (b *dataBuffer) logAttr(attr string, value interface{}) {
b.logEntry(" %-15s: %s", attr, value)
}

Expand Down Expand Up @@ -258,8 +258,8 @@ func (b *dataBuffer) logLinks(description string, sl ptrace.SpanLinkSlice) {
for i := 0; i < sl.Len(); i++ {
l := sl.At(i)
b.logEntry("SpanLink #%d", i)
b.logEntry(" -> Trace ID: %s", l.TraceID().HexString())
b.logEntry(" -> ID: %s", l.SpanID().HexString())
b.logEntry(" -> Trace ID: %s", l.TraceID())
b.logEntry(" -> ID: %s", l.SpanID())
b.logEntry(" -> TraceState: %s", l.TraceState().AsRaw())
b.logEntry(" -> DroppedAttributesCount: %d", l.DroppedAttributesCount())
if l.Attributes().Len() == 0 {
Expand Down
4 changes: 2 additions & 2 deletions exporter/loggingexporter/internal/otlptext/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func (textLogsMarshaler) MarshalLogs(ld plog.Logs) ([]byte, error) {
buf.logEntry("SeverityNumber: %s(%d)", lr.SeverityNumber(), lr.SeverityNumber())
buf.logEntry("Body: %s", valueToString(lr.Body()))
buf.logAttributes("Attributes", lr.Attributes())
buf.logEntry("Trace ID: %s", lr.TraceID().HexString())
buf.logEntry("Span ID: %s", lr.SpanID().HexString())
buf.logEntry("Trace ID: %s", lr.TraceID())
buf.logEntry("Span ID: %s", lr.SpanID())
buf.logEntry("Flags: %d", lr.Flags())
}
}
Expand Down
6 changes: 3 additions & 3 deletions exporter/loggingexporter/internal/otlptext/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ func (textTracesMarshaler) MarshalTraces(td ptrace.Traces) ([]byte, error) {
for k := 0; k < spans.Len(); k++ {
buf.logEntry("Span #%d", k)
span := spans.At(k)
buf.logAttr("Trace ID", span.TraceID().HexString())
buf.logAttr("Parent ID", span.ParentSpanID().HexString())
buf.logAttr("ID", span.SpanID().HexString())
buf.logAttr("Trace ID", span.TraceID())
buf.logAttr("Parent ID", span.ParentSpanID())
buf.logAttr("ID", span.SpanID())
buf.logAttr("Name", span.Name())
buf.logAttr("Kind", span.Kind().String())
buf.logAttr("Start time", span.StartTimestamp().String())
Expand Down
12 changes: 8 additions & 4 deletions exporter/otlphttpexporter/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,10 @@ func TestIssue_4221(t *testing.T) {
tr := ptraceotlp.NewExportRequest()
require.NoError(t, tr.UnmarshalProto(unbase64Data))
span := tr.Traces().ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0)
assert.Equal(t, "4303853f086f4f8c86cf198b6551df84", span.TraceID().HexString())
assert.Equal(t, "e5513c32795c41b9", span.SpanID().HexString())
traceID := span.TraceID()
assert.Equal(t, "4303853f086f4f8c86cf198b6551df84", hex.EncodeToString(traceID[:]))
spanID := span.SpanID()
assert.Equal(t, "e5513c32795c41b9", hex.EncodeToString(spanID[:]))
}))

exp := startTracesExporter(t, "", svr.URL)
Expand All @@ -281,14 +283,16 @@ func TestIssue_4221(t *testing.T) {
require.NoError(t, err)
copy(traceIDBytes[:], traceIDBytesSlice)
span.SetTraceID(traceIDBytes)
assert.Equal(t, "4303853f086f4f8c86cf198b6551df84", span.TraceID().HexString())
traceID := span.TraceID()
assert.Equal(t, "4303853f086f4f8c86cf198b6551df84", hex.EncodeToString(traceID[:]))

var spanIDBytes [8]byte
spanIDBytesSlice, err := hex.DecodeString("e5513c32795c41b9")
require.NoError(t, err)
copy(spanIDBytes[:], spanIDBytesSlice)
span.SetSpanID(spanIDBytes)
assert.Equal(t, "e5513c32795c41b9", span.SpanID().HexString())
spanID := span.SpanID()
assert.Equal(t, "e5513c32795c41b9", hex.EncodeToString(spanID[:]))

span.SetEndTimestamp(1634684637873000000)
span.Attributes().PutInt("span_index", 3)
Expand Down
14 changes: 13 additions & 1 deletion pdata/pcommon/spanid.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,19 @@ func NewSpanIDEmpty() SpanID {
return emptySpanID
}

// HexString returns hex representation of the SpanID.
// String returns string representation of the SpanID.
//
// Important: Don't rely on this method to get a string identifier of SpanID,
// Use hex.EncodeToString explicitly instead.
// This method meant to implement Stringer interface for display purposes only.
func (ms SpanID) String() string {
if ms.IsEmpty() {
return ""
}
return hex.EncodeToString(ms[:])
}

// Deprecated: [0.65.0] Call hex.EncodeToString explicitly instead.
func (ms SpanID) HexString() string {
if ms.IsEmpty() {
return ""
Expand Down
10 changes: 9 additions & 1 deletion pdata/pcommon/spanid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestNewSpanID(t *testing.T) {
func TestSpanID(t *testing.T) {
sid := SpanID([8]byte{1, 2, 3, 4, 4, 3, 2, 1})
assert.Equal(t, [8]byte{1, 2, 3, 4, 4, 3, 2, 1}, [8]byte(sid))
assert.False(t, sid.IsEmpty())
Expand All @@ -40,6 +40,14 @@ func TestSpanIDHexString(t *testing.T) {
assert.Equal(t, "1223ad1223ad1223", sid.HexString())
}

func TestSpanIDString(t *testing.T) {
sid := SpanID([8]byte{})
assert.Equal(t, "", sid.String())

sid = SpanID([8]byte{0x12, 0x23, 0xAD, 0x12, 0x23, 0xAD, 0x12, 0x23})
assert.Equal(t, "1223ad1223ad1223", sid.String())
}

func TestSpanIDImmutable(t *testing.T) {
initialBytes := [8]byte{0x12, 0x23, 0xAD, 0x12, 0x23, 0xAD, 0x12, 0x23}
sid := SpanID(initialBytes)
Expand Down
14 changes: 13 additions & 1 deletion pdata/pcommon/traceid.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,19 @@ func NewTraceIDEmpty() TraceID {
return emptyTraceID
}

// HexString returns hex representation of the TraceID.
// String returns string representation of the TraceID.
//
// Important: Don't rely on this method to get a string identifier of TraceID.
// Use hex.EncodeToString explicitly instead.
// This method meant to implement Stringer interface for display purposes only.
func (ms TraceID) String() string {
if ms.IsEmpty() {
return ""
}
return hex.EncodeToString(ms[:])
}

// Deprecated: [0.65.0] Call hex.EncodeToString explicitly instead.
func (ms TraceID) HexString() string {
if ms.IsEmpty() {
return ""
Expand Down
9 changes: 8 additions & 1 deletion pdata/pcommon/traceid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func TestTraceID(t *testing.T) {
tid := TraceID([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}, [16]byte(tid))
assert.False(t, tid.IsEmpty())
assert.Equal(t, "01020304050607080807060504030201", tid.HexString())
}

func TestNewTraceIDEmpty(t *testing.T) {
Expand All @@ -41,6 +40,14 @@ func TestTraceIDHexString(t *testing.T) {
assert.Equal(t, "12345678123456781234567812345678", tid.HexString())
}

func TestTraceIDString(t *testing.T) {
tid := TraceID([16]byte{})
assert.Equal(t, "", tid.String())

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

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 := TraceID(initialBytes)
Expand Down

0 comments on commit d55b152

Please sign in to comment.