Skip to content

Commit 019b019

Browse files
swecomergify[bot]TimPansino
authored
fix(NewRelicContextFormatter): add cause and context to stack traces (#1266)
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Timothy Pansino <[email protected]>
1 parent 84a5d46 commit 019b019

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

newrelic/api/log.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import logging
1717
import re
1818
import warnings
19-
from traceback import format_tb
19+
from traceback import format_exception
2020

2121
from newrelic.api.application import application_instance
2222
from newrelic.api.time_trace import get_linking_metadata
@@ -87,7 +87,7 @@ def format_exc_info(cls, exc_info, stack_trace_limit=0):
8787

8888
if stack_trace_limit is None or stack_trace_limit > 0:
8989
if exc_info[2] is not None:
90-
stack_trace = "".join(format_tb(exc_info[2], limit=stack_trace_limit)) or None
90+
stack_trace = "".join(format_exception(*exc_info, limit=stack_trace_limit)) or None
9191
else:
9292
stack_trace = None
9393
formatted["error.stack_trace"] = stack_trace

tests/agent_features/test_logs_in_context.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import sys
1818

1919
from io import StringIO as Buffer
20-
from traceback import format_tb
20+
from traceback import format_exception
2121

2222
import pytest
2323

@@ -237,12 +237,14 @@ def test_newrelic_logger_error_inside_transaction_no_stack_trace(log_buffer):
237237

238238
@background_task()
239239
def test_newrelic_logger_error_inside_transaction_with_stack_trace(log_buffer_with_stack_trace):
240-
expected_stack_trace = ""
241240
try:
242-
raise ExceptionForTest
241+
try:
242+
raise ExceptionForTest("cause")
243+
except ExceptionForTest:
244+
raise ExceptionForTest("exception-with-cause")
243245
except ExceptionForTest:
244246
_logger.exception("oops")
245-
expected_stack_trace = "".join(format_tb(sys.exc_info()[2]))
247+
expected_stack_trace = "".join(format_exception(*sys.exc_info()))
246248

247249
log_buffer_with_stack_trace.seek(0)
248250
message = json.load(log_buffer_with_stack_trace)
@@ -271,7 +273,7 @@ def test_newrelic_logger_error_inside_transaction_with_stack_trace(log_buffer_wi
271273
"thread.name": "MainThread",
272274
"process.name": "MainProcess",
273275
"error.class": "test_logs_in_context:ExceptionForTest",
274-
"error.message": "",
276+
"error.message": "exception-with-cause",
275277
"error.expected": False
276278
}
277279
expected_extra_txn_keys = (
@@ -331,12 +333,14 @@ def test_newrelic_logger_error_outside_transaction_no_stack_trace(log_buffer):
331333

332334

333335
def test_newrelic_logger_error_outside_transaction_with_stack_trace(log_buffer_with_stack_trace):
334-
expected_stack_trace = ""
335336
try:
336-
raise ExceptionForTest
337+
try:
338+
raise ExceptionForTest("cause")
339+
except ExceptionForTest:
340+
raise ExceptionForTest("exception-with-cause")
337341
except ExceptionForTest:
338342
_logger.exception("oops")
339-
expected_stack_trace = "".join(format_tb(sys.exc_info()[2]))
343+
expected_stack_trace = "".join(format_exception(*sys.exc_info()))
340344

341345
log_buffer_with_stack_trace.seek(0)
342346
message = json.load(log_buffer_with_stack_trace)
@@ -365,7 +369,7 @@ def test_newrelic_logger_error_outside_transaction_with_stack_trace(log_buffer_w
365369
"thread.name": "MainThread",
366370
"process.name": "MainProcess",
367371
"error.class": "test_logs_in_context:ExceptionForTest",
368-
"error.message": "",
372+
"error.message": "exception-with-cause",
369373
}
370374
expected_extra_txn_keys = (
371375
"entity.guid",

0 commit comments

Comments
 (0)