Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 3fb168f

Browse files
authored
Defer IDGenerator initialization until first use (#1228)
Initializing the `IDGenerator` from `init()` means that any downstream project which transitively imports this package somewhere in its dependency tree will incur `getrandom()` syscalls it has no control over at startup. This causes problems for us in [Ignition](https://github.com/coreos/ignition), where we're transitively pulling in this package via cloud.google.com/go/storage. Ignition runs very early during the boot process, which means that even though this isn't using `GRND_RANDOM`, the `getrandom` syscall can block until the entropy pool is ready. This is a real problem when running in VMs on systems which don't provide a virtualized RNG device (such as VMware) or which lack RDRAND. I can't find a good reference for this, but I think in general it should be considered good practice to avoid I/O like this in `init()` in favour of a more lazy approach (or an explicit `Initialize()` function for clients to call). Otherwise, *every* program which pulls in the package will pay for it, whether or not they intend to actually make use of the functionality those syscalls are priming. (While it's not relevant here, another advantage of not using `init()` for this is that I/O is fallible, and `init()` semantics means errors can't be handled sanely.) Let's rework things here so that we don't actually initialize the `IDGenerator` fields until the first time it's used.
1 parent 5bb2445 commit 3fb168f

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

trace/trace.go

+23-12
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ func startSpanInternal(name string, hasParent bool, parent SpanContext, remotePa
206206
span.spanContext = parent
207207

208208
cfg := config.Load().(*Config)
209+
if gen, ok := cfg.IDGenerator.(*defaultIDGenerator); ok {
210+
// lazy initialization
211+
gen.init()
212+
}
209213

210214
if !hasParent {
211215
span.spanContext.TraceID = cfg.IDGenerator.NewTraceID()
@@ -534,20 +538,9 @@ func (s *Span) String() string {
534538
var config atomic.Value // access atomically
535539

536540
func init() {
537-
gen := &defaultIDGenerator{}
538-
// initialize traceID and spanID generators.
539-
var rngSeed int64
540-
for _, p := range []interface{}{
541-
&rngSeed, &gen.traceIDAdd, &gen.nextSpanID, &gen.spanIDInc,
542-
} {
543-
binary.Read(crand.Reader, binary.LittleEndian, p)
544-
}
545-
gen.traceIDRand = rand.New(rand.NewSource(rngSeed))
546-
gen.spanIDInc |= 1
547-
548541
config.Store(&Config{
549542
DefaultSampler: ProbabilitySampler(defaultSamplingProbability),
550-
IDGenerator: gen,
543+
IDGenerator: &defaultIDGenerator{},
551544
MaxAttributesPerSpan: DefaultMaxAttributesPerSpan,
552545
MaxAnnotationEventsPerSpan: DefaultMaxAnnotationEventsPerSpan,
553546
MaxMessageEventsPerSpan: DefaultMaxMessageEventsPerSpan,
@@ -571,6 +564,24 @@ type defaultIDGenerator struct {
571564

572565
traceIDAdd [2]uint64
573566
traceIDRand *rand.Rand
567+
568+
initOnce sync.Once
569+
}
570+
571+
// init initializes the generator on the first call to avoid consuming entropy
572+
// unnecessarily.
573+
func (gen *defaultIDGenerator) init() {
574+
gen.initOnce.Do(func() {
575+
// initialize traceID and spanID generators.
576+
var rngSeed int64
577+
for _, p := range []interface{}{
578+
&rngSeed, &gen.traceIDAdd, &gen.nextSpanID, &gen.spanIDInc,
579+
} {
580+
binary.Read(crand.Reader, binary.LittleEndian, p)
581+
}
582+
gen.traceIDRand = rand.New(rand.NewSource(rngSeed))
583+
gen.spanIDInc |= 1
584+
})
574585
}
575586

576587
// NewSpanID returns a non-zero span ID from a randomly-chosen sequence.

0 commit comments

Comments
 (0)