From 18d7c0611ef3b9be218296ece89c249c24d6dc35 Mon Sep 17 00:00:00 2001 From: Daniel Lemm <61800298+ffe4@users.noreply.github.com> Date: Mon, 17 Aug 2020 21:29:15 +0200 Subject: [PATCH 1/2] Refactor is_valid to be an instance attribute --- .../src/opentelemetry/trace/span.py | 18 ++++-------------- .../tests/trace/test_defaultspan.py | 2 +- .../src/opentelemetry/sdk/trace/__init__.py | 2 +- opentelemetry-sdk/tests/trace/test_trace.py | 2 +- 4 files changed, 7 insertions(+), 17 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/trace/span.py b/opentelemetry-api/src/opentelemetry/trace/span.py index d207ecf565b..1c00a3f5d93 100644 --- a/opentelemetry-api/src/opentelemetry/trace/span.py +++ b/opentelemetry-api/src/opentelemetry/trace/span.py @@ -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 ( @@ -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. diff --git a/opentelemetry-api/tests/trace/test_defaultspan.py b/opentelemetry-api/tests/trace/test_defaultspan.py index d27f2b1bbcd..67c2fc33521 100644 --- a/opentelemetry-api/tests/trace/test_defaultspan.py +++ b/opentelemetry-api/tests/trace/test_defaultspan.py @@ -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) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index b76ba49ad4a..ee9e69cb540 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -763,7 +763,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 diff --git a/opentelemetry-sdk/tests/trace/test_trace.py b/opentelemetry-sdk/tests/trace/test_trace.py index 56bb9cfa574..9101a3c6d2a 100644 --- a/opentelemetry-sdk/tests/trace/test_trace.py +++ b/opentelemetry-sdk/tests/trace/test_trace.py @@ -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): From 1f1ff5ccb3c32aaa0f981732c6a69cdc65a90ed9 Mon Sep 17 00:00:00 2001 From: Daniel <61800298+ffe4@users.noreply.github.com> Date: Tue, 18 Aug 2020 08:47:26 +0200 Subject: [PATCH 2/2] Update CHANGELOG.md --- opentelemetry-api/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/opentelemetry-api/CHANGELOG.md b/opentelemetry-api/CHANGELOG.md index 2defb0a59a0..2412a4ff4cb 100644 --- a/opentelemetry-api/CHANGELOG.md +++ b/opentelemetry-api/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +- Refactor `SpanContext.is_valid` from a method to a data attribute + ([#1005](https://github.com/open-telemetry/opentelemetry-python/pull/1005)) + ## Version 0.12b0 Released 2020-08-14