Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Span add override parameters for start_time and end_time #179

Merged
merged 6 commits into from
Sep 28, 2019
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
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 @@ -340,27 +340,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