-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jason Parraga <[email protected]>
- Loading branch information
1 parent
9b78125
commit 3c1920b
Showing
2 changed files
with
86 additions
and
4 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
flyteadmin/pkg/rpc/adminservice/middleware/recovery_interceptor.go
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 middleware | ||
|
||
import ( | ||
"context" | ||
"github.com/flyteorg/flyte/flytestdlib/logger" | ||
"github.com/flyteorg/flyte/flytestdlib/promutils" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"runtime/debug" | ||
) | ||
|
||
// RecoveryInterceptor is a struct for creating gRPC interceptors that handle panics in go | ||
type RecoveryInterceptor struct { | ||
panicCounter prometheus.Counter | ||
} | ||
|
||
// NewRecoveryInterceptor creates a new RecoveryInterceptor with metrics under the provided scope | ||
func NewRecoveryInterceptor(adminScope promutils.Scope) *RecoveryInterceptor { | ||
panicCounter := adminScope.MustNewCounter("handler_panic", "panics encountered while handling gRPC requests") | ||
return &RecoveryInterceptor{ | ||
panicCounter: panicCounter, | ||
} | ||
} | ||
|
||
// UnaryServerInterceptor returns a new unary server interceptor for panic recovery. | ||
func (ri *RecoveryInterceptor) UnaryServerInterceptor() grpc.UnaryServerInterceptor { | ||
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (_ any, err error) { | ||
|
||
defer func() { | ||
if r := recover(); r != nil { | ||
ri.panicCounter.Inc() | ||
logger.Fatalf(ctx, "panic-ed for request: [%+v] to %s with err: %v with Stack: %v", req, info.FullMethod, r, string(debug.Stack())) | ||
// Return INTERNAL to client with no info as to not leak implementation details | ||
err = status.Errorf(codes.Internal, "") | ||
} | ||
}() | ||
|
||
resp, err := handler(ctx, req) | ||
return resp, err | ||
} | ||
} | ||
|
||
// StreamServerInterceptor returns a new streaming server interceptor for panic recovery. | ||
func (ri *RecoveryInterceptor) StreamServerInterceptor() grpc.StreamServerInterceptor { | ||
return func(srv any, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (err error) { | ||
|
||
defer func() { | ||
if r := recover(); r != nil { | ||
ri.panicCounter.Inc() | ||
logger.Fatalf(stream.Context(), "panic-ed for stream to %s with err: %v with Stack: %v", info.FullMethod, r, string(debug.Stack())) | ||
// Return INTERNAL to client with no info as to not leak implementation details | ||
err = status.Errorf(codes.Internal, "") | ||
} | ||
}() | ||
|
||
err = handler(srv, stream) | ||
return 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