Skip to content

Commit

Permalink
Protect start_time and end_time from being set manually by the user (#…
Browse files Browse the repository at this point in the history
…363)

Fixes #361
  • Loading branch information
ocelotl authored and c24t committed Jan 13, 2020
1 parent 908150c commit f0ba817
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
12 changes: 6 additions & 6 deletions ext/opentelemetry-ext-jaeger/tests/test_jaeger_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,18 @@ def test_translate_to_jaeger(self):
trace.Span(name=span_names[2], context=other_context, parent=None),
]

otel_spans[0].start_time = start_times[0]
otel_spans[0].start(start_time=start_times[0])
# added here to preserve order
otel_spans[0].set_attribute("key_bool", False)
otel_spans[0].set_attribute("key_string", "hello_world")
otel_spans[0].set_attribute("key_float", 111.22)
otel_spans[0].end_time = end_times[0]
otel_spans[0].end(end_time=end_times[0])

otel_spans[1].start_time = start_times[1]
otel_spans[1].end_time = end_times[1]
otel_spans[1].start(start_time=start_times[1])
otel_spans[1].end(end_time=end_times[1])

otel_spans[2].start_time = start_times[2]
otel_spans[2].end_time = end_times[2]
otel_spans[2].start(start_time=start_times[2])
otel_spans[2].end(end_time=end_times[2])

# pylint: disable=protected-access
spans = jaeger_exporter._translate_to_jaeger(otel_spans)
Expand Down
12 changes: 6 additions & 6 deletions ext/opentelemetry-ext-zipkin/tests/test_zipkin_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,18 @@ def test_export(self):
trace.Span(name=span_names[2], context=other_context, parent=None),
]

otel_spans[0].start_time = start_times[0]
otel_spans[0].start(start_time=start_times[0])
# added here to preserve order
otel_spans[0].set_attribute("key_bool", False)
otel_spans[0].set_attribute("key_string", "hello_world")
otel_spans[0].set_attribute("key_float", 111.22)
otel_spans[0].end_time = end_times[0]
otel_spans[0].end(end_time=end_times[0])

otel_spans[1].start_time = start_times[1]
otel_spans[1].end_time = end_times[1]
otel_spans[1].start(start_time=start_times[1])
otel_spans[1].end(end_time=end_times[1])

otel_spans[2].start_time = start_times[2]
otel_spans[2].end_time = end_times[2]
otel_spans[2].start(start_time=start_times[2])
otel_spans[2].end(end_time=end_times[2])

service_name = "test-service"
local_endpoint = {
Expand Down
18 changes: 14 additions & 4 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,18 @@ def __init__(
else:
self.links = BoundedList.from_seq(MAX_NUM_LINKS, links)

self.end_time = None # type: Optional[int]
self.start_time = None # type: Optional[int]
self._end_time = None # type: Optional[int]
self._start_time = None # type: Optional[int]
self.instrumentation_info = instrumentation_info

@property
def start_time(self):
return self._start_time

@property
def end_time(self):
return self._end_time

def __repr__(self):
return '{}(name="{}", context={})'.format(
type(self).__name__, self.name, self.context
Expand Down Expand Up @@ -243,7 +251,7 @@ def start(self, start_time: Optional[int] = None) -> None:
return
has_started = self.start_time is not None
if not has_started:
self.start_time = (
self._start_time = (
start_time if start_time is not None else time_ns()
)
if has_started:
Expand All @@ -259,7 +267,9 @@ def end(self, end_time: Optional[int] = None) -> 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 time_ns()
self._end_time = (
end_time if end_time is not None else time_ns()
)
if has_ended:
logger.warning("Calling end() on an ended span.")
return
Expand Down

0 comments on commit f0ba817

Please sign in to comment.