Skip to content

Commit

Permalink
fix(NewRelicContextFormatter): add cause and context to stack traces
Browse files Browse the repository at this point in the history
  • Loading branch information
sweco committed Nov 29, 2024
1 parent 4853f32 commit 7842509
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
4 changes: 2 additions & 2 deletions newrelic/api/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import logging
import re
import warnings
from traceback import format_tb
from traceback import format_exception

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

if stack_trace_limit is None or stack_trace_limit > 0:
if exc_info[2] is not None:
stack_trace = "".join(format_tb(exc_info[2], limit=stack_trace_limit)) or None
stack_trace = "".join(format_exception(*exc_info, limit=stack_trace_limit)) or None
else:
stack_trace = None
formatted["error.stack_trace"] = stack_trace
Expand Down
22 changes: 13 additions & 9 deletions tests/agent_features/test_logs_in_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import sys

from io import StringIO as Buffer
from traceback import format_tb
from traceback import format_exception

import pytest

Expand Down Expand Up @@ -237,12 +237,14 @@ def test_newrelic_logger_error_inside_transaction_no_stack_trace(log_buffer):

@background_task()
def test_newrelic_logger_error_inside_transaction_with_stack_trace(log_buffer_with_stack_trace):
expected_stack_trace = ""
try:
raise ExceptionForTest
try:
raise ExceptionForTest("cause")
except ExceptionForTest:
raise ExceptionForTest("exception-with-cause")
except ExceptionForTest:
_logger.exception("oops")
expected_stack_trace = "".join(format_tb(sys.exc_info()[2]))
expected_stack_trace = "".join(format_exception(*sys.exc_info()))

log_buffer_with_stack_trace.seek(0)
message = json.load(log_buffer_with_stack_trace)
Expand Down Expand Up @@ -271,7 +273,7 @@ def test_newrelic_logger_error_inside_transaction_with_stack_trace(log_buffer_wi
"thread.name": "MainThread",
"process.name": "MainProcess",
"error.class": "test_logs_in_context:ExceptionForTest",
"error.message": "",
"error.message": "exception-with-cause",
"error.expected": False
}
expected_extra_txn_keys = (
Expand Down Expand Up @@ -331,12 +333,14 @@ def test_newrelic_logger_error_outside_transaction_no_stack_trace(log_buffer):


def test_newrelic_logger_error_outside_transaction_with_stack_trace(log_buffer_with_stack_trace):
expected_stack_trace = ""
try:
raise ExceptionForTest
try:
raise ExceptionForTest("cause") # Simulate cause
except ExceptionForTest:
raise ExceptionForTest("exception-with-cause")
except ExceptionForTest:
_logger.exception("oops")
expected_stack_trace = "".join(format_tb(sys.exc_info()[2]))
expected_stack_trace = "".join(format_exception(*sys.exc_info()))

log_buffer_with_stack_trace.seek(0)
message = json.load(log_buffer_with_stack_trace)
Expand Down Expand Up @@ -365,7 +369,7 @@ def test_newrelic_logger_error_outside_transaction_with_stack_trace(log_buffer_w
"thread.name": "MainThread",
"process.name": "MainProcess",
"error.class": "test_logs_in_context:ExceptionForTest",
"error.message": "",
"error.message": "exception-with-cause",
}
expected_extra_txn_keys = (
"entity.guid",
Expand Down

0 comments on commit 7842509

Please sign in to comment.