Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle attributes in nrpkgerrors #441

Merged
Merged
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
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love to find a better solution to this. I've increased this timeout myself in a past PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 this is a very naive attempt at solving the problem. I would really like to understand why it is happening in the first place.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we set a variable for the integer here?

t.Error("server did not receive 2 spans")
}
}
Expand Down