Skip to content

Commit d2877fb

Browse files
authored
Merge pull request #149 from basvanbeek/master
improved propagation testing
2 parents b513d42 + 1e4e7d5 commit d2877fb

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed

propagation_test.go

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package zipkintracer_test
1616

1717
import (
1818
stdHTTP "net/http"
19+
"reflect"
1920
"testing"
2021

2122
"github.com/opentracing/opentracing-go"
@@ -24,6 +25,7 @@ import (
2425
"github.com/openzipkin/zipkin-go/model"
2526
zb3 "github.com/openzipkin/zipkin-go/propagation/b3"
2627
"github.com/openzipkin/zipkin-go/reporter"
28+
"google.golang.org/grpc/metadata"
2729
)
2830

2931
func TestHTTPExtractFlagsOnly(t *testing.T) {
@@ -325,3 +327,200 @@ func TestHTTPInjectSampledAndDebugTrace(t *testing.T) {
325327
t.Errorf("Debug want %s, have %s", want, have)
326328
}
327329
}
330+
331+
func TestTextMapCarrier(t *testing.T) {
332+
for injectOption := zipkintracer.B3InjectStandard; injectOption <= zipkintracer.B3InjectBoth; injectOption++ {
333+
tracer := zipkintracer.Wrap(nil, zipkintracer.WithB3InjectOption(injectOption))
334+
335+
otMap := make(opentracing.TextMapCarrier)
336+
337+
sampled := true
338+
parentID := model.ID(1)
339+
sc := zipkintracer.SpanContext{
340+
TraceID: model.TraceID{Low: 1},
341+
ID: model.ID(2),
342+
ParentID: &parentID,
343+
Sampled: &sampled,
344+
}
345+
346+
tracer.Inject(sc, opentracing.TextMap, otMap)
347+
348+
stdMap := make(map[string]string)
349+
350+
otMap.ForeachKey(func(key string, val string) error {
351+
stdMap[key] = val
352+
return nil
353+
})
354+
355+
otSC, err := tracer.Extract(opentracing.TextMap, otMap)
356+
357+
if err != nil {
358+
t.Errorf("[%d] Unexpected Extract failure %v", injectOption, err)
359+
}
360+
361+
sc2, ok := otSC.(zipkintracer.SpanContext)
362+
if !ok {
363+
t.Errorf("[%d] Expected valid SpanContext, got %+v", injectOption, otSC)
364+
}
365+
366+
if want, have := sc, sc2; !reflect.DeepEqual(want, have) {
367+
t.Errorf("[%d] SpanContext\nwant: %+v,\nhave: %+v", injectOption, want, have)
368+
}
369+
}
370+
}
371+
372+
func TestHTTPHeadersCarrier(t *testing.T) {
373+
for injectOption := zipkintracer.B3InjectStandard; injectOption <= zipkintracer.B3InjectBoth; injectOption++ {
374+
tracer := zipkintracer.Wrap(nil, zipkintracer.WithB3InjectOption(injectOption))
375+
376+
otHTTPHeaders := make(opentracing.HTTPHeadersCarrier)
377+
378+
sampled := true
379+
parentID := model.ID(1)
380+
sc := zipkintracer.SpanContext{
381+
TraceID: model.TraceID{Low: 1},
382+
ID: model.ID(2),
383+
ParentID: &parentID,
384+
Sampled: &sampled,
385+
}
386+
387+
tracer.Inject(sc, opentracing.TextMap, otHTTPHeaders)
388+
389+
stdMap := make(map[string]string)
390+
391+
otHTTPHeaders.ForeachKey(func(key string, val string) error {
392+
stdMap[key] = val
393+
return nil
394+
})
395+
396+
otSC, err := tracer.Extract(opentracing.TextMap, otHTTPHeaders)
397+
398+
if err != nil {
399+
t.Errorf("[%d] Unexpected Extract failure %v", injectOption, err)
400+
}
401+
402+
sc2, ok := otSC.(zipkintracer.SpanContext)
403+
if !ok {
404+
t.Errorf("[%d] Expected valid SpanContext, got %+v", injectOption, otSC)
405+
}
406+
407+
if want, have := sc, sc2; !reflect.DeepEqual(want, have) {
408+
t.Errorf("[%d] SpanContext\nwant: %+v,\nhave: %+v", injectOption, want, have)
409+
}
410+
}
411+
}
412+
413+
func TestNativeCarrier(t *testing.T) {
414+
for injectOption := zipkintracer.B3InjectStandard; injectOption <= zipkintracer.B3InjectBoth; injectOption++ {
415+
tracer := zipkintracer.Wrap(nil, zipkintracer.WithB3InjectOption(injectOption))
416+
417+
sampled := true
418+
parentID := model.ID(1)
419+
sc := zipkintracer.SpanContext{
420+
TraceID: model.TraceID{Low: 1},
421+
ID: model.ID(2),
422+
ParentID: &parentID,
423+
Sampled: &sampled,
424+
}
425+
426+
nativeMD := metadata.MD{}
427+
428+
tracer.Inject(sc, opentracing.TextMap, zb3.InjectGRPC(&nativeMD))
429+
430+
otSC, err := tracer.Extract(opentracing.TextMap, zb3.ExtractGRPC(&nativeMD))
431+
432+
if err != nil {
433+
t.Errorf("[%d] Unexpected Extract failure %v", injectOption, err)
434+
}
435+
436+
sc2, ok := otSC.(zipkintracer.SpanContext)
437+
if !ok {
438+
t.Errorf("[%d] Expected valid SpanContext, got %+v", injectOption, otSC)
439+
}
440+
441+
if want, have := sc, sc2; !reflect.DeepEqual(want, have) {
442+
t.Errorf("[%d] SpanContext\nwant: %+v,\nhave: %+v", injectOption, want, have)
443+
}
444+
}
445+
}
446+
447+
func TestBinaryFallbackPropagator(t *testing.T) {
448+
for injectOption := zipkintracer.B3InjectStandard; injectOption <= zipkintracer.B3InjectBoth; injectOption++ {
449+
tracer := zipkintracer.Wrap(nil, zipkintracer.WithB3InjectOption(injectOption))
450+
451+
otMap := make(opentracing.TextMapCarrier)
452+
453+
sampled := true
454+
parentID := model.ID(1)
455+
sc := zipkintracer.SpanContext{
456+
TraceID: model.TraceID{Low: 1},
457+
ID: model.ID(2),
458+
ParentID: &parentID,
459+
Sampled: &sampled,
460+
}
461+
462+
tracer.Inject(sc, opentracing.Binary, otMap)
463+
464+
otSC, err := tracer.Extract(opentracing.Binary, otMap)
465+
466+
if err != nil {
467+
t.Errorf("[%d] Unexpected Extract failure %v", injectOption, err)
468+
}
469+
470+
sc2, ok := otSC.(zipkintracer.SpanContext)
471+
if !ok {
472+
t.Errorf("[%d] Expected valid SpanContext, got %+v", injectOption, otSC)
473+
}
474+
475+
if want, have := sc, sc2; !reflect.DeepEqual(want, have) {
476+
t.Errorf("[%d] SpanContext\nwant: %+v,\nhave: %+v", injectOption, want, have)
477+
}
478+
}
479+
}
480+
481+
type customPropagator model.SpanContext
482+
483+
var _ zipkintracer.DelegatingCarrier = &customPropagator{}
484+
485+
func (c *customPropagator) SetState(sc model.SpanContext) error {
486+
*c = customPropagator(sc)
487+
return nil
488+
}
489+
func (c *customPropagator) State() (model.SpanContext, error) {
490+
return model.SpanContext(*c), nil
491+
}
492+
493+
func TestAccessorPropagator(t *testing.T) {
494+
495+
for injectOption := zipkintracer.B3InjectStandard; injectOption <= zipkintracer.B3InjectBoth; injectOption++ {
496+
tracer := zipkintracer.Wrap(nil, zipkintracer.WithB3InjectOption(injectOption))
497+
498+
otCustom := &customPropagator{}
499+
500+
sampled := true
501+
parentID := model.ID(1)
502+
sc := zipkintracer.SpanContext{
503+
TraceID: model.TraceID{Low: 1},
504+
ID: model.ID(2),
505+
ParentID: &parentID,
506+
Sampled: &sampled,
507+
}
508+
509+
tracer.Inject(sc, zipkintracer.Delegator, otCustom)
510+
511+
otSC, err := tracer.Extract(zipkintracer.Delegator, otCustom)
512+
513+
if err != nil {
514+
t.Errorf("[%d] Unexpected Extract failure %v", injectOption, err)
515+
}
516+
517+
sc2, ok := otSC.(zipkintracer.SpanContext)
518+
if !ok {
519+
t.Errorf("[%d] Expected valid SpanContext, got %+v", injectOption, otSC)
520+
}
521+
522+
if want, have := sc, sc2; !reflect.DeepEqual(want, have) {
523+
t.Errorf("[%d] SpanContext\nwant: %+v,\nhave: %+v", injectOption, want, have)
524+
}
525+
}
526+
}

0 commit comments

Comments
 (0)