From 619ace583252d5b2b031948a285d010bcf650e90 Mon Sep 17 00:00:00 2001 From: Dominic Green Date: Fri, 23 Feb 2018 14:21:56 +0000 Subject: [PATCH 1/3] much better that this lives back wih logging --- logging/logrus/DOC.md | 9 +-- logging/logrus/context.go | 10 +-- logging/logrus/ctxlogrus/DOC.md | 58 +++++++++++++++++ .../logrus/ctxlogrus/README.md | 0 logging/logrus/ctxlogrus/context.go | 65 +++++++++++++++++++ .../logrus/ctxlogrus}/doc.go | 4 +- .../logrus/ctxlogrus}/examples_test.go | 6 +- .../logrus/ctxlogrus}/noop.go | 2 +- logging/logrus/server_interceptors.go | 3 +- logging/zap/DOC.md | 5 +- logging/zap/context.go | 6 +- logging/zap/ctxzap/DOC.md | 65 +++++++++++++++++++ .../DOC.md => logging/zap/ctxzap/README.md | 0 logging/zap/ctxzap/context.go | 64 ++++++++++++++++++ {tags/zap => logging/zap/ctxzap}/doc.go | 8 +-- .../zap/ctxzap}/examples_test.go | 6 +- logging/zap/payload_interceptors.go | 6 +- logging/zap/server_interceptors.go | 5 +- tags/logrus/README.md | 1 - tags/logrus/context.go | 58 ++--------------- tags/zap/README.md | 1 - tags/zap/context.go | 54 ++------------- tracing/opentracing/server_interceptors.go | 2 +- 23 files changed, 301 insertions(+), 137 deletions(-) create mode 100644 logging/logrus/ctxlogrus/DOC.md rename tags/logrus/DOC.md => logging/logrus/ctxlogrus/README.md (100%) create mode 100644 logging/logrus/ctxlogrus/context.go rename {tags/logrus => logging/logrus/ctxlogrus}/doc.go (89%) rename {tags/logrus => logging/logrus/ctxlogrus}/examples_test.go (87%) rename {tags/logrus => logging/logrus/ctxlogrus}/noop.go (92%) create mode 100644 logging/zap/ctxzap/DOC.md rename tags/zap/DOC.md => logging/zap/ctxzap/README.md (100%) create mode 100644 logging/zap/ctxzap/context.go rename {tags/zap => logging/zap/ctxzap}/doc.go (55%) rename {tags/zap => logging/zap/ctxzap}/examples_test.go (87%) delete mode 120000 tags/logrus/README.md delete mode 120000 tags/zap/README.md diff --git a/logging/logrus/DOC.md b/logging/logrus/DOC.md index 60f2bddb1..1f631cb25 100644 --- a/logging/logrus/DOC.md +++ b/logging/logrus/DOC.md @@ -81,6 +81,7 @@ Please see examples and tests for examples of use. - [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/logrus/ctxlogrus](./ctxlogrus) - [github.com/grpc-ecosystem/go-grpc-middleware/tags/logrus](./../../tags/logrus) - [github.com/sirupsen/logrus](https://godoc.org/github.com/sirupsen/logrus) - [golang.org/x/net/context](https://godoc.org/golang.org/x/net/context) @@ -146,7 +147,7 @@ var ( func AddFields(ctx context.Context, fields logrus.Fields) ``` AddFields adds logrus fields to the logger. -Deprecated: should use the ctx_logrus.Extract instead +Deprecated: should use the ctxlogrus.Extract instead ## func [DefaultClientCodeToLevel](./options.go#L129) ``` go @@ -177,7 +178,7 @@ DurationToTimeMillisField converts the duration to milliseconds and uses the key func Extract(ctx context.Context) *logrus.Entry ``` Extract takes the call-scoped logrus.Entry from grpc_logrus middleware. -Deprecated: should use the ctx_logrus.Extract instead +Deprecated: should use the ctxlogrus.Extract instead ## func [PayloadStreamClientInterceptor](./payload_interceptors.go#L74) ``` go @@ -222,7 +223,7 @@ func StreamClientInterceptor(entry *logrus.Entry, opts ...Option) grpc.StreamCli ``` StreamServerInterceptor returns a new streaming client interceptor that optionally logs the execution of external gRPC calls. -## func [StreamServerInterceptor](./server_interceptors.go#L57) +## func [StreamServerInterceptor](./server_interceptors.go#L58) ``` go func StreamServerInterceptor(entry *logrus.Entry, opts ...Option) grpc.StreamServerInterceptor ``` @@ -234,7 +235,7 @@ func UnaryClientInterceptor(entry *logrus.Entry, opts ...Option) grpc.UnaryClien ``` UnaryClientInterceptor returns a new unary client interceptor that optionally logs the execution of external gRPC calls. -## func [UnaryServerInterceptor](./server_interceptors.go#L25) +## func [UnaryServerInterceptor](./server_interceptors.go#L26) ``` go func UnaryServerInterceptor(entry *logrus.Entry, opts ...Option) grpc.UnaryServerInterceptor ``` diff --git a/logging/logrus/context.go b/logging/logrus/context.go index b978f1437..a39bb7720 100644 --- a/logging/logrus/context.go +++ b/logging/logrus/context.go @@ -1,19 +1,19 @@ package grpc_logrus import ( - "github.com/grpc-ecosystem/go-grpc-middleware/tags/logrus" + "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus" "github.com/sirupsen/logrus" "golang.org/x/net/context" ) // AddFields adds logrus fields to the logger. -// Deprecated: should use the ctx_logrus.Extract instead +// Deprecated: should use the ctxlogrus.Extract instead func AddFields(ctx context.Context, fields logrus.Fields) { - ctx_logrus.AddFields(ctx, fields) + ctxlogrus.AddFields(ctx, fields) } // Extract takes the call-scoped logrus.Entry from grpc_logrus middleware. -// Deprecated: should use the ctx_logrus.Extract instead +// Deprecated: should use the ctxlogrus.Extract instead func Extract(ctx context.Context) *logrus.Entry { - return ctx_logrus.Extract(ctx) + return ctxlogrus.Extract(ctx) } diff --git a/logging/logrus/ctxlogrus/DOC.md b/logging/logrus/ctxlogrus/DOC.md new file mode 100644 index 000000000..4061f288f --- /dev/null +++ b/logging/logrus/ctxlogrus/DOC.md @@ -0,0 +1,58 @@ +# ctxlogrus +`import "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"` + +* [Overview](#pkg-overview) +* [Imported Packages](#pkg-imports) +* [Index](#pkg-index) + +## Overview +`ctxlogrus` is a ctxlogger that is backed by logrus + +It accepts a user-configured `logrus.Logger` that will be used for logging. The same `logrus.Logger` will +be populated into the `context.Context` passed into gRPC handler code. + +You can use `ctx_logrus.Extract` to log into a request-scoped `logrus.Logger` instance in your handler code. + +As `ctx_logrus.Extract` will iterate all tags on from `grpc_ctxtags` it is therefore expensive so it is advised that you +extract once at the start of the function from the context and reuse it for the remainder of the function (see examples). + +Please see examples and tests for examples of use. + +## Imported Packages + +- [github.com/grpc-ecosystem/go-grpc-middleware/tags](./../../../tags) +- [github.com/sirupsen/logrus](https://godoc.org/github.com/sirupsen/logrus) +- [golang.org/x/net/context](https://godoc.org/golang.org/x/net/context) + +## Index +* [func AddFields(ctx context.Context, fields logrus.Fields)](#AddFields) +* [func Extract(ctx context.Context) \*logrus.Entry](#Extract) +* [func ToContext(ctx context.Context, entry \*logrus.Entry) context.Context](#ToContext) + +#### Package files +[context.go](./context.go) [doc.go](./doc.go) [noop.go](./noop.go) + +## func [AddFields](./context.go#L21) +``` go +func AddFields(ctx context.Context, fields logrus.Fields) +``` +AddFields adds logrus fields to the logger. + +## func [Extract](./context.go#L35) +``` go +func Extract(ctx context.Context) *logrus.Entry +``` +Extract takes the call-scoped logrus.Entry from ctx_logrus middleware. + +If the ctx_logrus middleware wasn't used, a no-op `logrus.Entry` is returned. This makes it safe to +use regardless. + +## func [ToContext](./context.go#L59) +``` go +func ToContext(ctx context.Context, entry *logrus.Entry) context.Context +``` +ToContext adds the logrus.Entry to the context for extraction later. +Returning the new context that has been created. + +- - - +Generated by [godoc2ghmd](https://github.com/GandalfUK/godoc2ghmd) \ No newline at end of file diff --git a/tags/logrus/DOC.md b/logging/logrus/ctxlogrus/README.md similarity index 100% rename from tags/logrus/DOC.md rename to logging/logrus/ctxlogrus/README.md diff --git a/logging/logrus/ctxlogrus/context.go b/logging/logrus/ctxlogrus/context.go new file mode 100644 index 000000000..ff3e3353d --- /dev/null +++ b/logging/logrus/ctxlogrus/context.go @@ -0,0 +1,65 @@ +package ctxlogrus + +import ( + "github.com/grpc-ecosystem/go-grpc-middleware/tags" + "github.com/sirupsen/logrus" + "golang.org/x/net/context" +) + +type ctxLoggerMarker struct{} + +type ctxLogger struct { + logger *logrus.Entry + fields logrus.Fields +} + +var ( + ctxLoggerKey = &ctxLoggerMarker{} +) + +// AddFields adds logrus fields to the logger. +func AddFields(ctx context.Context, fields logrus.Fields) { + l, ok := ctx.Value(ctxLoggerKey).(*ctxLogger) + if !ok || l == nil { + return + } + for k, v := range fields { + l.fields[k] = v + } +} + +// Extract takes the call-scoped logrus.Entry from ctx_logrus middleware. +// +// If the ctx_logrus middleware wasn't used, a no-op `logrus.Entry` is returned. This makes it safe to +// use regardless. +func Extract(ctx context.Context) *logrus.Entry { + l, ok := ctx.Value(ctxLoggerKey).(*ctxLogger) + if !ok || l == nil { + return logrus.NewEntry(nullLogger) + } + + fields := logrus.Fields{} + + // Add grpc_ctxtags tags metadata until now. + tags := grpc_ctxtags.Extract(ctx) + for k, v := range tags.Values() { + fields[k] = v + } + + // Add logrus fields added until now. + for k, v := range l.fields { + fields[k] = v + } + + return l.logger.WithFields(fields) +} + +// ToContext adds the logrus.Entry to the context for extraction later. +// Returning the new context that has been created. +func ToContext(ctx context.Context, entry *logrus.Entry) context.Context { + l := &ctxLogger{ + logger: entry, + fields: logrus.Fields{}, + } + return context.WithValue(ctx, ctxLoggerKey, l) +} diff --git a/tags/logrus/doc.go b/logging/logrus/ctxlogrus/doc.go similarity index 89% rename from tags/logrus/doc.go rename to logging/logrus/ctxlogrus/doc.go index 6b1229753..95803fb65 100644 --- a/tags/logrus/doc.go +++ b/logging/logrus/ctxlogrus/doc.go @@ -1,5 +1,5 @@ /* -`ctx_logrus` is a ctxlogger that is backed by logrus +`ctxlogrus` is a ctxlogger that is backed by logrus It accepts a user-configured `logrus.Logger` that will be used for logging. The same `logrus.Logger` will be populated into the `context.Context` passed into gRPC handler code. @@ -11,4 +11,4 @@ extract once at the start of the function from the context and reuse it for the Please see examples and tests for examples of use. */ -package ctx_logrus +package ctxlogrus diff --git a/tags/logrus/examples_test.go b/logging/logrus/ctxlogrus/examples_test.go similarity index 87% rename from tags/logrus/examples_test.go rename to logging/logrus/ctxlogrus/examples_test.go index 93ae3266f..9af2af9f2 100644 --- a/tags/logrus/examples_test.go +++ b/logging/logrus/ctxlogrus/examples_test.go @@ -1,8 +1,8 @@ -package ctx_logrus_test +package ctxlogrus_test import ( + "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus" "github.com/grpc-ecosystem/go-grpc-middleware/tags" - "github.com/grpc-ecosystem/go-grpc-middleware/tags/logrus" pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/testing/testproto" "github.com/sirupsen/logrus" "golang.org/x/net/context" @@ -16,7 +16,7 @@ func Example_HandlerUsageUnaryPing() { // Add fields the ctxtags of the request which will be added to all extracted loggers. grpc_ctxtags.Extract(ctx).Set("custom_tags.string", "something").Set("custom_tags.int", 1337) // Extract a single request-scoped logrus.Logger and log messages. - l := ctx_logrus.Extract(ctx) + l := ctxlogrus.Extract(ctx) l.Info("some ping") l.Info("another ping") return &pb_testproto.PingResponse{Value: ping.Value}, nil diff --git a/tags/logrus/noop.go b/logging/logrus/ctxlogrus/noop.go similarity index 92% rename from tags/logrus/noop.go rename to logging/logrus/ctxlogrus/noop.go index 8b57c2a2a..7fcc0f64d 100644 --- a/tags/logrus/noop.go +++ b/logging/logrus/ctxlogrus/noop.go @@ -1,4 +1,4 @@ -package ctx_logrus +package ctxlogrus import ( "io/ioutil" diff --git a/logging/logrus/server_interceptors.go b/logging/logrus/server_interceptors.go index a9dd6e270..aa9cc5342 100644 --- a/logging/logrus/server_interceptors.go +++ b/logging/logrus/server_interceptors.go @@ -7,6 +7,7 @@ import ( "time" "github.com/grpc-ecosystem/go-grpc-middleware" + "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus" "github.com/grpc-ecosystem/go-grpc-middleware/tags/logrus" "github.com/sirupsen/logrus" "golang.org/x/net/context" @@ -124,5 +125,5 @@ func newLoggerForCall(ctx context.Context, entry *logrus.Entry, fullMethodString } callLog = callLog.WithFields(ctx_logrus.Extract(ctx).Data) - return ctx_logrus.ToContext(ctx, callLog) + return ctxlogrus.ToContext(ctx, callLog) } diff --git a/logging/zap/DOC.md b/logging/zap/DOC.md index 63c118930..00f3f853e 100644 --- a/logging/zap/DOC.md +++ b/logging/zap/DOC.md @@ -83,6 +83,7 @@ Please see examples and tests for examples of use. - [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) - [go.uber.org/zap](https://godoc.org/go.uber.org/zap) - [go.uber.org/zap/zapcore](https://godoc.org/go.uber.org/zap/zapcore) @@ -232,7 +233,7 @@ func StreamClientInterceptor(logger *zap.Logger, opts ...Option) grpc.StreamClie ``` StreamServerInterceptor returns a new streaming client interceptor that optionally logs the execution of external gRPC calls. -## func [StreamServerInterceptor](./server_interceptors.go#L50) +## func [StreamServerInterceptor](./server_interceptors.go#L51) ``` go func StreamServerInterceptor(logger *zap.Logger, opts ...Option) grpc.StreamServerInterceptor ``` @@ -244,7 +245,7 @@ func UnaryClientInterceptor(logger *zap.Logger, opts ...Option) grpc.UnaryClient ``` UnaryClientInterceptor returns a new unary client interceptor that optionally logs the execution of external gRPC calls. -## func [UnaryServerInterceptor](./server_interceptors.go#L24) +## func [UnaryServerInterceptor](./server_interceptors.go#L25) ``` go func UnaryServerInterceptor(logger *zap.Logger, opts ...Option) grpc.UnaryServerInterceptor ``` diff --git a/logging/zap/context.go b/logging/zap/context.go index d495d2c3d..b4bcf9f9c 100644 --- a/logging/zap/context.go +++ b/logging/zap/context.go @@ -1,7 +1,7 @@ package grpc_zap import ( - "github.com/grpc-ecosystem/go-grpc-middleware/tags/zap" + "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap" "go.uber.org/zap" "go.uber.org/zap/zapcore" "golang.org/x/net/context" @@ -10,11 +10,11 @@ import ( // AddFields adds zap fields to the logger. // Deprecated: should use the ctx_zap.AddFields instead func AddFields(ctx context.Context, fields ...zapcore.Field) { - ctx_zap.AddFields(ctx, fields...) + ctxzap.AddFields(ctx, fields...) } // Extract takes the call-scoped Logger from grpc_zap middleware. // Deprecated: should use the ctx_zap.Extract instead func Extract(ctx context.Context) *zap.Logger { - return ctx_zap.Extract(ctx) + return ctxzap.Extract(ctx) } diff --git a/logging/zap/ctxzap/DOC.md b/logging/zap/ctxzap/DOC.md new file mode 100644 index 000000000..86ecf2398 --- /dev/null +++ b/logging/zap/ctxzap/DOC.md @@ -0,0 +1,65 @@ +# ctxzap +`import "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"` + +* [Overview](#pkg-overview) +* [Imported Packages](#pkg-imports) +* [Index](#pkg-index) + +## Overview +`ctxzap` is a ctxlogger that is backed by Zap + +It accepts a user-configured `zap.Logger` that will be used for logging. The same `zap.Logger` will +be populated into the `context.Context` passed into gRPC handler code. + +You can use `ctxzap.Extract` to log into a request-scoped `zap.Logger` instance in your handler code. + +As `ctxzap.Extract` will iterate all tags on from `grpc_ctxtags` it is therefore expensive so it is advised that you +extract once at the start of the function from the context and reuse it for the remainder of the function (see examples). + +Please see examples and tests for examples of use. + +## Imported Packages + +- [github.com/grpc-ecosystem/go-grpc-middleware/tags](./../../../tags) +- [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) + +## Index +* [func AddFields(ctx context.Context, fields ...zapcore.Field)](#AddFields) +* [func Extract(ctx context.Context) \*zap.Logger](#Extract) +* [func TagsToFields(ctx context.Context) []zapcore.Field](#TagsToFields) +* [func ToContext(ctx context.Context, logger \*zap.Logger) context.Context](#ToContext) + +#### Package files +[context.go](./context.go) [doc.go](./doc.go) + +## func [AddFields](./context.go#L23) +``` go +func AddFields(ctx context.Context, fields ...zapcore.Field) +``` +AddFields adds zap fields to the logger. + +## func [Extract](./context.go#L35) +``` go +func Extract(ctx context.Context) *zap.Logger +``` +Extract takes the call-scoped Logger from grpc_zap middleware. + +It always returns a Logger that has all the grpc_ctxtags updated. + +## func [TagsToFields](./context.go#L48) +``` go +func TagsToFields(ctx context.Context) []zapcore.Field +``` +TagsToFields transforms the Tags on the supplied context into zap fields. + +## func [ToContext](./context.go#L59) +``` go +func ToContext(ctx context.Context, logger *zap.Logger) context.Context +``` +ToContext adds the zap.Logger to the context for extraction later. +Returning the new context that has been created. + +- - - +Generated by [godoc2ghmd](https://github.com/GandalfUK/godoc2ghmd) \ No newline at end of file diff --git a/tags/zap/DOC.md b/logging/zap/ctxzap/README.md similarity index 100% rename from tags/zap/DOC.md rename to logging/zap/ctxzap/README.md diff --git a/logging/zap/ctxzap/context.go b/logging/zap/ctxzap/context.go new file mode 100644 index 000000000..d38dbfda0 --- /dev/null +++ b/logging/zap/ctxzap/context.go @@ -0,0 +1,64 @@ +package ctxzap + +import ( + "github.com/grpc-ecosystem/go-grpc-middleware/tags" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "golang.org/x/net/context" +) + +type ctxMarker struct{} + +type ctxLogger struct { + logger *zap.Logger + fields []zapcore.Field +} + +var ( + ctxMarkerKey = &ctxMarker{} + nullLogger = zap.NewNop() +) + +// AddFields adds zap fields to the logger. +func AddFields(ctx context.Context, fields ...zapcore.Field) { + l, ok := ctx.Value(ctxMarkerKey).(*ctxLogger) + if !ok || l == nil { + return + } + l.fields = append(l.fields, fields...) + +} + +// Extract takes the call-scoped Logger from grpc_zap middleware. +// +// It always returns a Logger that has all the grpc_ctxtags updated. +func Extract(ctx context.Context) *zap.Logger { + l, ok := ctx.Value(ctxMarkerKey).(*ctxLogger) + if !ok || l == nil { + return nullLogger + } + // Add grpc_ctxtags tags metadata until now. + fields := TagsToFields(ctx) + // Add zap fields added until now. + fields = append(fields, l.fields...) + return l.logger.With(fields...) +} + +// TagsToFields transforms the Tags on the supplied context into zap fields. +func TagsToFields(ctx context.Context) []zapcore.Field { + fields := []zapcore.Field{} + tags := grpc_ctxtags.Extract(ctx) + for k, v := range tags.Values() { + fields = append(fields, zap.Any(k, v)) + } + return fields +} + +// ToContext adds the zap.Logger to the context for extraction later. +// Returning the new context that has been created. +func ToContext(ctx context.Context, logger *zap.Logger) context.Context { + l := &ctxLogger{ + logger: logger, + } + return context.WithValue(ctx, ctxMarkerKey, l) +} diff --git a/tags/zap/doc.go b/logging/zap/ctxzap/doc.go similarity index 55% rename from tags/zap/doc.go rename to logging/zap/ctxzap/doc.go index a4d96e2b5..3591e3585 100644 --- a/tags/zap/doc.go +++ b/logging/zap/ctxzap/doc.go @@ -1,14 +1,14 @@ /* -`ctx_zap` is a ctxlogger that is backed by Zap +`ctxzap` is a ctxlogger that is backed by Zap It accepts a user-configured `zap.Logger` that will be used for logging. The same `zap.Logger` will be populated into the `context.Context` passed into gRPC handler code. -You can use `ctx_zap.Extract` to log into a request-scoped `zap.Logger` instance in your handler code. +You can use `ctxzap.Extract` to log into a request-scoped `zap.Logger` instance in your handler code. -As `ctx_zap.Extract` will iterate all tags on from `grpc_ctxtags` it is therefore expensive so it is advised that you +As `ctxzap.Extract` will iterate all tags on from `grpc_ctxtags` it is therefore expensive so it is advised that you extract once at the start of the function from the context and reuse it for the remainder of the function (see examples). Please see examples and tests for examples of use. */ -package ctx_zap +package ctxzap diff --git a/tags/zap/examples_test.go b/logging/zap/ctxzap/examples_test.go similarity index 87% rename from tags/zap/examples_test.go rename to logging/zap/ctxzap/examples_test.go index 21e94641e..f5db24190 100644 --- a/tags/zap/examples_test.go +++ b/logging/zap/ctxzap/examples_test.go @@ -1,10 +1,10 @@ -package ctx_zap_test +package ctxzap_test import ( "context" + "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap" "github.com/grpc-ecosystem/go-grpc-middleware/tags" - "github.com/grpc-ecosystem/go-grpc-middleware/tags/zap" pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/testing/testproto" "go.uber.org/zap" ) @@ -18,7 +18,7 @@ func Example_HandlerUsageUnaryPing() { grpc_ctxtags.Extract(ctx).Set("custom_tags.string", "something").Set("custom_tags.int", 1337) // Extract a single request-scoped zap.Logger and log messages. - l := ctx_zap.Extract(ctx) + l := ctxzap.Extract(ctx) l.Info("some ping") l.Info("another ping") return &pb_testproto.PingResponse{Value: ping.Value}, nil diff --git a/logging/zap/payload_interceptors.go b/logging/zap/payload_interceptors.go index 1d0189aed..e32297020 100644 --- a/logging/zap/payload_interceptors.go +++ b/logging/zap/payload_interceptors.go @@ -7,7 +7,7 @@ import ( "github.com/golang/protobuf/jsonpb" "github.com/golang/protobuf/proto" "github.com/grpc-ecosystem/go-grpc-middleware/logging" - "github.com/grpc-ecosystem/go-grpc-middleware/tags/zap" + "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap" "go.uber.org/zap" "go.uber.org/zap/zapcore" "golang.org/x/net/context" @@ -29,7 +29,7 @@ func PayloadUnaryServerInterceptor(logger *zap.Logger, decider grpc_logging.Serv return handler(ctx, req) } // Use the provided zap.Logger for logging but use the fields from context. - logEntry := logger.With(append(serverCallFields(info.FullMethod), ctx_zap.TagsToFields(ctx)...)...) + logEntry := logger.With(append(serverCallFields(info.FullMethod), ctxzap.TagsToFields(ctx)...)...) logProtoMessageAsJson(logEntry, req, "grpc.request.content", "server request payload logged as grpc.request.content field") resp, err := handler(ctx, req) if err == nil { @@ -48,7 +48,7 @@ func PayloadStreamServerInterceptor(logger *zap.Logger, decider grpc_logging.Ser if !decider(stream.Context(), info.FullMethod, srv) { return handler(srv, stream) } - logEntry := logger.With(append(serverCallFields(info.FullMethod), ctx_zap.TagsToFields(stream.Context())...)...) + logEntry := logger.With(append(serverCallFields(info.FullMethod), ctxzap.TagsToFields(stream.Context())...)...) newStream := &loggingServerStream{ServerStream: stream, logger: logEntry} return handler(srv, newStream) } diff --git a/logging/zap/server_interceptors.go b/logging/zap/server_interceptors.go index af381b7aa..9a22f38ed 100644 --- a/logging/zap/server_interceptors.go +++ b/logging/zap/server_interceptors.go @@ -5,6 +5,7 @@ import ( "time" "github.com/grpc-ecosystem/go-grpc-middleware" + "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap" "github.com/grpc-ecosystem/go-grpc-middleware/tags/zap" "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -85,11 +86,11 @@ func serverCallFields(fullMethodString string) []zapcore.Field { } func newLoggerForCall(ctx context.Context, logger *zap.Logger, fullMethodString string, start time.Time) context.Context { - f := ctx_zap.TagsToFields(ctx) + f := ctxzap.TagsToFields(ctx) f = append(f, zap.String("grpc.start_time", start.Format(time.RFC3339))) if d, ok := ctx.Deadline(); ok { f = append(f, zap.String("grpc.request.deadline", d.Format(time.RFC3339))) } callLog := logger.With(append(f, serverCallFields(fullMethodString)...)...) - return ctx_zap.ToContext(ctx, callLog) + return ctxzap.ToContext(ctx, callLog) } diff --git a/tags/logrus/README.md b/tags/logrus/README.md deleted file mode 120000 index 71bfc07c9..000000000 --- a/tags/logrus/README.md +++ /dev/null @@ -1 +0,0 @@ -DOC.md \ No newline at end of file diff --git a/tags/logrus/context.go b/tags/logrus/context.go index 8bbf5db68..8d7a9895f 100644 --- a/tags/logrus/context.go +++ b/tags/logrus/context.go @@ -1,65 +1,19 @@ package ctx_logrus import ( - "github.com/grpc-ecosystem/go-grpc-middleware/tags" + "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus" "github.com/sirupsen/logrus" "golang.org/x/net/context" ) -type ctxLoggerMarker struct{} - -type ctxLogger struct { - logger *logrus.Entry - fields logrus.Fields -} - -var ( - ctxLoggerKey = &ctxLoggerMarker{} -) - // AddFields adds logrus fields to the logger. +// Deprecated: should use the ctxlogrus.Extract instead func AddFields(ctx context.Context, fields logrus.Fields) { - l, ok := ctx.Value(ctxLoggerKey).(*ctxLogger) - if !ok || l == nil { - return - } - for k, v := range fields { - l.fields[k] = v - } + ctxlogrus.AddFields(ctx, fields) } -// Extract takes the call-scoped logrus.Entry from ctx_logrus middleware. -// -// If the ctx_logrus middleware wasn't used, a no-op `logrus.Entry` is returned. This makes it safe to -// use regardless. +// Extract takes the call-scoped logrus.Entry from grpc_logrus middleware. +// Deprecated: should use the ctxlogrus.Extract instead func Extract(ctx context.Context) *logrus.Entry { - l, ok := ctx.Value(ctxLoggerKey).(*ctxLogger) - if !ok || l == nil { - return logrus.NewEntry(nullLogger) - } - - fields := logrus.Fields{} - - // Add grpc_ctxtags tags metadata until now. - tags := grpc_ctxtags.Extract(ctx) - for k, v := range tags.Values() { - fields[k] = v - } - - // Add logrus fields added until now. - for k, v := range l.fields { - fields[k] = v - } - - return l.logger.WithFields(fields) -} - -// ToContext adds the logrus.Entry to the context for extraction later. -// Returning the new context that has been created. -func ToContext(ctx context.Context, entry *logrus.Entry) context.Context { - l := &ctxLogger{ - logger: entry, - fields: logrus.Fields{}, - } - return context.WithValue(ctx, ctxLoggerKey, l) + return ctxlogrus.Extract(ctx) } diff --git a/tags/zap/README.md b/tags/zap/README.md deleted file mode 120000 index 71bfc07c9..000000000 --- a/tags/zap/README.md +++ /dev/null @@ -1 +0,0 @@ -DOC.md \ No newline at end of file diff --git a/tags/zap/context.go b/tags/zap/context.go index 24ad97bc7..a48906d35 100644 --- a/tags/zap/context.go +++ b/tags/zap/context.go @@ -1,64 +1,20 @@ package ctx_zap import ( - "github.com/grpc-ecosystem/go-grpc-middleware/tags" + "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap" "go.uber.org/zap" "go.uber.org/zap/zapcore" "golang.org/x/net/context" ) -type ctxMarker struct{} - -type ctxLogger struct { - logger *zap.Logger - fields []zapcore.Field -} - -var ( - ctxMarkerKey = &ctxMarker{} - nullLogger = zap.NewNop() -) - // AddFields adds zap fields to the logger. +// Deprecated: should use the ctx_zap.AddFields instead func AddFields(ctx context.Context, fields ...zapcore.Field) { - l, ok := ctx.Value(ctxMarkerKey).(*ctxLogger) - if !ok || l == nil { - return - } - l.fields = append(l.fields, fields...) - + ctxzap.AddFields(ctx, fields...) } // Extract takes the call-scoped Logger from grpc_zap middleware. -// -// It always returns a Logger that has all the grpc_ctxtags updated. +// Deprecated: should use the ctx_zap.Extract instead func Extract(ctx context.Context) *zap.Logger { - l, ok := ctx.Value(ctxMarkerKey).(*ctxLogger) - if !ok || l == nil { - return nullLogger - } - // Add grpc_ctxtags tags metadata until now. - fields := TagsToFields(ctx) - // Add zap fields added until now. - fields = append(fields, l.fields...) - return l.logger.With(fields...) -} - -// TagsToFields transforms the Tags on the supplied context into zap fields. -func TagsToFields(ctx context.Context) []zapcore.Field { - fields := []zapcore.Field{} - tags := grpc_ctxtags.Extract(ctx) - for k, v := range tags.Values() { - fields = append(fields, zap.Any(k, v)) - } - return fields -} - -// ToContext adds the zap.Logger to the context for extraction later. -// Returning the new context that has been created. -func ToContext(ctx context.Context, logger *zap.Logger) context.Context { - l := &ctxLogger{ - logger: logger, - } - return context.WithValue(ctx, ctxMarkerKey, l) + return ctxzap.Extract(ctx) } diff --git a/tracing/opentracing/server_interceptors.go b/tracing/opentracing/server_interceptors.go index 64604887f..498e8efae 100644 --- a/tracing/opentracing/server_interceptors.go +++ b/tracing/opentracing/server_interceptors.go @@ -16,7 +16,7 @@ import ( ) var ( - grpcTag = opentracing.Tag{string(ext.Component), "gRPC"} + grpcTag = opentracing.Tag{Key: string(ext.Component), Value: "gRPC"} ) // UnaryServerInterceptor returns a new unary server interceptor for OpenTracing. From c8b8d4956b27d7023e67f212f87013f2f244e93f Mon Sep 17 00:00:00 2001 From: Dominic Green Date: Wed, 28 Feb 2018 11:20:32 +0000 Subject: [PATCH 2/3] maintaining the public contract --- logging/zap/context.go | 4 ++-- tags/logrus/context.go | 6 ++++++ tags/zap/context.go | 14 ++++++++++++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/logging/zap/context.go b/logging/zap/context.go index b4bcf9f9c..cce9903a1 100644 --- a/logging/zap/context.go +++ b/logging/zap/context.go @@ -8,13 +8,13 @@ import ( ) // AddFields adds zap fields to the logger. -// Deprecated: should use the ctx_zap.AddFields instead +// Deprecated: should use the ctxzap.AddFields instead func AddFields(ctx context.Context, fields ...zapcore.Field) { ctxzap.AddFields(ctx, fields...) } // Extract takes the call-scoped Logger from grpc_zap middleware. -// Deprecated: should use the ctx_zap.Extract instead +// Deprecated: should use the ctxzap.Extract instead func Extract(ctx context.Context) *zap.Logger { return ctxzap.Extract(ctx) } diff --git a/tags/logrus/context.go b/tags/logrus/context.go index 8d7a9895f..2596be89f 100644 --- a/tags/logrus/context.go +++ b/tags/logrus/context.go @@ -17,3 +17,9 @@ func AddFields(ctx context.Context, fields logrus.Fields) { func Extract(ctx context.Context) *logrus.Entry { return ctxlogrus.Extract(ctx) } + +// ToContext adds the logrus.Entry to the context for extraction later. +// Depricated: should use ctxlogrus.ToContext instead +func ToContext(ctx context.Context, entry *logrus.Entry) context.Context { + return ctxlogrus.ToContext(ctx, entry) +} diff --git a/tags/zap/context.go b/tags/zap/context.go index a48906d35..7e04078f1 100644 --- a/tags/zap/context.go +++ b/tags/zap/context.go @@ -8,13 +8,23 @@ import ( ) // AddFields adds zap fields to the logger. -// Deprecated: should use the ctx_zap.AddFields instead +// Deprecated: should use the ctxzap.AddFields instead func AddFields(ctx context.Context, fields ...zapcore.Field) { ctxzap.AddFields(ctx, fields...) } // Extract takes the call-scoped Logger from grpc_zap middleware. -// Deprecated: should use the ctx_zap.Extract instead +// Deprecated: should use the ctxzap.Extract instead func Extract(ctx context.Context) *zap.Logger { return ctxzap.Extract(ctx) } + +// Deprecated: use ctxzap.TagsToFields +func TagsToFields(ctx context.Context) []zapcore.Field { + return ctxzap.TagsToFields(ctx) +} + +// Deprecated: use ctxzap.ToContext +func ToContext(ctx context.Context, logger *zap.Logger) context.Context { + return ctxzap.ToContext(ctx, logger) +} From 62a6cf8f95ca2544b52c53e6e17b513fd1eaf4b9 Mon Sep 17 00:00:00 2001 From: Dominic Green Date: Wed, 28 Feb 2018 11:55:55 +0000 Subject: [PATCH 3/3] fixing comments --- logging/zap/DOC.md | 4 ++-- tags/zap/context.go | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/logging/zap/DOC.md b/logging/zap/DOC.md index 00f3f853e..efe2fd496 100644 --- a/logging/zap/DOC.md +++ b/logging/zap/DOC.md @@ -156,7 +156,7 @@ var ( func AddFields(ctx context.Context, fields ...zapcore.Field) ``` AddFields adds zap fields to the logger. -Deprecated: should use the ctx_zap.AddFields instead +Deprecated: should use the ctxzap.AddFields instead ## func [DefaultClientCodeToLevel](./options.go#L127) ``` go @@ -188,7 +188,7 @@ DurationToTimeMillisField converts the duration to milliseconds and uses the key func Extract(ctx context.Context) *zap.Logger ``` Extract takes the call-scoped Logger from grpc_zap middleware. -Deprecated: should use the ctx_zap.Extract instead +Deprecated: should use the ctxzap.Extract instead ## func [PayloadStreamClientInterceptor](./payload_interceptors.go#L74) ``` go diff --git a/tags/zap/context.go b/tags/zap/context.go index 7e04078f1..be608ec91 100644 --- a/tags/zap/context.go +++ b/tags/zap/context.go @@ -19,11 +19,14 @@ func Extract(ctx context.Context) *zap.Logger { return ctxzap.Extract(ctx) } +// TagsToFields transforms the Tags on the supplied context into zap fields. // Deprecated: use ctxzap.TagsToFields func TagsToFields(ctx context.Context) []zapcore.Field { return ctxzap.TagsToFields(ctx) } +// ToContext adds the zap.Logger to the context for extraction later. +// Returning the new context that has been created. // Deprecated: use ctxzap.ToContext func ToContext(ctx context.Context, logger *zap.Logger) context.Context { return ctxzap.ToContext(ctx, logger)