-
Notifications
You must be signed in to change notification settings - Fork 11
/
grpc.go
58 lines (51 loc) · 1.43 KB
/
grpc.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 debug
import (
"context"
"google.golang.org/grpc"
"goa.design/clue/log"
)
// UnaryServerInterceptor return an interceptor that manages whether debug log
// entries are written. This interceptor should be used in conjunction with the
// MountDebugLogEnabler function.
func UnaryServerInterceptor() grpc.UnaryServerInterceptor {
return func(
ctx context.Context,
req interface{},
_ *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (interface{}, error) {
if debugLogs {
ctx = log.Context(ctx, log.WithDebug())
} else {
ctx = log.Context(ctx, log.WithNoDebug())
}
return handler(ctx, req)
}
}
// StreamServerInterceptor returns a stream interceptor that manages whether
// debug log entries are written. Note: a change in the debug setting is
// effective only for the next stream request. This interceptor should be used
// in conjunction with the MountDebugLogEnabler function.
func StreamServerInterceptor() grpc.StreamServerInterceptor {
return func(
srv interface{},
stream grpc.ServerStream,
_ *grpc.StreamServerInfo,
handler grpc.StreamHandler,
) error {
ctx := stream.Context()
if debugLogs {
ctx = log.Context(ctx, log.WithDebug())
} else {
ctx = log.Context(ctx, log.WithNoDebug())
}
return handler(srv, &streamWithContext{stream, ctx})
}
}
type streamWithContext struct {
grpc.ServerStream
ctx context.Context
}
func (s *streamWithContext) Context() context.Context {
return s.ctx
}