Skip to content

Commit

Permalink
Initial commit of grpc-middleware-sentry.
Browse files Browse the repository at this point in the history
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
johnbellone committed Jan 14, 2021
0 parents commit ef3cb86
Show file tree
Hide file tree
Showing 10 changed files with 503 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .editorconfig
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
7 changes: 7 additions & 0 deletions .gitignore
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
9 changes: 9 additions & 0 deletions LICENSE
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 added README.md
Empty file.
59 changes: 59 additions & 0 deletions client_interceptors.go
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
}
}
4 changes: 4 additions & 0 deletions doc.go
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
8 changes: 8 additions & 0 deletions go.mod
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
)
245 changes: 245 additions & 0 deletions go.sum

Large diffs are not rendered by default.

61 changes: 61 additions & 0 deletions options.go
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
}
}
77 changes: 77 additions & 0 deletions server_interceptors.go
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
}
}

0 comments on commit ef3cb86

Please sign in to comment.