Skip to content

Commit

Permalink
Refactor is_valid to be an instance attribute (#1005)
Browse files Browse the repository at this point in the history
  • Loading branch information
ffe4 authored Aug 27, 2020
1 parent e21ee21 commit f6a658e
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 17 deletions.
2 changes: 2 additions & 0 deletions opentelemetry-api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Refactor `SpanContext.is_valid` from a method to a data attribute
([#1005](https://github.com/open-telemetry/opentelemetry-python/pull/1005))
- Moved samplers from API to SDK
([#1023](https://github.com/open-telemetry/opentelemetry-python/pull/1023))

Expand Down
18 changes: 4 additions & 14 deletions opentelemetry-api/src/opentelemetry/trace/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ def __init__(
self.trace_flags = trace_flags
self.trace_state = trace_state
self.is_remote = is_remote
self.is_valid = (
self.trace_id != INVALID_TRACE_ID
and self.span_id != INVALID_SPAN_ID
)

def __repr__(self) -> str:
return (
Expand All @@ -199,20 +203,6 @@ def __repr__(self) -> str:
self.is_remote,
)

def is_valid(self) -> bool:
"""Get whether this `SpanContext` is valid.
A `SpanContext` is said to be invalid if its trace ID or span ID is
invalid (i.e. ``0``).
Returns:
True if the `SpanContext` is valid, false otherwise.
"""
return (
self.trace_id != INVALID_TRACE_ID
and self.span_id != INVALID_SPAN_ID
)


class DefaultSpan(Span):
"""The default Span that is used when no Span implementation is available.
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-api/tests/trace/test_defaultspan.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ def test_ctor(self):
def test_invalid_span(self):
self.assertIsNotNone(trace.INVALID_SPAN)
self.assertIsNotNone(trace.INVALID_SPAN.get_context())
self.assertFalse(trace.INVALID_SPAN.get_context().is_valid())
self.assertFalse(trace.INVALID_SPAN.get_context().is_valid)
2 changes: 1 addition & 1 deletion opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ def start_span( # pylint: disable=too-many-locals
):
raise TypeError("parent must be a Span, SpanContext or None.")

if parent_context is None or not parent_context.is_valid():
if parent_context is None or not parent_context.is_valid:
parent = parent_context = None
trace_id = generate_trace_id()
trace_flags = None
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def test_start_span_invalid_spancontext(self):
new_span = tracer.start_span(
"root", parent=trace_api.INVALID_SPAN_CONTEXT
)
self.assertTrue(new_span.context.is_valid())
self.assertTrue(new_span.context.is_valid)
self.assertIsNone(new_span.parent)

def test_instrumentation_info(self):
Expand Down

0 comments on commit f6a658e

Please sign in to comment.