Skip to content

Commit

Permalink
rollback examples
Browse files Browse the repository at this point in the history
  • Loading branch information
theruziev committed Jun 16, 2023
1 parent 7ac6872 commit 70d7244
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 0 deletions.
36 changes: 36 additions & 0 deletions interceptors/logging/examples/kit/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,42 @@ import (
"google.golang.org/grpc"
)

func ExampleInterceptorLogger() {
logger := log.NewNopLogger()

opts := []logging.Option{
logging.WithLogOnEvents(logging.StartCall, logging.FinishCall),
// Add any other option (check functions starting with logging.With).
}

// You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished.
_ = grpc.NewServer(
grpc.ChainUnaryInterceptor(
logging.UnaryServerInterceptor(examplekit.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
grpc.ChainStreamInterceptor(
logging.StreamServerInterceptor(examplekit.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
)
// ...user server.

// Similarly you can create client that will log for the unary and stream client started or finished calls.
_, _ = grpc.Dial(
"some-target",
grpc.WithChainUnaryInterceptor(
logging.UnaryClientInterceptor(examplekit.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
grpc.WithChainStreamInterceptor(
logging.StreamClientInterceptor(examplekit.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
)
// Output:
}

type kitExampleTestSuite struct {
*testpb.InterceptorTestSuite
logBuffer *bytes.Buffer
Expand Down
37 changes: 37 additions & 0 deletions interceptors/logging/examples/log/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"context"
"log"
"os"
"runtime"
"strings"
"testing"
Expand All @@ -20,6 +21,42 @@ import (
"google.golang.org/grpc"
)

func ExampleInterceptorLogger() {
logger := log.New(os.Stderr, "", log.Ldate|log.Ltime|log.Lshortfile)

opts := []logging.Option{
logging.WithLogOnEvents(logging.StartCall, logging.FinishCall),
// Add any other option (check functions starting with logging.With).
}

// You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished.
_ = grpc.NewServer(
grpc.ChainUnaryInterceptor(
logging.UnaryServerInterceptor(examplelog.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
grpc.ChainStreamInterceptor(
logging.StreamServerInterceptor(examplelog.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
)
// ...user server.

// Similarly you can create client that will log for the unary and stream client started or finished calls.
_, _ = grpc.Dial(
"some-target",
grpc.WithChainUnaryInterceptor(
logging.UnaryClientInterceptor(examplelog.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
grpc.WithChainStreamInterceptor(
logging.StreamClientInterceptor(examplelog.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
)
// Output:
}

type logExampleTestSuite struct {
*testpb.InterceptorTestSuite
logBuffer *bytes.Buffer
Expand Down
37 changes: 37 additions & 0 deletions interceptors/logging/examples/logr/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,46 @@ import (
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
"k8s.io/klog/v2"
"k8s.io/klog/v2/ktesting"
)

func ExampleInterceptorLogger() {
logger := klog.NewKlogr()

opts := []logging.Option{
logging.WithLogOnEvents(logging.StartCall, logging.FinishCall),
// Add any other option (check functions starting with logging.With).
}

// You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished.
_ = grpc.NewServer(
grpc.ChainUnaryInterceptor(
logging.UnaryServerInterceptor(examplelogr.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
grpc.ChainStreamInterceptor(
logging.StreamServerInterceptor(examplelogr.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
)
// ...user server.

// Similarly you can create client that will log for the unary and stream client started or finished calls.
_, _ = grpc.Dial(
"some-target",
grpc.WithChainUnaryInterceptor(
logging.UnaryClientInterceptor(examplelogr.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
grpc.WithChainStreamInterceptor(
logging.StreamClientInterceptor(examplelogr.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
)
// Output:
}

type logrExampleTestSuite struct {
*testpb.InterceptorTestSuite
logBuffer *ktesting.BufferTL
Expand Down
36 changes: 36 additions & 0 deletions interceptors/logging/examples/logrus/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,42 @@ import (
"google.golang.org/grpc"
)

func ExampleInterceptorLogger() {
logger := logrus.New()

opts := []logging.Option{
logging.WithLogOnEvents(logging.StartCall, logging.FinishCall),
// Add any other option (check functions starting with logging.With).
}

// You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished.
_ = grpc.NewServer(
grpc.ChainUnaryInterceptor(
logging.UnaryServerInterceptor(examplelogrus.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
grpc.ChainStreamInterceptor(
logging.StreamServerInterceptor(examplelogrus.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
)
// ...user server.

// Similarly you can create client that will log for the unary and stream client started or finished calls.
_, _ = grpc.Dial(
"some-target",
grpc.WithChainUnaryInterceptor(
logging.UnaryClientInterceptor(examplelogrus.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
grpc.WithChainStreamInterceptor(
logging.StreamClientInterceptor(examplelogrus.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
)
// Output:
}

type logrusExampleTestSuite struct {
*testpb.InterceptorTestSuite
logBuffer *bytes.Buffer
Expand Down
37 changes: 37 additions & 0 deletions interceptors/logging/examples/slog/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package exampleslog_test
import (
"bytes"
"context"
"os"
"runtime"
"strings"
"testing"
Expand All @@ -20,6 +21,42 @@ import (
"google.golang.org/grpc"
)

func ExampleInterceptorLogger() {
logger := slog.New(slog.NewTextHandler(os.Stderr))

opts := []logging.Option{
logging.WithLogOnEvents(logging.StartCall, logging.FinishCall),
// Add any other option (check functions starting with logging.With).
}

// You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished.
_ = grpc.NewServer(
grpc.ChainUnaryInterceptor(
logging.UnaryServerInterceptor(exampleslog.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
grpc.ChainStreamInterceptor(
logging.StreamServerInterceptor(exampleslog.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
)
// ...user server.

// Similarly you can create client that will log for the unary and stream client started or finished calls.
_, _ = grpc.Dial(
"some-target",
grpc.WithChainUnaryInterceptor(
logging.UnaryClientInterceptor(exampleslog.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
grpc.WithChainStreamInterceptor(
logging.StreamClientInterceptor(exampleslog.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
)
// Output:
}

type slogExampleTestSuite struct {
*testpb.InterceptorTestSuite
logBuffer *bytes.Buffer
Expand Down
2 changes: 2 additions & 0 deletions interceptors/logging/examples/zap/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"go.uber.org/zap"
)

// InterceptorLogger adapts zap logger to interceptor logger.
// This code is simple enough to be copied and not imported.
func InterceptorLogger(l *zap.Logger) logging.Logger {
return logging.LoggerFunc(func(ctx context.Context, lvl logging.Level, msg string, fields ...any) {
f := make([]zap.Field, 0, len(fields)/2)
Expand Down
36 changes: 36 additions & 0 deletions interceptors/logging/examples/zap/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,42 @@ import (
"google.golang.org/grpc"
)

func ExampleInterceptorLogger() {
logger := zap.NewExample()

opts := []logging.Option{
logging.WithLogOnEvents(logging.StartCall, logging.FinishCall),
// Add any other option (check functions starting with logging.With).
}

// You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished.
_ = grpc.NewServer(
grpc.ChainUnaryInterceptor(
logging.UnaryServerInterceptor(examplezap.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
grpc.ChainStreamInterceptor(
logging.StreamServerInterceptor(examplezap.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
)
// ...user server.

// Similarly you can create client that will log for the unary and stream client started or finished calls.
_, _ = grpc.Dial(
"some-target",
grpc.WithChainUnaryInterceptor(
logging.UnaryClientInterceptor(examplezap.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
grpc.WithChainStreamInterceptor(
logging.StreamClientInterceptor(examplezap.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
)
// Output:
}

type zapExampleTestSuite struct {
*testpb.InterceptorTestSuite
observedLogs *observer.ObservedLogs
Expand Down
37 changes: 37 additions & 0 deletions interceptors/logging/examples/zerolog/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"context"
"encoding/json"
"os"
"runtime"
"strings"
"testing"
Expand All @@ -21,6 +22,42 @@ import (
"google.golang.org/grpc"
)

func ExampleInterceptorLogger() {
logger := zerolog.New(os.Stderr)

opts := []logging.Option{
logging.WithLogOnEvents(logging.StartCall, logging.FinishCall),
// Add any other option (check functions starting with logging.With).
}

// You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished.
_ = grpc.NewServer(
grpc.ChainUnaryInterceptor(
logging.UnaryServerInterceptor(examplezerolog.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
grpc.ChainStreamInterceptor(
logging.StreamServerInterceptor(examplezerolog.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
)
// ...user server.

// Similarly you can create client that will log for the unary and stream client started or finished calls.
_, _ = grpc.Dial(
"some-target",
grpc.WithChainUnaryInterceptor(
logging.UnaryClientInterceptor(examplezerolog.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
grpc.WithChainStreamInterceptor(
logging.StreamClientInterceptor(examplezerolog.InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
)
// Output:
}

type zerologExampleTestSuite struct {
*testpb.InterceptorTestSuite
logBuffer *bytes.Buffer
Expand Down

0 comments on commit 70d7244

Please sign in to comment.