Skip to content

Commit 1b5a5c9

Browse files
committed
Change NewTraceID() method signature to be consistent with ID Generator interface
1 parent 1d1179a commit 1b5a5c9

File tree

2 files changed

+14
-24
lines changed

2 files changed

+14
-24
lines changed

propagators/aws/xray/xrayidgenerator/aws_xray_idgenerator.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
crand "crypto/rand"
1919
"encoding/binary"
2020
"encoding/hex"
21-
"errors"
2221
"math/rand"
2322
"strconv"
2423
"sync"
@@ -52,26 +51,23 @@ func (gen *IDGenerator) NewSpanID() trace.SpanID {
5251

5352
// NewTraceID returns a non-zero trace ID based on AWS X-Ray TraceID format.
5453
// (https://docs.aws.amazon.com/xray/latest/devguide/xray-api-sendingdata.html#xray-api-traceids)
55-
func (gen *IDGenerator) NewTraceID() (trace.TraceID, error) {
54+
func (gen *IDGenerator) NewTraceID() trace.TraceID {
5655
gen.Lock()
5756
defer gen.Unlock()
5857

5958
tid := trace.TraceID{}
60-
currentTime, err := getCurrentTimeHex()
61-
if err != nil {
62-
return trace.TraceID{}, err
63-
}
59+
currentTime := getCurrentTimeHex()
6460
copy(tid[:4], currentTime)
6561
gen.randSource.Read(tid[4:])
6662

67-
return tid, nil
63+
return tid
6864
}
6965

70-
func getCurrentTimeHex() ([]uint8, error) {
66+
func getCurrentTimeHex() []uint8 {
7167
currentTime := time.Now().Unix()
72-
currentTimeHex, err := hex.DecodeString(strconv.FormatInt(currentTime, 16))
73-
if err != nil {
74-
return nil, errors.New("cannot convert current timestamp to hex")
75-
}
76-
return currentTimeHex, nil
68+
// Ignore error since no expected error should result from this operation
69+
// Odd-length strings and non-hex digits are the only 2 error conditions for hex.DecodeString()
70+
// strconv.FromatInt() do not produce odd-length strings or non-hex digits
71+
currentTimeHex, _ := hex.DecodeString(strconv.FormatInt(currentTime, 16))
72+
return currentTimeHex
7773
}

propagators/aws/xray/xrayidgenerator/aws_xray_idgenerator_test.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ import (
2727

2828
func TestAwsXRayTraceIdIsValidLength(t *testing.T) {
2929
idg := NewIDGenerator()
30-
traceID, _ := idg.NewTraceID()
30+
traceID := idg.NewTraceID()
3131

3232
expectedTraceIDLength := 32
3333
assert.Equal(t, len(traceID.String()), expectedTraceIDLength, "TraceID has incorrect length.")
3434
}
3535

3636
func TestAwsXRayTraceIdIsUnique(t *testing.T) {
3737
idg := NewIDGenerator()
38-
traceID1, _ := idg.NewTraceID()
39-
traceID2, _ := idg.NewTraceID()
38+
traceID1 := idg.NewTraceID()
39+
traceID2 := idg.NewTraceID()
4040

4141
assert.NotEqual(t, traceID1.String(), traceID2.String(), "TraceID should be unique")
4242
}
@@ -47,10 +47,7 @@ func TestAwsXRayTraceIdTimeStampInBounds(t *testing.T) {
4747

4848
previousTime := time.Now().Unix()
4949

50-
traceID, err := idg.NewTraceID()
51-
if err != nil {
52-
t.Error(err)
53-
}
50+
traceID := idg.NewTraceID()
5451

5552
currentTime, err := strconv.ParseInt(traceID.String()[0:8], 16, 64)
5653
if err != nil {
@@ -66,10 +63,7 @@ func TestAwsXRayTraceIdTimeStampInBounds(t *testing.T) {
6663
func TestAwsXRayTraceIdIsNotNil(t *testing.T) {
6764
var nilTraceID trace.TraceID
6865
idg := NewIDGenerator()
69-
traceID, err := idg.NewTraceID()
70-
if err != nil {
71-
t.Error(err)
72-
}
66+
traceID := idg.NewTraceID()
7367

7468
assert.False(t, bytes.Equal(traceID[:], nilTraceID[:]), "TraceID cannot be Nil.")
7569
}

0 commit comments

Comments
 (0)