@@ -16,9 +16,46 @@ import (
1616 "github.com/stretchr/testify/require"
1717 "github.com/stretchr/testify/suite"
1818 "google.golang.org/grpc"
19+ "k8s.io/klog/v2"
1920 "k8s.io/klog/v2/ktesting"
2021)
2122
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+
2259type logrExampleTestSuite struct {
2360 * testpb.InterceptorTestSuite
2461 logBuffer * ktesting.BufferTL
0 commit comments