forked from grpc-ecosystem/go-grpc-middleware
/
context.go
64 lines (55 loc) · 1.56 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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)
}