2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2019-01-02 10:23:25 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-06-04 13:55:24 +01:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-01-02 10:23:25 +00:00
|
|
|
"io"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/codes"
|
2019-06-04 13:55:24 +01:00
|
|
|
"google.golang.org/grpc/peer"
|
2019-01-02 10:23:25 +00:00
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
2019-06-04 13:55:24 +01:00
|
|
|
"storj.io/storj/pkg/identity"
|
2019-01-02 10:23:25 +00:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
)
|
|
|
|
|
2019-07-31 13:09:45 +01:00
|
|
|
func (server *Server) logOnErrorStreamInterceptor(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (err error) {
|
2019-01-02 10:23:25 +00:00
|
|
|
err = handler(srv, ss)
|
|
|
|
if err != nil {
|
|
|
|
// no zap errors for canceled or wrong file downloads
|
|
|
|
if storage.ErrKeyNotFound.Has(err) ||
|
|
|
|
status.Code(err) == codes.Canceled ||
|
|
|
|
status.Code(err) == codes.Unavailable ||
|
|
|
|
err == io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
2019-09-03 10:39:26 +01:00
|
|
|
server.log.Error("gRPC stream error response", zap.Error(err))
|
2019-01-02 10:23:25 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:09:45 +01:00
|
|
|
func (server *Server) logOnErrorUnaryInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
2019-01-02 10:23:25 +00:00
|
|
|
resp, err = handler(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
// no zap errors for wrong file downloads
|
|
|
|
if status.Code(err) == codes.NotFound {
|
|
|
|
return resp, err
|
|
|
|
}
|
2019-09-03 10:39:26 +01:00
|
|
|
server.log.Error("gRPC unary error response", zap.Error(err))
|
2019-01-02 10:23:25 +00:00
|
|
|
}
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
2019-06-04 13:55:24 +01:00
|
|
|
// CombineInterceptors combines two UnaryServerInterceptors so they act as one
|
|
|
|
// (because grpc only allows you to pass one in).
|
|
|
|
func CombineInterceptors(a, b grpc.UnaryServerInterceptor) grpc.UnaryServerInterceptor {
|
2019-01-02 10:23:25 +00:00
|
|
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
|
|
|
return a(ctx, req, info, func(actx context.Context, areq interface{}) (interface{}, error) {
|
|
|
|
return b(actx, areq, info, func(bctx context.Context, breq interface{}) (interface{}, error) {
|
|
|
|
return handler(bctx, breq)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-06-04 13:55:24 +01:00
|
|
|
|
|
|
|
type nodeRequestLog struct {
|
|
|
|
GRPCService string `json:"grpc_service"`
|
|
|
|
GRPCMethod string `json:"grpc_method"`
|
|
|
|
PeerAddress string `json:"peer_address"`
|
|
|
|
PeerNodeID string `json:"peer_node_id"`
|
|
|
|
Msg interface{} `json:"msg"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareRequestLog(ctx context.Context, req, server interface{}, methodName string) ([]byte, error) {
|
|
|
|
reqLog := nodeRequestLog{
|
|
|
|
GRPCService: fmt.Sprintf("%T", server),
|
|
|
|
GRPCMethod: methodName,
|
|
|
|
PeerAddress: "<no peer???>",
|
|
|
|
Msg: req,
|
|
|
|
}
|
|
|
|
if peer, ok := peer.FromContext(ctx); ok {
|
|
|
|
reqLog.PeerAddress = peer.Addr.String()
|
|
|
|
if peerIdentity, err := identity.PeerIdentityFromPeer(peer); err == nil {
|
|
|
|
reqLog.PeerNodeID = peerIdentity.ID.String()
|
|
|
|
} else {
|
|
|
|
reqLog.PeerNodeID = fmt.Sprintf("<no peer id: %v>", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return json.Marshal(reqLog)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnaryMessageLoggingInterceptor creates a UnaryServerInterceptor which
|
|
|
|
// logs the full contents of incoming unary requests.
|
|
|
|
func UnaryMessageLoggingInterceptor(log *zap.Logger) grpc.UnaryServerInterceptor {
|
|
|
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
|
|
|
if jsonReq, err := prepareRequestLog(ctx, req, info.Server, info.FullMethod); err == nil {
|
|
|
|
log.Info(string(jsonReq))
|
|
|
|
} else {
|
2019-09-03 10:39:26 +01:00
|
|
|
log.Error("Failed to marshal request to JSON.",
|
|
|
|
zap.String("method", info.FullMethod), zap.Error(err),
|
|
|
|
)
|
2019-06-04 13:55:24 +01:00
|
|
|
}
|
|
|
|
return handler(ctx, req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StreamMessageLoggingInterceptor creates a StreamServerInterceptor which
|
|
|
|
// logs the full contents of incoming streaming requests.
|
|
|
|
func StreamMessageLoggingInterceptor(log *zap.Logger) grpc.StreamServerInterceptor {
|
|
|
|
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
|
|
|
|
// Are we even using any of these yet? I'm only guessing at how best to pass in things
|
|
|
|
// so that they make sense.
|
|
|
|
if jsonReq, err := prepareRequestLog(ss.Context(), srv, nil, info.FullMethod); err == nil {
|
|
|
|
log.Info(string(jsonReq))
|
|
|
|
} else {
|
2019-09-03 10:39:26 +01:00
|
|
|
log.Error("Failed to marshal request to JSON.",
|
|
|
|
zap.String("method", info.FullMethod), zap.Error(err),
|
|
|
|
)
|
2019-06-04 13:55:24 +01:00
|
|
|
}
|
|
|
|
return handler(srv, ss)
|
|
|
|
}
|
|
|
|
}
|