Skip to content

Commit

Permalink
make example interceptors private
Browse files Browse the repository at this point in the history
  • Loading branch information
theruziev committed Jun 24, 2023
1 parent 70d7244 commit 2814228
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 284 deletions.
33 changes: 0 additions & 33 deletions interceptors/logging/examples/kit/example.go

This file was deleted.

35 changes: 28 additions & 7 deletions interceptors/logging/examples/kit/example_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
// Copyright (c) The go-grpc-middleware Authors.
// Licensed under the Apache License 2.0.

package examplekit_test
package kit_test

import (
"bytes"
"context"
"fmt"
"runtime"
"strings"
"testing"

"github.com/go-kit/log"
examplekit "github.com/grpc-ecosystem/go-grpc-middleware/interceptors/logging/examples/kit"
"github.com/go-kit/log/level"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
"github.com/stretchr/testify/assert"
Expand All @@ -20,6 +21,26 @@ import (
"google.golang.org/grpc"
)

// InterceptorLogger adapts go-kit logger to interceptor logger.
// This code is simple enough to be copied and not imported.
func InterceptorLogger(l log.Logger) logging.Logger {
return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) {
largs := append([]any{"msg", msg}, fields...)
switch lvl {
case logging.LevelDebug:
_ = level.Debug(l).Log(largs...)
case logging.LevelInfo:
_ = level.Info(l).Log(largs...)
case logging.LevelWarn:
_ = level.Warn(l).Log(largs...)
case logging.LevelError:
_ = level.Error(l).Log(largs...)
default:
panic(fmt.Sprintf("unknown level %v", lvl))
}
})
}

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

Expand All @@ -31,11 +52,11 @@ func ExampleInterceptorLogger() {
// 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...),
logging.UnaryServerInterceptor(InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
grpc.ChainStreamInterceptor(
logging.StreamServerInterceptor(examplekit.InterceptorLogger(logger), opts...),
logging.StreamServerInterceptor(InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
)
Expand All @@ -45,11 +66,11 @@ func ExampleInterceptorLogger() {
_, _ = grpc.Dial(
"some-target",
grpc.WithChainUnaryInterceptor(
logging.UnaryClientInterceptor(examplekit.InterceptorLogger(logger), opts...),
logging.UnaryClientInterceptor(InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
grpc.WithChainStreamInterceptor(
logging.StreamClientInterceptor(examplekit.InterceptorLogger(logger), opts...),
logging.StreamClientInterceptor(InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
)
Expand All @@ -68,7 +89,7 @@ func TestSuite(t *testing.T) {
}

buffer := &bytes.Buffer{}
logger := examplekit.InterceptorLogger(log.NewLogfmtLogger(buffer))
logger := InterceptorLogger(log.NewLogfmtLogger(buffer))

s := &kitExampleTestSuite{
InterceptorTestSuite: &testpb.InterceptorTestSuite{
Expand Down
32 changes: 0 additions & 32 deletions interceptors/logging/examples/log/example.go

This file was deleted.

34 changes: 27 additions & 7 deletions interceptors/logging/examples/log/example_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// Copyright (c) The go-grpc-middleware Authors.
// Licensed under the Apache License 2.0.

package examplelog_test
package log_test

import (
"bytes"
"context"
"fmt"
"log"
"os"
"runtime"
"strings"
"testing"

examplelog "github.com/grpc-ecosystem/go-grpc-middleware/interceptors/logging/examples/log"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
"github.com/stretchr/testify/assert"
Expand All @@ -21,6 +21,26 @@ import (
"google.golang.org/grpc"
)

// InterceptorLogger adapts standard Go logger to interceptor logger.
// This code is simple enough to be copied and not imported.
func InterceptorLogger(l *log.Logger) logging.Logger {
return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) {
switch lvl {
case logging.LevelDebug:
msg = fmt.Sprintf("DEBUG :%v", msg)
case logging.LevelInfo:
msg = fmt.Sprintf("INFO :%v", msg)
case logging.LevelWarn:
msg = fmt.Sprintf("WARN :%v", msg)
case logging.LevelError:
msg = fmt.Sprintf("ERROR :%v", msg)
default:
panic(fmt.Sprintf("unknown level %v", lvl))
}
l.Println(append([]any{"msg", msg}, fields...))
})
}

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

Expand All @@ -32,11 +52,11 @@ func ExampleInterceptorLogger() {
// 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...),
logging.UnaryServerInterceptor(InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
grpc.ChainStreamInterceptor(
logging.StreamServerInterceptor(examplelog.InterceptorLogger(logger), opts...),
logging.StreamServerInterceptor(InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
)
Expand All @@ -46,11 +66,11 @@ func ExampleInterceptorLogger() {
_, _ = grpc.Dial(
"some-target",
grpc.WithChainUnaryInterceptor(
logging.UnaryClientInterceptor(examplelog.InterceptorLogger(logger), opts...),
logging.UnaryClientInterceptor(InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
grpc.WithChainStreamInterceptor(
logging.StreamClientInterceptor(examplelog.InterceptorLogger(logger), opts...),
logging.StreamClientInterceptor(InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
)
Expand All @@ -68,7 +88,7 @@ func TestSuite(t *testing.T) {
return
}
buffer := &bytes.Buffer{}
logger := examplelog.InterceptorLogger(log.New(buffer, "", 0))
logger := InterceptorLogger(log.New(buffer, "", 0))

s := &logExampleTestSuite{
InterceptorTestSuite: &testpb.InterceptorTestSuite{
Expand Down
40 changes: 0 additions & 40 deletions interceptors/logging/examples/logr/example.go

This file was deleted.

43 changes: 36 additions & 7 deletions interceptors/logging/examples/logr/example_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// Copyright (c) The go-grpc-middleware Authors.
// Licensed under the Apache License 2.0.

package examplelogr_test
package logr_test

import (
"context"
"fmt"
"runtime"
"strings"
"testing"

examplelogr "github.com/grpc-ecosystem/go-grpc-middleware/interceptors/logging/examples/logr"
"github.com/go-logr/logr"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
"github.com/stretchr/testify/assert"
Expand All @@ -20,6 +21,34 @@ import (
"k8s.io/klog/v2/ktesting"
)

// verbosity https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md#what-method-to-use
const (
debugVerbosity = 4
infoVerbosity = 2
warnVerbosity = 1
errorVerbosity = 0
)

// InterceptorLogger adapts logr logger to interceptor logger.
// This code is simple enough to be copied and not imported.
func InterceptorLogger(l logr.Logger) logging.Logger {
return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) {
l = l.WithValues(fields...)
switch lvl {
case logging.LevelDebug:
l.V(debugVerbosity).Info(msg)
case logging.LevelInfo:
l.V(infoVerbosity).Info(msg)
case logging.LevelWarn:
l.V(warnVerbosity).Info(msg)
case logging.LevelError:
l.V(errorVerbosity).Info(msg)
default:
panic(fmt.Sprintf("unknown level %v", lvl))
}
})
}

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

Expand All @@ -31,11 +60,11 @@ func ExampleInterceptorLogger() {
// 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...),
logging.UnaryServerInterceptor(InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
grpc.ChainStreamInterceptor(
logging.StreamServerInterceptor(examplelogr.InterceptorLogger(logger), opts...),
logging.StreamServerInterceptor(InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
)
Expand All @@ -45,11 +74,11 @@ func ExampleInterceptorLogger() {
_, _ = grpc.Dial(
"some-target",
grpc.WithChainUnaryInterceptor(
logging.UnaryClientInterceptor(examplelogr.InterceptorLogger(logger), opts...),
logging.UnaryClientInterceptor(InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
grpc.WithChainStreamInterceptor(
logging.StreamClientInterceptor(examplelogr.InterceptorLogger(logger), opts...),
logging.StreamClientInterceptor(InterceptorLogger(logger), opts...),
// Add any other interceptor you want.
),
)
Expand All @@ -69,7 +98,7 @@ func TestSuite(t *testing.T) {

buffer := &ktesting.BufferTL{}
cfg := ktesting.NewConfig()
logger := examplelogr.InterceptorLogger(ktesting.NewLogger(buffer, cfg))
logger := InterceptorLogger(ktesting.NewLogger(buffer, cfg))

s := &logrExampleTestSuite{
InterceptorTestSuite: &testpb.InterceptorTestSuite{
Expand Down
Loading

0 comments on commit 2814228

Please sign in to comment.