Skip to content

Commit

Permalink
fix(profiling): reduce log spam from lock collector (#12303)
Browse files Browse the repository at this point in the history
  • Loading branch information
taegyunkim authored Feb 12, 2025
1 parent 84949f3 commit 0fcafa2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
62 changes: 28 additions & 34 deletions ddtrace/profiling/collector/_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def _acquire(self, inner_func, *args, **kwargs):

self._self_recorder.push_event(event)
except Exception as e:
LOG.warning("Error recording lock acquire event: %s", e)
LOG.debug("Failed to record a lock acquire event: %s", e)
pass # nosec

def acquire(self, *args, **kwargs):
Expand Down Expand Up @@ -263,42 +263,36 @@ def _find_self_name(self, var_dict: typing.Dict):
return None

# Get lock acquire/release call location and variable name the lock is assigned to
# This function propagates ValueError if the frame depth is <= 3.
def _maybe_update_self_name(self):
if self._self_name is not None:
return
try:
# We expect the call stack to be like this:
# 0: this
# 1: _acquire/_release
# 2: acquire/release (or __enter__/__exit__)
# 3: caller frame
if config.enable_asserts:
frame = sys._getframe(1)
if frame.f_code.co_name not in {"_acquire", "_release"}:
raise AssertionError("Unexpected frame %s" % frame.f_code.co_name)
frame = sys._getframe(2)
if frame.f_code.co_name not in {
"acquire",
"release",
"__enter__",
"__exit__",
"__aenter__",
"__aexit__",
}:
raise AssertionError("Unexpected frame %s" % frame.f_code.co_name)
frame = sys._getframe(3)

# First, look at the local variables of the caller frame, and then the global variables
self._self_name = self._find_self_name(frame.f_locals) or self._find_self_name(frame.f_globals)

if not self._self_name:
self._self_name = ""
LOG.debug(
"Failed to get lock variable name, we only support local/global variables and their attributes."
)

except Exception as e:
LOG.warning("Error getting lock acquire/release call location and variable name: %s", e)
# We expect the call stack to be like this:
# 0: this
# 1: _acquire/_release
# 2: acquire/release (or __enter__/__exit__)
# 3: caller frame
if config.enable_asserts:
frame = sys._getframe(1)
if frame.f_code.co_name not in {"_acquire", "_release"}:
raise AssertionError("Unexpected frame %s" % frame.f_code.co_name)
frame = sys._getframe(2)
if frame.f_code.co_name not in {
"acquire",
"release",
"__enter__",
"__exit__",
"__aenter__",
"__aexit__",
}:
raise AssertionError("Unexpected frame %s" % frame.f_code.co_name)
frame = sys._getframe(3)

# First, look at the local variables of the caller frame, and then the global variables
self._self_name = self._find_self_name(frame.f_locals) or self._find_self_name(frame.f_globals)

if not self._self_name:
self._self_name = ""


class FunctionWrapper(wrapt.FunctionWrapper):
Expand Down
6 changes: 6 additions & 0 deletions releasenotes/notes/lock-log-df92d0fddb165ebe.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
profiling: fixes an issue where the profiler unnecessarily outputs log
messages when it was unable to record a lock acquire event.

0 comments on commit 0fcafa2

Please sign in to comment.