package trace

import (
	cryptorand "crypto/rand"
	"sync"
	"time"

	"github.com/oklog/ulid/v2"
)

var entropyPool = sync.Pool{
	New: func() interface{} {
		return ulid.Monotonic(cryptorand.Reader, 0)
	},
}

// GenerateTraceID 生成并发安全的 ULID trace ID
func GenerateTraceID() string {
	entropy := entropyPool.Get().(*ulid.MonotonicEntropy)
	defer entropyPool.Put(entropy)
	return ulid.MustNew(ulid.Timestamp(time.Now()), entropy).String()
}
