Skip to content

Commit 0e9bafc

Browse files
domgreenyifanzz
authored andcommitted
ctxlogger split out from grpc_logging (grpc-ecosystem#107)
* moving this out will make ctxlogger independent and mean you do not always have to use it withg grpc_logging loggers. It is then consumed by the grpc_logging loggers and we will depricate methods on there in future. * moving this out will make ctxlogger independent and mean you do not always have to use it withg grpc_logging loggers. It is then consumed by the grpc_logging loggers and we will depricate methods on there in future. * changes from commit * fixing * removing comment * removing interceptor for the context loggers * ctxlogger_zap > ctx_zap ... removing redundant logger from package name
1 parent c8ec2b5 commit 0e9bafc

31 files changed

+428
-223
lines changed

logging/DOC.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
* [Index](#pkg-index)
77

88
## <a name="pkg-overview">Overview</a>
9-
grpc_logging is a "parent" package for gRPC logging middlewares
9+
grpc_logging is a "parent" package for gRPC logging middlewares.
1010

1111
### General functionality of all middleware
12-
All logging middleware have an `Extract(ctx)` function that provides a request-scoped logger with gRPC-related fields
13-
(service and method names). Moreover, that logger will have fields populated from the `grpc_ctxtags.Tags` of the
14-
context.
12+
The gRPC logging middleware populates request-scoped data to `grpc_ctxtags.Tags` that relate to the current gRPC call
13+
(e.g. service and method names).
14+
15+
Once the gRPC logging middleware has added the gRPC specific Tags to the ctx they will then be written with the logs
16+
that are made using the `ctx_logrus` or `ctx_zap` loggers.
1517

1618
All logging middleware will emit a final log statement. It is based on the error returned by the handler function,
1719
the gRPC status code, an error (if any) and it will emit at a level controlled via `WithLevels`.

logging/doc.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

44
//
55
/*
6-
grpc_logging is a "parent" package for gRPC logging middlewares
6+
grpc_logging is a "parent" package for gRPC logging middlewares.
77
88
General functionality of all middleware
99
10-
All logging middleware have an `Extract(ctx)` function that provides a request-scoped logger with gRPC-related fields
11-
(service and method names). Moreover, that logger will have fields populated from the `grpc_ctxtags.Tags` of the
12-
context.
10+
The gRPC logging middleware populates request-scoped data to `grpc_ctxtags.Tags` that relate to the current gRPC call
11+
(e.g. service and method names).
12+
13+
Once the gRPC logging middleware has added the gRPC specific Tags to the ctx they will then be written with the logs
14+
that are made using the `ctx_logrus` or `ctx_zap` loggers.
1315
1416
All logging middleware will emit a final log statement. It is based on the error returned by the handler function,
1517
the gRPC status code, an error (if any) and it will emit at a level controlled via `WithLevels`.

logging/logrus/DOC.md

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@
1111
It accepts a user-configured `logrus.Entry` that will be used for logging completed gRPC calls. The same
1212
`logrus.Entry` will be used for logging completed gRPC calls, and be populated into the `context.Context` passed into gRPC handler code.
1313

14-
You can use `Extract` to log into a request-scoped `logrus.Entry` instance in your handler code. The fields set on the
15-
logger correspond to the grpc_ctxtags.Tags attached to the context.
16-
17-
As `Extract` will iterate all tags on from `grpc_ctxtags` it is therefore expensive so it is advised that you
18-
extract once at the start of the function from the context and reuse it for the remainder of the function (see examples).
14+
On calling `StreamServerInterceptor` or `UnaryServerInterceptor` this logging middleware will add gRPC call information
15+
to the ctx so that it will be present on subsequent use of the `ctx_zap` logger.
1916

2017
This package also implements request and response *payload* logging, both for server-side and client-side. These will be
2118
logged as structured `jsonbp` fields for every message received/sent (both unary and streaming). For that please use
@@ -32,7 +29,7 @@ Please see examples and tests for examples of use.
3229
- [github.com/golang/protobuf/proto](https://godoc.org/github.com/golang/protobuf/proto)
3330
- [github.com/grpc-ecosystem/go-grpc-middleware](./../..)
3431
- [github.com/grpc-ecosystem/go-grpc-middleware/logging](./..)
35-
- [github.com/grpc-ecosystem/go-grpc-middleware/tags](./../../tags)
32+
- [github.com/grpc-ecosystem/go-grpc-middleware/tags/logrus](./../../tags/logrus)
3633
- [github.com/sirupsen/logrus](https://godoc.org/github.com/sirupsen/logrus)
3734
- [golang.org/x/net/context](https://godoc.org/golang.org/x/net/context)
3835
- [google.golang.org/grpc](https://godoc.org/google.golang.org/grpc)
@@ -64,7 +61,7 @@ Please see examples and tests for examples of use.
6461
* [func WithLevels(f CodeToLevel) Option](#WithLevels)
6562

6663
#### <a name="pkg-files">Package files</a>
67-
[client_interceptors.go](./client_interceptors.go) [context.go](./context.go) [doc.go](./doc.go) [grpclogger.go](./grpclogger.go) [noop.go](./noop.go) [options.go](./options.go) [payload_interceptors.go](./payload_interceptors.go) [server_interceptors.go](./server_interceptors.go)
64+
[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)
6865

6966
## <a name="pkg-variables">Variables</a>
7067
``` go
@@ -88,11 +85,12 @@ var (
8885
)
8986
```
9087

91-
## <a name="AddFields">func</a> [AddFields](./context.go#L24)
88+
## <a name="AddFields">func</a> [AddFields](./context.go#L11)
9289
``` go
9390
func AddFields(ctx context.Context, fields logrus.Fields)
9491
```
9592
AddFields adds logrus fields to the logger.
93+
Deprecated: should use the ctx_logrus.Extract instead
9694

9795
## <a name="DefaultClientCodeToLevel">func</a> [DefaultClientCodeToLevel](./options.go#L120)
9896
``` go
@@ -118,22 +116,20 @@ func DurationToTimeMillisField(duration time.Duration) (key string, value interf
118116
```
119117
DurationToTimeMillisField converts the duration to milliseconds and uses the key `grpc.time_ms`.
120118

121-
## <a name="Extract">func</a> [Extract](./context.go#L38)
119+
## <a name="Extract">func</a> [Extract](./context.go#L17)
122120
``` go
123121
func Extract(ctx context.Context) *logrus.Entry
124122
```
125123
Extract takes the call-scoped logrus.Entry from grpc_logrus middleware.
124+
Deprecated: should use the ctx_logrus.Extract instead
126125

127-
If the grpc_logrus middleware wasn't used, a no-op `logrus.Entry` is returned. This makes it safe to
128-
use regardless.
129-
130-
## <a name="PayloadStreamClientInterceptor">func</a> [PayloadStreamClientInterceptor](./payload_interceptors.go#L77)
126+
## <a name="PayloadStreamClientInterceptor">func</a> [PayloadStreamClientInterceptor](./payload_interceptors.go#L74)
131127
``` go
132128
func PayloadStreamClientInterceptor(entry *logrus.Entry, decider grpc_logging.ClientPayloadLoggingDecider) grpc.StreamClientInterceptor
133129
```
134130
PayloadStreamServerInterceptor returns a new streaming client interceptor that logs the paylods of requests and responses.
135131

136-
## <a name="PayloadStreamServerInterceptor">func</a> [PayloadStreamServerInterceptor](./payload_interceptors.go#L48)
132+
## <a name="PayloadStreamServerInterceptor">func</a> [PayloadStreamServerInterceptor](./payload_interceptors.go#L45)
137133
``` go
138134
func PayloadStreamServerInterceptor(entry *logrus.Entry, decider grpc_logging.ServerPayloadLoggingDecider) grpc.StreamServerInterceptor
139135
```
@@ -142,13 +138,13 @@ PayloadUnaryServerInterceptor returns a new server server interceptors that logs
142138
This *only* works when placed *after* the `grpc_logrus.StreamServerInterceptor`. However, the logging can be done to a
143139
separate instance of the logger.
144140

145-
## <a name="PayloadUnaryClientInterceptor">func</a> [PayloadUnaryClientInterceptor](./payload_interceptors.go#L61)
141+
## <a name="PayloadUnaryClientInterceptor">func</a> [PayloadUnaryClientInterceptor](./payload_interceptors.go#L58)
146142
``` go
147143
func PayloadUnaryClientInterceptor(entry *logrus.Entry, decider grpc_logging.ClientPayloadLoggingDecider) grpc.UnaryClientInterceptor
148144
```
149145
PayloadUnaryClientInterceptor returns a new unary client interceptor that logs the paylods of requests and responses.
150146

151-
## <a name="PayloadUnaryServerInterceptor">func</a> [PayloadUnaryServerInterceptor](./payload_interceptors.go#L28)
147+
## <a name="PayloadUnaryServerInterceptor">func</a> [PayloadUnaryServerInterceptor](./payload_interceptors.go#L25)
152148
``` go
153149
func PayloadUnaryServerInterceptor(entry *logrus.Entry, decider grpc_logging.ServerPayloadLoggingDecider) grpc.UnaryServerInterceptor
154150
```
@@ -170,7 +166,7 @@ func StreamClientInterceptor(entry *logrus.Entry, opts ...Option) grpc.StreamCli
170166
```
171167
StreamServerInterceptor returns a new streaming client interceptor that optionally logs the execution of external gRPC calls.
172168

173-
## <a name="StreamServerInterceptor">func</a> [StreamServerInterceptor](./server_interceptors.go#L50)
169+
## <a name="StreamServerInterceptor">func</a> [StreamServerInterceptor](./server_interceptors.go#L48)
174170
``` go
175171
func StreamServerInterceptor(entry *logrus.Entry, opts ...Option) grpc.StreamServerInterceptor
176172
```
@@ -182,7 +178,7 @@ func UnaryClientInterceptor(entry *logrus.Entry, opts ...Option) grpc.UnaryClien
182178
```
183179
UnaryClientInterceptor returns a new unary client interceptor that optionally logs the execution of external gRPC calls.
184180

185-
## <a name="UnaryServerInterceptor">func</a> [UnaryServerInterceptor](./server_interceptors.go#L25)
181+
## <a name="UnaryServerInterceptor">func</a> [UnaryServerInterceptor](./server_interceptors.go#L23)
186182
``` go
187183
func UnaryServerInterceptor(entry *logrus.Entry, opts ...Option) grpc.UnaryServerInterceptor
188184
```

logging/logrus/context.go

Lines changed: 5 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,19 @@
1-
// Copyright 2016 Michal Witkowski. All Rights Reserved.
2-
// See LICENSE for licensing terms.
3-
41
package grpc_logrus
52

63
import (
7-
"github.com/grpc-ecosystem/go-grpc-middleware/tags"
4+
"github.com/grpc-ecosystem/go-grpc-middleware/tags/logrus"
85
"github.com/sirupsen/logrus"
96
"golang.org/x/net/context"
107
)
118

12-
type ctxMarker struct{}
13-
14-
type ctxLogger struct {
15-
logger *logrus.Entry
16-
fields logrus.Fields
17-
}
18-
19-
var (
20-
ctxMarkerKey = &ctxMarker{}
21-
)
22-
239
// AddFields adds logrus fields to the logger.
10+
// Deprecated: should use the ctx_logrus.Extract instead
2411
func AddFields(ctx context.Context, fields logrus.Fields) {
25-
l, ok := ctx.Value(ctxMarkerKey).(*ctxLogger)
26-
if !ok || l == nil {
27-
return
28-
}
29-
for k, v := range fields {
30-
l.fields[k] = v
31-
}
12+
ctx_logrus.AddFields(ctx, fields)
3213
}
3314

3415
// Extract takes the call-scoped logrus.Entry from grpc_logrus middleware.
35-
//
36-
// If the grpc_logrus middleware wasn't used, a no-op `logrus.Entry` is returned. This makes it safe to
37-
// use regardless.
16+
// Deprecated: should use the ctx_logrus.Extract instead
3817
func Extract(ctx context.Context) *logrus.Entry {
39-
l, ok := ctx.Value(ctxMarkerKey).(*ctxLogger)
40-
if !ok || l == nil {
41-
return logrus.NewEntry(nullLogger)
42-
}
43-
44-
fields := logrus.Fields{}
45-
46-
// Add grpc_ctxtags tags metadata until now.
47-
tags := grpc_ctxtags.Extract(ctx)
48-
for k, v := range tags.Values() {
49-
fields[k] = v
50-
}
51-
52-
// Add logrus fields added until now.
53-
for k, v := range l.fields {
54-
fields[k] = v
55-
}
56-
57-
return l.logger.WithFields(fields)
58-
}
59-
60-
func toContext(ctx context.Context, entry *logrus.Entry) context.Context {
61-
l := &ctxLogger{
62-
logger: entry,
63-
fields: logrus.Fields{},
64-
}
65-
return context.WithValue(ctx, ctxMarkerKey, l)
18+
return ctx_logrus.Extract(ctx)
6619
}

logging/logrus/doc.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
// Copyright 2017 Michal Witkowski. All Rights Reserved.
2-
// See LICENSE for licensing terms.
3-
41
/*
52
`grpc_logrus` is a gRPC logging middleware backed by Logrus loggers
63
74
It accepts a user-configured `logrus.Entry` that will be used for logging completed gRPC calls. The same
85
`logrus.Entry` will be used for logging completed gRPC calls, and be populated into the `context.Context` passed into gRPC handler code.
96
10-
You can use `Extract` to log into a request-scoped `logrus.Entry` instance in your handler code. The fields set on the
11-
logger correspond to the grpc_ctxtags.Tags attached to the context.
12-
13-
As `Extract` will iterate all tags on from `grpc_ctxtags` it is therefore expensive so it is advised that you
14-
extract once at the start of the function from the context and reuse it for the remainder of the function (see examples).
7+
On calling `StreamServerInterceptor` or `UnaryServerInterceptor` this logging middleware will add gRPC call information
8+
to the ctx so that it will be present on subsequent use of the `ctx_zap` logger.
159
1610
This package also implements request and response *payload* logging, both for server-side and client-side. These will be
1711
logged as structured `jsonbp` fields for every message received/sent (both unary and streaming). For that please use

logging/logrus/examples_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/grpc-ecosystem/go-grpc-middleware"
1010
"github.com/grpc-ecosystem/go-grpc-middleware/tags"
11+
"github.com/grpc-ecosystem/go-grpc-middleware/tags/logrus"
1112
pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/testing/testproto"
1213
"golang.org/x/net/context"
1314
"google.golang.org/grpc"
@@ -68,7 +69,7 @@ func Example_HandlerUsageUnaryPing() {
6869
// Add fields the ctxtags of the request which will be added to all extracted loggers.
6970
grpc_ctxtags.Extract(ctx).Set("custom_tags.string", "something").Set("custom_tags.int", 1337)
7071
// Extract a single request-scoped logrus.Logger and log messages.
71-
l := grpc_logrus.Extract(ctx)
72+
l := ctx_logrus.Extract(ctx)
7273
l.Info("some ping")
7374
l.Info("another ping")
7475
return &pb_testproto.PingResponse{Value: ping.Value}, nil

logging/logrus/payload_interceptors.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
// Copyright 2017 Michal Witkowski. All Rights Reserved.
2-
// See LICENSE for licensing terms.
3-
41
package grpc_logrus
52

63
import (
74
"bytes"
8-
95
"fmt"
106

117
"github.com/golang/protobuf/jsonpb"
128
"github.com/golang/protobuf/proto"
139
"github.com/grpc-ecosystem/go-grpc-middleware/logging"
10+
"github.com/grpc-ecosystem/go-grpc-middleware/tags/logrus"
1411
"github.com/sirupsen/logrus"
1512
"golang.org/x/net/context"
1613
"google.golang.org/grpc"
@@ -31,7 +28,7 @@ func PayloadUnaryServerInterceptor(entry *logrus.Entry, decider grpc_logging.Ser
3128
return handler(ctx, req)
3229
}
3330
// Use the provided logrus.Entry for logging but use the fields from context.
34-
logEntry := entry.WithFields(Extract(ctx).Data)
31+
logEntry := entry.WithFields(ctx_logrus.Extract(ctx).Data)
3532
logProtoMessageAsJson(logEntry, req, "grpc.request.content", "server request payload logged as grpc.request.content field")
3633
resp, err := handler(ctx, req)
3734
if err == nil {

logging/logrus/payload_interceptors_test.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
// Copyright 2017 Michal Witkowski. All Rights Reserved.
2-
// See LICENSE for licensing terms.
3-
41
package grpc_logrus_test
52

63
import (
74
"fmt"
8-
"runtime"
9-
"testing"
10-
5+
"io"
116
"io/ioutil"
7+
"runtime"
128
"strings"
13-
14-
"github.com/stretchr/testify/suite"
15-
"google.golang.org/grpc"
16-
17-
"io"
9+
"testing"
1810

1911
"github.com/grpc-ecosystem/go-grpc-middleware"
2012
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
@@ -23,7 +15,9 @@ import (
2315
"github.com/sirupsen/logrus"
2416
"github.com/stretchr/testify/assert"
2517
"github.com/stretchr/testify/require"
18+
"github.com/stretchr/testify/suite"
2619
"golang.org/x/net/context"
20+
"google.golang.org/grpc"
2721
)
2822

2923
var (

logging/logrus/server_interceptors.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
// Copyright 2017 Michal Witkowski. All Rights Reserved.
2-
// See LICENSE for licensing terms.
3-
41
package grpc_logrus
52

63
import (
74
"path"
85
"time"
96

107
"github.com/grpc-ecosystem/go-grpc-middleware"
8+
"github.com/grpc-ecosystem/go-grpc-middleware/tags/logrus"
119
"github.com/sirupsen/logrus"
1210
"golang.org/x/net/context"
1311
"google.golang.org/grpc"
@@ -39,7 +37,7 @@ func UnaryServerInterceptor(entry *logrus.Entry, opts ...Option) grpc.UnaryServe
3937
fields[logrus.ErrorKey] = err
4038
}
4139
levelLogf(
42-
Extract(newCtx).WithFields(fields), // re-extract logger from newCtx, as it may have extra fields that changed in the holder.
40+
ctx_logrus.Extract(newCtx).WithFields(fields), // re-extract logger from newCtx, as it may have extra fields that changed in the holder.
4341
level,
4442
"finished unary call")
4543
return resp, err
@@ -67,7 +65,7 @@ func StreamServerInterceptor(entry *logrus.Entry, opts ...Option) grpc.StreamSer
6765
fields[logrus.ErrorKey] = err
6866
}
6967
levelLogf(
70-
Extract(newCtx).WithFields(fields), // re-extract logger from newCtx, as it may have extra fields that changed in the holder.
68+
ctx_logrus.Extract(newCtx).WithFields(fields), // re-extract logger from newCtx, as it may have extra fields that changed in the holder.
7169
level,
7270
"finished streaming call")
7371
return err
@@ -101,5 +99,7 @@ func newLoggerForCall(ctx context.Context, entry *logrus.Entry, fullMethodString
10199
"grpc.service": service,
102100
"grpc.method": method,
103101
})
104-
return toContext(ctx, callLog)
102+
103+
callLog = callLog.WithFields(ctx_logrus.Extract(ctx).Data)
104+
return ctx_logrus.ToContext(ctx, callLog)
105105
}

logging/logrus/server_interceptors_test.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
1-
// Copyright 2017 Michal Witkowski. All Rights Reserved.
2-
// See LICENSE for licensing terms.
3-
41
package grpc_logrus_test
52

63
import (
7-
"runtime"
8-
"testing"
9-
10-
"github.com/stretchr/testify/suite"
11-
"google.golang.org/grpc"
12-
13-
"io"
14-
154
"fmt"
16-
5+
"io"
6+
"runtime"
177
"strings"
8+
"testing"
189

1910
"github.com/grpc-ecosystem/go-grpc-middleware"
2011
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
@@ -23,6 +14,8 @@ import (
2314
"github.com/sirupsen/logrus"
2415
"github.com/stretchr/testify/assert"
2516
"github.com/stretchr/testify/require"
17+
"github.com/stretchr/testify/suite"
18+
"google.golang.org/grpc"
2619
"google.golang.org/grpc/codes"
2720
)
2821

0 commit comments

Comments
 (0)