From 9ad924ff5d5d1c2994244e3a9f50be2f1589ecc2 Mon Sep 17 00:00:00 2001 From: guanwenbo Date: Fri, 18 Dec 2020 20:08:57 +0800 Subject: [PATCH 01/11] feat: costume tracer randomNumber func on config.New() called Signed-off-by: guanwenbo --- config/config.go | 4 ++++ config/options.go | 8 ++++++++ 2 files changed, 12 insertions(+) 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/options.go b/config/options.go index e0e50e83..27c23777 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 { } } +// WithRandonNunmber set the Tracer random number func +func WithRandonNunmber(f func() uint64) Option { + return func(c *Options) { + c.randomNumber = f + } +} + func applyOptions(options ...Option) Options { opts := Options{ injectors: make(map[interface{}]jaeger.Injector), From e547a1bfce85050c46a769c3fb90acda2a3fdf14 Mon Sep 17 00:00:00 2001 From: guanwenbo Date: Mon, 21 Dec 2020 10:05:40 +0800 Subject: [PATCH 02/11] fix: modified the bad spelling and added unit test Signed-off-by: guanwenbo --- config/config_test.go | 10 ++++++++++ config/options.go | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/config/config_test.go b/config/config_test.go index adb40dbd..a2eebd1e 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -899,3 +899,13 @@ func TestThrottlerDefaultConfig(t *testing.T) { assert.NoError(t, err) defer closeCloser(t, closer) } + +func TestWithRandomNumber(t *testing.T) { + cfg := &Configuration{ + ServiceName: "test-random-number", + } + randomNum := func() uint64 { return 1 } + _, closer, err := cfg.NewTracer(WithRandomNunmber(randomNum)) + assert.NoError(t, err) + defer closeCloser(t, closer) +} diff --git a/config/options.go b/config/options.go index 27c23777..7fcd2414 100644 --- a/config/options.go +++ b/config/options.go @@ -148,8 +148,8 @@ func Extractor(format interface{}, extractor jaeger.Extractor) Option { } } -// WithRandonNunmber set the Tracer random number func -func WithRandonNunmber(f func() uint64) Option { +// WithRandomNunmber set the Tracer random number func +func WithRandomNunmber(f func() uint64) Option { return func(c *Options) { c.randomNumber = f } From 29305a4f400190251a01a727ae0f1a0382e21479 Mon Sep 17 00:00:00 2001 From: guanwenbo Date: Mon, 21 Dec 2020 14:25:00 +0800 Subject: [PATCH 03/11] fix: modified bad spelling Signed-off-by: guanwenbo --- config/config_test.go | 2 +- config/options.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/config/config_test.go b/config/config_test.go index a2eebd1e..b69bfc0a 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -905,7 +905,7 @@ func TestWithRandomNumber(t *testing.T) { ServiceName: "test-random-number", } randomNum := func() uint64 { return 1 } - _, closer, err := cfg.NewTracer(WithRandomNunmber(randomNum)) + _, closer, err := cfg.NewTracer(WithRandomNumber(randomNum)) assert.NoError(t, err) defer closeCloser(t, closer) } diff --git a/config/options.go b/config/options.go index 7fcd2414..a5065072 100644 --- a/config/options.go +++ b/config/options.go @@ -148,8 +148,8 @@ func Extractor(format interface{}, extractor jaeger.Extractor) Option { } } -// WithRandomNunmber set the Tracer random number func -func WithRandomNunmber(f func() uint64) Option { +// WithRandomNumber set the Tracer random number func +func WithRandomNumber(f func() uint64) Option { return func(c *Options) { c.randomNumber = f } From 71e806a8566876864b9e8b77a2e151bc0d504a4b Mon Sep 17 00:00:00 2001 From: guanwenbo Date: Mon, 21 Dec 2020 14:27:35 +0800 Subject: [PATCH 04/11] fix: modified the comments Signed-off-by: guanwenbo --- config/options.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/options.go b/config/options.go index a5065072..a2b9cbc2 100644 --- a/config/options.go +++ b/config/options.go @@ -148,7 +148,7 @@ func Extractor(format interface{}, extractor jaeger.Extractor) Option { } } -// WithRandomNumber set the Tracer random number func +// 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 From ede9819254298c04b2108109604892eb56bb39b5 Mon Sep 17 00:00:00 2001 From: guanwenbo Date: Fri, 18 Dec 2020 20:08:57 +0800 Subject: [PATCH 05/11] feat: costume tracer randomNumber func on config.New() called Signed-off-by: guanwenbo --- config/config.go | 4 ++++ config/options.go | 8 ++++++++ 2 files changed, 12 insertions(+) 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/options.go b/config/options.go index e0e50e83..27c23777 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 { } } +// WithRandonNunmber set the Tracer random number func +func WithRandonNunmber(f func() uint64) Option { + return func(c *Options) { + c.randomNumber = f + } +} + func applyOptions(options ...Option) Options { opts := Options{ injectors: make(map[interface{}]jaeger.Injector), From 57aeb6a34853c1f9022b535bf60f5e03e84e5dd5 Mon Sep 17 00:00:00 2001 From: guanwenbo Date: Mon, 21 Dec 2020 10:05:40 +0800 Subject: [PATCH 06/11] fix: modified the bad spelling and added unit test Signed-off-by: guanwenbo --- config/config_test.go | 10 ++++++++++ config/options.go | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/config/config_test.go b/config/config_test.go index adb40dbd..a2eebd1e 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -899,3 +899,13 @@ func TestThrottlerDefaultConfig(t *testing.T) { assert.NoError(t, err) defer closeCloser(t, closer) } + +func TestWithRandomNumber(t *testing.T) { + cfg := &Configuration{ + ServiceName: "test-random-number", + } + randomNum := func() uint64 { return 1 } + _, closer, err := cfg.NewTracer(WithRandomNunmber(randomNum)) + assert.NoError(t, err) + defer closeCloser(t, closer) +} diff --git a/config/options.go b/config/options.go index 27c23777..7fcd2414 100644 --- a/config/options.go +++ b/config/options.go @@ -148,8 +148,8 @@ func Extractor(format interface{}, extractor jaeger.Extractor) Option { } } -// WithRandonNunmber set the Tracer random number func -func WithRandonNunmber(f func() uint64) Option { +// WithRandomNunmber set the Tracer random number func +func WithRandomNunmber(f func() uint64) Option { return func(c *Options) { c.randomNumber = f } From 8020a658a84d48cbcd097169dedbd72c652030af Mon Sep 17 00:00:00 2001 From: guanwenbo Date: Mon, 21 Dec 2020 14:25:00 +0800 Subject: [PATCH 07/11] fix: modified bad spelling Signed-off-by: guanwenbo --- config/config_test.go | 2 +- config/options.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/config/config_test.go b/config/config_test.go index a2eebd1e..b69bfc0a 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -905,7 +905,7 @@ func TestWithRandomNumber(t *testing.T) { ServiceName: "test-random-number", } randomNum := func() uint64 { return 1 } - _, closer, err := cfg.NewTracer(WithRandomNunmber(randomNum)) + _, closer, err := cfg.NewTracer(WithRandomNumber(randomNum)) assert.NoError(t, err) defer closeCloser(t, closer) } diff --git a/config/options.go b/config/options.go index 7fcd2414..a5065072 100644 --- a/config/options.go +++ b/config/options.go @@ -148,8 +148,8 @@ func Extractor(format interface{}, extractor jaeger.Extractor) Option { } } -// WithRandomNunmber set the Tracer random number func -func WithRandomNunmber(f func() uint64) Option { +// WithRandomNumber set the Tracer random number func +func WithRandomNumber(f func() uint64) Option { return func(c *Options) { c.randomNumber = f } From 8e592e8b289c7514b43cd650af179a69e12c3116 Mon Sep 17 00:00:00 2001 From: guanwenbo Date: Mon, 21 Dec 2020 14:27:35 +0800 Subject: [PATCH 08/11] fix: modified the comments Signed-off-by: guanwenbo --- config/options.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/options.go b/config/options.go index a5065072..a2b9cbc2 100644 --- a/config/options.go +++ b/config/options.go @@ -148,7 +148,7 @@ func Extractor(format interface{}, extractor jaeger.Extractor) Option { } } -// WithRandomNumber set the Tracer random number func +// 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 From 64f845f2a46b184fa2ee3caa340709155850affa Mon Sep 17 00:00:00 2001 From: guanwenbo Date: Wed, 23 Dec 2020 10:32:30 +0800 Subject: [PATCH 09/11] fix: test the creating of traceID with customer randomNum func Signed-off-by: guanwenbo --- config/config_test.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/config/config_test.go b/config/config_test.go index b69bfc0a..222e108e 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -901,11 +901,17 @@ func TestThrottlerDefaultConfig(t *testing.T) { } func TestWithRandomNumber(t *testing.T) { + const traceID uint64 = 1 + cfg := &Configuration{ ServiceName: "test-random-number", } - randomNum := func() uint64 { return 1 } - _, closer, err := cfg.NewTracer(WithRandomNumber(randomNum)) + 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, spanCtx.TraceID().Low ,traceID) defer closeCloser(t, closer) } From 31a03b90d8202d8e5f2fb271ae51a825edac5335 Mon Sep 17 00:00:00 2001 From: guanwenbo Date: Wed, 23 Dec 2020 10:51:41 +0800 Subject: [PATCH 10/11] fix: lint fmt Signed-off-by: guanwenbo --- config/config_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/config_test.go b/config/config_test.go index 222e108e..4adfff7c 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -908,10 +908,10 @@ func TestWithRandomNumber(t *testing.T) { } randomNum := func() uint64 { return traceID } tracer, closer, err := cfg.NewTracer(WithRandomNumber(randomNum)) - span:=tracer.StartSpan("test-span") + span := tracer.StartSpan("test-span") spanCtx := span.Context().(jaeger.SpanContext) assert.NoError(t, err) - assert.Equal(t, spanCtx.TraceID().Low ,traceID) + assert.Equal(t, spanCtx.TraceID().Low, traceID) defer closeCloser(t, closer) } From 4e06150f029c046ecd556884ffde688e0c1d24c6 Mon Sep 17 00:00:00 2001 From: guanwenbo Date: Wed, 23 Dec 2020 18:41:50 +0800 Subject: [PATCH 11/11] fix: wrong params order Signed-off-by: guanwenbo --- config/config_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config_test.go b/config/config_test.go index 4adfff7c..c6b072fb 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -912,6 +912,6 @@ func TestWithRandomNumber(t *testing.T) { spanCtx := span.Context().(jaeger.SpanContext) assert.NoError(t, err) - assert.Equal(t, spanCtx.TraceID().Low, traceID) + assert.Equal(t, traceID, spanCtx.TraceID().Low) defer closeCloser(t, closer) }