github.com/argoproj/argo-cd@v1.8.7/util/grpc/logging.go (about)

     1  package grpc
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/gogo/protobuf/proto"
     8  	grpc_logging "github.com/grpc-ecosystem/go-grpc-middleware/logging"
     9  	ctx_logrus "github.com/grpc-ecosystem/go-grpc-middleware/tags/logrus"
    10  	"github.com/sirupsen/logrus"
    11  	"golang.org/x/net/context"
    12  	"google.golang.org/grpc"
    13  )
    14  
    15  func logRequest(entry *logrus.Entry, info string, pbMsg interface{}, ctx context.Context, logClaims bool) {
    16  	if logClaims {
    17  		if data, err := json.Marshal(ctx.Value("claims")); err == nil {
    18  			entry = entry.WithField("grpc.request.claims", string(data))
    19  		}
    20  	}
    21  	if p, ok := pbMsg.(proto.Message); ok {
    22  		entry = entry.WithField("grpc.request.content", &jsonpbMarshalleble{p})
    23  	}
    24  	entry.Info(info)
    25  }
    26  
    27  type jsonpbMarshalleble struct {
    28  	proto.Message
    29  }
    30  
    31  func (j *jsonpbMarshalleble) MarshalJSON() ([]byte, error) {
    32  	b, err := proto.Marshal(j.Message)
    33  	if err != nil {
    34  		return nil, fmt.Errorf("jsonpb serializer failed: %v", err)
    35  	}
    36  	return b, nil
    37  }
    38  
    39  type loggingServerStream struct {
    40  	grpc.ServerStream
    41  	entry     *logrus.Entry
    42  	logClaims bool
    43  	info      string
    44  }
    45  
    46  func (l *loggingServerStream) SendMsg(m interface{}) error {
    47  	return l.ServerStream.SendMsg(m)
    48  }
    49  
    50  func (l *loggingServerStream) RecvMsg(m interface{}) error {
    51  	err := l.ServerStream.RecvMsg(m)
    52  	if err == nil {
    53  		logRequest(l.entry, l.info, m, l.ServerStream.Context(), l.logClaims)
    54  	}
    55  	return err
    56  }
    57  
    58  func PayloadStreamServerInterceptor(entry *logrus.Entry, logClaims bool, decider grpc_logging.ServerPayloadLoggingDecider) grpc.StreamServerInterceptor {
    59  	return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
    60  		if !decider(stream.Context(), info.FullMethod, srv) {
    61  			return handler(srv, stream)
    62  		}
    63  		logEntry := entry.WithFields(ctx_logrus.Extract(stream.Context()).Data)
    64  		newStream := &loggingServerStream{ServerStream: stream, entry: logEntry, logClaims: logClaims, info: fmt.Sprintf("received streaming call %s", info.FullMethod)}
    65  		return handler(srv, newStream)
    66  	}
    67  }
    68  
    69  func PayloadUnaryServerInterceptor(entry *logrus.Entry, logClaims bool, decider grpc_logging.ServerPayloadLoggingDecider) grpc.UnaryServerInterceptor {
    70  	return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
    71  		if !decider(ctx, info.FullMethod, info.Server) {
    72  			return handler(ctx, req)
    73  		}
    74  		logEntry := entry.WithFields(ctx_logrus.Extract(ctx).Data)
    75  		logRequest(logEntry, fmt.Sprintf("received unary call %s", info.FullMethod), req, ctx, logClaims)
    76  		resp, err := handler(ctx, req)
    77  		return resp, err
    78  	}
    79  }