Skip to content

Commit 840d771

Browse files
authored
Add testing for error trace attributes. (#217)
* Add testing for error trace attributes. * Correct usage of attribute validators. * Add exact_attrs to more validators * Correct testing logic
1 parent 3f33a51 commit 840d771

File tree

2 files changed

+87
-12
lines changed

2 files changed

+87
-12
lines changed

tests/agent_features/test_ignore_expected_errors.py

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@
2323
from newrelic.common.object_names import callable_name
2424

2525
from testing_support.fixtures import (
26-
reset_core_stats_engine,
27-
validate_transaction_errors,
2826
override_application_settings,
27+
reset_core_stats_engine,
28+
validate_error_event_attributes_outside_transaction,
2929
validate_error_event_sample_data,
30+
validate_error_trace_attributes_outside_transaction,
31+
validate_transaction_error_trace_attributes,
32+
validate_transaction_errors,
3033
validate_transaction_metrics,
31-
validate_transaction_error_event_count,
32-
validate_error_event_attributes_outside_transaction,
3334
)
3435

3536

@@ -158,6 +159,70 @@ def _test():
158159
_test()
159160

160161

162+
# =============== Test error trace attributes ===============
163+
164+
165+
error_trace_settings_matrix = [
166+
({}, False),
167+
(expected_runtime_error_settings, True),
168+
]
169+
override_expected_matrix = (True, False, None)
170+
171+
172+
@pytest.mark.parametrize("settings,expected", error_trace_settings_matrix)
173+
@pytest.mark.parametrize("override_expected", override_expected_matrix)
174+
def test_error_trace_attributes_inside_transaction(
175+
settings, expected, override_expected
176+
):
177+
expected = override_expected if override_expected is not None else expected
178+
179+
error_trace_attributes = {
180+
"intrinsic": {
181+
"error.expected": expected,
182+
},
183+
"agent": {},
184+
"user": {},
185+
}
186+
187+
@validate_transaction_error_trace_attributes(exact_attrs=error_trace_attributes)
188+
@background_task(name="test")
189+
@override_application_settings(settings)
190+
def _test():
191+
exercise(override_expected)
192+
193+
_test()
194+
195+
196+
@pytest.mark.parametrize("settings,expected", error_trace_settings_matrix)
197+
@pytest.mark.parametrize("override_expected", override_expected_matrix)
198+
def test_error_trace_attributes_outside_transaction(
199+
settings, expected, override_expected
200+
):
201+
expected = override_expected if override_expected is not None else expected
202+
203+
error_trace_attributes = {
204+
"intrinsic": {
205+
"error.class": _runtime_error_name,
206+
"error.message": _error_message,
207+
"error.expected": expected,
208+
"transactionName": None,
209+
},
210+
"agent": {},
211+
"user": {},
212+
}
213+
214+
@reset_core_stats_engine()
215+
@validate_error_trace_attributes_outside_transaction(
216+
_runtime_error_name,
217+
exact_attrs=error_trace_attributes
218+
)
219+
@override_application_settings(settings)
220+
def _test():
221+
exercise(override_expected)
222+
223+
_test()
224+
225+
161226
# =============== Test metrics not incremented ===============
162227

163228

@@ -415,4 +480,3 @@ def _test():
415480
exercise(**kwargs)
416481

417482
_test()
418-

tests/testing_support/fixtures.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ def wrapper(wrapped, instance, args, kwargs):
11581158

11591159

11601160
def validate_transaction_error_trace_attributes(required_params={},
1161-
forgone_params={}):
1161+
forgone_params={}, exact_attrs={}):
11621162
"""Check the error trace for attributes, expect only one error to be
11631163
present in the transaction.
11641164
"""
@@ -1178,15 +1178,15 @@ def _validate_transaction_error_trace(wrapped, instance, args, kwargs):
11781178
traced_error = error_data[0]
11791179

11801180
check_error_attributes(traced_error.parameters, required_params,
1181-
forgone_params, is_transaction=True)
1181+
forgone_params, exact_attrs, is_transaction=True)
11821182

11831183
return result
11841184

11851185
return _validate_transaction_error_trace
11861186

11871187

11881188
def check_error_attributes(parameters, required_params={}, forgone_params={},
1189-
is_transaction=True):
1189+
exact_attrs={}, is_transaction=True):
11901190

11911191
parameter_fields = ['userAttributes']
11921192
if is_transaction:
@@ -1202,10 +1202,10 @@ def check_error_attributes(parameters, required_params={}, forgone_params={},
12021202
assert 'request_params' not in parameters
12031203
assert 'request_uri' not in parameters
12041204

1205-
check_attributes(parameters, required_params, forgone_params)
1205+
check_attributes(parameters, required_params, forgone_params, exact_attrs)
12061206

12071207

1208-
def check_attributes(parameters, required_params={}, forgone_params={}):
1208+
def check_attributes(parameters, required_params={}, forgone_params={}, exact_attrs={}):
12091209
if required_params:
12101210
for param in required_params['agent']:
12111211
assert param in parameters['agentAttributes']
@@ -1223,6 +1223,17 @@ def check_attributes(parameters, required_params={}, forgone_params={}):
12231223
for param in forgone_params['user']:
12241224
assert param not in parameters['userAttributes']
12251225

1226+
if exact_attrs:
1227+
for param, value in exact_attrs['agent'].items():
1228+
assert parameters['agentAttributes'][param] == value, (
1229+
(param, value), parameters['agentAttributes'])
1230+
for param, value in exact_attrs['user'].items():
1231+
assert parameters['userAttributes'][param] == value, (
1232+
(param, value), parameters['userAttributes'])
1233+
for param, value in exact_attrs['intrinsic'].items():
1234+
assert parameters['intrinsics'][param] == value, (
1235+
(param, value), parameters['intrinsics'])
1236+
12261237

12271238
def validate_error_trace_collector_json():
12281239
@transient_function_wrapper('newrelic.core.stats_engine',
@@ -1605,7 +1616,7 @@ def _validate_error_event_attributes(wrapped, instance, args, kwargs):
16051616

16061617

16071618
def validate_error_trace_attributes_outside_transaction(err_name,
1608-
required_params={}, forgone_params={}):
1619+
required_params={}, forgone_params={}, exact_attrs={}):
16091620
@transient_function_wrapper('newrelic.core.stats_engine',
16101621
'StatsEngine.notice_error')
16111622
def _validate_error_trace_attributes_outside_transaction(wrapped, instance,
@@ -1618,7 +1629,7 @@ def _validate_error_trace_attributes_outside_transaction(wrapped, instance,
16181629
target_error = core_application_stats_engine_error(err_name)
16191630

16201631
check_error_attributes(target_error.parameters, required_params,
1621-
forgone_params, is_transaction=False)
1632+
forgone_params, exact_attrs, is_transaction=False)
16221633

16231634
return result
16241635

0 commit comments

Comments
 (0)