Skip to content

Commit 382c5d9

Browse files
hahuja2mfulb
authored andcommitted
chore(axiom): reduce code duplication
1 parent a232371 commit 382c5d9

File tree

2 files changed

+95
-138
lines changed

2 files changed

+95
-138
lines changed

axiom/nr_segment.c

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,23 +1170,44 @@ void nr_segment_record_exception(nr_segment_t* segment,
11701170
nr_segment_set_error(segment, error_message, error_class);
11711171
}
11721172

1173-
void nr_segment_set_error(nr_segment_t* segment,
1174-
const char* error_message,
1175-
const char* error_class) {
1176-
if ((NULL == segment) || (NULL == error_message && NULL == error_class)) {
1177-
return;
1178-
}
1179-
1173+
static void nr_segment_set_error_helper(nr_segment_t* segment,
1174+
const char* error_message,
1175+
const char* error_class,
1176+
const char* error_file,
1177+
int error_line,
1178+
char* error_context,
1179+
int error_no) {
11801180
if (NULL == segment->error) {
11811181
segment->error = nr_zalloc(sizeof(nr_segment_error_t));
11821182
}
11831183

11841184
nr_free(segment->error->error_message);
11851185
nr_free(segment->error->error_class);
1186+
if (NULL != error_file && NULL != error_context) {
1187+
nr_free(segment->error->error_file);
1188+
nr_free(segment->error->error_context);
1189+
}
11861190

11871191
segment->error->error_message
11881192
= error_message ? nr_strdup(error_message) : NULL;
11891193
segment->error->error_class = error_class ? nr_strdup(error_class) : NULL;
1194+
if (NULL != error_file && NULL != error_context) {
1195+
segment->error->error_file = error_file ? nr_strdup(error_file) : NULL;
1196+
segment->error->error_line = error_line ? error_line : 0;
1197+
segment->error->error_context
1198+
= error_context ? nr_strdup(error_context) : NULL;
1199+
segment->error->error_no = error_no ? error_no : 0;
1200+
}
1201+
}
1202+
1203+
void nr_segment_set_error(nr_segment_t* segment,
1204+
const char* error_message,
1205+
const char* error_class) {
1206+
if ((NULL == segment) || (NULL == error_message && NULL == error_class)) {
1207+
return;
1208+
}
1209+
nr_segment_set_error_helper(segment, error_message, error_class, NULL, 0,
1210+
NULL, 0);
11901211
}
11911212

11921213
void nr_segment_set_error_with_additional_params(nr_segment_t* segment,
@@ -1200,24 +1221,8 @@ void nr_segment_set_error_with_additional_params(nr_segment_t* segment,
12001221
|| NULL == error_file || (NULL == error_context)) {
12011222
return;
12021223
}
1203-
1204-
if (NULL == segment->error) {
1205-
segment->error = nr_zalloc(sizeof(nr_segment_error_t));
1206-
}
1207-
1208-
nr_free(segment->error->error_message);
1209-
nr_free(segment->error->error_class);
1210-
nr_free(segment->error->error_file);
1211-
nr_free(segment->error->error_context);
1212-
1213-
segment->error->error_message
1214-
= error_message ? nr_strdup(error_message) : NULL;
1215-
segment->error->error_class = error_class ? nr_strdup(error_class) : NULL;
1216-
segment->error->error_file = error_file ? nr_strdup(error_file) : NULL;
1217-
segment->error->error_line = error_line ? error_line : 0;
1218-
segment->error->error_context
1219-
= error_context ? nr_strdup(error_context) : NULL;
1220-
segment->error->error_no = error_no ? error_no : 0;
1224+
nr_segment_set_error_helper(segment, error_message, error_class, error_file,
1225+
error_line, error_context, error_no);
12211226
}
12221227

12231228
bool nr_segment_attributes_user_add(nr_segment_t* segment,

axiom/nr_txn.c

Lines changed: 65 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,9 +1241,6 @@ void nr_txn_destroy_fields(nrtxn_t* txn) {
12411241
nro_delete(txn->intrinsics);
12421242
nr_string_pool_destroy(&txn->datastore_products);
12431243
nr_slowsqls_destroy(&txn->slowsqls);
1244-
if (nr_error_get_option(txn->error) > 0) {
1245-
nr_error_destroy_additional_params(&txn->error);
1246-
}
12471244
nr_error_destroy(&txn->error);
12481245
nr_distributed_trace_destroy(&txn->distributed_trace);
12491246
nr_segment_destroy_tree(txn->segment_root);
@@ -1556,34 +1553,31 @@ nr_status_t nr_txn_record_error_worthy(const nrtxn_t* txn, int priority) {
15561553
return NR_SUCCESS;
15571554
}
15581555

1559-
void nr_txn_record_error(nrtxn_t* txn,
1560-
int priority,
1561-
bool add_to_current_segment,
1562-
const char* errmsg,
1563-
const char* errclass,
1564-
const char* stacktrace_json) {
1556+
static void nr_txn_record_error_helper(nrtxn_t* txn,
1557+
int priority,
1558+
bool add_to_current_segment,
1559+
const char* error_message,
1560+
const char* error_class,
1561+
const char* error_file,
1562+
int error_line,
1563+
char* error_context,
1564+
int error_no,
1565+
const char* stacktrace_json) {
15651566
nr_segment_t* current_segment = NULL;
15661567
char* span_id = NULL;
15671568
nr_error_t* error = NULL;
15681569

1569-
if (nrunlikely((0 == txn) || (0 == txn->options.err_enabled) || (0 == errmsg)
1570-
|| (0 == errclass) || (0 == txn->status.recording)
1571-
|| (0 == errmsg[0]) || (0 == errclass[0])
1572-
|| (0 == stacktrace_json))) {
1573-
return;
1574-
}
1575-
15761570
if ((txn->error) && (priority < nr_error_priority(txn->error))) {
15771571
/*priority of new error is lower, so we don't need to do anything */
15781572
return;
15791573
}
15801574

15811575
if (txn->high_security) {
1582-
errmsg = NR_TXN_HIGH_SECURITY_ERROR_MESSAGE;
1576+
error_message = NR_TXN_HIGH_SECURITY_ERROR_MESSAGE;
15831577
}
15841578

15851579
if (0 == txn->options.allow_raw_exception_messages) {
1586-
errmsg = NR_TXN_ALLOW_RAW_EXCEPTION_MESSAGE;
1580+
error_message = NR_TXN_ALLOW_RAW_EXCEPTION_MESSAGE;
15871581
}
15881582

15891583
/* Only try to get a span_id in cases where we know spans should be created.
@@ -1606,17 +1600,38 @@ void nr_txn_record_error(nrtxn_t* txn,
16061600
current_segment = nr_txn_get_current_segment(txn, NULL);
16071601

16081602
if (current_segment) {
1609-
nr_segment_set_error(current_segment, errmsg, errclass);
1610-
nrl_verbosedebug(NRL_TXN,
1611-
"recording segment error: msg='%.48s' cls='%.48s'"
1612-
"span_id='%.48s'",
1613-
NRSAFESTR(errmsg), NRSAFESTR(errclass),
1614-
NRSAFESTR(span_id));
1603+
if (NULL == error_file) {
1604+
nr_segment_set_error(current_segment, error_message, error_class);
1605+
nrl_verbosedebug(NRL_TXN,
1606+
"recording segment error: msg='%.48s' cls='%.48s'"
1607+
"span_id='%.48s'",
1608+
NRSAFESTR(error_message), NRSAFESTR(error_class),
1609+
NRSAFESTR(span_id));
1610+
} else {
1611+
nr_segment_set_error_with_additional_params(
1612+
current_segment, error_message, error_class, error_file,
1613+
error_line, error_context, error_no);
1614+
nrl_verbosedebug(NRL_TXN,
1615+
"recording segment error: msg='%.48s' cls='%.48s' "
1616+
"file='%.48s' line='%d' context='%.48s' num='%d'"
1617+
"span_id='%.48s'",
1618+
NRSAFESTR(error_message), NRSAFESTR(error_class),
1619+
NRSAFESTR(error_file), error_line,
1620+
NRSAFESTR(error_context), error_no,
1621+
NRSAFESTR(span_id));
1622+
}
16151623
}
16161624
}
16171625
}
1618-
error = nr_error_create(priority, errmsg, errclass, stacktrace_json, span_id,
1619-
nr_get_time());
1626+
if (NULL == error_file) {
1627+
error = nr_error_create(priority, error_message, error_class,
1628+
stacktrace_json, span_id, nr_get_time());
1629+
} else {
1630+
error = nr_error_create_additional_params(
1631+
priority, error_message, error_class, error_file, error_line,
1632+
error_context, error_no, stacktrace_json, span_id, nr_get_time());
1633+
}
1634+
16201635
/*
16211636
* Ensure previous error is destroyed only we have a valid one to replace it
16221637
* with.
@@ -1625,7 +1640,7 @@ void nr_txn_record_error(nrtxn_t* txn,
16251640
nrl_verbosedebug(NRL_TXN,
16261641
"The following returned NULL from create error: "
16271642
"priority=%d msg='%.48s' cls='%.48s' span_id='%.48s'",
1628-
priority, NRSAFESTR(errmsg), NRSAFESTR(errclass),
1643+
priority, NRSAFESTR(error_message), NRSAFESTR(error_class),
16291644
NRSAFESTR(span_id));
16301645

16311646
return;
@@ -1637,11 +1652,27 @@ void nr_txn_record_error(nrtxn_t* txn,
16371652
nrl_verbosedebug(NRL_TXN,
16381653
"recording error priority=%d msg='%.48s' cls='%.48s'"
16391654
"span_id='%.48s'",
1640-
priority, NRSAFESTR(errmsg), NRSAFESTR(errclass),
1655+
priority, NRSAFESTR(error_message), NRSAFESTR(error_class),
16411656
NRSAFESTR(span_id));
16421657
nr_free(span_id);
16431658
}
16441659

1660+
void nr_txn_record_error(nrtxn_t* txn,
1661+
int priority,
1662+
bool add_to_current_segment,
1663+
const char* errmsg,
1664+
const char* errclass,
1665+
const char* stacktrace_json) {
1666+
if (nrunlikely((0 == txn) || (0 == txn->options.err_enabled) || (0 == errmsg)
1667+
|| (0 == errclass) || (0 == txn->status.recording)
1668+
|| (0 == errmsg[0]) || (0 == errclass[0])
1669+
|| (0 == stacktrace_json))) {
1670+
return;
1671+
}
1672+
nr_txn_record_error_helper(txn, priority, add_to_current_segment, errmsg,
1673+
errclass, NULL, 0, NULL, 0, stacktrace_json);
1674+
}
1675+
16451676
void nr_txn_record_error_with_additional_attributes(
16461677
nrtxn_t* txn,
16471678
int priority,
@@ -1653,96 +1684,16 @@ void nr_txn_record_error_with_additional_attributes(
16531684
char* error_context,
16541685
int error_no,
16551686
const char* stacktrace_json) {
1656-
nr_segment_t* current_segment = NULL;
1657-
char* span_id = NULL;
1658-
nr_error_t* error = NULL;
1659-
16601687
if (nrunlikely((0 == txn) || (0 == txn->options.err_enabled)
16611688
|| (0 == error_message) || (0 == txn->status.recording)
16621689
|| (0 == error_message[0]) || (0 == stacktrace_json)
16631690
|| (0 == error_class) || (0 == error_file) || (0 == error_line)
16641691
|| (0 == error_context) || (0 == error_no))) {
16651692
return;
16661693
}
1667-
1668-
if ((txn->error) && (priority < nr_error_priority(txn->error))) {
1669-
/*priority of new error is lower, so we don't need to do anything */
1670-
return;
1671-
}
1672-
1673-
if (txn->high_security) {
1674-
error_message = NR_TXN_HIGH_SECURITY_ERROR_MESSAGE;
1675-
}
1676-
1677-
if (0 == txn->options.allow_raw_exception_messages) {
1678-
error_message = NR_TXN_ALLOW_RAW_EXCEPTION_MESSAGE;
1679-
}
1680-
1681-
/* Only try to get a span_id in cases where we know spans should be created.
1682-
*/
1683-
if (nr_txn_should_create_span_events(txn)) {
1684-
span_id = nr_txn_get_current_span_id(txn);
1685-
1686-
/*
1687-
* The specification says span_id MUST be included so if span events are
1688-
* enabled but the span_id doesn't exist, then don't create the error
1689-
* event.
1690-
*/
1691-
if (nrunlikely(NULL == span_id)) {
1692-
nrl_error(NRL_TXN,
1693-
"Expected span_id to create an error but span_id = NULL.");
1694-
return;
1695-
}
1696-
1697-
if (add_to_current_segment) {
1698-
current_segment = nr_txn_get_current_segment(txn, NULL);
1699-
1700-
if (current_segment) {
1701-
nr_segment_set_error_with_additional_params(
1702-
current_segment, error_message, error_class, error_file, error_line,
1703-
error_context, error_no);
1704-
nrl_verbosedebug(NRL_TXN,
1705-
"recording segment error: msg='%.48s' cls='%.48s' "
1706-
"file='%.48s' line='%d' context='%.48s' num='%d'"
1707-
"span_id='%.48s'",
1708-
NRSAFESTR(error_message), NRSAFESTR(error_class),
1709-
NRSAFESTR(error_file), error_line,
1710-
NRSAFESTR(error_context), error_no,
1711-
NRSAFESTR(span_id));
1712-
}
1713-
}
1714-
}
1715-
error = nr_error_create_additional_params(
1716-
priority, error_message, error_class, error_file, error_line,
1717-
error_context, error_no, stacktrace_json, span_id, nr_get_time());
1718-
1719-
/*
1720-
* Ensure previous error is destroyed only we have a valid one to replace it
1721-
* with.
1722-
*/
1723-
1724-
if (nrunlikely(NULL == error)) {
1725-
nrl_verbosedebug(NRL_TXN,
1726-
"The following returned NULL from create error: "
1727-
"priority=%d msg='%.48s' cls='%.48s' file='%.48s' "
1728-
"line='%d' context='%.48s' num='%d' span_id='%.48s'",
1729-
priority, NRSAFESTR(error_message), NRSAFESTR(error_class),
1730-
NRSAFESTR(error_file), error_line,
1731-
NRSAFESTR(error_context), error_no, NRSAFESTR(span_id));
1732-
return;
1733-
}
1734-
if (txn->error) {
1735-
nr_error_destroy_additional_params(&txn->error);
1736-
}
1737-
txn->error = error;
1738-
nrl_verbosedebug(NRL_TXN,
1739-
"recording error priority=%d msg='%.48s' cls='%.48s' "
1740-
"file='%.48s' line='%d' context='%.48s' num='%d'"
1741-
"span_id='%.48s'",
1742-
priority, NRSAFESTR(error_message), NRSAFESTR(error_class),
1743-
NRSAFESTR(error_file), error_line, NRSAFESTR(error_context),
1744-
error_no, NRSAFESTR(span_id));
1745-
nr_free(span_id);
1694+
nr_txn_record_error_helper(txn, priority, add_to_current_segment,
1695+
error_message, error_class, error_file, error_line,
1696+
error_context, error_no, stacktrace_json);
17461697
}
17471698

17481699
char* nr_txn_create_fn_supportability_metric(const char* function_name,
@@ -2586,10 +2537,11 @@ nr_analytics_event_t* nr_error_to_event(const nrtxn_t* txn) {
25862537
nro_set_hash_string(params, "error.class", nr_error_get_klass(txn->error));
25872538
nro_set_hash_string(params, "error.message",
25882539
nr_error_get_message(txn->error));
2589-
if (nr_error_get_option(txn->error) > 0) {
2540+
if (nr_error_get_file(txn->error) && nr_error_get_context(txn->error)) {
25902541
nro_set_hash_string(params, "error.file", nr_error_get_file(txn->error));
25912542
nro_set_hash_int(params, "error.line", nr_error_get_line(txn->error));
2592-
nro_set_hash_string(params, "error.context", nr_error_get_context(txn->error));
2543+
nro_set_hash_string(params, "error.context",
2544+
nr_error_get_context(txn->error));
25932545
nro_set_hash_int(params, "error.no", nr_error_get_no(txn->error));
25942546
}
25952547
nro_set_hash_string(params, "transactionName", txn->name);

0 commit comments

Comments
 (0)