Skip to content

Commit 5865ca8

Browse files
Fix bug in module path for Fargate and ignore the "/./" for Windows.
1 parent ac27849 commit 5865ca8

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

codeguru_profiler_agent/sdk_reporter/profile_encoder.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def _get_module_path(file_path, sys_paths):
1717
will get turned into great_app.simple_expansions.simple_interface given that the syspath contains
1818
/tmp/bin/python/site-packages
1919
20-
We are making sure we're removing the current path.
20+
We are making sure we're removing the current path for this special usecase, by checking if it contains "/./".
2121
For example, '/Users/mirelap/Documents/workspace/JSON/aws-codeguru-profiler-python-demo-application/sample-demo-django-app/./polls/views.py'
2222
will get turned into `polls.views' given that the file path contains the current path.
2323
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):
4040

4141
# remove suffix
4242
module_path = str(Path(module_path).with_suffix(""))
43+
4344
# remove drive (applicable for WINDOWS customers)
4445
module_path = os.path.splitdrive(module_path)[1]
4546

@@ -52,12 +53,17 @@ def _get_module_path(file_path, sys_paths):
5253

5354

5455
def _remove_prefix_path(module_path, sys_paths):
55-
current_path = str(Path().absolute())
56-
if current_path in module_path:
57-
return module_path.replace(current_path, "").replace("/./", "/")
56+
if "/./" in module_path and platform.system() != "Windows":
57+
module_path = module_path.replace("/./", "/")
58+
current_path = str(Path().absolute())
59+
if current_path != "/": # this may be Fargate
60+
return module_path.replace(current_path, "")
61+
return module_path
62+
5863
for root in sys_paths:
5964
if root in module_path:
6065
return module_path.replace(root, "")
66+
6167
return module_path
6268

6369
class ProfileEncoder:

test/unit/sdk_reporter/test_sdk_profile_encoder.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,19 @@ def before(self):
336336
self.current_path = str(Path().absolute())
337337
self.subject = ProfileEncoder(gzip=False, environment=environment).ModulePathExtractor(sys_path=[])
338338

339-
def test_it_removes_current_path(self):
340-
file_path = self.current_path + '/polls/views.py'
341-
assert self.subject.get_module_path(file_path) == "polls.views"
342-
343339
def test_it_removes_current_path_and_slash_and_dot(self):
344340
file_path = self.current_path + '/./polls/views.py'
341+
if platform.system() == "Windows":
342+
import os
343+
# This ignores the first D:.
344+
# This test just asserts the current behaviour, though the "/./" removal is not set for Windows.
345+
expected = self.current_path.replace(os.sep, ".")[3:] + ".polls.views"
346+
else:
347+
expected = "polls.views"
348+
assert self.subject.get_module_path(file_path) == expected
349+
350+
def test_it_removes_slash_and_dot(self):
351+
file_path = '/./polls/views.py'
345352
assert self.subject.get_module_path(file_path) == "polls.views"
346353

347354
def test_it_does_nothing_when_file_path_has_no_current_path(self):

0 commit comments

Comments
 (0)