diff --git a/skywalking/trace/span/__init__.py b/skywalking/trace/span/__init__.py index ec77a9b7..87ba9ecb 100644 --- a/skywalking/trace/span/__init__.py +++ b/skywalking/trace/span/__init__.py @@ -81,6 +81,11 @@ def raised(self) -> 'Span': ])] return self + def log(self, ex: Exception) -> 'Span': + self.error_occurred = True + self.logs.append(Log(items=LogItem(key='Traceback', val=str(ex)))) + return self + def tag(self, tag: Tag) -> 'Span': if not tag.overridable: self.tags.append(deepcopy(tag)) diff --git a/tests/plugin/__init__.py b/tests/plugin/__init__.py index 5bb74c8e..e6d912d3 100644 --- a/tests/plugin/__init__.py +++ b/tests/plugin/__init__.py @@ -14,11 +14,12 @@ # See the License for the specific language governing permissions and # limitations under the License. # - +import inspect import os import unittest from abc import ABC from collections import namedtuple +from os.path import dirname import requests from requests import Response @@ -31,6 +32,13 @@ class BasePluginTest(unittest.TestCase, ABC): compose = None # type: DockerCompose + @classmethod + def setUpClass(cls): + cls.compose = DockerCompose(filepath=dirname(inspect.getfile(cls))) + cls.compose.start() + + cls.compose.wait_for(cls.url(cls.collector_address())) + @classmethod def tearDownClass(cls): cls.compose.stop() @@ -57,8 +65,12 @@ def collector_address(cls): # type: () -> ServicePort return ServicePort(service='collector', port='12800') - def validate(self, expected_file_name): + def validate(self, expected_file_name=None): # type: (str) -> Response + + if expected_file_name is None: + expected_file_name = os.path.join(dirname(inspect.getfile(self.__class__)), 'expected.data.yml') + with open(expected_file_name) as expected_data_file: response = requests.post( url=self.__class__.url(self.__class__.collector_address(), path='/dataValidate'), diff --git a/tests/plugin/sw_flask/test_flask.py b/tests/plugin/sw_flask/test_flask.py index dfeaaa6c..81874514 100644 --- a/tests/plugin/sw_flask/test_flask.py +++ b/tests/plugin/sw_flask/test_flask.py @@ -14,11 +14,10 @@ # See the License for the specific language governing permissions and # limitations under the License. # - -import os +import inspect import time import unittest -from os.path import abspath, dirname +from os.path import dirname from testcontainers.compose import DockerCompose @@ -28,15 +27,15 @@ class TestPlugin(BasePluginTest): @classmethod def setUpClass(cls): - cls.compose = DockerCompose(filepath=dirname(abspath(__file__))) + cls.compose = DockerCompose(filepath=dirname(inspect.getfile(cls))) cls.compose.start() cls.compose.wait_for(cls.url(('consumer', '9090'), 'users')) - def test_request_plugin(self): + def test_plugin(self): time.sleep(3) - self.validate(expected_file_name=os.path.join(dirname(abspath(__file__)), 'expected.data.yml')) + self.validate() if __name__ == '__main__': diff --git a/tests/plugin/sw_http/test_http.py b/tests/plugin/sw_http/test_http.py index 856db043..05b42d24 100644 --- a/tests/plugin/sw_http/test_http.py +++ b/tests/plugin/sw_http/test_http.py @@ -15,31 +15,22 @@ # limitations under the License. # -import os import time import unittest -from os.path import abspath, dirname import requests -from testcontainers.compose import DockerCompose from tests.plugin import BasePluginTest class TestPlugin(BasePluginTest): - @classmethod - def setUpClass(cls): - cls.compose = DockerCompose(filepath=dirname(abspath(__file__))) - cls.compose.start() - cls.compose.wait_for(cls.url(cls.collector_address())) - - def test_request_plugin(self): + def test_plugin(self): print('traffic: ', requests.post(url=self.url(('consumer', '9090')))) time.sleep(3) - self.validate(expected_file_name=os.path.join(dirname(abspath(__file__)), 'expected.data.yml')) + self.validate() if __name__ == '__main__': diff --git a/tests/plugin/sw_http_wsgi/test_http_wsgi.py b/tests/plugin/sw_http_wsgi/test_http_wsgi.py index 856db043..05b42d24 100644 --- a/tests/plugin/sw_http_wsgi/test_http_wsgi.py +++ b/tests/plugin/sw_http_wsgi/test_http_wsgi.py @@ -15,31 +15,22 @@ # limitations under the License. # -import os import time import unittest -from os.path import abspath, dirname import requests -from testcontainers.compose import DockerCompose from tests.plugin import BasePluginTest class TestPlugin(BasePluginTest): - @classmethod - def setUpClass(cls): - cls.compose = DockerCompose(filepath=dirname(abspath(__file__))) - cls.compose.start() - cls.compose.wait_for(cls.url(cls.collector_address())) - - def test_request_plugin(self): + def test_plugin(self): print('traffic: ', requests.post(url=self.url(('consumer', '9090')))) time.sleep(3) - self.validate(expected_file_name=os.path.join(dirname(abspath(__file__)), 'expected.data.yml')) + self.validate() if __name__ == '__main__': diff --git a/tests/plugin/sw_requests/test_request.py b/tests/plugin/sw_requests/test_request.py index 856db043..05b42d24 100644 --- a/tests/plugin/sw_requests/test_request.py +++ b/tests/plugin/sw_requests/test_request.py @@ -15,31 +15,22 @@ # limitations under the License. # -import os import time import unittest -from os.path import abspath, dirname import requests -from testcontainers.compose import DockerCompose from tests.plugin import BasePluginTest class TestPlugin(BasePluginTest): - @classmethod - def setUpClass(cls): - cls.compose = DockerCompose(filepath=dirname(abspath(__file__))) - cls.compose.start() - cls.compose.wait_for(cls.url(cls.collector_address())) - - def test_request_plugin(self): + def test_plugin(self): print('traffic: ', requests.post(url=self.url(('consumer', '9090')))) time.sleep(3) - self.validate(expected_file_name=os.path.join(dirname(abspath(__file__)), 'expected.data.yml')) + self.validate() if __name__ == '__main__':