Skip to content

Commit

Permalink
Merge pull request #4 from aereal/prefer-contexts
Browse files Browse the repository at this point in the history
fix: use contexts instead of extras
  • Loading branch information
aereal authored Sep 22, 2021
2 parents 372ff36 + 2fba18a commit d40e951
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
25 changes: 18 additions & 7 deletions sentry/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,11 @@ func New(opts Options) httputil.Middleware {
}
hub.WithScope(func(scope *sentrysdk.Scope) {
msg := p.Title
pc := newProblemContext(p)
scope.SetContext("problemDetails", pc)
if p.Detail != "" {
scope.SetExtra("problem.detail", p.Detail)
msg = p.Detail
}
if p.Status != 0 {
scope.SetExtra("problem.status", p.Status)
}
if p.Instance != "" {
scope.SetExtra("problem.instance", p.Instance)
}
_ = hub.CaptureMessage(msg)
if opts.WaitForDelivery {
_ = hub.Flush(timeout)
Expand All @@ -95,3 +90,19 @@ func New(opts Options) httputil.Middleware {
})
}
}

type problemContext struct {
Detail string `json:"detail,omitempty"`
Status int `json:"status,omitempty"`
Instance string `json:"instance,omitempty"`
Type string `json:"problemType,omitempty"`
}

func newProblemContext(p *problems.DefaultProblem) *problemContext {
return &problemContext{
Detail: p.Detail,
Status: p.Status,
Instance: p.Instance,
Type: p.Type,
}
}
45 changes: 34 additions & 11 deletions sentry/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ func Test_ok(t *testing.T) {
{
Level: sentrysdk.LevelInfo,
Message: http.StatusText(http.StatusInternalServerError),
Extra: map[string]interface{}{
"problem.status": http.StatusInternalServerError,
Extra: map[string]interface{}{},
Contexts: map[string]interface {
}{
"problemDetails": &problemContext{
Type: "about:blank",
Status: http.StatusInternalServerError,
},
},
},
},
Expand All @@ -46,9 +51,14 @@ func Test_ok(t *testing.T) {
{
Level: sentrysdk.LevelInfo,
Message: "some details",
Extra: map[string]interface{}{
"problem.status": http.StatusInternalServerError,
"problem.detail": "some details",
Extra: map[string]interface{}{},
Contexts: map[string]interface {
}{
"problemDetails": &problemContext{
Type: "about:blank",
Status: http.StatusInternalServerError,
Detail: "some details",
},
},
},
},
Expand All @@ -61,9 +71,14 @@ func Test_ok(t *testing.T) {
{
Level: sentrysdk.LevelInfo,
Message: http.StatusText(http.StatusInternalServerError),
Extra: map[string]interface{}{
"problem.status": http.StatusInternalServerError,
"problem.instance": "http://instance.example/",
Extra: map[string]interface{}{},
Contexts: map[string]interface {
}{
"problemDetails": &problemContext{
Type: "about:blank",
Status: http.StatusInternalServerError,
Instance: "http://instance.example/",
},
},
},
},
Expand All @@ -82,8 +97,13 @@ func Test_ok(t *testing.T) {
{
Level: sentrysdk.LevelInfo,
Message: http.StatusText(http.StatusBadRequest),
Extra: map[string]interface{}{
"problem.status": http.StatusBadRequest,
Extra: map[string]interface{}{},
Contexts: map[string]interface {
}{
"problemDetails": &problemContext{
Type: "about:blank",
Status: http.StatusBadRequest,
},
},
},
},
Expand Down Expand Up @@ -165,10 +185,13 @@ func withSentryHub() httputil.Middleware {
var sentryEventCmpOptions = cmp.Options{
cmpopts.IgnoreFields(
sentry.Event{},
"Contexts", "EventID", "Platform", "Release", "Sdk", "ServerName", "Tags", "Timestamp",
"EventID", "Platform", "Release", "Sdk", "ServerName", "Tags", "Timestamp",
),
cmpopts.IgnoreFields(
sentry.Request{},
"Env",
),
cmpopts.IgnoreMapEntries(func(key string, value interface{}) bool {
return key != "problemDetails"
}),
}

0 comments on commit d40e951

Please sign in to comment.