Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix provider examples #529

Merged
merged 1 commit into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions providers/logr/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ package logr_test
import (
"context"
"testing"
"time"

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

middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
)

Expand All @@ -29,10 +29,10 @@ func Example_initializationWithCustomLevels() {
}
// Create a server, make sure we put the tags context before everything else.
_ = grpc.NewServer(
middleware.WithUnaryServerChain(
grpc.ChainUnaryInterceptor(
logging.UnaryServerInterceptor(grpclogr.InterceptorLogger(logger), opts...),
),
middleware.WithStreamServerChain(
grpc.ChainStreamInterceptor(
logging.StreamServerInterceptor(grpclogr.InterceptorLogger(logger), opts...),
),
)
Expand All @@ -47,10 +47,10 @@ func Example_initializationWithDurationFieldOverride() {
}
// Create a server, make sure we put the tags context before everything else.
_ = grpc.NewServer(
middleware.WithUnaryServerChain(
grpc.ChainUnaryInterceptor(
logging.UnaryServerInterceptor(grpclogr.InterceptorLogger(logger), opts...),
),
middleware.WithStreamServerChain(
grpc.ChainStreamInterceptor(
logging.StreamServerInterceptor(grpclogr.InterceptorLogger(logger), opts...),
),
)
Expand All @@ -61,7 +61,7 @@ func ExampleWithDecider() {
logger := klog.NewKlogr()
// Shared options for the logger, with a custom decider that log everything except successful calls from "/blah.foo.healthcheck/Check" method.
opts := []logging.Option{
logging.WithDecider(func(methodFullName string) logging.Decision {
logging.WithDecider(func(methodFullName string, _ error) logging.Decision {
// will not log gRPC calls if it was a call to healthcheck and no error was raised
if methodFullName == "/blah.foo.healthcheck/Check" {
return logging.NoLogCall
Expand All @@ -73,10 +73,10 @@ func ExampleWithDecider() {
}
// Create a server, make sure we put the tags context before everything else.
_ = []grpc.ServerOption{
middleware.WithUnaryServerChain(
grpc.ChainUnaryInterceptor(
logging.UnaryServerInterceptor(grpclogr.InterceptorLogger(logger), opts...),
),
middleware.WithStreamServerChain(
grpc.ChainStreamInterceptor(
logging.StreamServerInterceptor(grpclogr.InterceptorLogger(logger), opts...),
),
}
Expand All @@ -86,19 +86,22 @@ func ExampleServerPayloadLoggingDecider() {
// Logger is used, allowing pre-definition of certain fields by the user.
logger := klog.NewKlogr()
// Expect payload from "/blah.foo.healthcheck/Check" call to be logged.
payloadDecider := func(ctx context.Context, fullMethodName string, servingObject interface{}) bool {
return fullMethodName == "/blah.foo.healthcheck/Check"
payloadDecider := func(ctx context.Context, fullMethodName string, servingObject interface{}) logging.PayloadDecision {
if fullMethodName == "/blah.foo.healthcheck/Check" {
return logging.LogPayloadRequestAndResponse
}
return logging.NoPayloadLogging
}

// Create a server, make sure we put the tags context before everything else.
_ = []grpc.ServerOption{
middleware.WithUnaryServerChain(
grpc.ChainUnaryInterceptor(
logging.UnaryServerInterceptor(grpclogr.InterceptorLogger(logger)),
logging.PayloadUnaryServerInterceptor(grpclogr.InterceptorLogger(logger), payloadDecider),
logging.PayloadUnaryServerInterceptor(grpclogr.InterceptorLogger(logger), payloadDecider, time.RFC3339),
),
middleware.WithStreamServerChain(
grpc.ChainStreamInterceptor(
logging.StreamServerInterceptor(grpclogr.InterceptorLogger(logger)),
logging.PayloadStreamServerInterceptor(grpclogr.InterceptorLogger(logger), payloadDecider),
logging.PayloadStreamServerInterceptor(grpclogr.InterceptorLogger(logger), payloadDecider, time.RFC3339),
),
}
}
Expand Down
4 changes: 2 additions & 2 deletions providers/logr/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.14

require (
github.com/go-logr/logr v1.2.3
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.0-rc.2.0.20201002093600-73cf2ae9d891
github.com/stretchr/testify v1.5.1
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.0-rc.3
github.com/stretchr/testify v1.7.0
google.golang.org/grpc v1.37.0
k8s.io/klog/v2 v2.70.1
)
329 changes: 311 additions & 18 deletions providers/logr/go.sum

Large diffs are not rendered by default.

31 changes: 17 additions & 14 deletions providers/logrus/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ package logrus_test
import (
"context"
"testing"
"time"

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

middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
)

Expand All @@ -29,10 +29,10 @@ func Example_initializationWithCustomLevels() {
}
// Create a server, make sure we put the tags context before everything else.
_ = grpc.NewServer(
middleware.WithUnaryServerChain(
grpc.ChainUnaryInterceptor(
logging.UnaryServerInterceptor(grpclogrus.InterceptorLogger(logger), opts...),
),
middleware.WithStreamServerChain(
grpc.ChainStreamInterceptor(
logging.StreamServerInterceptor(grpclogrus.InterceptorLogger(logger), opts...),
),
)
Expand All @@ -47,10 +47,10 @@ func Example_initializationWithDurationFieldOverride() {
}
// Create a server, make sure we put the tags context before everything else.
_ = grpc.NewServer(
middleware.WithUnaryServerChain(
grpc.ChainUnaryInterceptor(
logging.UnaryServerInterceptor(grpclogrus.InterceptorLogger(logger), opts...),
),
middleware.WithStreamServerChain(
grpc.ChainStreamInterceptor(
logging.StreamServerInterceptor(grpclogrus.InterceptorLogger(logger), opts...),
),
)
Expand All @@ -61,7 +61,7 @@ func ExampleWithDecider() {
logger := logrus.New()
// Shared options for the logger, with a custom decider that log everything except successful calls from "/blah.foo.healthcheck/Check" method.
opts := []logging.Option{
logging.WithDecider(func(methodFullName string) logging.Decision {
logging.WithDecider(func(methodFullName string, _ error) logging.Decision {
// will not log gRPC calls if it was a call to healthcheck and no error was raised
if methodFullName == "/blah.foo.healthcheck/Check" {
return logging.NoLogCall
Expand All @@ -73,10 +73,10 @@ func ExampleWithDecider() {
}
// Create a server, make sure we put the tags context before everything else.
_ = []grpc.ServerOption{
middleware.WithUnaryServerChain(
grpc.ChainUnaryInterceptor(
logging.UnaryServerInterceptor(grpclogrus.InterceptorLogger(logger), opts...),
),
middleware.WithStreamServerChain(
grpc.ChainStreamInterceptor(
logging.StreamServerInterceptor(grpclogrus.InterceptorLogger(logger), opts...),
),
}
Expand All @@ -86,19 +86,22 @@ func ExampleServerPayloadLoggingDecider() {
// Logger is used, allowing pre-definition of certain fields by the user.
logger := logrus.New()
// Expect payload from "/blah.foo.healthcheck/Check" call to be logged.
payloadDecider := func(ctx context.Context, fullMethodName string, servingObject interface{}) bool {
return fullMethodName == "/blah.foo.healthcheck/Check"
payloadDecider := func(ctx context.Context, fullMethodName string, servingObject interface{}) logging.PayloadDecision {
if fullMethodName == "/blah.foo.healthcheck/Check" {
return logging.LogPayloadRequestAndResponse
}
return logging.NoPayloadLogging
}

// Create a server, make sure we put the tags context before everything else.
_ = []grpc.ServerOption{
middleware.WithUnaryServerChain(
grpc.ChainUnaryInterceptor(
logging.UnaryServerInterceptor(grpclogrus.InterceptorLogger(logger)),
logging.PayloadUnaryServerInterceptor(grpclogrus.InterceptorLogger(logger), payloadDecider),
logging.PayloadUnaryServerInterceptor(grpclogrus.InterceptorLogger(logger), payloadDecider, time.RFC3339),
),
middleware.WithStreamServerChain(
grpc.ChainStreamInterceptor(
logging.StreamServerInterceptor(grpclogrus.InterceptorLogger(logger)),
logging.PayloadStreamServerInterceptor(grpclogrus.InterceptorLogger(logger), payloadDecider),
logging.PayloadStreamServerInterceptor(grpclogrus.InterceptorLogger(logger), payloadDecider, time.RFC3339),
),
}
}
Expand Down
4 changes: 2 additions & 2 deletions providers/logrus/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/grpc-ecosystem/go-grpc-middleware/providers/logrus/v2
go 1.14

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