Skip to content

Commit

Permalink
Span add override parameters for start_time and end_time (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorhdzg authored and reyang committed Sep 28, 2019
1 parent e523140 commit 250b9fa
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
4 changes: 2 additions & 2 deletions opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class SpanKind(enum.Enum):
class Span:
"""A span represents a single operation within a trace."""

def start(self) -> None:
def start(self, start_time: int = None) -> None:
"""Sets the current time as the span's start time.
Each span represents a single operation. The span's start time is the
Expand All @@ -152,7 +152,7 @@ def start(self) -> None:
implementations are free to ignore or raise on further calls.
"""

def end(self) -> None:
def end(self, end_time: int = None) -> None:
"""Sets the current time as the span's end time.
The span's end time is the wall time at which the operation finished.
Expand Down
12 changes: 8 additions & 4 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,27 +242,31 @@ def add_lazy_link(self, link: "trace_api.Link") -> None:
return
self.links.append(link)

def start(self):
def start(self, start_time: int = None):
with self._lock:
if not self.is_recording_events():
return
has_started = self.start_time is not None
if not has_started:
self.start_time = util.time_ns()
self.start_time = (
start_time if start_time is not None else util.time_ns()
)
if has_started:
logger.warning("Calling start() on a started span.")
return
self.span_processor.on_start(self)

def end(self):
def end(self, end_time: int = None):
with self._lock:
if not self.is_recording_events():
return
if self.start_time is None:
raise RuntimeError("Calling end() on a not started span.")
has_ended = self.end_time is not None
if not has_ended:
self.end_time = util.time_ns()
self.end_time = (
end_time if end_time is not None else util.time_ns()
)
if has_ended:
logger.warning("Calling end() on an ended span.")
return
Expand Down
10 changes: 10 additions & 0 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,16 @@ def test_start_span(self):
span.start()
self.assertEqual(start_time, span.start_time)

def test_span_override_start_and_end_time(self):
"""Span sending custom start_time and end_time values"""
span = trace.Span("name", mock.Mock(spec=trace_api.SpanContext))
start_time = 123
span.start(start_time)
self.assertEqual(start_time, span.start_time)
end_time = 456
span.end(end_time)
self.assertEqual(end_time, span.end_time)

def test_ended_span(self):
""""Events, attributes are not allowed after span is ended"""
tracer = trace.Tracer("test_ended_span")
Expand Down

0 comments on commit 250b9fa

Please sign in to comment.