-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit of grpc-middleware-sentry.
This project is based heavily on the [grpc-go-middleware libraries][1] and a [pull-request][2] from the [sentry-go library][3]. [1]: https://github.com/grpc-ecosystem/go-grpc-middleware [2]: getsentry/sentry-go#312 [3]: getsentry/sentry-go#240
- Loading branch information
0 parents
commit ef3cb86
Showing
10 changed files
with
503 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,33 @@ | ||
; https://editorconfig.org/ | ||
|
||
root = true | ||
|
||
[*] | ||
insert_final_newline = true | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
indent_style = space | ||
indent_size = 2 | ||
continuation_indent_size = 8 | ||
max_line_length = 120 | ||
|
||
[{Makefile,go.mod,go.sum,*.go,.gitmodules}] | ||
indent_style = tab | ||
indent_size = 8 | ||
|
||
[*.md] | ||
indent_size = 4 | ||
trim_trailing_whitespace = false | ||
|
||
eclint_indent_style = unset | ||
|
||
[Dockerfile] | ||
indent_size = 4 | ||
|
||
[*.proto] | ||
indent_style = space | ||
indent_size = 2 | ||
|
||
[*.sql] | ||
indent_style = space | ||
indent_size = 2 |
This file contains 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,7 @@ | ||
tmp/ | ||
log/ | ||
.env | ||
.DS_Store | ||
server.crt | ||
server.key | ||
server.pub |
This file contains 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,9 @@ | ||
Copyright (c) 2021 Sentry (https://sentry.io) and individual contributors. | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Empty file.
This file contains 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,59 @@ | ||
package grpc_sentry | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/getsentry/sentry-go" | ||
|
||
"google.golang.org/grpc" | ||
) | ||
|
||
func UnaryClientInterceptor(opts ...ClientOption) grpc.UnaryClientInterceptor { | ||
o := evaluateClientOptions(opts) | ||
return func(ctx context.Context, | ||
method string, | ||
req, reply interface{}, | ||
cc *grpc.ClientConn, | ||
invoker grpc.UnaryInvoker, | ||
callOpts ...grpc.CallOption) error { | ||
|
||
hub := sentry.GetHubFromContext(ctx) | ||
if hub == nil { | ||
hub = sentry.CurrentHub().Clone() | ||
ctx = sentry.SetHubOnContext(ctx, hub) | ||
} | ||
|
||
err := invoker(ctx, method, req, reply, cc, callOpts...) | ||
|
||
if o.ReportOn(err) { | ||
hub.CaptureException(err) | ||
} | ||
|
||
return err | ||
} | ||
} | ||
|
||
func StreamClientInterceptor(opts ...ClientOption) grpc.StreamClientInterceptor { | ||
o := evaluateClientOptions(opts) | ||
return func(ctx context.Context, | ||
desc *grpc.StreamDesc, | ||
cc *grpc.ClientConn, | ||
method string, | ||
streamer grpc.Streamer, | ||
callOpts ...grpc.CallOption) (grpc.ClientStream, error) { | ||
|
||
hub := sentry.GetHubFromContext(ctx) | ||
if hub == nil { | ||
hub = sentry.CurrentHub().Clone() | ||
ctx = sentry.SetHubOnContext(ctx, hub) | ||
} | ||
|
||
clientStream, err := streamer(ctx, desc, cc, method, callOpts...) | ||
|
||
if o.ReportOn(err) { | ||
hub.CaptureException(err) | ||
} | ||
|
||
return clientStream, err | ||
} | ||
} |
This file contains 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,4 @@ | ||
/* | ||
`grpc_sentry` provides client and server interceptors to report to Sentry. | ||
*/ | ||
package grpc_sentry |
This file contains 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,8 @@ | ||
module github.com/johnbellone/go-grpc-sentry | ||
|
||
go 1.15 | ||
|
||
require ( | ||
github.com/getsentry/sentry-go v0.9.0 | ||
google.golang.org/grpc v1.35.0 | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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,61 @@ | ||
package grpc_sentry | ||
|
||
import ( | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
var ( | ||
defaultServerOptions = &serveroptions{ | ||
Repanic: false, | ||
ReportOn: ReportAlways, | ||
} | ||
|
||
defaultClientOptions = &clientoptions{} | ||
) | ||
|
||
type clientoptions struct { | ||
ReportOn func(error) bool | ||
} | ||
|
||
type serveroptions struct { | ||
Repanic bool | ||
|
||
ReportOn func(error) bool | ||
} | ||
|
||
func evaluateClientOptions(opts []ClientOption) *clientoptions { | ||
optCopy := &clientoptions{} | ||
*optCopy = *defaultClientOptions | ||
for _, o := range opts { | ||
o(optCopy) | ||
} | ||
return optCopy | ||
} | ||
|
||
func evaluateServerOptions(opts []ServerOption) *serveroptions { | ||
optCopy := &serveroptions{} | ||
*optCopy = *defaultServerOptions | ||
for _, o := range opts { | ||
o(optCopy) | ||
} | ||
return optCopy | ||
} | ||
|
||
type ClientOption func(*clientoptions) | ||
type ServerOption func(*serveroptions) | ||
|
||
func ReportAlways(error) bool { | ||
return true | ||
} | ||
|
||
func ReportOnCodes(cc ...codes.Code) func(error) bool { | ||
return func(err error) bool { | ||
for i := range cc { | ||
if status.Code(err) == cc[i] { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
} |
This file contains 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,77 @@ | ||
package grpc_sentry | ||
|
||
import( | ||
"context" | ||
|
||
"github.com/getsentry/sentry-go" | ||
|
||
"google.golang.org/grpc" | ||
) | ||
|
||
func UnaryServerInterceptor(opts ...ServerOption) grpc.UnaryServerInterceptor { | ||
o := evaluateServerOptions(opts) | ||
return func(ctx context.Context, | ||
req interface{}, | ||
info *grpc.UnaryServerInfo, | ||
handler grpc.UnaryHandler) (interface{}, error) { | ||
panicked := true | ||
hub := sentry.GetHubFromContext(ctx) | ||
if hub == nil { | ||
hub = sentry.CurrentHub().Clone() | ||
ctx = sentry.SetHubOnContext(ctx, hub) | ||
} | ||
|
||
defer func() { | ||
var err error | ||
if err := recover(); err != nil || panicked { | ||
hub.RecoverWithContext(ctx, err) | ||
} | ||
|
||
if o.Repanic { | ||
panic(err) | ||
} | ||
}() | ||
|
||
resp, err := handler(ctx, req) | ||
panicked = false | ||
|
||
if o.ReportOn(err) { | ||
hub.CaptureException(err) | ||
} | ||
|
||
return resp, err | ||
} | ||
} | ||
|
||
func StreamServerInterceptor(opts ...ServerOption) grpc.StreamServerInterceptor { | ||
o := evaluateServerOptions(opts) | ||
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { | ||
panicked := true | ||
ctx := ss.Context() | ||
hub := sentry.GetHubFromContext(ctx) | ||
if hub == nil { | ||
hub = sentry.CurrentHub().Clone() | ||
ctx = sentry.SetHubOnContext(ctx, hub) | ||
} | ||
|
||
defer func() { | ||
var err error | ||
if err := recover(); err != nil || panicked { | ||
hub.RecoverWithContext(ctx, err) | ||
} | ||
|
||
if o.Repanic { | ||
panic(err) | ||
} | ||
}() | ||
|
||
err := handler(srv, ss) | ||
panicked = false | ||
|
||
if err != nil && o.ReportOn(err) { | ||
hub.CaptureException(err) | ||
} | ||
|
||
return err | ||
} | ||
} |