Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Implement newrelic_notice_error_with_stacktrace #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions include/error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*!
* @file error.h
*
* @brief Function declarations necessary to support noticing errors.
*/
#ifndef LIBNEWRELIC_ERROR_H
#define LIBNEWRELIC_ERROR_H

#include "libnewrelic.h"

/**
* @brief Record an error in a transaction, optionally passing a custom stacktrace
*
* Given an active transaction, this function records an error
* inside of the transaction. A custom stacktrace can be passed as a string
* in JSON format like: "[\"first line\",\"second line\",\"third line\"]".
* Otherwise, if NULL is given, a stacktrace will be generated (if configured to do so).
*
* @param [in] transaction An active transaction.
* @param [in] priority The error's priority. The C SDK sends up one error per
* transaction. If multiple calls to this function are made during
* a single transaction, the error with the highest priority is
* reported to New Relic.
* @param [in] errmsg A string comprising the error message.
* @param [in] errclass A string comprising the error class.
* @param [in] errstacktrace A string comprising the error stacktrace, in NewRelic's JSON format, or NULL
*/
void newrelic_do_notice_error(newrelic_txn_t* transaction,
int priority,
const char* errmsg,
const char* errclass,
const char* errstacktrace);

#endif /* LIBNEWRELIC_ERROR_H */
22 changes: 22 additions & 0 deletions include/libnewrelic.h
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,28 @@ void newrelic_notice_error(newrelic_txn_t* transaction,
const char* errmsg,
const char* errclass);

/**
* @brief Record an error in a transaction, passing a custom stacktrace
*
* Given an active transaction, this function records an error
* inside of the transaction. A custom stacktrace must be passed as a string
* in JSON format like: "[\"first line\",\"second line\",\"third line\"]"
*
* @param [in] transaction An active transaction.
* @param [in] priority The error's priority. The C SDK sends up one error per
* transaction. If multiple calls to this function are made during
* a single transaction, the error with the highest priority is
* reported to New Relic.
* @param [in] errmsg A string comprising the error message.
* @param [in] errclass A string comprising the error class.
* @param [in] errstacktrace A string comprising the error stacktrace, in NewRelic's JSON format.
*/
void newrelic_notice_error_with_stacktrace(newrelic_txn_t* transaction,
int priority,
const char* errmsg,
const char* errclass,
const char* errstacktrace);

/**
* @brief A segment within a transaction.
*
Expand Down
40 changes: 33 additions & 7 deletions src/error.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "error.h"

#include "libnewrelic.h"
#include "stack.h"
#include "transaction.h"
Expand All @@ -10,6 +12,27 @@ void newrelic_notice_error(newrelic_txn_t* transaction,
int priority,
const char* errmsg,
const char* errclass) {
newrelic_do_notice_error(transaction, priority, errmsg, errclass, NULL);
}

void newrelic_notice_error_with_stacktrace(newrelic_txn_t* transaction,
int priority,
const char* errmsg,
const char* errclass,
const char* errstacktrace) {
if (NULL == errstacktrace) {
nrl_error(NRL_INSTRUMENT, "unable to add NULL/empty error stacktrace to transaction");
return;
}

newrelic_do_notice_error(transaction, priority, errmsg, errclass, errstacktrace);
}

void newrelic_do_notice_error(newrelic_txn_t* transaction,
int priority,
const char* errmsg,
const char* errclass,
const char* errstacktrace) {
if (NULL == transaction) {
nrl_error(NRL_INSTRUMENT, "unable to add error to NULL transaction");
return;
Expand All @@ -29,8 +52,6 @@ void newrelic_notice_error(newrelic_txn_t* transaction,

nrt_mutex_lock(&transaction->lock);
{
char* stacktrace_json;

if (0 == transaction->txn->options.err_enabled) {
nrl_error(NRL_INSTRUMENT,
"unable to add error to transaction when errors are disabled");
Expand All @@ -56,11 +77,16 @@ void newrelic_notice_error(newrelic_txn_t* transaction,
goto end;
}

stacktrace_json = newrelic_get_stack_trace_as_json();
nr_txn_record_error(transaction->txn, priority, errmsg, errclass,
stacktrace_json);
nr_free(stacktrace_json);
if (NULL == errstacktrace) {
char* stacktrace_json = newrelic_get_stack_trace_as_json();
nr_txn_record_error(transaction->txn, priority, errmsg, errclass,
stacktrace_json);
nr_free(stacktrace_json);
} else {
nr_txn_record_error(transaction->txn, priority, errmsg, errclass,
errstacktrace);
}
}
end:
nrt_mutex_unlock(&transaction->lock);
}
}