Skip to content

Commit 1bf31ca

Browse files
committed
Disable span pool by default, add option to enable it (#27)
* Add option to disable the use of the span pool * Use of span pool is disabled by default * Fix typo
1 parent 76881bf commit 1bf31ca

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

span.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,18 @@ func (s *spanImpl) FinishWithOptions(opts opentracing.FinishOptions) {
136136

137137
s.onFinish(s.raw)
138138
s.tracer.options.Recorder.RecordSpan(s.raw)
139+
140+
// Last chance to get options before the span is possbily reset.
141+
poolEnabled := s.tracer.options.EnableSpanPool
139142
if s.tracer.options.DebugAssertUseAfterFinish {
140143
// This makes it much more likely to catch a panic on any subsequent
141144
// operation since s.tracer is accessed on every call to `Lock`.
142145
s.reset()
143146
}
144-
spanPool.Put(s)
147+
148+
if poolEnabled {
149+
spanPool.Put(s)
150+
}
145151
}
146152

147153
func (s *spanImpl) SetBaggageItem(restrictedKey, val string) opentracing.Span {

tracer.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ type Options struct {
7373
// When set, it attempts to exacerbate issues emanating from use of Spans
7474
// after calling Finish by running additional assertions.
7575
DebugAssertUseAfterFinish bool
76+
// EnableSpanPool enables the use of a pool, so that the tracer reuses spans
77+
// after Finish has been called on it. Adds a slight performance gain as it
78+
// reduces allocations. However, if you have any use-after-finish race
79+
// conditions the code may panic.
80+
EnableSpanPool bool
7681
}
7782

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

124129
func (t *tracerImpl) getSpan() *spanImpl {
125-
sp := spanPool.Get().(*spanImpl)
126-
sp.reset()
127-
return sp
130+
if t.options.EnableSpanPool {
131+
sp := spanPool.Get().(*spanImpl)
132+
sp.reset()
133+
return sp
134+
}
135+
return &spanImpl{}
128136
}
129137

130138
func (t *tracerImpl) StartSpanWithOptions(

0 commit comments

Comments
 (0)