Skip to content

Commit 70d7244

Browse files
committed
rollback examples
1 parent 7ac6872 commit 70d7244

File tree

8 files changed

+258
-0
lines changed

8 files changed

+258
-0
lines changed

interceptors/logging/examples/kit/example_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,42 @@ import (
2020
"google.golang.org/grpc"
2121
)
2222

23+
func ExampleInterceptorLogger() {
24+
logger := log.NewNopLogger()
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(examplekit.InterceptorLogger(logger), opts...),
35+
// Add any other interceptor you want.
36+
),
37+
grpc.ChainStreamInterceptor(
38+
logging.StreamServerInterceptor(examplekit.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(examplekit.InterceptorLogger(logger), opts...),
49+
// Add any other interceptor you want.
50+
),
51+
grpc.WithChainStreamInterceptor(
52+
logging.StreamClientInterceptor(examplekit.InterceptorLogger(logger), opts...),
53+
// Add any other interceptor you want.
54+
),
55+
)
56+
// Output:
57+
}
58+
2359
type kitExampleTestSuite struct {
2460
*testpb.InterceptorTestSuite
2561
logBuffer *bytes.Buffer

interceptors/logging/examples/log/example_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"bytes"
88
"context"
99
"log"
10+
"os"
1011
"runtime"
1112
"strings"
1213
"testing"
@@ -20,6 +21,42 @@ import (
2021
"google.golang.org/grpc"
2122
)
2223

24+
func ExampleInterceptorLogger() {
25+
logger := log.New(os.Stderr, "", log.Ldate|log.Ltime|log.Lshortfile)
26+
27+
opts := []logging.Option{
28+
logging.WithLogOnEvents(logging.StartCall, logging.FinishCall),
29+
// Add any other option (check functions starting with logging.With).
30+
}
31+
32+
// You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished.
33+
_ = grpc.NewServer(
34+
grpc.ChainUnaryInterceptor(
35+
logging.UnaryServerInterceptor(examplelog.InterceptorLogger(logger), opts...),
36+
// Add any other interceptor you want.
37+
),
38+
grpc.ChainStreamInterceptor(
39+
logging.StreamServerInterceptor(examplelog.InterceptorLogger(logger), opts...),
40+
// Add any other interceptor you want.
41+
),
42+
)
43+
// ...user server.
44+
45+
// Similarly you can create client that will log for the unary and stream client started or finished calls.
46+
_, _ = grpc.Dial(
47+
"some-target",
48+
grpc.WithChainUnaryInterceptor(
49+
logging.UnaryClientInterceptor(examplelog.InterceptorLogger(logger), opts...),
50+
// Add any other interceptor you want.
51+
),
52+
grpc.WithChainStreamInterceptor(
53+
logging.StreamClientInterceptor(examplelog.InterceptorLogger(logger), opts...),
54+
// Add any other interceptor you want.
55+
),
56+
)
57+
// Output:
58+
}
59+
2360
type logExampleTestSuite struct {
2461
*testpb.InterceptorTestSuite
2562
logBuffer *bytes.Buffer

interceptors/logging/examples/logr/example_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -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+
2259
type logrExampleTestSuite struct {
2360
*testpb.InterceptorTestSuite
2461
logBuffer *ktesting.BufferTL

interceptors/logging/examples/logrus/example_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,42 @@ import (
2121
"google.golang.org/grpc"
2222
)
2323

24+
func ExampleInterceptorLogger() {
25+
logger := logrus.New()
26+
27+
opts := []logging.Option{
28+
logging.WithLogOnEvents(logging.StartCall, logging.FinishCall),
29+
// Add any other option (check functions starting with logging.With).
30+
}
31+
32+
// You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished.
33+
_ = grpc.NewServer(
34+
grpc.ChainUnaryInterceptor(
35+
logging.UnaryServerInterceptor(examplelogrus.InterceptorLogger(logger), opts...),
36+
// Add any other interceptor you want.
37+
),
38+
grpc.ChainStreamInterceptor(
39+
logging.StreamServerInterceptor(examplelogrus.InterceptorLogger(logger), opts...),
40+
// Add any other interceptor you want.
41+
),
42+
)
43+
// ...user server.
44+
45+
// Similarly you can create client that will log for the unary and stream client started or finished calls.
46+
_, _ = grpc.Dial(
47+
"some-target",
48+
grpc.WithChainUnaryInterceptor(
49+
logging.UnaryClientInterceptor(examplelogrus.InterceptorLogger(logger), opts...),
50+
// Add any other interceptor you want.
51+
),
52+
grpc.WithChainStreamInterceptor(
53+
logging.StreamClientInterceptor(examplelogrus.InterceptorLogger(logger), opts...),
54+
// Add any other interceptor you want.
55+
),
56+
)
57+
// Output:
58+
}
59+
2460
type logrusExampleTestSuite struct {
2561
*testpb.InterceptorTestSuite
2662
logBuffer *bytes.Buffer

interceptors/logging/examples/slog/example_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package exampleslog_test
66
import (
77
"bytes"
88
"context"
9+
"os"
910
"runtime"
1011
"strings"
1112
"testing"
@@ -20,6 +21,42 @@ import (
2021
"google.golang.org/grpc"
2122
)
2223

24+
func ExampleInterceptorLogger() {
25+
logger := slog.New(slog.NewTextHandler(os.Stderr))
26+
27+
opts := []logging.Option{
28+
logging.WithLogOnEvents(logging.StartCall, logging.FinishCall),
29+
// Add any other option (check functions starting with logging.With).
30+
}
31+
32+
// You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished.
33+
_ = grpc.NewServer(
34+
grpc.ChainUnaryInterceptor(
35+
logging.UnaryServerInterceptor(exampleslog.InterceptorLogger(logger), opts...),
36+
// Add any other interceptor you want.
37+
),
38+
grpc.ChainStreamInterceptor(
39+
logging.StreamServerInterceptor(exampleslog.InterceptorLogger(logger), opts...),
40+
// Add any other interceptor you want.
41+
),
42+
)
43+
// ...user server.
44+
45+
// Similarly you can create client that will log for the unary and stream client started or finished calls.
46+
_, _ = grpc.Dial(
47+
"some-target",
48+
grpc.WithChainUnaryInterceptor(
49+
logging.UnaryClientInterceptor(exampleslog.InterceptorLogger(logger), opts...),
50+
// Add any other interceptor you want.
51+
),
52+
grpc.WithChainStreamInterceptor(
53+
logging.StreamClientInterceptor(exampleslog.InterceptorLogger(logger), opts...),
54+
// Add any other interceptor you want.
55+
),
56+
)
57+
// Output:
58+
}
59+
2360
type slogExampleTestSuite struct {
2461
*testpb.InterceptorTestSuite
2562
logBuffer *bytes.Buffer

interceptors/logging/examples/zap/example.go

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"go.uber.org/zap"
1212
)
1313

14+
// InterceptorLogger adapts zap logger to interceptor logger.
15+
// This code is simple enough to be copied and not imported.
1416
func InterceptorLogger(l *zap.Logger) logging.Logger {
1517
return logging.LoggerFunc(func(ctx context.Context, lvl logging.Level, msg string, fields ...any) {
1618
f := make([]zap.Field, 0, len(fields)/2)

interceptors/logging/examples/zap/example_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,42 @@ import (
2020
"google.golang.org/grpc"
2121
)
2222

23+
func ExampleInterceptorLogger() {
24+
logger := zap.NewExample()
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(examplezap.InterceptorLogger(logger), opts...),
35+
// Add any other interceptor you want.
36+
),
37+
grpc.ChainStreamInterceptor(
38+
logging.StreamServerInterceptor(examplezap.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(examplezap.InterceptorLogger(logger), opts...),
49+
// Add any other interceptor you want.
50+
),
51+
grpc.WithChainStreamInterceptor(
52+
logging.StreamClientInterceptor(examplezap.InterceptorLogger(logger), opts...),
53+
// Add any other interceptor you want.
54+
),
55+
)
56+
// Output:
57+
}
58+
2359
type zapExampleTestSuite struct {
2460
*testpb.InterceptorTestSuite
2561
observedLogs *observer.ObservedLogs

interceptors/logging/examples/zerolog/example_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"bytes"
88
"context"
99
"encoding/json"
10+
"os"
1011
"runtime"
1112
"strings"
1213
"testing"
@@ -21,6 +22,42 @@ import (
2122
"google.golang.org/grpc"
2223
)
2324

25+
func ExampleInterceptorLogger() {
26+
logger := zerolog.New(os.Stderr)
27+
28+
opts := []logging.Option{
29+
logging.WithLogOnEvents(logging.StartCall, logging.FinishCall),
30+
// Add any other option (check functions starting with logging.With).
31+
}
32+
33+
// You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished.
34+
_ = grpc.NewServer(
35+
grpc.ChainUnaryInterceptor(
36+
logging.UnaryServerInterceptor(examplezerolog.InterceptorLogger(logger), opts...),
37+
// Add any other interceptor you want.
38+
),
39+
grpc.ChainStreamInterceptor(
40+
logging.StreamServerInterceptor(examplezerolog.InterceptorLogger(logger), opts...),
41+
// Add any other interceptor you want.
42+
),
43+
)
44+
// ...user server.
45+
46+
// Similarly you can create client that will log for the unary and stream client started or finished calls.
47+
_, _ = grpc.Dial(
48+
"some-target",
49+
grpc.WithChainUnaryInterceptor(
50+
logging.UnaryClientInterceptor(examplezerolog.InterceptorLogger(logger), opts...),
51+
// Add any other interceptor you want.
52+
),
53+
grpc.WithChainStreamInterceptor(
54+
logging.StreamClientInterceptor(examplezerolog.InterceptorLogger(logger), opts...),
55+
// Add any other interceptor you want.
56+
),
57+
)
58+
// Output:
59+
}
60+
2461
type zerologExampleTestSuite struct {
2562
*testpb.InterceptorTestSuite
2663
logBuffer *bytes.Buffer

0 commit comments

Comments
 (0)