Skip to content

Commit

Permalink
Fix grpc recover and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
DasSkelett committed Apr 5, 2022
1 parent 8808f5b commit 1cbe7b1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
21 changes: 17 additions & 4 deletions internal/services/api_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ import (
"github.com/freifunkMUC/wg-access-server/proto/proto"

"github.com/freifunkMUC/wg-embed/pkg/wgembed"
grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
grpcMiddleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpcLogrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
grpcRecovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
"github.com/improbable-eng/grpc-web/go/grpcweb"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

type ApiServices struct {
Expand All @@ -27,9 +31,18 @@ func ApiRouter(deps *ApiServices) http.Handler {
// Native GRPC server
server := grpc.NewServer([]grpc.ServerOption{
grpc.MaxRecvMsgSize(int(1 * math.Pow(2, 20))), // 1MB
grpc.UnaryInterceptor(func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
return grpc_logrus.UnaryServerInterceptor(traces.Logger(ctx))(ctx, req, info, handler)
}),
grpc.UnaryInterceptor(grpcMiddleware.ChainUnaryServer(
func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
// wrapped in anonymous func to get ctx
return grpcLogrus.UnaryServerInterceptor(traces.Logger(ctx))(ctx, req, info, handler)
},
grpcRecovery.UnaryServerInterceptor(
grpcRecovery.WithRecoveryHandlerContext(func(ctx context.Context, p interface{}) (err error) {
// add trace id to error message so it's visible for the client
return status.Errorf(codes.Internal, "%v; trace = %s", p, traces.TraceID(ctx))
}),
),
)),
}...)

// Register GRPC services
Expand Down
4 changes: 2 additions & 2 deletions internal/services/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

func HealthEndpoint() http.Handler {
return http.HandlerFunc(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
fmt.Fprintf(w, "ok")
}))
})
}
3 changes: 1 addition & 2 deletions internal/services/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"
"runtime/debug"

"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
"github.com/freifunkMUC/wg-access-server/internal/traces"
)

Expand All @@ -19,7 +18,7 @@ func RecoveryMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
ctxlogrus.Extract(r.Context()).
traces.Logger(r.Context()).
WithField("stack", string(debug.Stack())).
Error(err)
w.WriteHeader(500)
Expand Down

0 comments on commit 1cbe7b1

Please sign in to comment.