Skip to content

Commit 23823f9

Browse files
committed
chore(axiom): reduce code duplication
1 parent f917ced commit 23823f9

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
@@ -1240,9 +1240,6 @@ void nr_txn_destroy_fields(nrtxn_t* txn) {
12401240
nro_delete(txn->intrinsics);
12411241
nr_string_pool_destroy(&txn->datastore_products);
12421242
nr_slowsqls_destroy(&txn->slowsqls);
1243-
if (nr_error_get_option(txn->error) > 0) {
1244-
nr_error_destroy_additional_params(&txn->error);
1245-
}
12461243
nr_error_destroy(&txn->error);
12471244
nr_distributed_trace_destroy(&txn->distributed_trace);
12481245
nr_segment_destroy_tree(txn->segment_root);
@@ -1555,34 +1552,31 @@ nr_status_t nr_txn_record_error_worthy(const nrtxn_t* txn, int priority) {
15551552
return NR_SUCCESS;
15561553
}
15571554

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

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

15801574
if (txn->high_security) {
1581-
errmsg = NR_TXN_HIGH_SECURITY_ERROR_MESSAGE;
1575+
error_message = NR_TXN_HIGH_SECURITY_ERROR_MESSAGE;
15821576
}
15831577

15841578
if (0 == txn->options.allow_raw_exception_messages) {
1585-
errmsg = NR_TXN_ALLOW_RAW_EXCEPTION_MESSAGE;
1579+
error_message = NR_TXN_ALLOW_RAW_EXCEPTION_MESSAGE;
15861580
}
15871581

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

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

16301645
return;
@@ -1636,11 +1651,27 @@ void nr_txn_record_error(nrtxn_t* txn,
16361651
nrl_verbosedebug(NRL_TXN,
16371652
"recording error priority=%d msg='%.48s' cls='%.48s'"
16381653
"span_id='%.48s'",
1639-
priority, NRSAFESTR(errmsg), NRSAFESTR(errclass),
1654+
priority, NRSAFESTR(error_message), NRSAFESTR(error_class),
16401655
NRSAFESTR(span_id));
16411656
nr_free(span_id);
16421657
}
16431658

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

17471698
char* nr_txn_create_fn_supportability_metric(const char* function_name,
@@ -2585,10 +2536,11 @@ nr_analytics_event_t* nr_error_to_event(const nrtxn_t* txn) {
25852536
nro_set_hash_string(params, "error.class", nr_error_get_klass(txn->error));
25862537
nro_set_hash_string(params, "error.message",
25872538
nr_error_get_message(txn->error));
2588-
if (nr_error_get_option(txn->error) > 0) {
2539+
if (nr_error_get_file(txn->error) && nr_error_get_context(txn->error)) {
25892540
nro_set_hash_string(params, "error.file", nr_error_get_file(txn->error));
25902541
nro_set_hash_int(params, "error.line", nr_error_get_line(txn->error));
2591-
nro_set_hash_string(params, "error.context", nr_error_get_context(txn->error));
2542+
nro_set_hash_string(params, "error.context",
2543+
nr_error_get_context(txn->error));
25922544
nro_set_hash_int(params, "error.no", nr_error_get_no(txn->error));
25932545
}
25942546
nro_set_hash_string(params, "transactionName", txn->name);

0 commit comments

Comments
 (0)