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

feat: add support for exception mechanism metadata #564

Merged
merged 1 commit into from Feb 16, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions interfaces.go
Expand Up @@ -204,13 +204,30 @@ func NewRequest(r *http.Request) *Request {
}
}

// Mechanism is the mechanism by which an exception was generated and handled.
type Mechanism struct {
Type string `json:"type,omitempty"`
Description string `json:"description,omitempty"`
HelpLink string `json:"help_link,omitempty"`
Handled *bool `json:"handled,omitempty"`
Data map[string]interface{} `json:"data,omitempty"`
}

// SetUnhandled indicates that the exception is an unhandled exception, i.e.
// from a panic.
func (m *Mechanism) SetUnhandled() {
h := false
m.Handled = &h
}
Comment on lines +216 to +221
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to add some way to make it easier to set the Handled field, since it's a bit annoying otherwise. AWS SDKs, for instance, have utility functions like func Bool(b bool) *bool, but that felt like overkill here. Adding a corresponding SetHandled() also felt like it wouldn't be useful. Happy to go either (or any other) way, though.


// Exception specifies an error that occurred.
type Exception struct {
Type string `json:"type,omitempty"` // used as the main issue title
Value string `json:"value,omitempty"` // used as the main issue subtitle
Module string `json:"module,omitempty"`
ThreadID string `json:"thread_id,omitempty"`
Stacktrace *Stacktrace `json:"stacktrace,omitempty"`
Mechanism *Mechanism `json:"mechanism,omitempty"`
}

// SDKMetaData is a struct to stash data which is needed at some point in the SDK's event processing pipeline
Expand Down
49 changes: 49 additions & 0 deletions interfaces_test.go
Expand Up @@ -160,6 +160,55 @@ func TestEventMarshalJSON(t *testing.T) {
}
}

func TestMechanismMarshalJSON(t *testing.T) {
mechanism := &Mechanism{
Type: "some type",
Description: "some description",
HelpLink: "some help link",
Data: map[string]interface{}{
"some data": "some value",
"some numeric data": 12345,
},
}

got, err := json.Marshal(mechanism)
if err != nil {
t.Fatal(err)
}

want := `{"type":"some type","description":"some description","help_link":"some help link",` +
`"data":{"some data":"some value","some numeric data":12345}}`

if diff := cmp.Diff(want, string(got)); diff != "" {
t.Errorf("Event mismatch (-want +got):\n%s", diff)
}
}

func TestMechanismMarshalJSON_withHandled(t *testing.T) {
mechanism := &Mechanism{
Type: "some type",
Description: "some description",
HelpLink: "some help link",
Data: map[string]interface{}{
"some data": "some value",
"some numeric data": 12345,
},
}
mechanism.SetUnhandled()

got, err := json.Marshal(mechanism)
if err != nil {
t.Fatal(err)
}

want := `{"type":"some type","description":"some description","help_link":"some help link",` +
`"handled":false,"data":{"some data":"some value","some numeric data":12345}}`

if diff := cmp.Diff(want, string(got)); diff != "" {
t.Errorf("Event mismatch (-want +got):\n%s", diff)
}
}

func TestStructSnapshots(t *testing.T) {
testSpan := &Span{
TraceID: TraceIDFromHex("d6c4f03650bd47699ec65c84352b6208"),
Expand Down