From 5865ca8aa49f141fc91aa303d7e8d1e296aa234a Mon Sep 17 00:00:00 2001 From: Mirela Popoveniuc Date: Tue, 29 Jun 2021 15:01:55 +0100 Subject: [PATCH] Fix bug in module path for Fargate and ignore the "/./" for Windows. --- .../sdk_reporter/profile_encoder.py | 14 ++++++++++---- .../unit/sdk_reporter/test_sdk_profile_encoder.py | 15 +++++++++++---- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/codeguru_profiler_agent/sdk_reporter/profile_encoder.py b/codeguru_profiler_agent/sdk_reporter/profile_encoder.py index 048541a..d1e44f7 100644 --- a/codeguru_profiler_agent/sdk_reporter/profile_encoder.py +++ b/codeguru_profiler_agent/sdk_reporter/profile_encoder.py @@ -17,7 +17,7 @@ def _get_module_path(file_path, sys_paths): will get turned into great_app.simple_expansions.simple_interface given that the syspath contains /tmp/bin/python/site-packages - We are making sure we're removing the current path. + We are making sure we're removing the current path for this special usecase, by checking if it contains "/./". For example, '/Users/mirelap/Documents/workspace/JSON/aws-codeguru-profiler-python-demo-application/sample-demo-django-app/./polls/views.py' will get turned into `polls.views' given that the file path contains the current path. This should not happen usually, but we've found a case where the "/." is added when calling traceback.walk_stack(..) @@ -40,6 +40,7 @@ def _get_module_path(file_path, sys_paths): # remove suffix module_path = str(Path(module_path).with_suffix("")) + # remove drive (applicable for WINDOWS customers) module_path = os.path.splitdrive(module_path)[1] @@ -52,12 +53,17 @@ def _get_module_path(file_path, sys_paths): def _remove_prefix_path(module_path, sys_paths): - current_path = str(Path().absolute()) - if current_path in module_path: - return module_path.replace(current_path, "").replace("/./", "/") + if "/./" in module_path and platform.system() != "Windows": + module_path = module_path.replace("/./", "/") + current_path = str(Path().absolute()) + if current_path != "/": # this may be Fargate + return module_path.replace(current_path, "") + return module_path + for root in sys_paths: if root in module_path: return module_path.replace(root, "") + return module_path class ProfileEncoder: diff --git a/test/unit/sdk_reporter/test_sdk_profile_encoder.py b/test/unit/sdk_reporter/test_sdk_profile_encoder.py index 7d426a7..2332a63 100644 --- a/test/unit/sdk_reporter/test_sdk_profile_encoder.py +++ b/test/unit/sdk_reporter/test_sdk_profile_encoder.py @@ -336,12 +336,19 @@ def before(self): self.current_path = str(Path().absolute()) self.subject = ProfileEncoder(gzip=False, environment=environment).ModulePathExtractor(sys_path=[]) - def test_it_removes_current_path(self): - file_path = self.current_path + '/polls/views.py' - assert self.subject.get_module_path(file_path) == "polls.views" - def test_it_removes_current_path_and_slash_and_dot(self): file_path = self.current_path + '/./polls/views.py' + if platform.system() == "Windows": + import os + # This ignores the first D:. + # This test just asserts the current behaviour, though the "/./" removal is not set for Windows. + expected = self.current_path.replace(os.sep, ".")[3:] + ".polls.views" + else: + expected = "polls.views" + assert self.subject.get_module_path(file_path) == expected + + def test_it_removes_slash_and_dot(self): + file_path = '/./polls/views.py' assert self.subject.get_module_path(file_path) == "polls.views" def test_it_does_nothing_when_file_path_has_no_current_path(self):