/
entry.go
37 lines (32 loc) · 863 Bytes
/
entry.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
package gflog
import (
"context"
"go.uber.org/zap"
"google.golang.org/grpc"
"time"
)
func UnaryEntryInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
nowt := time.Now()
defer func() {
latency := time.Since(nowt)
zaps := []zap.Field{
zap.String("uri", info.FullMethod),
zap.Duration("latency", latency),
}
Info(ctx, "grpc-entry", zaps...)
}()
return handler(ctx, req)
}
func StreamEntryInterceptor(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
ctx := ss.Context()
nowt := time.Now()
defer func() {
latency := time.Since(nowt)
zaps := []zap.Field{
zap.String("uri", info.FullMethod),
zap.Duration("latency", latency),
}
Info(ctx, "grpc-entry", zaps...)
}()
return handler(srv, ss)
}