From b837c9f6278fbace77b4d60acb540209f50aac37 Mon Sep 17 00:00:00 2001 From: Hector Hernandez Guzman Date: Thu, 26 Sep 2019 13:40:42 -0700 Subject: [PATCH 1/6] Span add override parameters for start_time and end_time --- opentelemetry-api/src/opentelemetry/trace/__init__.py | 4 ++-- .../src/opentelemetry/sdk/trace/__init__.py | 8 ++++++-- opentelemetry-sdk/tests/trace/test_trace.py | 10 ++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py index b79cdeb4df5..55cdc567d5d 100644 --- a/opentelemetry-api/src/opentelemetry/trace/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/__init__.py @@ -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 @@ -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. diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index a694476e1fe..37f9c35e07a 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -340,19 +340,21 @@ 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() + if start_time is not None : + self.start_time = start_time 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 @@ -361,6 +363,8 @@ def end(self): has_ended = self.end_time is not None if not has_ended: self.end_time = util.time_ns() + if end_time is not None: + self.end_time = end_time if has_ended: logger.warning("Calling end() on an ended span.") return diff --git a/opentelemetry-sdk/tests/trace/test_trace.py b/opentelemetry-sdk/tests/trace/test_trace.py index 0570affc411..378534453c2 100644 --- a/opentelemetry-sdk/tests/trace/test_trace.py +++ b/opentelemetry-sdk/tests/trace/test_trace.py @@ -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") From a12fe5dab59cc38414e8e6795f4de9ffd0c694ee Mon Sep 17 00:00:00 2001 From: Hector Hernandez Guzman Date: Thu, 26 Sep 2019 16:26:00 -0700 Subject: [PATCH 2/6] Make lint happy --- opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index 37f9c35e07a..e844bfcf94f 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -347,7 +347,7 @@ def start(self, start_time: int = None): has_started = self.start_time is not None if not has_started: self.start_time = util.time_ns() - if start_time is not None : + if start_time is not None: self.start_time = start_time if has_started: logger.warning("Calling start() on a started span.") From 276ecb4138ac1a772be41c5fd35b8d3ddec9a1ad Mon Sep 17 00:00:00 2001 From: Hector Hernandez Guzman Date: Fri, 27 Sep 2019 11:27:19 -0700 Subject: [PATCH 3/6] Addressing comments --- .../src/opentelemetry/sdk/trace/__init__.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index e844bfcf94f..1e1594e348d 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -346,9 +346,10 @@ def start(self, start_time: int = None): return has_started = self.start_time is not None if not has_started: - self.start_time = util.time_ns() - if start_time is not None: - self.start_time = start_time + if start_time is not None: + self.start_time = start_time + else: + self.start_time = util.time_ns() if has_started: logger.warning("Calling start() on a started span.") return @@ -362,9 +363,10 @@ def end(self, end_time: int = 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() - if end_time is not None: - self.end_time = end_time + if end_time is not None: + self.end_time = end_time + else: + self.end_time = util.time_ns() if has_ended: logger.warning("Calling end() on an ended span.") return From 2b90351052f8edb389be395a88e0fa38b606079d Mon Sep 17 00:00:00 2001 From: Hector Hernandez Guzman Date: Fri, 27 Sep 2019 14:10:59 -0700 Subject: [PATCH 4/6] Addressing comments --- .../src/opentelemetry/sdk/trace/__init__.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index 1e1594e348d..23620146816 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -346,10 +346,7 @@ def start(self, start_time: int = None): return has_started = self.start_time is not None if not has_started: - if start_time is not None: - self.start_time = start_time - else: - self.start_time = util.time_ns() + self.start_time = start_time or util.time_ns() if has_started: logger.warning("Calling start() on a started span.") return @@ -363,10 +360,7 @@ def end(self, end_time: int = None): raise RuntimeError("Calling end() on a not started span.") has_ended = self.end_time is not None if not has_ended: - if end_time is not None: - self.end_time = end_time - else: - self.end_time = util.time_ns() + self.end_time = end_time or util.time_ns() if has_ended: logger.warning("Calling end() on an ended span.") return From eccef1a9686f2527335cfe5b18e22a1e79e84b90 Mon Sep 17 00:00:00 2001 From: Hector Hernandez Guzman Date: Fri, 27 Sep 2019 17:06:21 -0700 Subject: [PATCH 5/6] Allowing 0 as start and end time --- opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index 23620146816..d24955d1639 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -346,7 +346,7 @@ def start(self, start_time: int = None): return has_started = self.start_time is not None if not has_started: - self.start_time = start_time or 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 @@ -360,7 +360,7 @@ def end(self, end_time: int = 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 = end_time or 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 From a187fec187ce06bcf78bfbd64195cfba783a31cd Mon Sep 17 00:00:00 2001 From: Hector Hernandez Guzman Date: Fri, 27 Sep 2019 17:16:43 -0700 Subject: [PATCH 6/6] Fix lint issues --- opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index d24955d1639..37294ee05f7 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -346,7 +346,9 @@ def start(self, start_time: int = None): return has_started = self.start_time is not None if not has_started: - self.start_time = start_time if start_time is not None else 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 @@ -360,7 +362,9 @@ def end(self, end_time: int = 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 = end_time if end_time is not None else 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