Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document usage of start_as_current_span as decorator #2708

Merged
merged 1 commit into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,14 @@ def start_as_current_span(
with opentelemetry.trace.use_span(span, end_on_exit=True):
do_work()

This can also be used as a decorator::

@tracer.start_as_current_span("name"):
def function():
...

function()

Args:
name: The name of the span to be created.
context: An optional Context containing the span's parent. Defaults to the
Expand Down
52 changes: 43 additions & 9 deletions opentelemetry-api/tests/trace/test_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,59 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

from opentelemetry import trace
from contextlib import contextmanager
from unittest import TestCase
from unittest.mock import Mock

from opentelemetry.trace import (
INVALID_SPAN,
NoOpTracer,
Span,
Tracer,
get_current_span,
)

class TestTracer(unittest.TestCase):

class TestTracer(TestCase):
def setUp(self):
self.tracer = trace.NoOpTracer()
self.tracer = NoOpTracer()

def test_start_span(self):
with self.tracer.start_span("") as span:
self.assertIsInstance(span, trace.Span)
self.assertIsInstance(span, Span)

def test_start_as_current_span(self):
def test_start_as_current_span_context_manager(self):
with self.tracer.start_as_current_span("") as span:
self.assertIsInstance(span, trace.Span)
self.assertIsInstance(span, Span)

def test_start_as_current_span_decorator(self):

mock_call = Mock()

class MockTracer(Tracer):
def start_span(self, *args, **kwargs):
return INVALID_SPAN

@contextmanager
def start_as_current_span(self, *args, **kwargs): # type: ignore
mock_call()
yield INVALID_SPAN

mock_tracer = MockTracer()

@mock_tracer.start_as_current_span("name")
def function(): # type: ignore
pass

function() # type: ignore
function() # type: ignore
function() # type: ignore

self.assertEqual(mock_call.call_count, 3)

def test_get_current_span(self):
with self.tracer.start_as_current_span("test") as span:
trace.get_current_span().set_attribute("test", "test")
self.assertEqual(span, trace.INVALID_SPAN)
get_current_span().set_attribute("test", "test")
self.assertEqual(span, INVALID_SPAN)
self.assertFalse(hasattr("span", "attributes"))