diff --git a/config/config.go b/config/config.go index 165754b2..26b461d2 100644 --- a/config/config.go +++ b/config/config.go @@ -277,6 +277,10 @@ func (c Configuration) NewTracer(options ...Option) (opentracing.Tracer, io.Clos tracerOptions = append(tracerOptions, jaeger.TracerOptions.Gen128Bit(true)) } + if opts.randomNumber != nil { + tracerOptions = append(tracerOptions, jaeger.TracerOptions.RandomNumber(opts.randomNumber)) + } + for _, tag := range opts.tags { tracerOptions = append(tracerOptions, jaeger.TracerOptions.Tag(tag.Key, tag.Value)) } diff --git a/config/config_test.go b/config/config_test.go index adb40dbd..c6b072fb 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -899,3 +899,19 @@ func TestThrottlerDefaultConfig(t *testing.T) { assert.NoError(t, err) defer closeCloser(t, closer) } + +func TestWithRandomNumber(t *testing.T) { + const traceID uint64 = 1 + + cfg := &Configuration{ + ServiceName: "test-random-number", + } + randomNum := func() uint64 { return traceID } + tracer, closer, err := cfg.NewTracer(WithRandomNumber(randomNum)) + span := tracer.StartSpan("test-span") + spanCtx := span.Context().(jaeger.SpanContext) + + assert.NoError(t, err) + assert.Equal(t, traceID, spanCtx.TraceID().Low) + defer closeCloser(t, closer) +} diff --git a/config/options.go b/config/options.go index e0e50e83..a2b9cbc2 100644 --- a/config/options.go +++ b/config/options.go @@ -40,6 +40,7 @@ type Options struct { tags []opentracing.Tag injectors map[interface{}]jaeger.Injector extractors map[interface{}]jaeger.Extractor + randomNumber func() uint64 } // Metrics creates an Option that initializes Metrics in the tracer, @@ -147,6 +148,13 @@ func Extractor(format interface{}, extractor jaeger.Extractor) Option { } } +// WithRandomNumber supplies a random number generator function to the Tracer used to generate trace and span IDs. +func WithRandomNumber(f func() uint64) Option { + return func(c *Options) { + c.randomNumber = f + } +} + func applyOptions(options ...Option) Options { opts := Options{ injectors: make(map[interface{}]jaeger.Injector),