Skip to content

Commit 11f4d10

Browse files
committed
TUN-6868: Return left padded tracing ID when tracing identity is converted to string
1 parent 60a12fc commit 11f4d10

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

tracing/identity.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Identity struct {
2424

2525
// TODO: TUN-6604 Remove this. To reconstruct into Jaeger propagation format, convert tracingContext to tracing.Identity
2626
func (tc *Identity) String() string {
27-
return fmt.Sprintf("%x%x:%x:0:%x", tc.traceIDUpper, tc.traceIDLower, tc.spanID, tc.flags)
27+
return fmt.Sprintf("%016x%016x:%x:0:%x", tc.traceIDUpper, tc.traceIDLower, tc.spanID, tc.flags)
2828
}
2929

3030
func (tc *Identity) MarshalBinary() ([]byte, error) {
@@ -68,14 +68,9 @@ func NewIdentity(trace string) (*Identity, error) {
6868
return nil, fmt.Errorf("trace '%s' doesn't have exactly 4 parts separated by %s", trace, separator)
6969
}
7070
const base = 16
71-
tracingID := parts[0]
72-
if len(tracingID) == 0 {
73-
return nil, fmt.Errorf("missing tracing ID")
74-
}
75-
if len(tracingID) != 32 {
76-
// Correctly left pad the trace to a length of 32
77-
left := traceID128bitsWidth - len(tracingID)
78-
tracingID = strings.Repeat("0", left) + tracingID
71+
tracingID, err := padTracingID(parts[0])
72+
if err != nil {
73+
return nil, err
7974
}
8075
traceIDUpper, err := strconv.ParseUint(tracingID[:16], base, 64)
8176
if err != nil {
@@ -100,3 +95,16 @@ func NewIdentity(trace string) (*Identity, error) {
10095
flags: uint8(flags),
10196
}, nil
10297
}
98+
99+
func padTracingID(tracingID string) (string, error) {
100+
if len(tracingID) == 0 {
101+
return "", fmt.Errorf("missing tracing ID")
102+
}
103+
if len(tracingID) == traceID128bitsWidth {
104+
return tracingID, nil
105+
}
106+
// Correctly left pad the trace to a length of 32
107+
left := traceID128bitsWidth - len(tracingID)
108+
paddedTracingID := strings.Repeat("0", left) + tracingID
109+
return paddedTracingID, nil
110+
}

tracing/identity_test.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,55 @@ func TestNewIdentity(t *testing.T) {
1010
testCases := []struct {
1111
testCase string
1212
trace string
13-
valid bool
13+
expected string
1414
}{
1515
{
1616
testCase: "full length trace",
1717
trace: "ec31ad8a01fde11fdcabe2efdce36873:52726f6cabc144f5:0:1",
18-
valid: true,
18+
expected: "ec31ad8a01fde11fdcabe2efdce36873:52726f6cabc144f5:0:1",
1919
},
2020
{
2121
testCase: "short trace ID",
2222
trace: "ad8a01fde11fdcabe2efdce36873:52726f6cabc144f5:0:1",
23-
valid: true,
23+
expected: "0000ad8a01fde11fdcabe2efdce36873:52726f6cabc144f5:0:1",
24+
},
25+
{
26+
testCase: "short trace ID with 0s in the middle",
27+
trace: "ad8a01fde11f000002efdce36873:52726f6cabc144f5:0:1",
28+
expected: "0000ad8a01fde11f000002efdce36873:52726f6cabc144f5:0:1",
29+
},
30+
{
31+
testCase: "short trace ID with 0s in the beginning and middle",
32+
trace: "001ad8a01fde11fdcabe2efdce36873:52726f6cabc144f5:0:1",
33+
expected: "0001ad8a01fde11fdcabe2efdce36873:52726f6cabc144f5:0:1",
2434
},
2535
{
2636
testCase: "no trace",
2737
trace: "",
28-
valid: false,
2938
},
3039
{
3140
testCase: "missing flags",
3241
trace: "ec31ad8a01fde11fdcabe2efdce36873:52726f6cabc144f5:0",
33-
valid: false,
3442
},
3543
{
3644
testCase: "missing separator",
3745
trace: "ec31ad8a01fde11fdcabe2efdce3687352726f6cabc144f501",
38-
valid: false,
3946
},
4047
}
4148

4249
for _, testCase := range testCases {
4350
identity, err := NewIdentity(testCase.trace)
44-
if testCase.valid {
45-
require.NoError(t, err, testCase.testCase)
46-
require.Equal(t, testCase.trace, identity.String())
51+
if testCase.expected != "" {
52+
require.NoError(t, err)
53+
require.Equal(t, testCase.expected, identity.String())
54+
55+
serializedIdentity, err := identity.MarshalBinary()
56+
require.NoError(t, err)
57+
deserializedIdentity := new(Identity)
58+
err = deserializedIdentity.UnmarshalBinary(serializedIdentity)
59+
require.NoError(t, err)
60+
require.Equal(t, identity, deserializedIdentity)
61+
4762
} else {
4863
require.Error(t, err)
4964
require.Nil(t, identity)

0 commit comments

Comments
 (0)