From e375512920c152e3b75fca740afd92cf637b1847 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20M=2E=20Mac=C3=ADas=20Ojeda?= Date: Tue, 3 Jul 2018 16:43:24 +0300 Subject: [PATCH 1/3] Proof of concept --- logging/zap/DOC.md | 18 +++++++++--------- logging/zap/payload_interceptors.go | 19 +++++++++++-------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/logging/zap/DOC.md b/logging/zap/DOC.md index e1c5630a1..08fcfcf3d 100644 --- a/logging/zap/DOC.md +++ b/logging/zap/DOC.md @@ -31,7 +31,7 @@ Below is a JSON formatted example of a log that would be logged by the server in { "level": "info", // string zap log levels "msg": "finished unary call", // string log message - + "grpc.code": "OK", // string grpc status code "grpc.method": "Ping", // string method name "grpc.service": "mwitkow.testproto.TestService", // string full name of the called service @@ -39,7 +39,7 @@ Below is a JSON formatted example of a log that would be logged by the server in "grpc.request.deadline": "2006-01-02T15:04:05Z07:00", // string RFC3339 deadline of the current request if supplied "grpc.request.value": "something", // string value on the request "grpc.time_ms": 1.345, // float32 run time of the call in ms - + "peer.address": { "IP": "127.0.0.1", // string IP address of calling party "Port": 60216, // int port call is coming in on @@ -47,7 +47,7 @@ Below is a JSON formatted example of a log that would be logged by the server in }, "span.kind": "server", // string client | server "system": "grpc" // string - + "custom_field": "custom_value", // string user defined field "custom_tags.int": 1337, // int user defined tag on the ctx "custom_tags.string": "something", // string user defined tag on the ctx @@ -59,7 +59,7 @@ Below is a JSON formatted example of a log that would be logged by the payload i { "level": "info", // string zap log levels "msg": "client request payload logged as grpc.request.content", // string log message - + "grpc.request.content": { // object content of RPC request "msg" : { // object ZAP specific inner object "value": "something", // string defined by caller @@ -68,7 +68,7 @@ Below is a JSON formatted example of a log that would be logged by the payload i }, "grpc.method": "Ping", // string method being called "grpc.service": "mwitkow.testproto.TestService", // string service being called - + "span.kind": "client", // string client | server "system": "grpc" // string } @@ -207,7 +207,7 @@ _ = grpc.NewServer( * [Package (InitializationWithDurationFieldOverride)](#example__initializationWithDurationFieldOverride) #### Package files -[client_interceptors.go](./client_interceptors.go) [context.go](./context.go) [doc.go](./doc.go) [grpclogger.go](./grpclogger.go) [options.go](./options.go) [payload_interceptors.go](./payload_interceptors.go) [server_interceptors.go](./server_interceptors.go) +[client_interceptors.go](./client_interceptors.go) [context.go](./context.go) [doc.go](./doc.go) [grpclogger.go](./grpclogger.go) [options.go](./options.go) [payload_interceptors.go](./payload_interceptors.go) [server_interceptors.go](./server_interceptors.go) ## Variables ``` go @@ -232,8 +232,8 @@ DefaultDurationToField is the default implementation of converting request durat ``` go var ( - // JsonPbMarshaller is the marshaller used for serializing protobuf messages. - JsonPbMarshaller = &jsonpb.Marshaler{} + // JsonPbMarshaler is the marshaler used for serializing protobuf messages. + JsonPbMarshaler Marshaler = &runtime.JSONPb{} ) ``` @@ -399,4 +399,4 @@ func WithLevels(f CodeToLevel) Option WithLevels customizes the function for mapping gRPC return codes and interceptor log level statements. - - - -Generated by [godoc2ghmd](https://github.com/GandalfUK/godoc2ghmd) \ No newline at end of file +Generated by [godoc2ghmd](https://github.com/GandalfUK/godoc2ghmd) diff --git a/logging/zap/payload_interceptors.go b/logging/zap/payload_interceptors.go index 293cdf9a6..bd8109a32 100644 --- a/logging/zap/payload_interceptors.go +++ b/logging/zap/payload_interceptors.go @@ -1,12 +1,10 @@ package grpc_zap import ( - "bytes" "fmt" - "github.com/golang/protobuf/jsonpb" + "github.com/gengo/grpc-gateway/runtime" "github.com/golang/protobuf/proto" - "github.com/grpc-ecosystem/go-grpc-middleware/logging" "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap" "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -14,9 +12,14 @@ import ( "google.golang.org/grpc" ) +// Marshaler interface for JsonPbMarshaler +type Marshaler interface { + Marshal(v interface{}) ([]byte, error) +} + var ( - // JsonPbMarshaller is the marshaller used for serializing protobuf messages. - JsonPbMarshaller = &jsonpb.Marshaler{} + // JsonPbMarshaler is the marshaler used for serializing protobuf messages. + JsonPbMarshaler Marshaler = &runtime.JSONPb{} ) // PayloadUnaryServerInterceptor returns a new unary server interceptors that logs the payloads of requests. @@ -141,9 +144,9 @@ func (j *jsonpbObjectMarshaler) MarshalLogObject(e zapcore.ObjectEncoder) error } func (j *jsonpbObjectMarshaler) MarshalJSON() ([]byte, error) { - b := &bytes.Buffer{} - if err := JsonPbMarshaller.Marshal(b, j.pb); err != nil { + bytes, err := JsonPbMarshaler.Marshal(j.pb) + if err != nil { return nil, fmt.Errorf("jsonpb serializer failed: %v", err) } - return b.Bytes(), nil + return bytes, nil } From 124f5a0353f8a859b966a38538ab7860707ba5e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20M=2E=20Mac=C3=ADas=20Ojeda?= Date: Tue, 3 Jul 2018 17:06:29 +0300 Subject: [PATCH 2/3] Bugfix from goimports --- logging/zap/payload_interceptors.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/logging/zap/payload_interceptors.go b/logging/zap/payload_interceptors.go index bd8109a32..25b8193f7 100644 --- a/logging/zap/payload_interceptors.go +++ b/logging/zap/payload_interceptors.go @@ -3,9 +3,10 @@ package grpc_zap import ( "fmt" - "github.com/gengo/grpc-gateway/runtime" "github.com/golang/protobuf/proto" + "github.com/grpc-ecosystem/go-grpc-middleware/logging" "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap" + "github.com/grpc-ecosystem/grpc-gateway/runtime" "go.uber.org/zap" "go.uber.org/zap/zapcore" "golang.org/x/net/context" From 23d94e9b30441f36b35ac2850166e2ed32868b43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20M=2E=20Mac=C3=ADas=20Ojeda?= Date: Tue, 3 Jul 2018 17:56:33 +0300 Subject: [PATCH 3/3] Autogenerated docs --- logging/zap/DOC.md | 53 +++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/logging/zap/DOC.md b/logging/zap/DOC.md index 08fcfcf3d..09b0b8908 100644 --- a/logging/zap/DOC.md +++ b/logging/zap/DOC.md @@ -31,7 +31,7 @@ Below is a JSON formatted example of a log that would be logged by the server in { "level": "info", // string zap log levels "msg": "finished unary call", // string log message - + "grpc.code": "OK", // string grpc status code "grpc.method": "Ping", // string method name "grpc.service": "mwitkow.testproto.TestService", // string full name of the called service @@ -39,7 +39,7 @@ Below is a JSON formatted example of a log that would be logged by the server in "grpc.request.deadline": "2006-01-02T15:04:05Z07:00", // string RFC3339 deadline of the current request if supplied "grpc.request.value": "something", // string value on the request "grpc.time_ms": 1.345, // float32 run time of the call in ms - + "peer.address": { "IP": "127.0.0.1", // string IP address of calling party "Port": 60216, // int port call is coming in on @@ -47,7 +47,7 @@ Below is a JSON formatted example of a log that would be logged by the server in }, "span.kind": "server", // string client | server "system": "grpc" // string - + "custom_field": "custom_value", // string user defined field "custom_tags.int": 1337, // int user defined tag on the ctx "custom_tags.string": "something", // string user defined tag on the ctx @@ -59,7 +59,7 @@ Below is a JSON formatted example of a log that would be logged by the payload i { "level": "info", // string zap log levels "msg": "client request payload logged as grpc.request.content", // string log message - + "grpc.request.content": { // object content of RPC request "msg" : { // object ZAP specific inner object "value": "something", // string defined by caller @@ -68,7 +68,7 @@ Below is a JSON formatted example of a log that would be logged by the payload i }, "grpc.method": "Ping", // string method being called "grpc.service": "mwitkow.testproto.TestService", // string service being called - + "span.kind": "client", // string client | server "system": "grpc" // string } @@ -162,12 +162,12 @@ _ = grpc.NewServer( ## Imported Packages -- [github.com/golang/protobuf/jsonpb](https://godoc.org/github.com/golang/protobuf/jsonpb) - [github.com/golang/protobuf/proto](https://godoc.org/github.com/golang/protobuf/proto) -- [github.com/grpc-ecosystem/go-grpc-middleware](./../..) -- [github.com/grpc-ecosystem/go-grpc-middleware/logging](./..) -- [github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap](./ctxzap) -- [github.com/grpc-ecosystem/go-grpc-middleware/tags/zap](./../../tags/zap) +- [github.com/grpc-ecosystem/go-grpc-middleware](https://godoc.org/github.com/grpc-ecosystem/go-grpc-middleware) +- [github.com/grpc-ecosystem/go-grpc-middleware/logging](https://godoc.org/github.com/grpc-ecosystem/go-grpc-middleware/logging) +- [github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap](https://godoc.org/github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap) +- [github.com/grpc-ecosystem/go-grpc-middleware/tags/zap](https://godoc.org/github.com/grpc-ecosystem/go-grpc-middleware/tags/zap) +- [github.com/grpc-ecosystem/grpc-gateway/runtime](https://godoc.org/github.com/grpc-ecosystem/grpc-gateway/runtime) - [go.uber.org/zap](https://godoc.org/go.uber.org/zap) - [go.uber.org/zap/zapcore](https://godoc.org/go.uber.org/zap/zapcore) - [golang.org/x/net/context](https://godoc.org/golang.org/x/net/context) @@ -194,6 +194,7 @@ _ = grpc.NewServer( * [func UnaryServerInterceptor(logger \*zap.Logger, opts ...Option) grpc.UnaryServerInterceptor](#UnaryServerInterceptor) * [type CodeToLevel](#CodeToLevel) * [type DurationToField](#DurationToField) +* [type Marshaler](#Marshaler) * [type Option](#Option) * [func WithCodes(f grpc\_logging.ErrorToCode) Option](#WithCodes) * [func WithDecider(f grpc\_logging.Decider) Option](#WithDecider) @@ -207,7 +208,7 @@ _ = grpc.NewServer( * [Package (InitializationWithDurationFieldOverride)](#example__initializationWithDurationFieldOverride) #### Package files -[client_interceptors.go](./client_interceptors.go) [context.go](./context.go) [doc.go](./doc.go) [grpclogger.go](./grpclogger.go) [options.go](./options.go) [payload_interceptors.go](./payload_interceptors.go) [server_interceptors.go](./server_interceptors.go) +[client_interceptors.go](./client_interceptors.go) [context.go](./context.go) [doc.go](./doc.go) [grpclogger.go](./grpclogger.go) [options.go](./options.go) [payload_interceptors.go](./payload_interceptors.go) [server_interceptors.go](./server_interceptors.go) ## Variables ``` go @@ -230,13 +231,6 @@ var DefaultDurationToField = DurationToTimeMillisField ``` DefaultDurationToField is the default implementation of converting request duration to a Zap field. -``` go -var ( - // JsonPbMarshaler is the marshaler used for serializing protobuf messages. - JsonPbMarshaler Marshaler = &runtime.JSONPb{} -) -``` - ## func [AddFields](./context.go#L12) ``` go func AddFields(ctx context.Context, fields ...zapcore.Field) @@ -296,13 +290,13 @@ _ = func(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.Pin -## func [PayloadStreamClientInterceptor](./payload_interceptors.go#L74) +## func [PayloadStreamClientInterceptor](./payload_interceptors.go#L78) ``` go func PayloadStreamClientInterceptor(logger *zap.Logger, decider grpc_logging.ClientPayloadLoggingDecider) grpc.StreamClientInterceptor ``` PayloadStreamClientInterceptor returns a new streaming client interceptor that logs the paylods of requests and responses. -## func [PayloadStreamServerInterceptor](./payload_interceptors.go#L46) +## func [PayloadStreamServerInterceptor](./payload_interceptors.go#L50) ``` go func PayloadStreamServerInterceptor(logger *zap.Logger, decider grpc_logging.ServerPayloadLoggingDecider) grpc.StreamServerInterceptor ``` @@ -311,13 +305,13 @@ PayloadStreamServerInterceptor returns a new server server interceptors that log This *only* works when placed *after* the `grpc_zap.StreamServerInterceptor`. However, the logging can be done to a separate instance of the logger. -## func [PayloadUnaryClientInterceptor](./payload_interceptors.go#L58) +## func [PayloadUnaryClientInterceptor](./payload_interceptors.go#L62) ``` go func PayloadUnaryClientInterceptor(logger *zap.Logger, decider grpc_logging.ClientPayloadLoggingDecider) grpc.UnaryClientInterceptor ``` PayloadUnaryClientInterceptor returns a new unary client interceptor that logs the paylods of requests and responses. -## func [PayloadUnaryServerInterceptor](./payload_interceptors.go#L26) +## func [PayloadUnaryServerInterceptor](./payload_interceptors.go#L30) ``` go func PayloadUnaryServerInterceptor(logger *zap.Logger, decider grpc_logging.ServerPayloadLoggingDecider) grpc.UnaryServerInterceptor ``` @@ -369,6 +363,21 @@ type DurationToField func(duration time.Duration) zapcore.Field ``` DurationToField function defines how to produce duration fields for logging +## type [Marshaler](./payload_interceptors.go#L17-L19) +``` go +type Marshaler interface { + Marshal(v interface{}) ([]byte, error) +} +``` +Marshaler interface for JsonPbMarshaler + +``` go +var ( + // JsonPbMarshaler is the marshaler used for serializing protobuf messages. + JsonPbMarshaler Marshaler = &runtime.JSONPb{} +) +``` + ## type [Option](./options.go#L48) ``` go type Option func(*options)