Skip to content

Commit 2c975a1

Browse files
Fix for issue #53: last_frame_line_no is None (#58)
- When last_frame_line_no is None, drop the frame as we can't get the line of code associated from the linecache. - Adjust unit tests to make them consistent. --------- Co-authored-by: Henry Huynh <82241632+henryhuynh210@users.noreply.github.com>
1 parent e1cbc9a commit 2c975a1

File tree

12 files changed

+63
-37
lines changed

12 files changed

+63
-37
lines changed

.github/workflows/python-package.yml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,28 @@ jobs:
1414

1515
runs-on: ${{ matrix.os }}
1616
strategy:
17+
fail-fast: false
1718
matrix:
18-
os: [ubuntu-latest, windows-latest, macos-latest]
19-
python-version: ['3.6', '3.7', '3.8', '3.9']
19+
os: [ubuntu-20.04, ubuntu-latest, windows-latest, macos-latest]
20+
python-version: ['3.6', '3.7', '3.8', '3.9', '3.10', '3.11']
21+
exclude:
22+
- os: ubuntu-latest
23+
python-version: '3.6'
24+
- os: ubuntu-20.04
25+
python-version: '3.7'
26+
- os: ubuntu-20.04
27+
python-version: '3.8'
28+
- os: ubuntu-20.04
29+
python-version: '3.9'
30+
- os: ubuntu-20.04
31+
python-version: '3.10'
32+
- os: ubuntu-20.04
33+
python-version: '3.11'
2034

2135
steps:
22-
- uses: actions/checkout@v2
36+
- uses: actions/checkout@v3
2337
- name: Set up Python ${{ matrix.python-version }}
24-
uses: actions/setup-python@v2
38+
uses: actions/setup-python@v3
2539
with:
2640
python-version: ${{ matrix.python-version }}
2741
- name: Install pip

.github/workflows/python-publish-live.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ jobs:
1212
deploy:
1313
runs-on: ubuntu-latest
1414
steps:
15-
- uses: actions/checkout@v2
15+
- uses: actions/checkout@v3
1616
- name: Set up Python
17-
uses: actions/setup-python@v2
17+
uses: actions/setup-python@v3
1818
with:
1919
python-version: '3.x'
2020
- name: Install dependencies

.github/workflows/python-publish-test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ jobs:
1212
deploy:
1313
runs-on: ubuntu-latest
1414
steps:
15-
- uses: actions/checkout@v2
15+
- uses: actions/checkout@v3
1616
- name: Set up Python
17-
uses: actions/setup-python@v2
17+
uses: actions/setup-python@v3
1818
with:
1919
python-version: '3.x'
2020
- name: Install dependencies

CHANGELOG.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
CHANGELOG
33
=========
44

5+
1.2.5 (layer_v12)
6+
===================
7+
* Fix bug which causes agent to crash if line_no was None.
8+
59
1.2.4 (layer_v11)
610
===================
711
* Updated lambda bootstrap code to support profiling python 3.9 lambda functions.
812

913
1.2.3 (layer_v10)
1014
===================
11-
* Fix bug to sent agent overhead in the rigth format: int and as part of memoryInMB (instead of the previous string as part of memory_usage_mb).
15+
* Fix bug to sent agent overhead in the right format: int and as part of memoryInMB (instead of the previous string as part of memory_usage_mb).
1216

1317
1.2.2 (layer_v9)
1418
===================

codeguru_profiler_agent/agent_metadata/agent_metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# NOTE: Please do not alter the value for the following constants without the full knowledge of the use of them.
1010
# These constants are used in several scripts, including setup.py.
1111
__agent_name__ = "CodeGuruProfiler-python"
12-
__agent_version__ = "1.2.4"
12+
__agent_version__ = "1.2.5"
1313

1414

1515
def look_up_fleet_info(

codeguru_profiler_agent/sampler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ def sample(self):
5656
seen_threads_count=all_threads_count)
5757

5858
def _get_all_threads(self):
59-
return self._thread_lister._current_frames().items()
59+
return list(self._thread_lister._current_frames().items())
6060

6161
def _threads_to_sample_from(self, all_threads):
6262
if len(all_threads) > self._max_threads:
63+
if isinstance(all_threads, dict):
64+
all_threads = list(all_threads.keys())
6365
return random.sample(all_threads, self._max_threads)
6466
else:
65-
return all_threads
67+
return list(all_threads)

codeguru_profiler_agent/sampling_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ def _extract_stack(stack, max_depth):
7373
)
7474
if len(result) < max_depth:
7575
last_frame, last_frame_line_no = stack[-1]
76-
_maybe_append_synthetic_frame(result, last_frame, last_frame_line_no)
76+
# If the line_no is None, ignore the line as we can't get the line
77+
# of code from the line cache
78+
if last_frame_line_no != None:
79+
_maybe_append_synthetic_frame(result, last_frame, last_frame_line_no)
7780
return result[:max_depth]
7881

7982

test/acceptance/test_end_to_end_profile_and_save_to_file.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import shutil
44
import tempfile
55
import os
6+
import time
67

78
from datetime import timedelta
89
from unittest.mock import patch
@@ -56,14 +57,15 @@ def test_it_samples_and_saves_a_profile_to_a_file(self):
5657
profiler = Profiler(
5758
profiling_group_name=DUMMY_TEST_PROFILING_GROUP_NAME,
5859
environment_override={
59-
"initial_sampling_interval": timedelta(),
60+
"initial_sampling_interval": timedelta(seconds=1),
6061
"reporting_mode": "file",
6162
"file_prefix": file_prefix,
6263
'agent_metadata': AgentMetadata(fleet_info=DefaultFleetInfo())
6364
})
6465

6566
try:
6667
profiler.start()
68+
time.sleep(2)
6769
finally:
6870
profiler.stop()
6971

@@ -74,7 +76,12 @@ def test_it_samples_and_saves_a_profile_to_a_file(self):
7476
os.listdir(self.temporary_directory)[0]))
7577

7678
with (open(resulting_profile_path)) as profiling_result_file:
77-
resulting_json = json.loads(profiling_result_file.read())
79+
file_content = profiling_result_file.read()
80+
81+
try:
82+
resulting_json = json.loads(file_content)
83+
except json.JSONDecodeError as e:
84+
raise
7885

7986
self.assert_valid_agent_metadata(resulting_json["agentMetadata"])
8087
assert test_start_time <= resulting_json["start"] <= resulting_json["end"] <= test_end_time

test/integration/test_live_backend_reporting.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import boto3
22
import pytest
3-
import socket
3+
import os
44
import time
5+
import platform
56

67
from datetime import timedelta
78

@@ -19,8 +20,7 @@
1920

2021

2122
@pytest.mark.skipif(
22-
socket.getfqdn().endswith("internal.cloudapp.net"), # hosts running ubuntu and windows in GitHub
23-
socket.getfqdn().endswith("ip6.arpa"), # hosts running macs in GitHub
23+
os.getenv("GITHUB_ACTIONS") == "true",
2424
reason="This integration test is skipped on any shared fleet from Amazon or GitHub "
2525
"because it needs credentials to access the backend service. "
2626
"For information on how to run this locally, read the README.md file from the test/integration/ folder.")

test/unit/aws_lambda/test_profiler_decorator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
import time
23
import codeguru_profiler_agent.aws_lambda.profiler_decorator
34

45
from unittest.mock import MagicMock
@@ -77,11 +78,13 @@ def around(self):
7778
@with_lambda_profiler(profiling_group_name="pg_name", region_name="eu-north-1",
7879
environment_override={'cpu_limit_percentage': 42}, env=self.env)
7980
def handler_function(event, context):
81+
time.sleep(0.5)
8082
return True
8183

8284
self.handler = handler_function
8385
yield \
8486
codeguru_profiler_agent.aws_lambda.profiler_decorator.clear_static_profiler()
87+
codeguru_profiler_agent.aws_lambda.profiler_decorator.clear_static_profiler()
8588

8689
def test_given_profiling_group_is_used(self):
8790
self.handler({}, self.context)

0 commit comments

Comments
 (0)