|
1 | 1 | // Copyright (c) The go-grpc-middleware Authors. |
2 | 2 | // Licensed under the Apache License 2.0. |
3 | 3 |
|
4 | | -package zap_test |
| 4 | +package examplezap_test |
5 | 5 |
|
6 | 6 | import ( |
7 | 7 | "context" |
8 | | - "fmt" |
| 8 | + examplezap "github.com/grpc-ecosystem/go-grpc-middleware/interceptors/logging/examples/zap" |
| 9 | + "runtime" |
| 10 | + "strings" |
| 11 | + "testing" |
9 | 12 |
|
10 | 13 | "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" |
| 14 | + "github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | + "github.com/stretchr/testify/suite" |
11 | 18 | "go.uber.org/zap" |
| 19 | + "go.uber.org/zap/zaptest/observer" |
12 | 20 | "google.golang.org/grpc" |
13 | 21 | ) |
14 | 22 |
|
15 | | -// InterceptorLogger adapts zap logger to interceptor logger. |
16 | | -// This code is simple enough to be copied and not imported. |
17 | | -func InterceptorLogger(l *zap.Logger) logging.Logger { |
18 | | - return logging.LoggerFunc(func(ctx context.Context, lvl logging.Level, msg string, fields ...any) { |
19 | | - f := make([]zap.Field, 0, len(fields)/2) |
20 | | - for i := 0; i < len(fields); i += 2 { |
21 | | - i := logging.Fields(fields).Iterator() |
22 | | - if i.Next() { |
23 | | - k, v := i.At() |
24 | | - f = append(f, zap.Any(k, v)) |
25 | | - } |
26 | | - } |
27 | | - l = l.WithOptions(zap.AddCallerSkip(1)).With(f...) |
28 | | - |
29 | | - switch lvl { |
30 | | - case logging.LevelDebug: |
31 | | - l.Debug(msg) |
32 | | - case logging.LevelInfo: |
33 | | - l.Info(msg) |
34 | | - case logging.LevelWarn: |
35 | | - l.Warn(msg) |
36 | | - case logging.LevelError: |
37 | | - l.Error(msg) |
38 | | - default: |
39 | | - panic(fmt.Sprintf("unknown level %v", lvl)) |
40 | | - } |
41 | | - }) |
| 23 | +type zapExampleTestSuite struct { |
| 24 | + *testpb.InterceptorTestSuite |
| 25 | + observedLogs *observer.ObservedLogs |
42 | 26 | } |
43 | 27 |
|
44 | | -func ExampleInterceptorLogger() { |
45 | | - logger := zap.NewExample() |
| 28 | +func TestSuite(t *testing.T) { |
| 29 | + if strings.HasPrefix(runtime.Version(), "go1.7") { |
| 30 | + t.Skipf("Skipping due to json.RawMessage incompatibility with go1.7") |
| 31 | + return |
| 32 | + } |
| 33 | + observedZapCore, observedLogs := observer.New(zap.DebugLevel) |
| 34 | + logger := examplezap.InterceptorLogger(zap.New(observedZapCore)) |
| 35 | + s := &zapExampleTestSuite{ |
| 36 | + InterceptorTestSuite: &testpb.InterceptorTestSuite{ |
| 37 | + TestService: &testpb.TestPingService{}, |
| 38 | + }, |
| 39 | + observedLogs: observedLogs, |
| 40 | + } |
46 | 41 |
|
47 | | - opts := []logging.Option{ |
48 | | - logging.WithLogOnEvents(logging.StartCall, logging.FinishCall), |
49 | | - // Add any other option (check functions starting with logging.With). |
| 42 | + s.InterceptorTestSuite.ServerOpts = []grpc.ServerOption{ |
| 43 | + grpc.StreamInterceptor(logging.StreamServerInterceptor(logger)), |
| 44 | + grpc.UnaryInterceptor(logging.UnaryServerInterceptor(logger)), |
50 | 45 | } |
51 | 46 |
|
52 | | - // You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished. |
53 | | - _ = grpc.NewServer( |
54 | | - grpc.ChainUnaryInterceptor( |
55 | | - logging.UnaryServerInterceptor(InterceptorLogger(logger), opts...), |
56 | | - // Add any other interceptor you want. |
57 | | - ), |
58 | | - grpc.ChainStreamInterceptor( |
59 | | - logging.StreamServerInterceptor(InterceptorLogger(logger), opts...), |
60 | | - // Add any other interceptor you want. |
61 | | - ), |
62 | | - ) |
63 | | - // ...user server. |
| 47 | + suite.Run(t, s) |
| 48 | +} |
| 49 | + |
| 50 | +func (s *zapExampleTestSuite) TestPing() { |
| 51 | + ctx := context.Background() |
| 52 | + _, err := s.Client.Ping(ctx, testpb.GoodPing) |
| 53 | + assert.NoError(s.T(), err, "there must be not be an on a successful call") |
| 54 | + require.Equal(s.T(), 2, s.observedLogs.Len()) |
| 55 | + line := s.observedLogs.All()[0] |
| 56 | + |
| 57 | + contextMap := line.ContextMap() |
| 58 | + require.Equal(s.T(), zap.InfoLevel, line.Level) |
| 59 | + require.Equal(s.T(), "started call", line.Entry.Message) |
| 60 | + |
| 61 | + require.Equal(s.T(), "Ping", contextMap["grpc.method"]) |
| 62 | + require.Equal(s.T(), "grpc", contextMap["protocol"]) |
| 63 | + require.Equal(s.T(), "server", contextMap["grpc.component"]) |
64 | 64 |
|
65 | | - // Similarly you can create client that will log for the unary and stream client started or finished calls. |
66 | | - _, _ = grpc.Dial( |
67 | | - "some-target", |
68 | | - grpc.WithChainUnaryInterceptor( |
69 | | - logging.UnaryClientInterceptor(InterceptorLogger(logger), opts...), |
70 | | - // Add any other interceptor you want. |
71 | | - ), |
72 | | - grpc.WithChainStreamInterceptor( |
73 | | - logging.StreamClientInterceptor(InterceptorLogger(logger), opts...), |
74 | | - // Add any other interceptor you want. |
75 | | - ), |
76 | | - ) |
77 | | - // Output: |
| 65 | + require.Contains(s.T(), contextMap["peer.address"], "127.0.0.1") |
| 66 | + require.NotEmpty(s.T(), contextMap["grpc.start_time"]) |
| 67 | + require.NotEmpty(s.T(), contextMap["grpc.time_ms"]) |
78 | 68 | } |
0 commit comments