forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue argoproj#340 - add gRPC payload logging interceptor
- Loading branch information
Alexander Matyushentsev
authored and
Alexander Matyushentsev
committed
Jul 19, 2018
1 parent
82fda1c
commit fbf7891
Showing
3 changed files
with
101 additions
and
4 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
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,82 @@ | ||
package grpc | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"golang.org/x/net/context" | ||
"google.golang.org/grpc" | ||
|
||
"github.com/gogo/protobuf/proto" | ||
"github.com/grpc-ecosystem/go-grpc-middleware/logging" | ||
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus" | ||
"github.com/grpc-ecosystem/go-grpc-middleware/tags/logrus" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func logRequest(entry *logrus.Entry, info string, pbMsg interface{}, ctx context.Context, logClaims bool) { | ||
if logClaims { | ||
if data, err := json.Marshal(ctx.Value("claims")); err == nil { | ||
entry = entry.WithField("grpc.request.claims", string(data)) | ||
} | ||
} | ||
if p, ok := pbMsg.(proto.Message); ok { | ||
entry = entry.WithField("grpc.request.content", &jsonpbMarshalleble{p}) | ||
} | ||
entry.Info(info) | ||
} | ||
|
||
type jsonpbMarshalleble struct { | ||
proto.Message | ||
} | ||
|
||
func (j *jsonpbMarshalleble) MarshalJSON() ([]byte, error) { | ||
b := &bytes.Buffer{} | ||
if err := grpc_logrus.JsonPbMarshaller.Marshal(b, j.Message); err != nil { | ||
return nil, fmt.Errorf("jsonpb serializer failed: %v", err) | ||
} | ||
return b.Bytes(), nil | ||
} | ||
|
||
type loggingServerStream struct { | ||
grpc.ServerStream | ||
entry *logrus.Entry | ||
logClaims bool | ||
info string | ||
} | ||
|
||
func (l *loggingServerStream) SendMsg(m interface{}) error { | ||
return l.ServerStream.SendMsg(m) | ||
} | ||
|
||
func (l *loggingServerStream) RecvMsg(m interface{}) error { | ||
err := l.ServerStream.RecvMsg(m) | ||
if err == nil { | ||
logRequest(l.entry, l.info, m, l.ServerStream.Context(), l.logClaims) | ||
} | ||
return err | ||
} | ||
|
||
func PayloadStreamServerInterceptor(entry *logrus.Entry, logClaims bool, decider grpc_logging.ServerPayloadLoggingDecider) grpc.StreamServerInterceptor { | ||
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { | ||
if !decider(stream.Context(), info.FullMethod, srv) { | ||
return handler(srv, stream) | ||
} | ||
logEntry := entry.WithFields(ctx_logrus.Extract(stream.Context()).Data) | ||
newStream := &loggingServerStream{ServerStream: stream, entry: logEntry, logClaims: logClaims, info: fmt.Sprintf("received streaming call %s", info.FullMethod)} | ||
return handler(srv, newStream) | ||
} | ||
} | ||
|
||
func PayloadUnaryServerInterceptor(entry *logrus.Entry, logClaims bool, decider grpc_logging.ServerPayloadLoggingDecider) grpc.UnaryServerInterceptor { | ||
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { | ||
if !decider(ctx, info.FullMethod, info.Server) { | ||
return handler(ctx, req) | ||
} | ||
logEntry := entry.WithFields(ctx_logrus.Extract(ctx).Data) | ||
logRequest(logEntry, fmt.Sprintf("received unary call %s", info.FullMethod), req, ctx, logClaims) | ||
resp, err := handler(ctx, req) | ||
return resp, err | ||
} | ||
} |