Skip to content

Commit da37b0f

Browse files
authored
Merge pull request #17 from DirectDuck/update_sentry
Update sentry hub usage
2 parents 4153044 + 94fd1e6 commit da37b0f

1 file changed

Lines changed: 45 additions & 37 deletions

File tree

sentry.go

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ import (
1313
"github.com/vmkteam/zenrpc/v2"
1414
)
1515

16-
// NewSentryHubContext creates new context with Sentry Hub.
17-
// Deprecated: use appkit.NewSentryHubContext.
18-
func NewSentryHubContext(ctx context.Context, sentryHub *sentry.Hub) context.Context {
19-
return appkit.NewSentryHubContext(ctx, sentryHub)
20-
}
21-
2216
// WithSentry sets additional parameters for current Sentry scope. Extras: params, duration, ip. Tags: platform,
2317
// version, method. It's also handles panic.
2418
func WithSentry(serverName string) zenrpc.MiddlewareFunc {
@@ -36,7 +30,7 @@ func WithSentry(serverName string) zenrpc.MiddlewareFunc {
3630
}
3731
}
3832

39-
if hub, ok := appkit.SentryHubFromContext(ctx); ok {
33+
if hub := sentry.GetHubFromContext(ctx); hub != nil {
4034
start, platform, version, ip, xRequestID := time.Now(), appkit.PlatformFromContext(ctx), appkit.VersionFromContext(ctx), appkit.IPFromContext(ctx), appkit.XRequestIDFromContext(ctx)
4135

4236
methodName := fullMethodName(serverName, zenrpc.NamespaceFromContext(ctx), method)
@@ -80,22 +74,29 @@ func WithErrorLogger(pf Printf, serverName string) zenrpc.MiddlewareFunc {
8074

8175
pf("ip=%s platform=%q version=%q method=%s duration=%v params=%s xRequestId=%q err=%q", ip, platform, version, methodName, duration, params, xRequestID, r.Error)
8276

83-
sentry.WithScope(func(scope *sentry.Scope) {
84-
scope.SetExtras(map[string]interface{}{
85-
"params": params,
86-
"duration": duration.String(),
87-
"ip": ip,
88-
"error.data": r.Error.Data,
89-
"error.code": r.Error.Code,
90-
})
91-
scope.SetTags(map[string]string{
92-
"platform": platform,
93-
"version": version,
94-
"method": methodName,
95-
"xRequestId": xRequestID,
96-
})
97-
sentry.CaptureException(r.Error)
77+
// initialize hub and scope
78+
currentHub, scope := sentry.CurrentHub(), sentry.NewScope()
79+
80+
// set hub and scope from context, if present
81+
if hub := sentry.GetHubFromContext(ctx); hub != nil {
82+
scope = hub.Scope()
83+
currentHub = hub
84+
}
85+
86+
scope.SetExtras(map[string]interface{}{
87+
"params": params,
88+
"duration": duration.String(),
89+
"ip": ip,
90+
"error.data": r.Error.Data,
91+
"error.code": r.Error.Code,
9892
})
93+
scope.SetTags(map[string]string{
94+
"platform": platform,
95+
"version": version,
96+
"method": methodName,
97+
"xRequestId": xRequestID,
98+
})
99+
currentHub.CaptureException(r.Error)
99100

100101
// remove sensitive error data from response
101102
r.Error.Err = nil
@@ -145,22 +146,29 @@ func WithErrorSLog(pf Print, serverName string, fn LogAttrs) zenrpc.MiddlewareFu
145146

146147
pf(ctx, "rpc error", append(logArgs, args...)...)
147148

148-
sentry.WithScope(func(scope *sentry.Scope) {
149-
scope.SetExtras(map[string]interface{}{
150-
"params": params,
151-
"duration": duration.String(),
152-
"ip": ip,
153-
"error.data": r.Error.Data,
154-
"error.code": r.Error.Code,
155-
})
156-
scope.SetTags(map[string]string{
157-
"platform": platform,
158-
"version": version,
159-
"method": methodName,
160-
"xRequestId": xRequestID,
161-
})
162-
sentry.CaptureException(r.Error)
149+
// initialize hub and scope
150+
currentHub, scope := sentry.CurrentHub(), sentry.NewScope()
151+
152+
// set hub and scope from context, if present
153+
if hub := sentry.GetHubFromContext(ctx); hub != nil {
154+
scope = hub.Scope()
155+
currentHub = hub
156+
}
157+
158+
scope.SetExtras(map[string]interface{}{
159+
"params": params,
160+
"duration": duration.String(),
161+
"ip": ip,
162+
"error.data": r.Error.Data,
163+
"error.code": r.Error.Code,
164+
})
165+
scope.SetTags(map[string]string{
166+
"platform": platform,
167+
"version": version,
168+
"method": methodName,
169+
"xRequestId": xRequestID,
163170
})
171+
currentHub.CaptureException(r.Error)
164172

165173
// remove sensitive error data from response
166174
r.Error.Err = nil

0 commit comments

Comments
 (0)