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

Commit

Permalink
Add Configuration support for custom Injector and Extractor
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Liu <[email protected]>
  • Loading branch information
martinxsliu committed Feb 12, 2018
1 parent f6dd779 commit d48354e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
11 changes: 10 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ func (c Configuration) New(
tracerOptions = append(tracerOptions, jaeger.TracerOptions.ContribObserver(cobs))
}

for format, injector := range opts.injectors {
tracerOptions = append(tracerOptions, jaeger.TracerOptions.Injector(format, injector))
}

for format, extractor := range opts.extractors {
tracerOptions = append(tracerOptions, jaeger.TracerOptions.Extractor(format, extractor))
}

if c.BaggageRestrictions != nil {
mgr := remote.NewRestrictionManager(
serviceName,
Expand All @@ -193,7 +201,8 @@ func (c Configuration) New(
serviceName,
sampler,
reporter,
tracerOptions...)
tracerOptions...,
)

return tracer, closer, nil
}
Expand Down
29 changes: 29 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,32 @@ func TestConfigWithGen128Bit(t *testing.T) {
require.True(t, traceID.High != 0)
require.True(t, traceID.Low != 0)
}

func TestConfigWithInjector(t *testing.T) {
c := Configuration{}
tracer, closer, err := c.New("test", Injector("custom.format", fakeInjector{}))
require.NoError(t, err)
defer closer.Close()

span := tracer.StartSpan("test")
defer span.Finish()

err = tracer.Inject(span.Context(), "unknown.format", nil)
require.Error(t, err)

err = tracer.Inject(span.Context(), "custom.format", nil)
require.NoError(t, err)
}

func TestConfigWithExtractor(t *testing.T) {
c := Configuration{}
tracer, closer, err := c.New("test", Extractor("custom.format", fakeExtractor{}))
require.NoError(t, err)
defer closer.Close()

_, err = tracer.Extract("unknown.format", nil)
require.Error(t, err)

_, err = tracer.Extract("custom.format", nil)
require.NoError(t, err)
}
21 changes: 20 additions & 1 deletion config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type Options struct {
gen128Bit bool
zipkinSharedRPCSpan bool
tags []opentracing.Tag
injectors map[interface{}]jaeger.Injector
extractors map[interface{}]jaeger.Extractor
}

// Metrics creates an Option that initializes Metrics in the tracer,
Expand Down Expand Up @@ -98,8 +100,25 @@ func Tag(key string, value interface{}) Option {
}
}

// Injector registers an Injector with the given format.
func Injector(format interface{}, injector jaeger.Injector) Option {
return func(c *Options) {
c.injectors[format] = injector
}
}

// Extractor registers an Extractor with the given format.
func Extractor(format interface{}, extractor jaeger.Extractor) Option {
return func(c *Options) {
c.extractors[format] = extractor
}
}

func applyOptions(options ...Option) Options {
opts := Options{}
opts := Options{
injectors: make(map[interface{}]jaeger.Injector),
extractors: make(map[interface{}]jaeger.Extractor),
}
for _, option := range options {
option(&opts)
}
Expand Down
16 changes: 14 additions & 2 deletions config/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,24 @@ func TestApplyOptionsDefaults(t *testing.T) {

type fakeObserver struct{}

func (o fakeObserver) OnStartSpan(operationName string, options opentracing.StartSpanOptions) jaeger.SpanObserver {
func (fakeObserver) OnStartSpan(operationName string, options opentracing.StartSpanOptions) jaeger.SpanObserver {
return nil
}

type fakeContribObserver struct{}

func (o fakeContribObserver) OnStartSpan(span opentracing.Span, operationName string, options opentracing.StartSpanOptions) (jaeger.ContribSpanObserver, bool) {
func (fakeContribObserver) OnStartSpan(span opentracing.Span, operationName string, options opentracing.StartSpanOptions) (jaeger.ContribSpanObserver, bool) {
return nil, false
}

type fakeInjector struct{}

func (fakeInjector) Inject(ctx jaeger.SpanContext, carrier interface{}) error {
return nil
}

type fakeExtractor struct{}

func (fakeExtractor) Extract(carrier interface{}) (jaeger.SpanContext, error) {
return jaeger.SpanContext{}, nil
}

0 comments on commit d48354e

Please sign in to comment.