github.com/newrelic/go-agent@v3.26.0+incompatible/internal/trace_id_generator.go (about) 1 // Copyright 2020 New Relic Corporation. All rights reserved. 2 // SPDX-License-Identifier: Apache-2.0 3 4 package internal 5 6 import ( 7 "fmt" 8 "math/rand" 9 "sync" 10 ) 11 12 // TraceIDGenerator creates identifiers for distributed tracing. 13 type TraceIDGenerator struct { 14 sync.Mutex 15 rnd *rand.Rand 16 } 17 18 // NewTraceIDGenerator creates a new trace identifier generator. 19 func NewTraceIDGenerator(seed int64) *TraceIDGenerator { 20 return &TraceIDGenerator{ 21 rnd: rand.New(rand.NewSource(seed)), 22 } 23 } 24 25 // GenerateTraceID creates a new trace identifier. 26 func (tg *TraceIDGenerator) GenerateTraceID() string { 27 tg.Lock() 28 defer tg.Unlock() 29 30 u1 := tg.rnd.Uint32() 31 u2 := tg.rnd.Uint32() 32 bits := (uint64(u1) << 32) | uint64(u2) 33 return fmt.Sprintf("%016x", bits) 34 }