-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- make context propagation robust to unavailability of root tracer
- Loading branch information
DRV2SI
committed
Feb 20, 2023
1 parent
b85e476
commit a099a02
Showing
3 changed files
with
138 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
contrib/opencensus-ext-threading/tests/test_noop_tracer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import unittest | ||
from unittest.mock import patch, MagicMock | ||
|
||
from opencensus.trace.tracers.noop_tracer import NoopTracer | ||
from opencensus.ext.threading.trace import wrap_submit, wrap_apply_async | ||
|
||
|
||
class TestNoopTracer(unittest.TestCase): | ||
""" | ||
In case no OpenCensus context is present (i.e. we have a NoopTracer), do _not_ pass down tracer in apply_async | ||
and submit; instead invoke function directly. | ||
""" | ||
|
||
@patch("opencensus.ext.threading.trace.wrap_task_func") | ||
@patch("opencensus.trace.execution_context.get_opencensus_tracer") | ||
def test_noop_tracer_apply_async( | ||
self, get_opencensus_tracer_mock: MagicMock, wrap_task_func_mock: MagicMock | ||
): | ||
mock_tracer = NoopTracer() | ||
get_opencensus_tracer_mock.return_value = mock_tracer | ||
submission_function_mock = MagicMock() | ||
original_function_mock = MagicMock() | ||
|
||
wrap_apply_async(submission_function_mock)(None, original_function_mock) | ||
|
||
# check whether invocation of original function _has_ happened | ||
submission_function_mock.assert_called_once_with( | ||
None, original_function_mock, args=(), kwds={} | ||
) | ||
|
||
# ensure that the function has _not_ been wrapped | ||
wrap_task_func_mock.assert_not_called() | ||
|
||
@patch("opencensus.ext.threading.trace.wrap_task_func") | ||
@patch("opencensus.trace.execution_context.get_opencensus_tracer") | ||
def test_noop_tracer_wrap_submit( | ||
self, get_opencensus_tracer_mock: MagicMock, wrap_task_func_mock: MagicMock | ||
): | ||
mock_tracer = NoopTracer() | ||
get_opencensus_tracer_mock.return_value = mock_tracer | ||
submission_function_mock = MagicMock() | ||
original_function_mock = MagicMock() | ||
|
||
wrap_submit(submission_function_mock)(None, original_function_mock) | ||
|
||
# check whether invocation of original function _has_ happened | ||
submission_function_mock.assert_called_once_with(None, original_function_mock) | ||
|
||
# ensure that the function has _not_ been wrapped | ||
wrap_task_func_mock.assert_not_called() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import unittest | ||
from unittest.mock import patch, MagicMock | ||
from opencensus.ext.threading.trace import wrap_submit, wrap_apply_async | ||
|
||
|
||
class TestTracer(unittest.TestCase): | ||
""" | ||
Ensures that sampler, exporter, propagator are passed through | ||
in case global tracer is present. | ||
""" | ||
|
||
@patch("opencensus.trace.propagation.binary_format.BinaryFormatPropagator") | ||
@patch("opencensus.ext.threading.trace.wrap_task_func") | ||
@patch("opencensus.trace.execution_context.get_opencensus_tracer") | ||
def test_apply_async_context_passed( | ||
self, | ||
get_opencensus_tracer_mock: MagicMock, | ||
wrap_task_func_mock: MagicMock, | ||
binary_format_propagator_mock: MagicMock, | ||
): | ||
mock_tracer = NoNoopTracerMock() | ||
# ensure that unique object is generated | ||
mock_tracer.sampler = MagicMock() | ||
mock_tracer.exporter = MagicMock() | ||
mock_tracer.propagator = MagicMock() | ||
|
||
get_opencensus_tracer_mock.return_value = mock_tracer | ||
|
||
submission_function_mock = MagicMock() | ||
original_function_mock = MagicMock() | ||
|
||
wrap_apply_async(submission_function_mock)(None, original_function_mock) | ||
|
||
# check whether invocation of original function _has_ happened | ||
call = submission_function_mock.call_args_list[0].kwargs | ||
|
||
self.assertEqual(id(call["kwds"]["sampler"]), id(mock_tracer.sampler)) | ||
self.assertEqual(id(call["kwds"]["exporter"]), id(mock_tracer.exporter)) | ||
self.assertEqual(id(call["kwds"]["propagator"]), id(mock_tracer.propagator)) | ||
|
||
@patch("opencensus.trace.propagation.binary_format.BinaryFormatPropagator") | ||
@patch("opencensus.ext.threading.trace.wrap_task_func") | ||
@patch("opencensus.trace.execution_context.get_opencensus_tracer") | ||
def test_wrap_submit_context_passed( | ||
self, | ||
get_opencensus_tracer_mock: MagicMock, | ||
wrap_task_func_mock: MagicMock, | ||
binary_format_propagator_mock: MagicMock, | ||
): | ||
mock_tracer = NoNoopTracerMock() | ||
# ensure that unique object is generated | ||
mock_tracer.sampler = MagicMock() | ||
mock_tracer.exporter = MagicMock() | ||
mock_tracer.propagator = MagicMock() | ||
|
||
get_opencensus_tracer_mock.return_value = mock_tracer | ||
|
||
submission_function_mock = MagicMock() | ||
original_function_mock = MagicMock() | ||
|
||
wrap_submit(submission_function_mock)(None, original_function_mock) | ||
|
||
# check whether invocation of original function _has_ happened | ||
call = submission_function_mock.call_args_list[0].kwargs | ||
|
||
self.assertEqual(id(call["sampler"]), id(mock_tracer.sampler)) | ||
self.assertEqual(id(call["exporter"]), id(mock_tracer.exporter)) | ||
self.assertEqual(id(call["propagator"]), id(mock_tracer.propagator)) | ||
|
||
|
||
class NoNoopTracerMock(MagicMock): | ||
pass |