Skip to content

Commit 7ac0846

Browse files
authored
fix provider examples (#529)
1 parent 782d4b3 commit 7ac0846

File tree

15 files changed

+1786
-185
lines changed

15 files changed

+1786
-185
lines changed

providers/logr/examples_test.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ package logr_test
66
import (
77
"context"
88
"testing"
9+
"time"
910

1011
grpclogr "github.com/grpc-ecosystem/go-grpc-middleware/providers/logr/v2"
1112
"google.golang.org/grpc"
1213
"k8s.io/klog/v2"
1314

14-
middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2"
1515
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
1616
)
1717

@@ -29,10 +29,10 @@ func Example_initializationWithCustomLevels() {
2929
}
3030
// Create a server, make sure we put the tags context before everything else.
3131
_ = grpc.NewServer(
32-
middleware.WithUnaryServerChain(
32+
grpc.ChainUnaryInterceptor(
3333
logging.UnaryServerInterceptor(grpclogr.InterceptorLogger(logger), opts...),
3434
),
35-
middleware.WithStreamServerChain(
35+
grpc.ChainStreamInterceptor(
3636
logging.StreamServerInterceptor(grpclogr.InterceptorLogger(logger), opts...),
3737
),
3838
)
@@ -47,10 +47,10 @@ func Example_initializationWithDurationFieldOverride() {
4747
}
4848
// Create a server, make sure we put the tags context before everything else.
4949
_ = grpc.NewServer(
50-
middleware.WithUnaryServerChain(
50+
grpc.ChainUnaryInterceptor(
5151
logging.UnaryServerInterceptor(grpclogr.InterceptorLogger(logger), opts...),
5252
),
53-
middleware.WithStreamServerChain(
53+
grpc.ChainStreamInterceptor(
5454
logging.StreamServerInterceptor(grpclogr.InterceptorLogger(logger), opts...),
5555
),
5656
)
@@ -61,7 +61,7 @@ func ExampleWithDecider() {
6161
logger := klog.NewKlogr()
6262
// Shared options for the logger, with a custom decider that log everything except successful calls from "/blah.foo.healthcheck/Check" method.
6363
opts := []logging.Option{
64-
logging.WithDecider(func(methodFullName string) logging.Decision {
64+
logging.WithDecider(func(methodFullName string, _ error) logging.Decision {
6565
// will not log gRPC calls if it was a call to healthcheck and no error was raised
6666
if methodFullName == "/blah.foo.healthcheck/Check" {
6767
return logging.NoLogCall
@@ -73,10 +73,10 @@ func ExampleWithDecider() {
7373
}
7474
// Create a server, make sure we put the tags context before everything else.
7575
_ = []grpc.ServerOption{
76-
middleware.WithUnaryServerChain(
76+
grpc.ChainUnaryInterceptor(
7777
logging.UnaryServerInterceptor(grpclogr.InterceptorLogger(logger), opts...),
7878
),
79-
middleware.WithStreamServerChain(
79+
grpc.ChainStreamInterceptor(
8080
logging.StreamServerInterceptor(grpclogr.InterceptorLogger(logger), opts...),
8181
),
8282
}
@@ -86,19 +86,22 @@ func ExampleServerPayloadLoggingDecider() {
8686
// Logger is used, allowing pre-definition of certain fields by the user.
8787
logger := klog.NewKlogr()
8888
// Expect payload from "/blah.foo.healthcheck/Check" call to be logged.
89-
payloadDecider := func(ctx context.Context, fullMethodName string, servingObject interface{}) bool {
90-
return fullMethodName == "/blah.foo.healthcheck/Check"
89+
payloadDecider := func(ctx context.Context, fullMethodName string, servingObject interface{}) logging.PayloadDecision {
90+
if fullMethodName == "/blah.foo.healthcheck/Check" {
91+
return logging.LogPayloadRequestAndResponse
92+
}
93+
return logging.NoPayloadLogging
9194
}
9295

9396
// Create a server, make sure we put the tags context before everything else.
9497
_ = []grpc.ServerOption{
95-
middleware.WithUnaryServerChain(
98+
grpc.ChainUnaryInterceptor(
9699
logging.UnaryServerInterceptor(grpclogr.InterceptorLogger(logger)),
97-
logging.PayloadUnaryServerInterceptor(grpclogr.InterceptorLogger(logger), payloadDecider),
100+
logging.PayloadUnaryServerInterceptor(grpclogr.InterceptorLogger(logger), payloadDecider, time.RFC3339),
98101
),
99-
middleware.WithStreamServerChain(
102+
grpc.ChainStreamInterceptor(
100103
logging.StreamServerInterceptor(grpclogr.InterceptorLogger(logger)),
101-
logging.PayloadStreamServerInterceptor(grpclogr.InterceptorLogger(logger), payloadDecider),
104+
logging.PayloadStreamServerInterceptor(grpclogr.InterceptorLogger(logger), payloadDecider, time.RFC3339),
102105
),
103106
}
104107
}

providers/logr/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ go 1.14
44

55
require (
66
github.com/go-logr/logr v1.2.3
7-
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.0-rc.2.0.20201002093600-73cf2ae9d891
8-
github.com/stretchr/testify v1.5.1
7+
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.0-rc.3
8+
github.com/stretchr/testify v1.7.0
99
google.golang.org/grpc v1.37.0
1010
k8s.io/klog/v2 v2.70.1
1111
)

providers/logr/go.sum

Lines changed: 311 additions & 18 deletions
Large diffs are not rendered by default.

providers/logrus/examples_test.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ package logrus_test
66
import (
77
"context"
88
"testing"
9+
"time"
910

1011
grpclogrus "github.com/grpc-ecosystem/go-grpc-middleware/providers/logrus/v2"
1112
"github.com/sirupsen/logrus"
1213
"google.golang.org/grpc"
1314

14-
middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2"
1515
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
1616
)
1717

@@ -29,10 +29,10 @@ func Example_initializationWithCustomLevels() {
2929
}
3030
// Create a server, make sure we put the tags context before everything else.
3131
_ = grpc.NewServer(
32-
middleware.WithUnaryServerChain(
32+
grpc.ChainUnaryInterceptor(
3333
logging.UnaryServerInterceptor(grpclogrus.InterceptorLogger(logger), opts...),
3434
),
35-
middleware.WithStreamServerChain(
35+
grpc.ChainStreamInterceptor(
3636
logging.StreamServerInterceptor(grpclogrus.InterceptorLogger(logger), opts...),
3737
),
3838
)
@@ -47,10 +47,10 @@ func Example_initializationWithDurationFieldOverride() {
4747
}
4848
// Create a server, make sure we put the tags context before everything else.
4949
_ = grpc.NewServer(
50-
middleware.WithUnaryServerChain(
50+
grpc.ChainUnaryInterceptor(
5151
logging.UnaryServerInterceptor(grpclogrus.InterceptorLogger(logger), opts...),
5252
),
53-
middleware.WithStreamServerChain(
53+
grpc.ChainStreamInterceptor(
5454
logging.StreamServerInterceptor(grpclogrus.InterceptorLogger(logger), opts...),
5555
),
5656
)
@@ -61,7 +61,7 @@ func ExampleWithDecider() {
6161
logger := logrus.New()
6262
// Shared options for the logger, with a custom decider that log everything except successful calls from "/blah.foo.healthcheck/Check" method.
6363
opts := []logging.Option{
64-
logging.WithDecider(func(methodFullName string) logging.Decision {
64+
logging.WithDecider(func(methodFullName string, _ error) logging.Decision {
6565
// will not log gRPC calls if it was a call to healthcheck and no error was raised
6666
if methodFullName == "/blah.foo.healthcheck/Check" {
6767
return logging.NoLogCall
@@ -73,10 +73,10 @@ func ExampleWithDecider() {
7373
}
7474
// Create a server, make sure we put the tags context before everything else.
7575
_ = []grpc.ServerOption{
76-
middleware.WithUnaryServerChain(
76+
grpc.ChainUnaryInterceptor(
7777
logging.UnaryServerInterceptor(grpclogrus.InterceptorLogger(logger), opts...),
7878
),
79-
middleware.WithStreamServerChain(
79+
grpc.ChainStreamInterceptor(
8080
logging.StreamServerInterceptor(grpclogrus.InterceptorLogger(logger), opts...),
8181
),
8282
}
@@ -86,19 +86,22 @@ func ExampleServerPayloadLoggingDecider() {
8686
// Logger is used, allowing pre-definition of certain fields by the user.
8787
logger := logrus.New()
8888
// Expect payload from "/blah.foo.healthcheck/Check" call to be logged.
89-
payloadDecider := func(ctx context.Context, fullMethodName string, servingObject interface{}) bool {
90-
return fullMethodName == "/blah.foo.healthcheck/Check"
89+
payloadDecider := func(ctx context.Context, fullMethodName string, servingObject interface{}) logging.PayloadDecision {
90+
if fullMethodName == "/blah.foo.healthcheck/Check" {
91+
return logging.LogPayloadRequestAndResponse
92+
}
93+
return logging.NoPayloadLogging
9194
}
9295

9396
// Create a server, make sure we put the tags context before everything else.
9497
_ = []grpc.ServerOption{
95-
middleware.WithUnaryServerChain(
98+
grpc.ChainUnaryInterceptor(
9699
logging.UnaryServerInterceptor(grpclogrus.InterceptorLogger(logger)),
97-
logging.PayloadUnaryServerInterceptor(grpclogrus.InterceptorLogger(logger), payloadDecider),
100+
logging.PayloadUnaryServerInterceptor(grpclogrus.InterceptorLogger(logger), payloadDecider, time.RFC3339),
98101
),
99-
middleware.WithStreamServerChain(
102+
grpc.ChainStreamInterceptor(
100103
logging.StreamServerInterceptor(grpclogrus.InterceptorLogger(logger)),
101-
logging.PayloadStreamServerInterceptor(grpclogrus.InterceptorLogger(logger), payloadDecider),
104+
logging.PayloadStreamServerInterceptor(grpclogrus.InterceptorLogger(logger), payloadDecider, time.RFC3339),
102105
),
103106
}
104107
}

providers/logrus/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/grpc-ecosystem/go-grpc-middleware/providers/logrus/v2
33
go 1.14
44

55
require (
6-
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.0-rc.2.0.20201002093600-73cf2ae9d891
6+
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.0-rc.3
77
github.com/sirupsen/logrus v1.5.0
8-
google.golang.org/grpc v1.30.0
8+
google.golang.org/grpc v1.37.0
99
)

0 commit comments

Comments
 (0)