-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
160 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
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,51 @@ | ||
package hwgrpc | ||
|
||
import ( | ||
"context" | ||
"runtime/debug" | ||
"telemetry" | ||
|
||
"common/hwerr" | ||
"common/locale" | ||
|
||
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery" | ||
"github.com/prometheus/client_golang/prometheus" | ||
zlog "github.com/rs/zerolog/log" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
) | ||
|
||
var panicsRecovered = telemetry.NewLazyCounter(prometheus.CounterOpts{ | ||
Name: "services_panics_recovered_total", | ||
Help: "Total number of panics recovered by PanicRecoverInterceptor", | ||
}) | ||
|
||
func recoveryHandlerFn() recovery.RecoveryHandlerFuncContext { | ||
return func(ctx context.Context, recovered any) (err error) { | ||
zlog.Ctx(ctx). | ||
Error(). | ||
Any("recovered", recovered). | ||
Str("stack", string(debug.Stack())). | ||
Msg("recovered a panic") | ||
|
||
panicsRecovered.Counter().Inc() | ||
|
||
return hwerr.NewStatusError(ctx, codes.Internal, "panic recovered", locale.GenericError(ctx)) | ||
} | ||
} | ||
|
||
func UnaryPanicRecoverInterceptor() grpc.UnaryServerInterceptor { | ||
panicsRecovered.Ensure() | ||
|
||
return recovery.UnaryServerInterceptor( | ||
recovery.WithRecoveryHandlerContext(recoveryHandlerFn()), | ||
) | ||
} | ||
|
||
func StreamPanicRecoverInterceptor() grpc.StreamServerInterceptor { | ||
panicsRecovered.Ensure() | ||
|
||
return recovery.StreamServerInterceptor( | ||
recovery.WithRecoveryHandlerContext(recoveryHandlerFn()), | ||
) | ||
} |
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,80 @@ | ||
package hwgrpc | ||
|
||
import ( | ||
"context" | ||
"telemetry" | ||
"testing" | ||
|
||
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb" | ||
"github.com/stretchr/testify/suite" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
type recoveryAssertService struct { | ||
testpb.TestServiceServer | ||
} | ||
|
||
func (s *recoveryAssertService) Ping(ctx context.Context, ping *testpb.PingRequest) (*testpb.PingResponse, error) { | ||
if ping.GetValue() == "panic" { | ||
panic("very bad thing happened") | ||
} | ||
return s.TestServiceServer.Ping(ctx, ping) | ||
} | ||
|
||
func (s *recoveryAssertService) PingList(ping *testpb.PingListRequest, stream testpb.TestService_PingListServer) error { | ||
if ping.Value == "panic" { | ||
panic("very bad thing happened") | ||
} | ||
return s.TestServiceServer.PingList(ping, stream) | ||
} | ||
|
||
type RecoverySuite struct { | ||
*testpb.InterceptorTestSuite | ||
} | ||
|
||
func TestPanicRecoverInterceptor(t *testing.T) { | ||
telemetry.SetupMetrics(context.Background(), nil) | ||
s := &RecoverySuite{ | ||
InterceptorTestSuite: &testpb.InterceptorTestSuite{ | ||
TestService: &recoveryAssertService{TestServiceServer: &testpb.TestPingService{}}, | ||
ServerOpts: []grpc.ServerOption{ | ||
grpc.StreamInterceptor(StreamPanicRecoverInterceptor()), | ||
grpc.UnaryInterceptor(UnaryPanicRecoverInterceptor()), | ||
}, | ||
}, | ||
} | ||
suite.Run(t, s) | ||
} | ||
|
||
func (s *RecoverySuite) TestUnary_SuccessfulRequest() { | ||
_, err := s.Client.Ping(s.SimpleCtx(), testpb.GoodPing) | ||
s.Require().NoError(err) | ||
} | ||
|
||
func (s *RecoverySuite) TestUnary_PanicRequest() { | ||
_, err := s.Client.Ping(s.SimpleCtx(), &testpb.PingRequest{Value: "panic"}) | ||
s.Require().Error(err) | ||
st, ok := status.FromError(err) | ||
s.Require().True(ok, "not a status error") | ||
s.Require().Equal(codes.Internal, st.Code()) | ||
} | ||
|
||
func (s *RecoverySuite) TestStream_SuccessfulReceive() { | ||
stream, err := s.Client.PingList(s.SimpleCtx(), testpb.GoodPingList) | ||
s.Require().NoError(err, "should not fail on establishing the stream") | ||
pong, err := stream.Recv() | ||
s.Require().NoError(err, "no error must occur") | ||
s.Require().NotNil(pong, "pong must not be nil") | ||
} | ||
|
||
func (s *RecoverySuite) TestStream_PanickingReceive() { | ||
stream, err := s.Client.PingList(s.SimpleCtx(), &testpb.PingListRequest{Value: "panic"}) | ||
s.Require().NoError(err, "should not fail on establishing the stream") | ||
_, err = stream.Recv() | ||
s.Require().Error(err) | ||
st, ok := status.FromError(err) | ||
s.Require().True(ok, "not a status error") | ||
s.Require().Equal(codes.Internal, st.Code()) | ||
} |
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