Skip to content

Commit

Permalink
Disable span pool by default, add option to enable it (#27)
Browse files Browse the repository at this point in the history
* Add option to disable the use of the span pool

* Use of span pool is disabled by default

* Fix typo
  • Loading branch information
bg451 committed May 27, 2016
1 parent 76881bf commit 1bf31ca
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 7 additions & 1 deletion span.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,18 @@ func (s *spanImpl) FinishWithOptions(opts opentracing.FinishOptions) {

s.onFinish(s.raw)
s.tracer.options.Recorder.RecordSpan(s.raw)

// Last chance to get options before the span is possbily reset.
poolEnabled := s.tracer.options.EnableSpanPool
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()
}
spanPool.Put(s)

if poolEnabled {
spanPool.Put(s)
}
}

func (s *spanImpl) SetBaggageItem(restrictedKey, val string) opentracing.Span {
Expand Down
14 changes: 11 additions & 3 deletions tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ type Options struct {
// When set, it attempts to exacerbate issues emanating from use of Spans
// after calling Finish by running additional assertions.
DebugAssertUseAfterFinish bool
// EnableSpanPool enables the use of a pool, so that the tracer reuses spans
// after Finish has been called on it. Adds a slight performance gain as it
// reduces allocations. However, if you have any use-after-finish race
// conditions the code may panic.
EnableSpanPool bool
}

// DefaultOptions returns an Options object with a 1 in 64 sampling rate and
Expand Down Expand Up @@ -122,9 +127,12 @@ func (t *tracerImpl) StartSpan(
}

func (t *tracerImpl) getSpan() *spanImpl {
sp := spanPool.Get().(*spanImpl)
sp.reset()
return sp
if t.options.EnableSpanPool {
sp := spanPool.Get().(*spanImpl)
sp.reset()
return sp
}
return &spanImpl{}
}

func (t *tracerImpl) StartSpanWithOptions(
Expand Down

0 comments on commit 1bf31ca

Please sign in to comment.