Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(profiler): Do not call getcwd from module root #2329

Merged
merged 4 commits into from
Aug 30, 2023
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
12 changes: 6 additions & 6 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,15 @@ def _capture_envelope(envelope):
SDK_INFO["name"] = sdk_name
logger.debug("Setting SDK name to '%s'", sdk_name)

if has_profiling_enabled(self.options):
try:
setup_profiler(self.options)
except Exception as e:
logger.debug("Can not set up profiler. (%s)", e)

finally:
_client_init_debug.set(old_debug)

if has_profiling_enabled(self.options):
try:
setup_profiler(self.options)
except ValueError as e:
logger.debug(str(e))

self._setup_instrumentation(self.options.get("functions_to_trace", []))

@property
Expand Down
5 changes: 1 addition & 4 deletions sentry_sdk/profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,10 @@ def teardown_profiler():
MAX_STACK_DEPTH = 128


CWD = os.getcwd()


def extract_stack(
raw_frame, # type: Optional[FrameType]
cache, # type: LRUCache
cwd=CWD, # type: str
cwd, # type: str
max_stack_depth=MAX_STACK_DEPTH, # type: int
):
# type: (...) -> ExtractedStack
Expand Down
37 changes: 30 additions & 7 deletions tests/test_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,10 @@ def test_extract_stack_with_max_depth(depth, max_stack_depth, actual_depth):
# increase the max_depth by the `base_stack_depth` to account
# for the extra frames pytest will add
_, frame_ids, frames = extract_stack(
frame, LRUCache(max_size=1), max_stack_depth=max_stack_depth + base_stack_depth
frame,
LRUCache(max_size=1),
max_stack_depth=max_stack_depth + base_stack_depth,
cwd=os.getcwd(),
)
assert len(frame_ids) == base_stack_depth + actual_depth
assert len(frames) == base_stack_depth + actual_depth
Expand All @@ -527,8 +530,9 @@ def test_extract_stack_with_max_depth(depth, max_stack_depth, actual_depth):
def test_extract_stack_with_cache(frame, depth):
# make sure cache has enough room or this test will fail
cache = LRUCache(max_size=depth)
_, _, frames1 = extract_stack(frame, cache)
_, _, frames2 = extract_stack(frame, cache)
cwd = os.getcwd()
_, _, frames1 = extract_stack(frame, cache, cwd=cwd)
_, _, frames2 = extract_stack(frame, cache, cwd=cwd)

assert len(frames1) > 0
assert len(frames2) > 0
Expand Down Expand Up @@ -667,7 +671,16 @@ def test_thread_scheduler_single_background_thread(scheduler_class):
)
@mock.patch("sentry_sdk.profiler.MAX_PROFILE_DURATION_NS", 1)
def test_max_profile_duration_reached(scheduler_class):
sample = [("1", extract_stack(get_frame(), LRUCache(max_size=1)))]
sample = [
(
"1",
extract_stack(
get_frame(),
LRUCache(max_size=1),
cwd=os.getcwd(),
),
),
]

with scheduler_class(frequency=1000) as scheduler:
transaction = Transaction(sampled=True)
Expand Down Expand Up @@ -711,8 +724,18 @@ def ensure_running(self):


sample_stacks = [
extract_stack(get_frame(), LRUCache(max_size=1), max_stack_depth=1),
extract_stack(get_frame(), LRUCache(max_size=1), max_stack_depth=2),
extract_stack(
get_frame(),
LRUCache(max_size=1),
max_stack_depth=1,
cwd=os.getcwd(),
),
extract_stack(
get_frame(),
LRUCache(max_size=1),
max_stack_depth=2,
cwd=os.getcwd(),
),
]


Expand Down Expand Up @@ -805,7 +828,7 @@ def ensure_running(self):
"stacks": [[0], [1, 0]],
"thread_metadata": thread_metadata,
},
id="two identical stacks",
id="two different stacks",
),
],
)
Expand Down