Skip to content

Commit 2814228

Browse files
committed
make example interceptors private
1 parent 70d7244 commit 2814228

14 files changed

+201
-284
lines changed

interceptors/logging/examples/kit/example.go

-33
This file was deleted.

interceptors/logging/examples/kit/example_test.go

+28-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
// Copyright (c) The go-grpc-middleware Authors.
22
// Licensed under the Apache License 2.0.
33

4-
package examplekit_test
4+
package kit_test
55

66
import (
77
"bytes"
88
"context"
9+
"fmt"
910
"runtime"
1011
"strings"
1112
"testing"
1213

1314
"github.com/go-kit/log"
14-
examplekit "github.com/grpc-ecosystem/go-grpc-middleware/interceptors/logging/examples/kit"
15+
"github.com/go-kit/log/level"
1516
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
1617
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
1718
"github.com/stretchr/testify/assert"
@@ -20,6 +21,26 @@ import (
2021
"google.golang.org/grpc"
2122
)
2223

24+
// InterceptorLogger adapts go-kit logger to interceptor logger.
25+
// This code is simple enough to be copied and not imported.
26+
func InterceptorLogger(l log.Logger) logging.Logger {
27+
return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) {
28+
largs := append([]any{"msg", msg}, fields...)
29+
switch lvl {
30+
case logging.LevelDebug:
31+
_ = level.Debug(l).Log(largs...)
32+
case logging.LevelInfo:
33+
_ = level.Info(l).Log(largs...)
34+
case logging.LevelWarn:
35+
_ = level.Warn(l).Log(largs...)
36+
case logging.LevelError:
37+
_ = level.Error(l).Log(largs...)
38+
default:
39+
panic(fmt.Sprintf("unknown level %v", lvl))
40+
}
41+
})
42+
}
43+
2344
func ExampleInterceptorLogger() {
2445
logger := log.NewNopLogger()
2546

@@ -31,11 +52,11 @@ func ExampleInterceptorLogger() {
3152
// You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished.
3253
_ = grpc.NewServer(
3354
grpc.ChainUnaryInterceptor(
34-
logging.UnaryServerInterceptor(examplekit.InterceptorLogger(logger), opts...),
55+
logging.UnaryServerInterceptor(InterceptorLogger(logger), opts...),
3556
// Add any other interceptor you want.
3657
),
3758
grpc.ChainStreamInterceptor(
38-
logging.StreamServerInterceptor(examplekit.InterceptorLogger(logger), opts...),
59+
logging.StreamServerInterceptor(InterceptorLogger(logger), opts...),
3960
// Add any other interceptor you want.
4061
),
4162
)
@@ -45,11 +66,11 @@ func ExampleInterceptorLogger() {
4566
_, _ = grpc.Dial(
4667
"some-target",
4768
grpc.WithChainUnaryInterceptor(
48-
logging.UnaryClientInterceptor(examplekit.InterceptorLogger(logger), opts...),
69+
logging.UnaryClientInterceptor(InterceptorLogger(logger), opts...),
4970
// Add any other interceptor you want.
5071
),
5172
grpc.WithChainStreamInterceptor(
52-
logging.StreamClientInterceptor(examplekit.InterceptorLogger(logger), opts...),
73+
logging.StreamClientInterceptor(InterceptorLogger(logger), opts...),
5374
// Add any other interceptor you want.
5475
),
5576
)
@@ -68,7 +89,7 @@ func TestSuite(t *testing.T) {
6889
}
6990

7091
buffer := &bytes.Buffer{}
71-
logger := examplekit.InterceptorLogger(log.NewLogfmtLogger(buffer))
92+
logger := InterceptorLogger(log.NewLogfmtLogger(buffer))
7293

7394
s := &kitExampleTestSuite{
7495
InterceptorTestSuite: &testpb.InterceptorTestSuite{

interceptors/logging/examples/log/example.go

-32
This file was deleted.

interceptors/logging/examples/log/example_test.go

+27-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// Copyright (c) The go-grpc-middleware Authors.
22
// Licensed under the Apache License 2.0.
33

4-
package examplelog_test
4+
package log_test
55

66
import (
77
"bytes"
88
"context"
9+
"fmt"
910
"log"
1011
"os"
1112
"runtime"
1213
"strings"
1314
"testing"
1415

15-
examplelog "github.com/grpc-ecosystem/go-grpc-middleware/interceptors/logging/examples/log"
1616
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
1717
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
1818
"github.com/stretchr/testify/assert"
@@ -21,6 +21,26 @@ import (
2121
"google.golang.org/grpc"
2222
)
2323

24+
// InterceptorLogger adapts standard Go logger to interceptor logger.
25+
// This code is simple enough to be copied and not imported.
26+
func InterceptorLogger(l *log.Logger) logging.Logger {
27+
return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) {
28+
switch lvl {
29+
case logging.LevelDebug:
30+
msg = fmt.Sprintf("DEBUG :%v", msg)
31+
case logging.LevelInfo:
32+
msg = fmt.Sprintf("INFO :%v", msg)
33+
case logging.LevelWarn:
34+
msg = fmt.Sprintf("WARN :%v", msg)
35+
case logging.LevelError:
36+
msg = fmt.Sprintf("ERROR :%v", msg)
37+
default:
38+
panic(fmt.Sprintf("unknown level %v", lvl))
39+
}
40+
l.Println(append([]any{"msg", msg}, fields...))
41+
})
42+
}
43+
2444
func ExampleInterceptorLogger() {
2545
logger := log.New(os.Stderr, "", log.Ldate|log.Ltime|log.Lshortfile)
2646

@@ -32,11 +52,11 @@ func ExampleInterceptorLogger() {
3252
// You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished.
3353
_ = grpc.NewServer(
3454
grpc.ChainUnaryInterceptor(
35-
logging.UnaryServerInterceptor(examplelog.InterceptorLogger(logger), opts...),
55+
logging.UnaryServerInterceptor(InterceptorLogger(logger), opts...),
3656
// Add any other interceptor you want.
3757
),
3858
grpc.ChainStreamInterceptor(
39-
logging.StreamServerInterceptor(examplelog.InterceptorLogger(logger), opts...),
59+
logging.StreamServerInterceptor(InterceptorLogger(logger), opts...),
4060
// Add any other interceptor you want.
4161
),
4262
)
@@ -46,11 +66,11 @@ func ExampleInterceptorLogger() {
4666
_, _ = grpc.Dial(
4767
"some-target",
4868
grpc.WithChainUnaryInterceptor(
49-
logging.UnaryClientInterceptor(examplelog.InterceptorLogger(logger), opts...),
69+
logging.UnaryClientInterceptor(InterceptorLogger(logger), opts...),
5070
// Add any other interceptor you want.
5171
),
5272
grpc.WithChainStreamInterceptor(
53-
logging.StreamClientInterceptor(examplelog.InterceptorLogger(logger), opts...),
73+
logging.StreamClientInterceptor(InterceptorLogger(logger), opts...),
5474
// Add any other interceptor you want.
5575
),
5676
)
@@ -68,7 +88,7 @@ func TestSuite(t *testing.T) {
6888
return
6989
}
7090
buffer := &bytes.Buffer{}
71-
logger := examplelog.InterceptorLogger(log.New(buffer, "", 0))
91+
logger := InterceptorLogger(log.New(buffer, "", 0))
7292

7393
s := &logExampleTestSuite{
7494
InterceptorTestSuite: &testpb.InterceptorTestSuite{

interceptors/logging/examples/logr/example.go

-40
This file was deleted.

interceptors/logging/examples/logr/example_test.go

+36-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
// Copyright (c) The go-grpc-middleware Authors.
22
// Licensed under the Apache License 2.0.
33

4-
package examplelogr_test
4+
package logr_test
55

66
import (
77
"context"
8+
"fmt"
89
"runtime"
910
"strings"
1011
"testing"
1112

12-
examplelogr "github.com/grpc-ecosystem/go-grpc-middleware/interceptors/logging/examples/logr"
13+
"github.com/go-logr/logr"
1314
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
1415
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
1516
"github.com/stretchr/testify/assert"
@@ -20,6 +21,34 @@ import (
2021
"k8s.io/klog/v2/ktesting"
2122
)
2223

24+
// verbosity https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md#what-method-to-use
25+
const (
26+
debugVerbosity = 4
27+
infoVerbosity = 2
28+
warnVerbosity = 1
29+
errorVerbosity = 0
30+
)
31+
32+
// InterceptorLogger adapts logr logger to interceptor logger.
33+
// This code is simple enough to be copied and not imported.
34+
func InterceptorLogger(l logr.Logger) logging.Logger {
35+
return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) {
36+
l = l.WithValues(fields...)
37+
switch lvl {
38+
case logging.LevelDebug:
39+
l.V(debugVerbosity).Info(msg)
40+
case logging.LevelInfo:
41+
l.V(infoVerbosity).Info(msg)
42+
case logging.LevelWarn:
43+
l.V(warnVerbosity).Info(msg)
44+
case logging.LevelError:
45+
l.V(errorVerbosity).Info(msg)
46+
default:
47+
panic(fmt.Sprintf("unknown level %v", lvl))
48+
}
49+
})
50+
}
51+
2352
func ExampleInterceptorLogger() {
2453
logger := klog.NewKlogr()
2554

@@ -31,11 +60,11 @@ func ExampleInterceptorLogger() {
3160
// You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished.
3261
_ = grpc.NewServer(
3362
grpc.ChainUnaryInterceptor(
34-
logging.UnaryServerInterceptor(examplelogr.InterceptorLogger(logger), opts...),
63+
logging.UnaryServerInterceptor(InterceptorLogger(logger), opts...),
3564
// Add any other interceptor you want.
3665
),
3766
grpc.ChainStreamInterceptor(
38-
logging.StreamServerInterceptor(examplelogr.InterceptorLogger(logger), opts...),
67+
logging.StreamServerInterceptor(InterceptorLogger(logger), opts...),
3968
// Add any other interceptor you want.
4069
),
4170
)
@@ -45,11 +74,11 @@ func ExampleInterceptorLogger() {
4574
_, _ = grpc.Dial(
4675
"some-target",
4776
grpc.WithChainUnaryInterceptor(
48-
logging.UnaryClientInterceptor(examplelogr.InterceptorLogger(logger), opts...),
77+
logging.UnaryClientInterceptor(InterceptorLogger(logger), opts...),
4978
// Add any other interceptor you want.
5079
),
5180
grpc.WithChainStreamInterceptor(
52-
logging.StreamClientInterceptor(examplelogr.InterceptorLogger(logger), opts...),
81+
logging.StreamClientInterceptor(InterceptorLogger(logger), opts...),
5382
// Add any other interceptor you want.
5483
),
5584
)
@@ -69,7 +98,7 @@ func TestSuite(t *testing.T) {
6998

7099
buffer := &ktesting.BufferTL{}
71100
cfg := ktesting.NewConfig()
72-
logger := examplelogr.InterceptorLogger(ktesting.NewLogger(buffer, cfg))
101+
logger := InterceptorLogger(ktesting.NewLogger(buffer, cfg))
73102

74103
s := &logrExampleTestSuite{
75104
InterceptorTestSuite: &testpb.InterceptorTestSuite{

0 commit comments

Comments
 (0)