-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use map instead of string to configure propagators (#239)
* Use map instead of string to configure propagators * Adding PropagatorStack
- Loading branch information
alrex
authored
Nov 18, 2019
1 parent
a411674
commit eb40dd3
Showing
8 changed files
with
209 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package lightstep | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/opentracing/opentracing-go" | ||
) | ||
|
||
// PropagatorStack provides a Propagator interface that supports | ||
// multiple propagators per format. | ||
type PropagatorStack struct { | ||
propagators []Propagator | ||
} | ||
|
||
// PushPropagator adds a Propagator to a list of configured propagators | ||
func (stack *PropagatorStack) PushPropagator(p Propagator) { | ||
stack.propagators = append(stack.propagators, p) | ||
} | ||
|
||
// Inject iterates through configured propagators and calls | ||
// their Inject functions | ||
func (stack PropagatorStack) Inject( | ||
spanContext opentracing.SpanContext, | ||
opaqueCarrier interface{}, | ||
) error { | ||
if len(stack.propagators) == 0 { | ||
return errors.New("No valid propagator configured") | ||
} | ||
for _, propagator := range stack.propagators { | ||
propagator.Inject(spanContext, opaqueCarrier) | ||
} | ||
return nil | ||
} | ||
|
||
// Extract iterates through configured propagators and | ||
// returns the first successfully extracted context | ||
func (stack PropagatorStack) Extract( | ||
opaqueCarrier interface{}, | ||
) (opentracing.SpanContext, error) { | ||
if len(stack.propagators) == 0 { | ||
return nil, errors.New("No valid propagator configured") | ||
} | ||
|
||
for _, propagator := range stack.propagators { | ||
context, err := propagator.Extract(opaqueCarrier) | ||
if err == nil { | ||
return context, nil | ||
} | ||
} | ||
|
||
return nil, errors.New("No valid propagator configured") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package lightstep_test | ||
|
||
import ( | ||
. "github.com/lightstep/lightstep-tracer-go" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"github.com/opentracing/opentracing-go" | ||
) | ||
|
||
var _ = Describe("Propagator Stack", func() { | ||
var stack PropagatorStack | ||
Context("With no propagators", func() { | ||
var knownCarrier1 opentracing.TextMapCarrier | ||
|
||
var knownContext1 = SpanContext{ | ||
SpanID: 6397081719746291766, | ||
TraceID: 506100417967962170, | ||
Baggage: map[string]string{"checked": "baggage"}, | ||
} | ||
BeforeEach(func() { | ||
stack = PropagatorStack{} | ||
}) | ||
|
||
It("should return error on inject", func() { | ||
err := stack.Inject(knownContext1, knownCarrier1) | ||
Expect(err).ToNot(BeNil()) | ||
}) | ||
|
||
It("should return error on extract", func() { | ||
_, err := stack.Extract(knownCarrier1) | ||
Expect(err).ToNot(BeNil()) | ||
}) | ||
}) | ||
Context("With one propagator", func() { | ||
var knownCarrier1 opentracing.TextMapCarrier | ||
|
||
var knownContext1 = SpanContext{ | ||
SpanID: 6397081719746291766, | ||
TraceID: 506100417967962170, | ||
Baggage: map[string]string{"checked": "baggage"}, | ||
} | ||
BeforeEach(func() { | ||
knownCarrier1 = opentracing.TextMapCarrier{} | ||
stack = PropagatorStack{} | ||
stack.PushPropagator(LightStepPropagator) | ||
}) | ||
|
||
It("should inject trace", func() { | ||
err := stack.Inject(knownContext1, knownCarrier1) | ||
Expect(err).To(BeNil()) | ||
Expect(len(knownCarrier1)).To(Equal(4)) | ||
Expect(knownCarrier1["ot-tracer-traceid"]).To(Equal("70607a611a8383a")) | ||
Expect(knownCarrier1["ot-tracer-spanid"]).To(Equal("58c6ffee509f6836")) | ||
Expect(knownCarrier1["ot-tracer-sampled"]).To(Equal("true")) | ||
Expect(knownCarrier1["ot-baggage-checked"]).To(Equal("baggage")) | ||
}) | ||
|
||
It("should extract trace", func() { | ||
knownCarrier2 := opentracing.TextMapCarrier{ | ||
"ot-tracer-traceid": "70607a611a8383a", | ||
"ot-tracer-spanid": "58c6ffee509f6836", | ||
"ot-tracer-sampled": "true", | ||
"ot-baggage-checked": "baggage", | ||
} | ||
ctx, err := stack.Extract(knownCarrier2) | ||
Expect(err).To(BeNil()) | ||
// check if spancontext is correct | ||
spanContext, _ := ctx.(SpanContext) | ||
|
||
Expect(spanContext.TraceID).To(Equal(knownContext1.TraceID)) | ||
Expect(spanContext.SpanID).To(Equal(knownContext1.SpanID)) | ||
Expect(spanContext.Baggage).To(Equal(knownContext1.Baggage)) | ||
}) | ||
}) | ||
Context("With multiple propagator", func() { | ||
var knownCarrier1 opentracing.TextMapCarrier | ||
|
||
var knownContext1 = SpanContext{ | ||
SpanID: 6397081719746291766, | ||
TraceID: 506100417967962170, | ||
Baggage: map[string]string{"checked": "baggage"}, | ||
} | ||
BeforeEach(func() { | ||
knownCarrier1 = opentracing.TextMapCarrier{} | ||
stack = PropagatorStack{} | ||
stack.PushPropagator(LightStepPropagator) | ||
stack.PushPropagator(B3Propagator) | ||
}) | ||
|
||
It("should inject trace using both propagators", func() { | ||
err := stack.Inject(knownContext1, knownCarrier1) | ||
Expect(err).To(BeNil()) | ||
Expect(len(knownCarrier1)).To(Equal(7)) | ||
|
||
Expect(knownCarrier1["ot-tracer-traceid"]).To(Equal("70607a611a8383a")) | ||
Expect(knownCarrier1["ot-tracer-spanid"]).To(Equal("58c6ffee509f6836")) | ||
Expect(knownCarrier1["ot-tracer-sampled"]).To(Equal("true")) | ||
Expect(knownCarrier1["x-b3-traceid"]).To(Equal("70607a611a8383a")) | ||
Expect(knownCarrier1["x-b3-spanid"]).To(Equal("58c6ffee509f6836")) | ||
Expect(knownCarrier1["x-b3-sampled"]).To(Equal("1")) | ||
Expect(knownCarrier1["ot-baggage-checked"]).To(Equal("baggage")) | ||
}) | ||
|
||
It("should extract trace", func() { | ||
knownCarrier2 := opentracing.TextMapCarrier{ | ||
"ot-tracer-traceid": "70607a611a8383a", | ||
"ot-tracer-spanid": "58c6ffee509f6836", | ||
"ot-tracer-sampled": "true", | ||
"ot-baggage-checked": "baggage", | ||
} | ||
ctx, err := stack.Extract(knownCarrier2) | ||
Expect(err).To(BeNil()) | ||
spanContext, _ := ctx.(SpanContext) | ||
|
||
Expect(spanContext.TraceID).To(Equal(knownContext1.TraceID)) | ||
Expect(spanContext.SpanID).To(Equal(knownContext1.SpanID)) | ||
Expect(spanContext.Baggage).To(Equal(knownContext1.Baggage)) | ||
|
||
knownCarrier3 := opentracing.TextMapCarrier{ | ||
"x-b3-traceid": "70607a611a8383a", | ||
"x-b3-spanid": "58c6ffee509f6836", | ||
"x-b3-sampled": "true", | ||
"ot-baggage-checked": "baggage", | ||
} | ||
ctx, err = stack.Extract(knownCarrier3) | ||
Expect(err).To(BeNil()) | ||
spanContext, _ = ctx.(SpanContext) | ||
|
||
Expect(spanContext.TraceID).To(Equal(knownContext1.TraceID)) | ||
Expect(spanContext.SpanID).To(Equal(knownContext1.SpanID)) | ||
Expect(spanContext.Baggage).To(Equal(knownContext1.Baggage)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters