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 zap logger example and add tests #577

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion interceptors/logging/examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,31 @@ require (
github.com/go-kit/log v0.2.1
github.com/go-logr/logr v1.2.4
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.0
github.com/phuslu/log v1.0.83
github.com/rs/zerolog v1.29.0
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.0
go.uber.org/zap v1.24.0
golang.org/x/exp v0.0.0-20230321023759-10a507213a29
google.golang.org/grpc v1.53.0
k8s.io/klog/v2 v2.90.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/grpc-ecosystem/go-grpc-middleware/v2 => ../../../
7 changes: 5 additions & 2 deletions interceptors/logging/examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZb
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/phuslu/log v1.0.83 h1:zfqz5tfFPLF8w0jEscpDxE2aFg1Y1kcbORDPliKdIbU=
github.com/phuslu/log v1.0.83/go.mod h1:yAZh4pv6KxAsJDmJIcVSMxkMiUF7mJbpFN3vROkf0dc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -32,9 +30,12 @@ github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6us
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
Expand Down Expand Up @@ -62,8 +63,10 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
k8s.io/klog/v2 v2.90.1 h1:m4bYOKall2MmOiRaR1J+We67Do7vm9KiQVlT96lnHUw=
k8s.io/klog/v2 v2.90.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
54 changes: 54 additions & 0 deletions interceptors/logging/examples/kit/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@
package kit_test

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

"github.com/go-kit/log"
"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"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
)

Expand Down Expand Up @@ -68,3 +76,49 @@ func ExampleInterceptorLogger() {
)
// Output:
}

type kitExampleTestSuite struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid "suite" patterns, especially for single method test. Why not single unit test?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the test suite because that already has a server and client I can easily use for a test. I try to find another way to do that

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you have any idea how to that this without test suite I will try to implement that.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid "suite" patterns, especially for single method test. Why not single unit test?

Hello, pardon my curiosity but I wonder why should we avoid suite pattern since it gave us a server and client inside?
It gave us expandable way to add more tests in the future.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test suite types in Go like this are uncommon and unnecessary. If you need a server in a test, you can just create one. If you want to isolate the logic, create a function. Test suites take OOP beyond what is usually necessary in Go. Just write test functions, skip the suite.

*testpb.InterceptorTestSuite
logBuffer *bytes.Buffer
}

func TestSuite(t *testing.T) {
if strings.HasPrefix(runtime.Version(), "go1.7") {
t.Skipf("Skipping due to json.RawMessage incompatibility with go1.7")
return
}

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

s := &kitExampleTestSuite{
InterceptorTestSuite: &testpb.InterceptorTestSuite{
TestService: &testpb.TestPingService{},
},
logBuffer: buffer,
}

s.InterceptorTestSuite.ServerOpts = []grpc.ServerOption{
grpc.StreamInterceptor(logging.StreamServerInterceptor(logger)),
grpc.UnaryInterceptor(logging.UnaryServerInterceptor(logger)),
}

suite.Run(t, s)
}

func (s *kitExampleTestSuite) TestPing() {
ctx := context.Background()
_, err := s.Client.Ping(ctx, testpb.GoodPing)
assert.NoError(s.T(), err, "there must be not be an on a successful call")
logStr := s.logBuffer.String()
require.Contains(s.T(), logStr, "level=info")
require.Contains(s.T(), logStr, "msg=\"started call\"")
require.Contains(s.T(), logStr, "protocol=grpc")
require.Contains(s.T(), logStr, "grpc.component=server")
require.Contains(s.T(), logStr, "grpc.service=testing.testpb.v1.TestService")
require.Contains(s.T(), logStr, "grpc.method=Ping")
require.Contains(s.T(), logStr, "grpc.method_type=unary")
require.Contains(s.T(), logStr, "start_time=")
require.Contains(s.T(), logStr, "grpc.time_ms=")

}
53 changes: 53 additions & 0 deletions interceptors/logging/examples/log/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@
package log_test

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

"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"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
)

Expand Down Expand Up @@ -68,3 +76,48 @@ func ExampleInterceptorLogger() {
)
// Output:
}

type logExampleTestSuite struct {
*testpb.InterceptorTestSuite
logBuffer *bytes.Buffer
}

func TestSuite(t *testing.T) {
if strings.HasPrefix(runtime.Version(), "go1.7") {
t.Skipf("Skipping due to json.RawMessage incompatibility with go1.7")
return
}
buffer := &bytes.Buffer{}
logger := InterceptorLogger(log.New(buffer, "", 0))

s := &logExampleTestSuite{
InterceptorTestSuite: &testpb.InterceptorTestSuite{
TestService: &testpb.TestPingService{},
},
logBuffer: buffer,
}

s.InterceptorTestSuite.ServerOpts = []grpc.ServerOption{
grpc.StreamInterceptor(logging.StreamServerInterceptor(logger)),
grpc.UnaryInterceptor(logging.UnaryServerInterceptor(logger)),
}

suite.Run(t, s)
}

func (s *logExampleTestSuite) TestPing() {
ctx := context.Background()
_, err := s.Client.Ping(ctx, testpb.GoodPing)
assert.NoError(s.T(), err, "there must be not be an on a successful call")
logStr := s.logBuffer.String()
require.Contains(s.T(), logStr, "msg INFO")
require.Contains(s.T(), logStr, ":started call")
require.Contains(s.T(), logStr, "protocol grpc")
require.Contains(s.T(), logStr, "grpc.component server")
require.Contains(s.T(), logStr, "grpc.service testing.testpb.v1.TestService")
require.Contains(s.T(), logStr, "grpc.method Ping")
require.Contains(s.T(), logStr, "grpc.method_type unary")
require.Contains(s.T(), logStr, "start_time ")
require.Contains(s.T(), logStr, "grpc.time_ms")

}
53 changes: 53 additions & 0 deletions interceptors/logging/examples/logr/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ package logr_test
import (
"context"
"fmt"
"runtime"
"strings"
"testing"

"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"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
"k8s.io/klog/v2"
"k8s.io/klog/v2/ktesting"
)

// verbosity https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md#what-method-to-use
Expand Down Expand Up @@ -76,3 +84,48 @@ func ExampleInterceptorLogger() {
)
// Output:
}

type logrExampleTestSuite struct {
*testpb.InterceptorTestSuite
logBuffer *ktesting.BufferTL
}

func TestSuite(t *testing.T) {
if strings.HasPrefix(runtime.Version(), "go1.7") {
t.Skipf("Skipping due to json.RawMessage incompatibility with go1.7")
return
}

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

s := &logrExampleTestSuite{
InterceptorTestSuite: &testpb.InterceptorTestSuite{
TestService: &testpb.TestPingService{},
},
logBuffer: buffer,
}

s.InterceptorTestSuite.ServerOpts = []grpc.ServerOption{
grpc.StreamInterceptor(logging.StreamServerInterceptor(logger)),
grpc.UnaryInterceptor(logging.UnaryServerInterceptor(logger)),
}

suite.Run(t, s)
}

func (s *logrExampleTestSuite) TestPing() {
ctx := context.Background()
_, err := s.Client.Ping(ctx, testpb.GoodPing)
assert.NoError(s.T(), err, "there must be not be an on a successful call")
logStr := s.logBuffer.String()
require.Contains(s.T(), logStr, "started call")
require.Contains(s.T(), logStr, "protocol=\"grpc\"")
require.Contains(s.T(), logStr, "grpc.component=\"server\"")
require.Contains(s.T(), logStr, "grpc.service=\"testing.testpb.v1.TestService\"")
require.Contains(s.T(), logStr, "grpc.method=\"Ping\"")
require.Contains(s.T(), logStr, "grpc.method_type=\"unary\"")
require.Contains(s.T(), logStr, "start_time=")
require.Contains(s.T(), logStr, "grpc.time_ms=")
}
62 changes: 61 additions & 1 deletion interceptors/logging/examples/logrus/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@
package logrus_test

import (
"bytes"
"context"
"encoding/json"
"fmt"
"runtime"
"strings"
"testing"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
)

Expand All @@ -18,7 +27,7 @@ func InterceptorLogger(l logrus.FieldLogger) logging.Logger {
return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) {
f := make(map[string]any, len(fields)/2)
i := logging.Fields(fields).Iterator()
if i.Next() {
for i.Next() {
k, v := i.At()
f[k] = v
}
Expand Down Expand Up @@ -74,3 +83,54 @@ func ExampleInterceptorLogger() {
)
// Output:
}

type logrusExampleTestSuite struct {
*testpb.InterceptorTestSuite
logBuffer *bytes.Buffer
}

func TestSuite(t *testing.T) {
if strings.HasPrefix(runtime.Version(), "go1.7") {
t.Skipf("Skipping due to json.RawMessage incompatibility with go1.7")
return
}

buffer := &bytes.Buffer{}
testLogger := logrus.New()
testLogger.Out = buffer
testLogger.SetFormatter(&logrus.JSONFormatter{})
logger := InterceptorLogger(testLogger)

s := &logrusExampleTestSuite{
InterceptorTestSuite: &testpb.InterceptorTestSuite{
TestService: &testpb.TestPingService{},
},
logBuffer: buffer,
}

s.InterceptorTestSuite.ServerOpts = []grpc.ServerOption{
grpc.StreamInterceptor(logging.StreamServerInterceptor(logger, logging.WithLogOnEvents(logging.StartCall))),
grpc.UnaryInterceptor(logging.UnaryServerInterceptor(logger, logging.WithLogOnEvents(logging.StartCall))),
}

suite.Run(t, s)
}

func (s *logrusExampleTestSuite) TestPing() {
ctx := context.Background()
_, err := s.Client.Ping(ctx, testpb.GoodPing)
assert.NoError(s.T(), err, "there must be not be an on a successful call")
var logMap map[string]interface{}
err = json.Unmarshal(s.logBuffer.Bytes(), &logMap)
require.NoError(s.T(), err)

require.Equal(s.T(), "started call", logMap["msg"])
require.Equal(s.T(), "grpc", logMap["protocol"])
require.Equal(s.T(), "server", logMap["grpc.component"])
require.Equal(s.T(), "testing.testpb.v1.TestService", logMap["grpc.service"])
require.Equal(s.T(), "Ping", logMap["grpc.method"])
require.Equal(s.T(), "unary", logMap["grpc.method_type"])
require.Contains(s.T(), logMap["peer.address"], "127.0.0.1")
require.NotEmpty(s.T(), logMap["grpc.start_time"])
require.NotEmpty(s.T(), logMap["grpc.time_ms"])
}
Loading