Skip to content

Commit

Permalink
feat: extend configuration to support custom randomNumber func (#555)
Browse files Browse the repository at this point in the history
* feat: costume tracer randomNumber func on config.New() called

Signed-off-by: guanwenbo <guanwenbo@inke.cn>

* fix: modified the bad spelling and added unit test

Signed-off-by: guanwenbo <guanwenbo@inke.cn>

* fix: modified bad spelling

Signed-off-by: guanwenbo <guanwenbo@inke.cn>

* fix: modified the comments

Signed-off-by: guanwenbo <guanwenbo@inke.cn>

* feat: costume tracer randomNumber func on config.New() called

Signed-off-by: guanwenbo <guanwenbostar@163.com>

* fix: modified the bad spelling and added unit test

Signed-off-by: guanwenbo <guanwenbostar@163.com>

* fix: modified bad spelling

Signed-off-by: guanwenbo <guanwenbostar@163.com>

* fix: modified the comments

Signed-off-by: guanwenbo <guanwenbostar@163.com>

* fix: test the creating of traceID with customer randomNum func

Signed-off-by: guanwenbo <guanwenbostar@163.com>

* fix: lint fmt

Signed-off-by: guanwenbo <guanwenbostar@163.com>

* fix: wrong params order

Signed-off-by: guanwenbo <guanwenbostar@163.com>

Co-authored-by: guanwenbo <guanwenbo@inke.cn>
  • Loading branch information
guanwenbogit and guanwenbo committed Dec 23, 2020
1 parent 17fd3e8 commit fe3fa55
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
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")
spanCtx := span.Context().(jaeger.SpanContext)

assert.NoError(t, err)
assert.Equal(t, traceID, spanCtx.TraceID().Low)
defer closeCloser(t, closer)
}
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

0 comments on commit fe3fa55

Please sign in to comment.