Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions ddtrace/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,25 @@ def __init__(self,
self._tracer = tracer
self._parent = None

# state
self._finished = False

def finish(self, finish_time=None):
""" Mark the end time of the span and submit it to the tracer.
If the span has already been finished don't do anything

:param int finish_time: the end time of the span in seconds.
Defaults to now.
"""
ft = finish_time or time.time()
# be defensive so we don't die if start isn't set
self.duration = ft - (self.start or ft)
if self._finished:
return
self._finished = True

if self.duration is None:
ft = finish_time or time.time()
# be defensive so we don't die if start isn't set
self.duration = ft - (self.start or ft)

if self._tracer:
self._tracer.record(self)

Expand Down
23 changes: 22 additions & 1 deletion tests/test_span.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ def test_finish():
s2 = Span(tracer=None, name="foo")
s2.finish()

def test_finish_called_multiple_times():
# we should only record a span the first time finish is called on it
dt = DummyTracer()
assert dt.spans_recorded == 0
s = Span(dt, 'bar')
s.finish()
s.finish()
assert dt.spans_recorded == 1


def test_finish_set_span_duration():
# If set the duration on a span, the span should be recorded with this
# duration
dt = DummyTracer()
assert dt.last_span is None
s = Span(dt, 'foo')
s.duration = 1337.0
s.finish()
assert dt.last_span.duration == 1337.0

def test_traceback_with_error():
s = Span(None, "foo")
try:
Expand Down Expand Up @@ -121,7 +141,8 @@ class DummyTracer(object):

def __init__(self):
self.last_span = None
self.spans_recorded = 0

def record(self, span):
self.last_span = span

self.spans_recorded += 1