forked from jaegertracing/jaeger-client-go
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support pluggable injectors/extrators; implement Zipkin propagator (j…
- Loading branch information
1 parent
9ed65b0
commit fbffee0
Showing
5 changed files
with
217 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package jaeger | ||
|
||
import ( | ||
"github.com/opentracing/opentracing-go" | ||
) | ||
|
||
// ZipkinSpanFormat is an OpenTracing carrier format constant | ||
const ZipkinSpanFormat = "zipkin-span-format" | ||
|
||
// ZipkinSpan is a type of Carrier used for integration with Zipkin-aware RPC frameworks | ||
// (like TChannel). It does not support baggage, only trace IDs. | ||
type ZipkinSpan interface { | ||
TraceID() uint64 | ||
SpanID() uint64 | ||
ParentID() uint64 | ||
Flags() byte | ||
SetTraceID(traceID uint64) | ||
SetSpanID(spanID uint64) | ||
SetParentID(parentID uint64) | ||
SetFlags(flags byte) | ||
} | ||
|
||
type zipkinPropagator struct { | ||
tracer *tracer | ||
} | ||
|
||
func (p *zipkinPropagator) InjectSpan( | ||
sp opentracing.Span, | ||
abstractCarrier interface{}, | ||
) error { | ||
sc, ok := sp.(*span) | ||
if !ok { | ||
return opentracing.ErrInvalidSpan | ||
} | ||
carrier, ok := abstractCarrier.(ZipkinSpan) | ||
if !ok { | ||
return opentracing.ErrInvalidCarrier | ||
} | ||
|
||
sc.RLock() | ||
defer sc.RUnlock() | ||
|
||
carrier.SetTraceID(sc.TraceContext.TraceID()) | ||
carrier.SetSpanID(sc.TraceContext.SpanID()) | ||
carrier.SetParentID(sc.TraceContext.ParentID()) | ||
carrier.SetFlags(sc.TraceContext.flags) | ||
|
||
return nil | ||
} | ||
|
||
func (p *zipkinPropagator) Join( | ||
operationName string, | ||
abstractCarrier interface{}, | ||
) (opentracing.Span, error) { | ||
carrier, ok := abstractCarrier.(ZipkinSpan) | ||
if !ok { | ||
return nil, opentracing.ErrInvalidCarrier | ||
} | ||
if carrier.TraceID() == 0 { | ||
return nil, opentracing.ErrTraceNotFound | ||
} | ||
sp := p.tracer.newSpan() | ||
sp.TraceContext.traceID = carrier.TraceID() | ||
sp.TraceContext.spanID = carrier.SpanID() | ||
sp.TraceContext.parentID = carrier.ParentID() | ||
sp.TraceContext.flags = carrier.Flags() | ||
|
||
return p.tracer.startSpanInternal( | ||
sp, | ||
operationName, | ||
p.tracer.timeNow(), | ||
nil, | ||
true, // join with external trace | ||
), nil | ||
} |
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 jaeger | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestZipkinPropagator(t *testing.T) { | ||
tracer, tCloser := NewTracer("x", NewConstSampler(true), NewNullReporter()) | ||
defer tCloser.Close() | ||
|
||
carrier := &TestZipkinSpan{} | ||
sp := tracer.StartSpan("y") | ||
|
||
// Note: we intentionally use string as format, as that's what TChannel would need to do | ||
if err := tracer.Inject(sp, "zipkin-span-format", carrier); err != nil { | ||
t.Fatalf("Inject failed: %+v", err) | ||
} | ||
sp1 := sp.(*span) | ||
assert.Equal(t, sp1.traceID, carrier.traceID) | ||
assert.Equal(t, sp1.spanID, carrier.spanID) | ||
assert.Equal(t, sp1.parentID, carrier.parentID) | ||
assert.Equal(t, sp1.flags, carrier.flags) | ||
|
||
sp2, err := tracer.Join("z", "zipkin-span-format", carrier) | ||
if err != nil { | ||
t.Fatalf("Extract failed: %+v", err) | ||
} | ||
sp3 := sp2.(*span) | ||
assert.Equal(t, sp1.traceID, sp3.traceID) | ||
assert.Equal(t, sp1.spanID, sp3.spanID) | ||
assert.Equal(t, sp1.parentID, sp3.parentID) | ||
assert.Equal(t, sp1.flags, sp3.flags) | ||
} | ||
|
||
// TestZipkinSpan is a mock-up of TChannel's internal Span struct | ||
type TestZipkinSpan struct { | ||
traceID uint64 | ||
parentID uint64 | ||
spanID uint64 | ||
flags byte | ||
} | ||
|
||
func (s TestZipkinSpan) TraceID() uint64 { return s.traceID } | ||
func (s TestZipkinSpan) ParentID() uint64 { return s.parentID } | ||
func (s TestZipkinSpan) SpanID() uint64 { return s.spanID } | ||
func (s TestZipkinSpan) Flags() byte { return s.flags } | ||
func (s *TestZipkinSpan) SetTraceID(traceID uint64) { s.traceID = traceID } | ||
func (s *TestZipkinSpan) SetSpanID(spanID uint64) { s.spanID = spanID } | ||
func (s *TestZipkinSpan) SetParentID(parentID uint64) { s.parentID = parentID } | ||
func (s *TestZipkinSpan) SetFlags(flags byte) { s.flags = flags } |