|
41 | 41 | try: |
42 | 42 | import grpc |
43 | 43 |
|
44 | | - from newrelic.core.infinite_tracing_pb2 import ( |
45 | | - Span as _, # NOQA # pylint: disable=W0611,C0412 |
| 44 | + from newrelic.core.infinite_tracing_pb2 import ( # pylint: disable=W0611,C0412 # noqa: F401 |
| 45 | + Span, |
46 | 46 | ) |
47 | 47 | except ImportError: |
48 | 48 | grpc = None |
@@ -1034,13 +1034,12 @@ def apply_server_side_settings(server_side_config=None, settings=_settings): |
1034 | 1034 | apply_config_setting(settings_snapshot, "event_harvest_config.whitelist", frozenset(harvest_limits)) |
1035 | 1035 |
|
1036 | 1036 | # Override span event harvest config |
1037 | | - span_event_harvest_config = server_side_config.get('span_event_harvest_config', {}) |
| 1037 | + span_event_harvest_config = server_side_config.get("span_event_harvest_config", {}) |
1038 | 1038 | span_event_harvest_limit = span_event_harvest_config.get("harvest_limit", None) |
1039 | 1039 | if span_event_harvest_limit is not None: |
1040 | 1040 | apply_config_setting( |
1041 | | - settings_snapshot, |
1042 | | - 'event_harvest_config.harvest_limits.span_event_data', |
1043 | | - span_event_harvest_limit) |
| 1041 | + settings_snapshot, "event_harvest_config.harvest_limits.span_event_data", span_event_harvest_limit |
| 1042 | + ) |
1044 | 1043 |
|
1045 | 1044 | # This will be removed at some future point |
1046 | 1045 | # Special case for account_id which will be sent instead of |
@@ -1103,53 +1102,71 @@ def ignore_status_code(status): |
1103 | 1102 | def is_expected_error( |
1104 | 1103 | exc_info, |
1105 | 1104 | status_code=None, |
| 1105 | + settings=None, |
1106 | 1106 | ): |
| 1107 | + """Check if an error is expected based on rules matching. Default is False when settings lookup fails.""" |
1107 | 1108 | return error_matches_rules( |
1108 | 1109 | "expected", |
1109 | 1110 | exc_info, |
1110 | 1111 | status_code=status_code, |
| 1112 | + settings=settings, |
1111 | 1113 | ) |
1112 | 1114 |
|
1113 | 1115 |
|
1114 | 1116 | def should_ignore_error( |
1115 | 1117 | exc_info, |
1116 | 1118 | status_code=None, |
| 1119 | + settings=None, |
1117 | 1120 | ): |
| 1121 | + """Check if an error should be ignored based on rules matching. Default is True when settings lookup fails.""" |
1118 | 1122 | return error_matches_rules( |
1119 | 1123 | "ignore", |
1120 | 1124 | exc_info, |
1121 | 1125 | status_code=status_code, |
| 1126 | + settings=settings, |
1122 | 1127 | ) |
1123 | 1128 |
|
1124 | 1129 |
|
1125 | 1130 | def error_matches_rules( |
1126 | 1131 | rules_prefix, |
1127 | 1132 | exc_info, |
1128 | 1133 | status_code=None, |
| 1134 | + settings=None, |
1129 | 1135 | ): |
| 1136 | + """ |
| 1137 | + Attempt to match exception to rules based on prefix. |
| 1138 | +
|
| 1139 | + rules_prefix is one of [ignore, expected] |
| 1140 | + exc_info is an exception tuple of (exc, val, tb) |
| 1141 | + status_code is an optional value or callable taking in exc_info that returns an int-like object |
| 1142 | + origin is either the current application or trace. |
| 1143 | + """ |
1130 | 1144 | # Delay imports to prevent lockups |
1131 | | - from newrelic.api.application import application_instance |
1132 | 1145 | from newrelic.core.trace_cache import trace_cache |
1133 | 1146 |
|
1134 | | - trace = trace_cache().current_trace() |
1135 | | - settings = trace and trace.settings |
1136 | | - |
1137 | | - if not settings: |
1138 | | - # Retrieve application settings |
1139 | | - application = application_instance() |
1140 | | - settings = application and application.settings |
1141 | | - |
1142 | | - # Default to global settings |
1143 | | - settings = settings or global_settings() |
1144 | | - |
1145 | 1147 | if not settings: |
1146 | | - return False |
| 1148 | + # Pull from current transaction if no settings provided |
| 1149 | + tc = trace_cache() |
| 1150 | + transaction = tc.current_transaction() |
| 1151 | + settings = transaction and transaction.settings |
| 1152 | + |
| 1153 | + if not settings: |
| 1154 | + # Pull from active trace if no settings on transaction |
| 1155 | + trace = tc.current_trace() |
| 1156 | + settings = trace and trace.settings |
| 1157 | + |
| 1158 | + if not settings: |
| 1159 | + # Unable to find rules to match with |
| 1160 | + _logger.error( |
| 1161 | + "Failed to retrieve exception rules: No settings supplied, or found on transaction or trace." |
| 1162 | + ) |
| 1163 | + return None |
1147 | 1164 |
|
1148 | 1165 | # Retrieve settings based on prefix |
1149 | 1166 | classes_rules = getattr(settings.error_collector, "%s_classes" % rules_prefix, set()) |
1150 | 1167 | status_codes_rules = getattr(settings.error_collector, "%s_status_codes" % rules_prefix, set()) |
1151 | 1168 |
|
1152 | | - module, name, fullnames, message = parse_exc_info(exc_info) |
| 1169 | + _, _, fullnames, _ = parse_exc_info(exc_info) |
1153 | 1170 | fullname = fullnames[0] |
1154 | 1171 |
|
1155 | 1172 | # Check class names |
|
0 commit comments