Skip to content
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
14 changes: 10 additions & 4 deletions codeguru_profiler_agent/sdk_reporter/profile_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(..)
Expand All @@ -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]

Expand All @@ -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:
Expand Down
15 changes: 11 additions & 4 deletions test/unit/sdk_reporter/test_sdk_profile_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not understand this, could you explain with a concrete example what we expect for each Windows or other os?
I kind of expected the module path the turn out the same for all os

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Windows I'm not updating the module path because I didn't test uwsgi on Windows, this is just a test to show what it is now.

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):
Expand Down