Skip to content

Commit

Permalink
Make Span type public to allow access to non-std methods for testing (j…
Browse files Browse the repository at this point in the history
…aegertracing#117)

Make Span public to allow access to non-std methods for testing
For example, allow access to span.OperationName(), introduced in the same PR
  • Loading branch information
yurishkuro authored Mar 20, 2017
1 parent 709b40e commit bf2a774
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 74 deletions.
4 changes: 2 additions & 2 deletions observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestEmptyObserver(t *testing.T) {
defer closer.Close()
s := tracer.StartSpan("test", ext.RPCServerOption(nil))
s.Finish()
assert.Equal(t, s.(*span).observer, noopSpanObserver)
assert.Equal(t, s.(*Span).observer, noopSpanObserver)
}

func TestObservers(t *testing.T) {
Expand All @@ -49,7 +49,7 @@ func TestObservers(t *testing.T) {
s := tracer.StartSpan("test", ext.RPCServerOption(nil))

forEachObs := func(f func(so *testSpanObserver)) {
observers := s.(*span).observer.(spanObserver).observers
observers := s.(*Span).observer.(spanObserver).observers
assert.Len(t, observers, 2)
for _, so := range observers {
f(so.(*testSpanObserver))
Expand Down
12 changes: 6 additions & 6 deletions propagation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ func TestSpanPropagator(t *testing.T) {
otSpans := reporter.GetSpans()
require.Equal(t, len(tests)+1, len(otSpans), "unexpected number of spans reporter")

spans := make([]*span, len(otSpans))
spans := make([]*Span, len(otSpans))
for i, s := range otSpans {
spans[i] = s.(*span)
spans[i] = s.(*Span)
}

// The last span is the original one.
Expand Down Expand Up @@ -158,9 +158,9 @@ func TestBaggagePropagationHTTP(t *testing.T) {

sp1 := tracer.StartSpan("s1")
sp1.SetBaggageItem("Some_Key", "12345")
assert.Equal(t, "12345", sp1.BaggageItem("some-KEY"), "baggage: %+v", sp1.(*span).context.baggage)
assert.Equal(t, "12345", sp1.BaggageItem("some-KEY"), "baggage: %+v", sp1.(*Span).context.baggage)
sp1.SetBaggageItem("Some_Key", "98:765") // colon : should be escaped as %3A
assert.Equal(t, "98:765", sp1.BaggageItem("some-KEY"), "baggage: %+v", sp1.(*span).context.baggage)
assert.Equal(t, "98:765", sp1.BaggageItem("some-KEY"), "baggage: %+v", sp1.(*Span).context.baggage)

h := http.Header{}
h.Add("header1", "value1") // make sure this does not get unmarshalled as baggage
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestJaegerBaggageHeader(t *testing.T) {
ctx, err := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(h))
require.NoError(t, err)

sp := tracer.StartSpan("root", opentracing.ChildOf(ctx)).(*span)
sp := tracer.StartSpan("root", opentracing.ChildOf(ctx)).(*Span)

assert.Equal(t, "value1", sp.BaggageItem("key1"))
assert.Equal(t, "value two", sp.BaggageItem("key 2"))
Expand Down Expand Up @@ -238,7 +238,7 @@ func TestDebugCorrelationID(t *testing.T) {
require.NoError(t, err)
assert.EqualValues(t, 0, ctx.(SpanContext).parentID)
assert.EqualValues(t, "value1", ctx.(SpanContext).debugID)
sp := tracer.StartSpan("root", opentracing.ChildOf(ctx)).(*span)
sp := tracer.StartSpan("root", opentracing.ChildOf(ctx)).(*Span)
assert.EqualValues(t, 0, sp.context.parentID)
assert.True(t, sp.context.traceID.IsValid())
assert.True(t, sp.context.IsSampled())
Expand Down
12 changes: 6 additions & 6 deletions reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
// Reporter is called by the tracer when a span is completed to report the span to the tracing collector.
type Reporter interface {
// Report submits a new span to collectors, possibly asynchronously and/or with buffering.
Report(span *span)
Report(span *Span)

// Close does a clean shutdown of the reporter, flushing any traces that may be buffered in memory.
Close()
Expand All @@ -51,7 +51,7 @@ func NewNullReporter() Reporter {
}

// Report implements Report() method of Reporter by doing nothing.
func (r *nullReporter) Report(span *span) {
func (r *nullReporter) Report(span *Span) {
// no-op
}

Expand All @@ -72,7 +72,7 @@ func NewLoggingReporter(logger Logger) Reporter {
}

// Report implements Report() method of Reporter by logging the span to the logger.
func (r *loggingReporter) Report(span *span) {
func (r *loggingReporter) Report(span *Span) {
r.logger.Infof("Reporting span %+v", span)
}

Expand All @@ -98,7 +98,7 @@ func NewInMemoryReporter() *InMemoryReporter {
}

// Report implements Report() method of Reporter by storing the span in the buffer.
func (r *InMemoryReporter) Report(span *span) {
func (r *InMemoryReporter) Report(span *Span) {
r.lock.Lock()
r.spans = append(r.spans, span)
r.lock.Unlock()
Expand Down Expand Up @@ -144,7 +144,7 @@ func NewCompositeReporter(reporters ...Reporter) Reporter {
}

// Report implements Report() method of Reporter by delegating to each underlying reporter.
func (r *compositeReporter) Report(span *span) {
func (r *compositeReporter) Report(span *Span) {
for _, reporter := range r.reporters {
reporter.Report(span)
}
Expand Down Expand Up @@ -203,7 +203,7 @@ func NewRemoteReporter(sender transport.Transport, opts ...ReporterOption) Repor

// Report implements Report() method of Reporter.
// It passes the span to a background go-routine for submission to Jaeger.
func (r *remoteReporter) Report(span *span) {
func (r *remoteReporter) Report(span *Span) {
thriftSpan := buildThriftSpan(span)
select {
case r.queue <- thriftSpan:
Expand Down
4 changes: 2 additions & 2 deletions reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (s *reporterSuite) TestClientSpanAnnotations() {
s.flushReporter()
s.Equal(2, len(s.collector.Spans()))
zSpan := s.collector.Spans()[0] // child span is reported first
s.EqualValues(zSpan.ID, sp2.(*span).context.spanID)
s.EqualValues(zSpan.ID, sp2.(*Span).context.spanID)
s.Equal(2, len(zSpan.Annotations), "expecting two annotations, cs and cr")
s.Equal(1, len(zSpan.BinaryAnnotations), "expecting one binary annotation sa")
s.NotNil(findAnnotation(zSpan, "cs"), "expecting cs annotation")
Expand Down Expand Up @@ -256,7 +256,7 @@ func (s *reporterSuite) TestMemoryReporterReport() {
sp := s.tracer.StartSpan("leela")
ext.PeerService.Set(sp, s.serviceName)
reporter := NewInMemoryReporter()
reporter.Report(sp.(*span))
reporter.Report(sp.(*Span))
s.Equal(1, reporter.SpansSubmitted(), "expected number of spans submitted")
reporter.Close()
}
Expand Down
75 changes: 47 additions & 28 deletions span.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import (
"github.com/uber/jaeger-client-go/utils"
)

type span struct {
// Span implements opentracing.Span
type Span struct {
sync.RWMutex

tracer *tracer
Expand Down Expand Up @@ -82,8 +83,8 @@ type Tag struct {
value interface{}
}

// Sets or changes the operation name.
func (s *span) SetOperationName(operationName string) opentracing.Span {
// SetOperationName sets or changes the operation name.
func (s *Span) SetOperationName(operationName string) opentracing.Span {
s.Lock()
defer s.Unlock()
if s.context.IsSampled() {
Expand All @@ -94,7 +95,7 @@ func (s *span) SetOperationName(operationName string) opentracing.Span {
}

// SetTag implements SetTag() of opentracing.Span
func (s *span) SetTag(key string, value interface{}) opentracing.Span {
func (s *Span) SetTag(key string, value interface{}) opentracing.Span {
s.observer.OnSetTag(key, value)
if key == string(ext.SamplingPriority) && setSamplingPriority(s, key, value) {
return s
Expand All @@ -107,7 +108,7 @@ func (s *span) SetTag(key string, value interface{}) opentracing.Span {
return s
}

func (s *span) setTagNoLocking(key string, value interface{}) {
func (s *Span) setTagNoLocking(key string, value interface{}) {
handled := false
if handler, ok := specialTagHandlers[key]; ok {
handled = handler(s, key, value)
Expand All @@ -117,15 +118,16 @@ func (s *span) setTagNoLocking(key string, value interface{}) {
}
}

func (s *span) setTracerTags(tags []Tag) {
func (s *Span) setTracerTags(tags []Tag) {
s.Lock()
for _, tag := range tags {
s.tags = append(s.tags, tag)
}
s.Unlock()
}

func (s *span) LogFields(fields ...log.Field) {
// LogFields implements opentracing.Span API
func (s *Span) LogFields(fields ...log.Field) {
s.Lock()
defer s.Unlock()
if !s.context.IsSampled() {
Expand All @@ -138,7 +140,8 @@ func (s *span) LogFields(fields ...log.Field) {
s.appendLog(lr)
}

func (s *span) LogKV(alternatingKeyValues ...interface{}) {
// LogKV implements opentracing.Span API
func (s *Span) LogKV(alternatingKeyValues ...interface{}) {
s.RLock()
sampled := s.context.IsSampled()
s.RUnlock()
Expand All @@ -153,15 +156,18 @@ func (s *span) LogKV(alternatingKeyValues ...interface{}) {
s.LogFields(fields...)
}

func (s *span) LogEvent(event string) {
// LogEvent implements opentracing.Span API
func (s *Span) LogEvent(event string) {
s.Log(opentracing.LogData{Event: event})
}

func (s *span) LogEventWithPayload(event string, payload interface{}) {
// LogEventWithPayload implements opentracing.Span API
func (s *Span) LogEventWithPayload(event string, payload interface{}) {
s.Log(opentracing.LogData{Event: event, Payload: payload})
}

func (s *span) Log(ld opentracing.LogData) {
// Log implements opentracing.Span API
func (s *Span) Log(ld opentracing.LogData) {
s.Lock()
defer s.Unlock()
if s.context.IsSampled() {
Expand All @@ -173,13 +179,13 @@ func (s *span) Log(ld opentracing.LogData) {
}

// this function should only be called while holding a Write lock
func (s *span) appendLog(lr opentracing.LogRecord) {
func (s *Span) appendLog(lr opentracing.LogRecord) {
// TODO add logic to limit number of logs per span (issue #46)
s.logs = append(s.logs, lr)
}

// SetBaggageItem implements SetBaggageItem() of opentracing.SpanContext
func (s *span) SetBaggageItem(key, value string) opentracing.Span {
func (s *Span) SetBaggageItem(key, value string) opentracing.Span {
key = normalizeBaggageKey(key)
s.Lock()
defer s.Unlock()
Expand All @@ -188,18 +194,20 @@ func (s *span) SetBaggageItem(key, value string) opentracing.Span {
}

// BaggageItem implements BaggageItem() of opentracing.SpanContext
func (s *span) BaggageItem(key string) string {
func (s *Span) BaggageItem(key string) string {
key = normalizeBaggageKey(key)
s.RLock()
defer s.RUnlock()
return s.context.baggage[key]
}

func (s *span) Finish() {
// Finish implements opentracing.Span API
func (s *Span) Finish() {
s.FinishWithOptions(opentracing.FinishOptions{})
}

func (s *span) FinishWithOptions(options opentracing.FinishOptions) {
// FinishWithOptions implements opentracing.Span API
func (s *Span) FinishWithOptions(options opentracing.FinishOptions) {
if options.FinishTime.IsZero() {
options.FinishTime = s.tracer.timeNow()
}
Expand All @@ -220,42 +228,53 @@ func (s *span) FinishWithOptions(options opentracing.FinishOptions) {
s.tracer.reportSpan(s)
}

func (s *span) Context() opentracing.SpanContext {
// Context implements opentracing.Span API
func (s *Span) Context() opentracing.SpanContext {
return s.context
}

func (s *span) Tracer() opentracing.Tracer {
// Tracer implements opentracing.Span API
func (s *Span) Tracer() opentracing.Tracer {
return s.tracer
}

func (s *span) String() string {
func (s *Span) String() string {
s.RLock()
defer s.RUnlock()
return s.context.String()
}

func (s *span) peerDefined() bool {
// OperationName allows retrieving current operation name.
func (s *Span) OperationName() string {
s.RLock()
defer s.RUnlock()
return s.operationName
}

func (s *Span) peerDefined() bool {
return s.peer.ServiceName != "" || s.peer.Ipv4 != 0 || s.peer.Port != 0
}

func (s *span) isRPC() bool {
func (s *Span) isRPC() bool {
s.RLock()
defer s.RUnlock()
return s.spanKind == string(ext.SpanKindRPCClientEnum) || s.spanKind == string(ext.SpanKindRPCServerEnum)
}

func (s *span) isRPCClient() bool {
func (s *Span) isRPCClient() bool {
s.RLock()
defer s.RUnlock()
return s.spanKind == string(ext.SpanKindRPCClientEnum)
}

var specialTagHandlers = map[string]func(*span, string, interface{}) bool{
var specialTagHandlers = map[string]func(*Span, string, interface{}) bool{
string(ext.SpanKind): setSpanKind,
string(ext.PeerHostIPv4): setPeerIPv4,
string(ext.PeerPort): setPeerPort,
string(ext.PeerService): setPeerService,
}

func setSpanKind(s *span, key string, value interface{}) bool {
func setSpanKind(s *Span, key string, value interface{}) bool {
if val, ok := value.(string); ok {
s.spanKind = val
return true
Expand All @@ -267,7 +286,7 @@ func setSpanKind(s *span, key string, value interface{}) bool {
return false
}

func setPeerIPv4(s *span, key string, value interface{}) bool {
func setPeerIPv4(s *Span, key string, value interface{}) bool {
if val, ok := value.(string); ok {
if ip, err := utils.ParseIPToUint32(val); err == nil {
s.peer.Ipv4 = int32(ip)
Expand All @@ -285,7 +304,7 @@ func setPeerIPv4(s *span, key string, value interface{}) bool {
return false
}

func setPeerPort(s *span, key string, value interface{}) bool {
func setPeerPort(s *Span, key string, value interface{}) bool {
if val, ok := value.(string); ok {
if port, err := utils.ParsePort(val); err == nil {
s.peer.Port = int16(port)
Expand All @@ -303,15 +322,15 @@ func setPeerPort(s *span, key string, value interface{}) bool {
return false
}

func setPeerService(s *span, key string, value interface{}) bool {
func setPeerService(s *Span, key string, value interface{}) bool {
if val, ok := value.(string); ok {
s.peer.ServiceName = val
return true
}
return false
}

func setSamplingPriority(s *span, key string, value interface{}) bool {
func setSamplingPriority(s *Span, key string, value interface{}) bool {
s.Lock()
defer s.Unlock()
if val, ok := value.(uint16); ok {
Expand Down
15 changes: 13 additions & 2 deletions span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestBaggageIterator(t *testing.T) {
tracer, closer := NewTracer("DOOP", NewConstSampler(true), NewNullReporter())
defer closer.Close()

sp1 := tracer.StartSpan("s1").(*span)
sp1 := tracer.StartSpan("s1").(*Span)
sp1.SetBaggageItem("Some_Key", "12345")
sp1.SetBaggageItem("Some-other-key", "42")
expectedBaggage := map[string]string{"some-key": "12345", "some-other-key": "42"}
Expand Down Expand Up @@ -62,7 +62,18 @@ func TestSpanProperties(t *testing.T) {
tracer, closer := NewTracer("DOOP", NewConstSampler(true), NewNullReporter())
defer closer.Close()

sp1 := tracer.StartSpan("s1").(*span)
sp1 := tracer.StartSpan("s1").(*Span)
assert.Equal(t, tracer, sp1.Tracer())
assert.NotNil(t, sp1.Context())
}

func TestSpanOperationName(t *testing.T) {
tracer, closer := NewTracer("DOOP", NewConstSampler(true), NewNullReporter())
defer closer.Close()

sp1 := tracer.StartSpan("s1").(*Span)
sp1.SetOperationName("s2")
sp1.Finish()

assert.Equal(t, "s2", sp1.OperationName())
}
Loading

0 comments on commit bf2a774

Please sign in to comment.