Skip to content

Commit 7be4ac7

Browse files
authored
Remove possible exceptions in complete_root. (#181)
* Remove possible exceptions in complete_root. * Correct tests to follow new logic in complete_root * Add str method to trace cache * Restore exception to complete_root, and catch * Add repr method to time trace
1 parent 56923f5 commit 7be4ac7

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

newrelic/api/time_trace.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ def settings(self):
6767
def _is_leaf(self):
6868
return self.child_count == len(self.children)
6969

70+
def __str__(self):
71+
name = getattr(self, "name", None)
72+
return "<id:%d name:%s __str__:%s>" % (id(self), name, super(TimeTrace, self).__str__())
73+
74+
def __repr__(self):
75+
return str(self)
76+
7077
def __enter__(self):
7178
self.parent = parent = self.parent or current_trace()
7279
if not parent:

newrelic/api/transaction.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,11 @@ def __exit__(self, exc, value, tb):
396396
_logger.exception('Runtime instrumentation error. Attempt to '
397397
'drop the trace but where none is active. '
398398
'Report this issue to New Relic support.'),
399-
raise
399+
return
400+
except Exception:
401+
_logger.exception('Runtime instrumentation error. Exception '
402+
'occurred during exit. Report this issue to New Relic support.')
403+
return
400404

401405
# Record error if one was registered.
402406

newrelic/core/trace_cache.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ class TraceCache(object):
100100
def __init__(self):
101101
self._cache = weakref.WeakValueDictionary()
102102

103+
def __str__(self):
104+
return "<newrelic.core.trace_cache.TraceCache object at 0x%x %s>" % (id(self), str(dict(self._cache.items())))
105+
106+
def __repr__(self):
107+
return str(self)
108+
103109
def current_thread_id(self):
104110
"""Returns the thread ID for the caller.
105111

tests/agent_features/test_background_task.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,18 @@ def test_sentinel_exited_complete_root_exception():
3131
This test forces a transaction to exit while it still has an active trace
3232
this causes an exception to be raised in TraceCache complete_root(). It
3333
verifies that the sentinel.exited property is set to true if an exception
34-
is raised in complete_root()
34+
is raised in complete_root(), and that the exception is caught.
3535
"""
36-
expected_error = "not the current trace"
3736

38-
try:
39-
txn = None
40-
sentinel = None
41-
txn = BackgroundTask(application_instance(), "Parent")
42-
txn.__enter__()
43-
sentinel = txn.root_span
44-
trace = FunctionTrace("trace")
45-
trace.__enter__()
46-
txn.__exit__(None, None, None)
47-
assert False, "Did not raise exception"
48-
except RuntimeError as e:
49-
assert str(e) == expected_error
50-
finally:
51-
assert sentinel.exited
52-
# Make sure to exit properly so cleanup is performed
53-
trace.__exit__(None, None, None)
54-
txn.__exit__(None, None, None)
37+
txn = None
38+
sentinel = None
39+
txn = BackgroundTask(application_instance(), "Parent")
40+
txn.__enter__()
41+
sentinel = txn.root_span
42+
trace = FunctionTrace("trace")
43+
trace.__enter__()
44+
txn.__exit__(None, None, None)
45+
assert sentinel.exited
46+
# Make sure to exit properly so cleanup is performed
47+
trace.__exit__(None, None, None)
48+
txn.__exit__(None, None, None)

0 commit comments

Comments
 (0)