...
1 package kit_test
2
3 import (
4 "bytes"
5 "encoding/json"
6 "io"
7 "testing"
8
9 "github.com/grpc-ecosystem/go-grpc-middleware/logging/kit/ctxkit"
10
11 "context"
12
13 "github.com/go-kit/log"
14 "github.com/go-kit/log/level"
15 grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
16 grpc_testing "github.com/grpc-ecosystem/go-grpc-middleware/testing"
17 pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/testing/testproto"
18 )
19
20 var (
21 goodPing = &pb_testproto.PingRequest{Value: "something", SleepTimeMs: 9999}
22 )
23
24 type loggingPingService struct {
25 pb_testproto.TestServiceServer
26 }
27
28 func (s *loggingPingService) Ping(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.PingResponse, error) {
29 grpc_ctxtags.Extract(ctx).Set("custom_tags.string", "something").Set("custom_tags.int", 1337)
30 ctxkit.AddFields(ctx, []interface{}{"custom_field", "custom_value"}...)
31 level.Info(ctxkit.Extract(ctx)).Log("msg", "some ping")
32 return s.TestServiceServer.Ping(ctx, ping)
33 }
34
35 func (s *loggingPingService) PingError(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.Empty, error) {
36 return s.TestServiceServer.PingError(ctx, ping)
37 }
38
39 func (s *loggingPingService) PingList(ping *pb_testproto.PingRequest, stream pb_testproto.TestService_PingListServer) error {
40 grpc_ctxtags.Extract(stream.Context()).Set("custom_tags.string", "something").Set("custom_tags.int", 1337)
41 ctxkit.AddFields(stream.Context(), []interface{}{"custom_field", "custom_value"}...)
42 level.Info(ctxkit.Extract(stream.Context())).Log("msg", "some pinglist")
43 return s.TestServiceServer.PingList(ping, stream)
44 }
45
46 func (s *loggingPingService) PingEmpty(ctx context.Context, empty *pb_testproto.Empty) (*pb_testproto.PingResponse, error) {
47 return s.TestServiceServer.PingEmpty(ctx, empty)
48 }
49
50 type kitBaseSuite struct {
51 *grpc_testing.InterceptorTestSuite
52 mutexBuffer *grpc_testing.MutexReadWriter
53 buffer *bytes.Buffer
54 logger log.Logger
55 timestampFormat string
56 }
57
58 func newKitBaseSuite(t *testing.T) *kitBaseSuite {
59 b := &bytes.Buffer{}
60 muB := grpc_testing.NewMutexReadWriter(b)
61 logger := log.NewJSONLogger(log.NewSyncWriter(muB))
62 return &kitBaseSuite{
63 logger: logger,
64 buffer: b,
65 mutexBuffer: muB,
66 InterceptorTestSuite: &grpc_testing.InterceptorTestSuite{
67 TestService: &loggingPingService{&grpc_testing.TestPingService{T: t}},
68 },
69 }
70 }
71
72 func (s *kitBaseSuite) SetupTest() {
73 s.mutexBuffer.Lock()
74 s.buffer.Reset()
75 s.mutexBuffer.Unlock()
76 }
77
78 func (s *kitBaseSuite) getOutputJSONs() []map[string]interface{} {
79 ret := make([]map[string]interface{}, 0)
80 dec := json.NewDecoder(s.mutexBuffer)
81
82 for {
83 var val map[string]interface{}
84 err := dec.Decode(&val)
85 if err == io.EOF {
86 break
87 }
88 if err != nil {
89 s.T().Fatalf("failed decoding output from go-kit JSON: %v", err)
90 }
91
92 ret = append(ret, val)
93 }
94
95 return ret
96 }
97
View as plain text