Skip to content

Commit

Permalink
Make changes based on PR comments (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
wilguo committed Nov 27, 2020
1 parent 6a341d4 commit a33ff23
Showing 1 changed file with 4 additions and 17 deletions.
21 changes: 4 additions & 17 deletions idgenerator/aws/xray/aws_xray_idgenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
crand "crypto/rand"
"encoding/binary"
"encoding/hex"
"errors"
"math/rand"
"strconv"
"sync"
Expand All @@ -26,16 +27,6 @@ import (
"go.opentelemetry.io/otel/trace"
)

const (
errConvertTimeToHex errorConst = "cannot convert current timestamp to hex"
)

type errorConst string

func (e errorConst) Error() string {
return string(e)
}

type IDGenerator struct {
sync.Mutex
randSource *rand.Rand
Expand All @@ -45,10 +36,7 @@ type IDGenerator struct {
func NewIDGenerator() *IDGenerator {
gen := &IDGenerator{}
var rngSeed int64
err := binary.Read(crand.Reader, binary.LittleEndian, &rngSeed)
if err != nil {
panic(err)
}
_ = binary.Read(crand.Reader, binary.LittleEndian, &rngSeed)
gen.randSource = rand.New(rand.NewSource(rngSeed))
return gen
}
Expand All @@ -71,8 +59,7 @@ func (gen *IDGenerator) NewTraceID() (trace.TraceID, error) {
tid := trace.TraceID{}
currentTime, err := getCurrentTimeHex()
if err != nil {
var nilTraceID trace.TraceID
return nilTraceID, err
return trace.TraceID{}, err
}
copy(tid[:4], currentTime)
gen.randSource.Read(tid[4:])
Expand All @@ -84,7 +71,7 @@ func getCurrentTimeHex() ([]uint8, error) {
currentTime := time.Now().Unix()
currentTimeHex, err := hex.DecodeString(strconv.FormatInt(currentTime, 16))
if err != nil {
return nil, errConvertTimeToHex
return nil, errors.New("cannot convert current timestamp to hex")
}
return currentTimeHex, nil
}

0 comments on commit a33ff23

Please sign in to comment.