Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extend configuration to support custom randomNumber func #555

Merged
4 changes: 4 additions & 0 deletions config/config.go
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
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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs make fmt

spanCtx := span.Context().(jaeger.SpanContext)

assert.NoError(t, err)
assert.Equal(t, spanCtx.TraceID().Low ,traceID)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: wrong argument order, expected value (traceID) goes first

defer closeCloser(t, closer)
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
}
8 changes: 8 additions & 0 deletions config/options.go
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