/
interceptor.go
139 lines (108 loc) · 3.27 KB
/
interceptor.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
package fxgrpcserver
import (
"context"
"time"
"github.com/ekkinox/fx-template/modules/fxlogger"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
type LoggerInterceptor struct {
logger *fxlogger.Logger
}
func NewLoggerInterceptor(logger *fxlogger.Logger) *LoggerInterceptor {
return &LoggerInterceptor{
logger: logger,
}
}
func (i *LoggerInterceptor) UnaryInterceptor() func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
reqId, traceParent := i.extractMetadata(ctx)
grpcLogger := i.logger.
With().
Str("x-request-id", reqId).
Str("traceparent", traceParent).
Logger()
grpcLogger.
Info().
Str("grpc-type", "unary").
Str("grpc-method", info.FullMethod).
Msg("grpc call start")
newCtx := grpcLogger.WithContext(ctx)
now := time.Now()
resp, err := handler(newCtx, req)
if err != nil {
errStatus := status.Convert(err)
grpcLogger.
Error().
Err(err).
Int32("grpc-code", int32(errStatus.Code())).
Str("grpc-status", errStatus.Code().String()).
Str("grpc-duration", time.Since(now).String()).
Msg("grpc call error")
} else {
grpcLogger.
Info().
Int32("grpc-code", int32(codes.OK)).
Str("grpc-status", codes.OK.String()).
Str("grpc-duration", time.Since(now).String()).
Msg("grpc call success")
}
return resp, err
}
}
func (i *LoggerInterceptor) StreamInterceptor() func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
ctx := ss.Context()
reqId, traceParent := i.extractMetadata(ctx)
grpcLogger := i.logger.
With().
Str("x-request-id", reqId).
Str("traceparent", traceParent).
Logger()
grpcLogger.
Info().
Str("grpc-type", "server-streaming").
Str("grpc-method", info.FullMethod).
Msg("grpc call start")
newCtx := grpcLogger.WithContext(ctx)
wrappedStream := NewWrappedStream(ss, newCtx)
now := time.Now()
err := handler(srv, wrappedStream)
if err != nil {
errStatus := status.Convert(err)
grpcLogger.
Error().
Err(err).
Int32("grpc-code", int32(errStatus.Code())).
Str("grpc-status", errStatus.Code().String()).
Str("grpc-duration", time.Since(now).String()).
Msg("grpc call error")
} else {
grpcLogger.
Info().
Int32("grpc-code", int32(codes.OK)).
Str("grpc-status", codes.OK.String()).
Str("grpc-duration", time.Since(now).String()).
Msg("grpc call success")
}
return err
}
}
func (i *LoggerInterceptor) extractMetadata(ctx context.Context) (string, string) {
var reqId string
var traceParent string
md, _ := metadata.FromIncomingContext(ctx)
if requestId, ok := md["x-request-id"]; ok && len(requestId) > 0 {
reqId = requestId[0]
} else {
reqId = generateRequestId()
}
if requestTraceParent, ok := md["traceparent"]; ok && len(requestTraceParent) > 0 {
traceParent = requestTraceParent[0]
} else {
traceParent = ""
}
return reqId, traceParent
}