Skip to content

Commit

Permalink
Add DefaultSpan in the API (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosalberto authored and c24t committed Jul 26, 2019
1 parent 8a453ee commit 73cadac
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
13 changes: 13 additions & 0 deletions opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions opentelemetry-api/tests/trace/__init__.py
Original file line number Diff line number Diff line change
@@ -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.
31 changes: 31 additions & 0 deletions opentelemetry-api/tests/trace/test_defaultspan.py
Original file line number Diff line number Diff line change
@@ -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())

0 comments on commit 73cadac

Please sign in to comment.