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(NewRelicContextFormatter): add cause and context to stack traces #1266

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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")
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