Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

feat: extend configuration to support custom randomNumber func #555

Merged
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
16 changes: 16 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
}
8 changes: 8 additions & 0 deletions config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down