zitadel/internal/api/grpc/server/middleware/sentry_interceptor.go

35 lines
824 B
Go

package middleware
import (
"context"
"github.com/getsentry/sentry-go"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func SentryHandler() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
return sendErrToSentry(ctx, req, handler)
}
}
func sendErrToSentry(ctx context.Context, req interface{}, handler grpc.UnaryHandler) (interface{}, error) {
resp, err := handler(ctx, req)
code := status.Code(err)
switch code {
case codes.Canceled,
codes.Unknown,
codes.DeadlineExceeded,
codes.ResourceExhausted,
codes.Aborted,
codes.Unimplemented,
codes.Internal,
codes.Unavailable,
codes.DataLoss:
sentry.CaptureException(err)
}
return resp, err
}