-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzap.go
148 lines (119 loc) · 4.17 KB
/
zap.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package zap
import (
"context"
"path"
"time"
tz "github.com/alexfalkowski/go-service/telemetry/logger/zap"
tm "github.com/alexfalkowski/go-service/transport/meta"
"github.com/alexfalkowski/go-service/transport/strings"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const (
service = "grpc"
)
// UnaryServerInterceptor for zap.
func UnaryServerInterceptor(logger *zap.Logger) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
p := path.Dir(info.FullMethod)[1:]
if strings.IsHealth(p) {
return handler(ctx, req)
}
start := time.Now()
resp, err := handler(ctx, req)
fields := []zapcore.Field{
zap.Stringer(tm.DurationKey, time.Since(start)),
zap.String(tm.ServiceKey, service),
zap.String(tm.PathKey, info.FullMethod),
}
fields = append(fields, tz.Meta(ctx)...)
code := status.Code(err)
fields = append(fields, zap.Any(tm.CodeKey, code))
tz.LogWithFunc(message(info.FullMethod), err, CodeToLogFunc(code, logger), fields...)
return resp, err
}
}
// StreamServerInterceptor for zap.
func StreamServerInterceptor(logger *zap.Logger) grpc.StreamServerInterceptor {
return func(srv any, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
p := path.Dir(info.FullMethod)[1:]
if strings.IsHealth(p) {
return handler(srv, stream)
}
start := time.Now()
ctx := stream.Context()
err := handler(srv, stream)
fields := []zapcore.Field{
zap.Stringer(tm.DurationKey, time.Since(start)),
zap.String(tm.ServiceKey, service),
zap.String(tm.PathKey, info.FullMethod),
}
fields = append(fields, tz.Meta(ctx)...)
code := status.Code(err)
fields = append(fields, zap.Any(tm.CodeKey, code))
tz.LogWithFunc(message(info.FullMethod), err, CodeToLogFunc(code, logger), fields...)
return err
}
}
// UnaryClientInterceptor for zap.
func UnaryClientInterceptor(logger *zap.Logger) grpc.UnaryClientInterceptor {
return func(ctx context.Context, fullMethod string, req, resp any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
p := path.Dir(fullMethod)[1:]
if strings.IsHealth(p) {
return invoker(ctx, fullMethod, req, resp, cc, opts...)
}
start := time.Now()
err := invoker(ctx, fullMethod, req, resp, cc, opts...)
fields := []zapcore.Field{
zap.Stringer(tm.DurationKey, time.Since(start)),
zap.String(tm.ServiceKey, service),
zap.String(tm.PathKey, fullMethod),
}
fields = append(fields, tz.Meta(ctx)...)
code := status.Code(err)
fields = append(fields, zap.Any(tm.CodeKey, code))
tz.LogWithFunc(message(cc.Target()+fullMethod), err, CodeToLogFunc(code, logger), fields...)
return err
}
}
// StreamClientInterceptor for zap.
func StreamClientInterceptor(logger *zap.Logger) grpc.StreamClientInterceptor {
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, fullMethod string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
p := path.Dir(fullMethod)[1:]
if strings.IsHealth(p) {
return streamer(ctx, desc, cc, fullMethod, opts...)
}
start := time.Now()
stream, err := streamer(ctx, desc, cc, fullMethod, opts...)
fields := []zapcore.Field{
zap.Stringer(tm.DurationKey, time.Since(start)),
zap.String(tm.ServiceKey, service),
zap.String(tm.PathKey, fullMethod),
}
fields = append(fields, tz.Meta(ctx)...)
code := status.Code(err)
fields = append(fields, zap.Any(tm.CodeKey, code))
tz.LogWithFunc(message(cc.Target()+fullMethod), err, CodeToLogFunc(code, logger), fields...)
return stream, err
}
}
// CodeToLogFunc for zap.
//
//nolint:exhaustive
func CodeToLogFunc(code codes.Code, logger *zap.Logger) tz.LogFunc {
switch code {
case codes.OK:
return logger.Info
case codes.Canceled, codes.InvalidArgument, codes.NotFound, codes.AlreadyExists, codes.PermissionDenied, codes.Unauthenticated,
codes.ResourceExhausted, codes.FailedPrecondition, codes.Aborted, codes.OutOfRange:
return logger.Warn
default:
return logger.Error
}
}
func message(msg string) string {
return "grpc: get " + msg
}