diff --git a/opentelemetry-api/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py index b6d856bb85..47f089192a 100644 --- a/opentelemetry-api/src/opentelemetry/trace/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/__init__.py @@ -175,10 +175,23 @@ def is_valid(self) -> bool: """ +class DefaultSpan(Span): + """The default Span that is used when no Span implementation is available. + + All operations are no-op except context propagation. + """ + def __init__(self, context: 'SpanContext') -> None: + self._context = context + + def get_context(self) -> 'SpanContext': + return self._context + + INVALID_SPAN_ID = 0 INVALID_TRACE_ID = 0 INVALID_SPAN_CONTEXT = SpanContext(INVALID_TRACE_ID, INVALID_SPAN_ID, DEFAULT_TRACEOPTIONS, DEFAULT_TRACESTATE) +INVALID_SPAN = DefaultSpan(INVALID_SPAN_CONTEXT) class Tracer: diff --git a/opentelemetry-api/tests/trace/__init__.py b/opentelemetry-api/tests/trace/__init__.py new file mode 100644 index 0000000000..d853a7bcf6 --- /dev/null +++ b/opentelemetry-api/tests/trace/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2019, OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/opentelemetry-api/tests/trace/test_defaultspan.py b/opentelemetry-api/tests/trace/test_defaultspan.py new file mode 100644 index 0000000000..9e6b57dc3d --- /dev/null +++ b/opentelemetry-api/tests/trace/test_defaultspan.py @@ -0,0 +1,31 @@ +# Copyright 2019, OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest + +from opentelemetry import trace + + +class TestDefaultSpan(unittest.TestCase): + def test_ctor(self): + context = trace.SpanContext(1, 1, + trace.DEFAULT_TRACEOPTIONS, + trace.DEFAULT_TRACESTATE) + span = trace.DefaultSpan(context) + self.assertEqual(context, span.get_context()) + + 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())