diff --git a/debug.go b/debug.go index 67bb350c3fbf..9b2264c560a4 100644 --- a/debug.go +++ b/debug.go @@ -26,7 +26,7 @@ func (s *spanImpl) maybeAssertSanityLocked() { msg: fmt.Sprintf("span used after Finish()"), }) } - if s.tracer.Options.DebugAssertSingleGoroutine { + if s.tracer.options.DebugAssertSingleGoroutine { startID := curGoroutineID() curID, ok := s.raw.Tags[debugGoroutineIDTag].(uint64) if !ok { diff --git a/span.go b/span.go index e23e62cd6e0c..25da1e4ed1b1 100644 --- a/span.go +++ b/span.go @@ -9,6 +9,22 @@ import ( "github.com/opentracing/opentracing-go/ext" ) +// Span provides access to the essential details of the span, for use +// by basictracer consumers. These methods may only be called prior +// to (*opentracing.Span).Finish(). +type Span interface { + opentracing.Span + + // Context contains trace identifiers + Context() Context + + // Operation names the work done by this span instance + Operation() string + + // Start indicates when the span began + Start() time.Time +} + // Implements the `Span` interface. Created via tracerImpl (see // `basictracer.New()`). type spanImpl struct { @@ -50,7 +66,7 @@ func (s *spanImpl) SetOperationName(operationName string) opentracing.Span { } func (s *spanImpl) trim() bool { - return !s.raw.Sampled && s.tracer.TrimUnsampledSpans + return !s.raw.Sampled && s.tracer.options.TrimUnsampledSpans } func (s *spanImpl) SetTag(key string, value interface{}) opentracing.Span { @@ -119,8 +135,8 @@ func (s *spanImpl) FinishWithOptions(opts opentracing.FinishOptions) { s.raw.Duration = duration s.onFinish(s.raw) - s.tracer.Recorder.RecordSpan(s.raw) - if s.tracer.Options.DebugAssertUseAfterFinish { + s.tracer.options.Recorder.RecordSpan(s.raw) + if s.tracer.options.DebugAssertUseAfterFinish { // This makes it much more likely to catch a panic on any subsequent // operation since s.tracer is accessed on every call to `Lock`. s.reset() @@ -163,3 +179,15 @@ func (s *spanImpl) BaggageItem(restrictedKey string) string { func (s *spanImpl) Tracer() opentracing.Tracer { return s.tracer } + +func (s *spanImpl) Context() Context { + return s.raw.Context +} + +func (s *spanImpl) Operation() string { + return s.raw.Operation +} + +func (s *spanImpl) Start() time.Time { + return s.raw.Start +} diff --git a/tracer.go b/tracer.go index 938957c4d055..35f1c0153aba 100644 --- a/tracer.go +++ b/tracer.go @@ -6,6 +6,15 @@ import ( opentracing "github.com/opentracing/opentracing-go" ) +// Tracer extends the opentracing.Tracer interface with methods to +// probe implementation state, for use by basictracer consumers. +type Tracer interface { + opentracing.Tracer + + // Options gets the Options used in New() or NewWithOptions(). + Options() Options +} + // Options allows creating a customized Tracer via NewWithOptions. The object // must not be updated when there is an active tracer using it. type Options struct { @@ -78,7 +87,7 @@ func DefaultOptions() Options { // NewWithOptions creates a customized Tracer. func NewWithOptions(opts Options) opentracing.Tracer { - rval := &tracerImpl{Options: opts} + rval := &tracerImpl{options: opts} rval.textPropagator = &textMapPropagator{rval} rval.binaryPropagator = &binaryPropagator{rval} rval.accessorPropagator = &accessorPropagator{rval} @@ -97,7 +106,7 @@ func New(recorder SpanRecorder) opentracing.Tracer { // Implements the `Tracer` interface. type tracerImpl struct { - Options + options Options textPropagator *textMapPropagator binaryPropagator *binaryPropagator accessorPropagator *accessorPropagator @@ -135,7 +144,7 @@ func (t *tracerImpl) StartSpanWithOptions( sp := t.getSpan() if opts.Parent == nil { sp.raw.TraceID, sp.raw.SpanID = randomID2() - sp.raw.Sampled = t.ShouldSample(sp.raw.TraceID) + sp.raw.Sampled = t.options.ShouldSample(sp.raw.TraceID) } else { pr := opts.Parent.(*spanImpl) sp.raw.TraceID = pr.raw.TraceID @@ -168,12 +177,12 @@ func (t *tracerImpl) startSpanInternal( tags opentracing.Tags, ) opentracing.Span { sp.tracer = t - sp.event = t.NewSpanEventListener() + sp.event = t.options.NewSpanEventListener() sp.raw.Operation = operationName sp.raw.Start = startTime sp.raw.Duration = -1 sp.raw.Tags = tags - if t.Options.DebugAssertSingleGoroutine { + if t.options.DebugAssertSingleGoroutine { sp.SetTag(debugGoroutineIDTag, curGoroutineID()) } defer sp.onCreate(operationName) @@ -210,3 +219,7 @@ func (t *tracerImpl) Join(operationName string, format interface{}, carrier inte } return nil, opentracing.ErrUnsupportedFormat } + +func (t *tracerImpl) Options() Options { + return t.options +}