@@ -16,9 +16,46 @@ import (
16
16
"github.com/stretchr/testify/require"
17
17
"github.com/stretchr/testify/suite"
18
18
"google.golang.org/grpc"
19
+ "k8s.io/klog/v2"
19
20
"k8s.io/klog/v2/ktesting"
20
21
)
21
22
23
+ func ExampleInterceptorLogger () {
24
+ logger := klog .NewKlogr ()
25
+
26
+ opts := []logging.Option {
27
+ logging .WithLogOnEvents (logging .StartCall , logging .FinishCall ),
28
+ // Add any other option (check functions starting with logging.With).
29
+ }
30
+
31
+ // You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished.
32
+ _ = grpc .NewServer (
33
+ grpc .ChainUnaryInterceptor (
34
+ logging .UnaryServerInterceptor (examplelogr .InterceptorLogger (logger ), opts ... ),
35
+ // Add any other interceptor you want.
36
+ ),
37
+ grpc .ChainStreamInterceptor (
38
+ logging .StreamServerInterceptor (examplelogr .InterceptorLogger (logger ), opts ... ),
39
+ // Add any other interceptor you want.
40
+ ),
41
+ )
42
+ // ...user server.
43
+
44
+ // Similarly you can create client that will log for the unary and stream client started or finished calls.
45
+ _ , _ = grpc .Dial (
46
+ "some-target" ,
47
+ grpc .WithChainUnaryInterceptor (
48
+ logging .UnaryClientInterceptor (examplelogr .InterceptorLogger (logger ), opts ... ),
49
+ // Add any other interceptor you want.
50
+ ),
51
+ grpc .WithChainStreamInterceptor (
52
+ logging .StreamClientInterceptor (examplelogr .InterceptorLogger (logger ), opts ... ),
53
+ // Add any other interceptor you want.
54
+ ),
55
+ )
56
+ // Output:
57
+ }
58
+
22
59
type logrExampleTestSuite struct {
23
60
* testpb.InterceptorTestSuite
24
61
logBuffer * ktesting.BufferTL
0 commit comments