Skip to content

Commit 65246e7

Browse files
TimPansinolrafeei
andauthored
Fix Trace Finalizer Crashes (#652)
* Patch crashes in various traces with None settings * Add tests for graphql trace types to unittests * Add test to ensure traces don't crash in finalizer * [Mega-Linter] Apply linters fixes * Bump tests Co-authored-by: TimPansino <[email protected]> Co-authored-by: Lalleh Rafeei <[email protected]>
1 parent 9a56f3a commit 65246e7

6 files changed

+548
-438
lines changed

newrelic/api/database_trace.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ def _log_async_warning(self):
127127

128128
def finalize_data(self, transaction, exc=None, value=None, tb=None):
129129
self.stack_trace = None
130+
self.sql_format = "off"
130131

131132
connect_params = None
132133
cursor_params = None
@@ -206,8 +207,8 @@ def finalize_data(self, transaction, exc=None, value=None, tb=None):
206207
transaction._explain_plan_count += 1
207208

208209
self.sql_format = (
209-
tt.record_sql if tt.record_sql else ""
210-
) # If tt.record_sql is None, then the empty string will default to sql being obfuscated
210+
tt.record_sql if tt.record_sql else "off"
211+
) # If tt.record_sql is None, then default to sql being off
211212
self.connect_params = connect_params
212213
self.cursor_params = cursor_params
213214
self.sql_parameters = sql_parameters

newrelic/api/graphql_trace.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,13 @@ def finalize_data(self, transaction, exc=None, value=None, tb=None):
6969
self._add_agent_attribute("graphql.operation.type", self.operation_type)
7070
self._add_agent_attribute("graphql.operation.name", self.operation_name)
7171

72+
settings = transaction.settings
73+
if settings and settings.agent_limits and settings.agent_limits.sql_query_length_maximum:
74+
limit = transaction.settings.agent_limits.sql_query_length_maximum
75+
else:
76+
limit = 0
77+
7278
# Attach formatted graphql
73-
limit = transaction.settings.agent_limits.sql_query_length_maximum
7479
self.graphql = graphql = self.formatted[:limit]
7580
self._add_agent_attribute("graphql.operation.query", graphql)
7681

newrelic/api/transaction.py

+24-12
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@
2525
import weakref
2626
from collections import OrderedDict
2727

28-
from newrelic.api.application import application_instance
2928
import newrelic.core.database_node
3029
import newrelic.core.error_node
31-
from newrelic.core.log_event_node import LogEventNode
3230
import newrelic.core.root_node
3331
import newrelic.core.transaction_node
3432
import newrelic.packages.six as six
33+
from newrelic.api.application import application_instance
3534
from newrelic.api.time_trace import TimeTrace, get_linking_metadata
3635
from newrelic.common.encoding_utils import (
3736
DistributedTracePayload,
@@ -63,6 +62,7 @@
6362
)
6463
from newrelic.core.config import DEFAULT_RESERVOIR_SIZE, LOG_EVENT_RESERVOIR_SIZE
6564
from newrelic.core.custom_event import create_custom_event
65+
from newrelic.core.log_event_node import LogEventNode
6666
from newrelic.core.stack_trace import exception_stack
6767
from newrelic.core.stats_engine import CustomMetrics, SampledDataSet
6868
from newrelic.core.thread_utilization import utilization_tracker
@@ -324,8 +324,12 @@ def __init__(self, application, enabled=None, source=None):
324324
self.enabled = True
325325

326326
if self._settings:
327-
self._custom_events = SampledDataSet(capacity=self._settings.event_harvest_config.harvest_limits.custom_event_data)
328-
self._log_events = SampledDataSet(capacity=self._settings.event_harvest_config.harvest_limits.log_event_data)
327+
self._custom_events = SampledDataSet(
328+
capacity=self._settings.event_harvest_config.harvest_limits.custom_event_data
329+
)
330+
self._log_events = SampledDataSet(
331+
capacity=self._settings.event_harvest_config.harvest_limits.log_event_data
332+
)
329333
else:
330334
self._custom_events = SampledDataSet(capacity=DEFAULT_RESERVOIR_SIZE)
331335
self._log_events = SampledDataSet(capacity=LOG_EVENT_RESERVOIR_SIZE)
@@ -1473,31 +1477,35 @@ def set_transaction_name(self, name, group=None, priority=None):
14731477
self._group = group
14741478
self._name = name
14751479

1476-
14771480
def record_log_event(self, message, level=None, timestamp=None, priority=None):
14781481
settings = self.settings
1479-
if not (settings and settings.application_logging and settings.application_logging.enabled and settings.application_logging.forwarding and settings.application_logging.forwarding.enabled):
1482+
if not (
1483+
settings
1484+
and settings.application_logging
1485+
and settings.application_logging.enabled
1486+
and settings.application_logging.forwarding
1487+
and settings.application_logging.forwarding.enabled
1488+
):
14801489
return
1481-
1490+
14821491
timestamp = timestamp if timestamp is not None else time.time()
14831492
level = str(level) if level is not None else "UNKNOWN"
1484-
1493+
14851494
if not message or message.isspace():
14861495
_logger.debug("record_log_event called where message was missing. No log event will be sent.")
14871496
return
1488-
1497+
14891498
message = truncate(message, MAX_LOG_MESSAGE_LENGTH)
14901499

14911500
event = LogEventNode(
14921501
timestamp=timestamp,
14931502
level=level,
14941503
message=message,
1495-
attributes=get_linking_metadata(),
1504+
attributes=get_linking_metadata(),
14961505
)
14971506

14981507
self._log_events.add(event, priority=priority)
14991508

1500-
15011509
def record_exception(self, exc=None, value=None, tb=None, params=None, ignore_errors=None):
15021510
# Deprecation Warning
15031511
warnings.warn(
@@ -1603,6 +1611,8 @@ def _process_node(self, node):
16031611

16041612
if type(node) is newrelic.core.database_node.DatabaseNode:
16051613
settings = self._settings
1614+
if not settings:
1615+
return
16061616
if not settings.collect_traces:
16071617
return
16081618
if not settings.slow_sql.enabled and not settings.transaction_tracer.explain_enabled:
@@ -1869,7 +1879,9 @@ def record_log_event(message, level=None, timestamp=None, application=None, prio
18691879
"record_log_event has been called but no transaction or application was running. As a result, "
18701880
"the following event has not been recorded. message: %r level: %r timestamp %r. To correct "
18711881
"this problem, supply an application object as a parameter to this record_log_event call.",
1872-
message, level, timestamp,
1882+
message,
1883+
level,
1884+
timestamp,
18731885
)
18741886
elif application.enabled:
18751887
application.record_log_event(message, level, timestamp, priority=priority)

0 commit comments

Comments
 (0)