-
Notifications
You must be signed in to change notification settings - Fork 11
/
server_interceptors.go
58 lines (43 loc) · 1.7 KB
/
server_interceptors.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
package accesslog
import (
"context"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/peer"
"github.com/cloud-barista/cb-ladybug/src/grpc-api/logger"
)
// ===== [ Constants and Variables ] =====
// ===== [ Types ] =====
// ===== [ Implementations ] =====
// ===== [ Private Functions ] =====
// ===== [ Public Functions ] =====
// UnaryServerInterceptor - rpc unary receive 정보를 기록하는 서버 인터셉터
func UnaryServerInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
startTime := time.Now()
resp, err := handler(ctx, req)
elapsed := time.Now().Sub(startTime)
clientIP := "unknown"
if p, ok := peer.FromContext(ctx); ok {
clientIP = p.Addr.String()
}
logger := logger.NewLogger()
logger.Info("[", clientIP, "] grpc server unary received : ", info.FullMethod, " service [", elapsed.Nanoseconds()/1000000, " ms]")
return resp, err
}
}
// StreamServerInterceptor - rpc stream receive 정보를 기록하는 서버 인터셉터
func StreamServerInterceptor() grpc.StreamServerInterceptor {
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
startTime := time.Now()
err := handler(srv, stream)
elapsed := time.Now().Sub(startTime)
clientIP := "unknown"
if p, ok := peer.FromContext(stream.Context()); ok {
clientIP = p.Addr.String()
}
logger := logger.NewLogger()
logger.Info("[", clientIP, "] grpc server stream received : ", info.FullMethod, " service [", elapsed.Nanoseconds()/1000000, " ms]")
return err
}
}