Skip to content

Commit

Permalink
Merge pull request #441 from iamemilio/support_attributes_nrpkgerrors
Browse files Browse the repository at this point in the history
Handle attributes in nrpkgerrors
  • Loading branch information
nr-swilloughby authored May 18, 2022
2 parents 294568e + 7451f0b commit 399106a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
32 changes: 30 additions & 2 deletions v3/integrations/nrpkgerrors/nrkpgerrors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,22 @@ func gamma(e error) error { return errors.WithStack(e) }

func theta(e error) error { return errors.WithMessage(e, "theta") }

func basicNRError(e error) newrelic.Error { return newrelic.Error{Message: e.Error()} }
func withAttributes(e error) newrelic.Error {
return newrelic.Error{
Message: e.Error(),
Attributes: map[string]interface{}{
"testAttribute": 1,
"foo": 2,
},
}
}

func TestWrappedStackTrace(t *testing.T) {
testcases := []struct {
Error error
ExpectTopFrame string
Error error
ExpectTopFrame string
ExpectAttributes map[string]interface{}
}{
{Error: basicError{}, ExpectTopFrame: ""},
{Error: alpha(basicError{}), ExpectTopFrame: "alpha"},
Expand All @@ -43,6 +55,8 @@ func TestWrappedStackTrace(t *testing.T) {
{Error: alpha(theta(beta(basicError{}))), ExpectTopFrame: "beta"},
{Error: alpha(theta(beta(theta(basicError{})))), ExpectTopFrame: "beta"},
{Error: theta(basicError{}), ExpectTopFrame: ""},
{Error: basicNRError(basicError{}), ExpectTopFrame: ""},
{Error: withAttributes(basicError{}), ExpectTopFrame: "", ExpectAttributes: map[string]interface{}{"testAttribute": 1, "foo": 2}},
}

for idx, tc := range testcases {
Expand All @@ -53,6 +67,20 @@ func TestWrappedStackTrace(t *testing.T) {
t.Errorf("testcase %d: expected %s got %s",
idx, tc.ExpectTopFrame, fn)
}
// check that error attributes are equal if they are expected
if tc.ExpectAttributes != nil {
errorAttributes := e.(newrelic.Error).ErrorAttributes()
if len(tc.ExpectAttributes) != len(errorAttributes) {
t.Errorf("testcase %d: error attribute size expected %d got %d",
idx, len(tc.ExpectAttributes), len(errorAttributes))
}
for k, v := range errorAttributes {
if tc.ExpectAttributes[k] != v {
t.Errorf("testcase %d: expected attribute %s:%v got %s:%v",
idx, k, tc.ExpectAttributes[k], k, v)
}
}
}
}
}

Expand Down
15 changes: 12 additions & 3 deletions v3/integrations/nrpkgerrors/nrpkgerrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,18 @@ func errorClass(e error) string {
// newrelic.Transaction.NoticeError it gives an improved stacktrace and class
// type.
func Wrap(e error) error {
attributes := make(map[string]interface{})
switch error := e.(type) {
case newrelic.Error:
// if e is type newrelic.Error, copy attributes into wrapped error
for key, value := range error.ErrorAttributes() {
attributes[key] = value
}
}
return newrelic.Error{
Message: e.Error(),
Class: errorClass(e),
Stack: stackTrace(e),
Message: e.Error(),
Class: errorClass(e),
Stack: stackTrace(e),
Attributes: attributes,
}
}
2 changes: 1 addition & 1 deletion v3/newrelic/trace_observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ func TestTrObsOKSendBackoffNo(t *testing.T) {
}
// If the default backoff of 15 seconds is used, the second span will not
// be received in time.
if !s.DidSpansArrive(t, 2, 4*time.Second) {
if !s.DidSpansArrive(t, 2, 8*time.Second) {
t.Error("server did not receive 2 spans")
}
}
Expand Down

0 comments on commit 399106a

Please sign in to comment.