Skip to content

Commit d40e951

Browse files
authored
Merge pull request #4 from aereal/prefer-contexts
fix: use contexts instead of extras
2 parents 372ff36 + 2fba18a commit d40e951

File tree

2 files changed

+52
-18
lines changed

2 files changed

+52
-18
lines changed

sentry/handler.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,11 @@ func New(opts Options) httputil.Middleware {
7777
}
7878
hub.WithScope(func(scope *sentrysdk.Scope) {
7979
msg := p.Title
80+
pc := newProblemContext(p)
81+
scope.SetContext("problemDetails", pc)
8082
if p.Detail != "" {
81-
scope.SetExtra("problem.detail", p.Detail)
8283
msg = p.Detail
8384
}
84-
if p.Status != 0 {
85-
scope.SetExtra("problem.status", p.Status)
86-
}
87-
if p.Instance != "" {
88-
scope.SetExtra("problem.instance", p.Instance)
89-
}
9085
_ = hub.CaptureMessage(msg)
9186
if opts.WaitForDelivery {
9287
_ = hub.Flush(timeout)
@@ -95,3 +90,19 @@ func New(opts Options) httputil.Middleware {
9590
})
9691
}
9792
}
93+
94+
type problemContext struct {
95+
Detail string `json:"detail,omitempty"`
96+
Status int `json:"status,omitempty"`
97+
Instance string `json:"instance,omitempty"`
98+
Type string `json:"problemType,omitempty"`
99+
}
100+
101+
func newProblemContext(p *problems.DefaultProblem) *problemContext {
102+
return &problemContext{
103+
Detail: p.Detail,
104+
Status: p.Status,
105+
Instance: p.Instance,
106+
Type: p.Type,
107+
}
108+
}

sentry/handler_test.go

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,13 @@ func Test_ok(t *testing.T) {
3232
{
3333
Level: sentrysdk.LevelInfo,
3434
Message: http.StatusText(http.StatusInternalServerError),
35-
Extra: map[string]interface{}{
36-
"problem.status": http.StatusInternalServerError,
35+
Extra: map[string]interface{}{},
36+
Contexts: map[string]interface {
37+
}{
38+
"problemDetails": &problemContext{
39+
Type: "about:blank",
40+
Status: http.StatusInternalServerError,
41+
},
3742
},
3843
},
3944
},
@@ -46,9 +51,14 @@ func Test_ok(t *testing.T) {
4651
{
4752
Level: sentrysdk.LevelInfo,
4853
Message: "some details",
49-
Extra: map[string]interface{}{
50-
"problem.status": http.StatusInternalServerError,
51-
"problem.detail": "some details",
54+
Extra: map[string]interface{}{},
55+
Contexts: map[string]interface {
56+
}{
57+
"problemDetails": &problemContext{
58+
Type: "about:blank",
59+
Status: http.StatusInternalServerError,
60+
Detail: "some details",
61+
},
5262
},
5363
},
5464
},
@@ -61,9 +71,14 @@ func Test_ok(t *testing.T) {
6171
{
6272
Level: sentrysdk.LevelInfo,
6373
Message: http.StatusText(http.StatusInternalServerError),
64-
Extra: map[string]interface{}{
65-
"problem.status": http.StatusInternalServerError,
66-
"problem.instance": "http://instance.example/",
74+
Extra: map[string]interface{}{},
75+
Contexts: map[string]interface {
76+
}{
77+
"problemDetails": &problemContext{
78+
Type: "about:blank",
79+
Status: http.StatusInternalServerError,
80+
Instance: "http://instance.example/",
81+
},
6782
},
6883
},
6984
},
@@ -82,8 +97,13 @@ func Test_ok(t *testing.T) {
8297
{
8398
Level: sentrysdk.LevelInfo,
8499
Message: http.StatusText(http.StatusBadRequest),
85-
Extra: map[string]interface{}{
86-
"problem.status": http.StatusBadRequest,
100+
Extra: map[string]interface{}{},
101+
Contexts: map[string]interface {
102+
}{
103+
"problemDetails": &problemContext{
104+
Type: "about:blank",
105+
Status: http.StatusBadRequest,
106+
},
87107
},
88108
},
89109
},
@@ -165,10 +185,13 @@ func withSentryHub() httputil.Middleware {
165185
var sentryEventCmpOptions = cmp.Options{
166186
cmpopts.IgnoreFields(
167187
sentry.Event{},
168-
"Contexts", "EventID", "Platform", "Release", "Sdk", "ServerName", "Tags", "Timestamp",
188+
"EventID", "Platform", "Release", "Sdk", "ServerName", "Tags", "Timestamp",
169189
),
170190
cmpopts.IgnoreFields(
171191
sentry.Request{},
172192
"Env",
173193
),
194+
cmpopts.IgnoreMapEntries(func(key string, value interface{}) bool {
195+
return key != "problemDetails"
196+
}),
174197
}

0 commit comments

Comments
 (0)