-
Notifications
You must be signed in to change notification settings - Fork 241
Add grpc unary server interceptor #312
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
Closed
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Package sentrygrpc provides Sentry integration for servers and clients based | ||
// on the google.golang.org/grpc package. | ||
package sentrygrpc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package sentrygrpc | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/getsentry/sentry-go" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
// UnaryServerInterceptor is a grpc interceptor that reports errors and panics | ||
// to sentry. It also sets *sentry.Hub to context. | ||
func UnaryServerInterceptor(opts UnaryServerInterceptorOptions) grpc.UnaryServerInterceptor { | ||
if opts.ReportOn == nil { | ||
opts.ReportOn = ReportAlways | ||
} | ||
|
||
return func( | ||
ctx context.Context, | ||
req interface{}, | ||
info *grpc.UnaryServerInfo, | ||
handler grpc.UnaryHandler, | ||
) (_ interface{}, err error) { | ||
shouichi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
hub := sentry.GetHubFromContext(ctx) | ||
if hub == nil { | ||
hub = sentry.CurrentHub().Clone() | ||
ctx = sentry.SetHubOnContext(ctx, hub) | ||
} | ||
|
||
defer func() { | ||
if r := recover(); r != nil { | ||
hub.RecoverWithContext(ctx, r) | ||
|
||
if opts.Repanic { | ||
panic(r) | ||
} | ||
|
||
err = status.Errorf(codes.Internal, "%s", r) | ||
rhcarvalho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}() | ||
|
||
resp, err := handler(ctx, req) | ||
|
||
if opts.ReportOn(err) { | ||
hub.CaptureException(err) | ||
} | ||
|
||
return resp, err | ||
} | ||
} | ||
|
||
// UnaryServerInterceptor configure UnaryServerInterceptor. | ||
type UnaryServerInterceptorOptions struct { | ||
// Repanic configures whether to panic again after recovering from a | ||
// panic. Use this option if you have other panic handlers. | ||
Repanic bool | ||
|
||
// ReportOn configures whether to report an error. Defaults to | ||
// ReportAlways. | ||
ReportOn ReportOn | ||
} | ||
|
||
// ReportOn decides error should be reported to sentry. | ||
type ReportOn func(error) bool | ||
|
||
// ReportAlways returns true if err is non-nil. | ||
func ReportAlways(err error) bool { | ||
return err != nil | ||
} | ||
|
||
// ReportOnCodes returns true if error code matches on of the given codes. | ||
func ReportOnCodes(cc ...codes.Code) ReportOn { | ||
return func(err error) bool { | ||
c := status.Code(err) | ||
for i := range cc { | ||
if c == cc[i] { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.