github.com/weaveworks/common@v0.0.0-20230728070032-dd9e68f319d5/middleware/grpc_logging_test.go (about)

     1  package middleware
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"errors"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/go-kit/log"
    11  	"github.com/go-kit/log/level"
    12  	"github.com/stretchr/testify/require"
    13  	"google.golang.org/grpc"
    14  
    15  	"github.com/weaveworks/common/logging"
    16  )
    17  
    18  func BenchmarkGRPCServerLog_UnaryServerInterceptor_NoError(b *testing.B) {
    19  	logger := logging.GoKit(level.NewFilter(log.NewNopLogger(), level.AllowError()))
    20  	l := GRPCServerLog{Log: logger, WithRequest: false, DisableRequestSuccessLog: true}
    21  	ctx := context.Background()
    22  	info := &grpc.UnaryServerInfo{FullMethod: "Test"}
    23  
    24  	handler := func(ctx context.Context, req interface{}) (interface{}, error) {
    25  		return nil, nil
    26  	}
    27  
    28  	b.ResetTimer()
    29  	b.ReportAllocs()
    30  
    31  	for n := 0; n < b.N; n++ {
    32  		_, _ = l.UnaryServerInterceptor(ctx, nil, info, handler)
    33  	}
    34  }
    35  
    36  type doNotLogError struct{ Err error }
    37  
    38  func (i doNotLogError) Error() string                                     { return i.Err.Error() }
    39  func (i doNotLogError) Unwrap() error                                     { return i.Err }
    40  func (i doNotLogError) ShouldLog(_ context.Context, _ time.Duration) bool { return false }
    41  
    42  func TestGrpcLogging(t *testing.T) {
    43  	ctx := context.Background()
    44  	info := &grpc.UnaryServerInfo{FullMethod: "Test"}
    45  	for _, tc := range []struct {
    46  		err         error
    47  		logContains []string
    48  	}{{
    49  		err:         context.Canceled,
    50  		logContains: []string{"level=debug", "context canceled"},
    51  	}, {
    52  		err:         errors.New("yolo"),
    53  		logContains: []string{"level=warn", "err=yolo"},
    54  	}, {
    55  		err:         nil,
    56  		logContains: []string{"level=debug", "method=Test"},
    57  	}, {
    58  		err:         doNotLogError{Err: errors.New("yolo")},
    59  		logContains: nil,
    60  	}} {
    61  		t.Run("", func(t *testing.T) {
    62  			buf := bytes.NewBuffer(nil)
    63  			logger := logging.GoKit(log.NewLogfmtLogger(buf))
    64  			l := GRPCServerLog{Log: logger, WithRequest: true, DisableRequestSuccessLog: false}
    65  
    66  			handler := func(ctx context.Context, req interface{}) (interface{}, error) {
    67  				return nil, tc.err
    68  			}
    69  
    70  			_, err := l.UnaryServerInterceptor(ctx, nil, info, handler)
    71  			require.ErrorIs(t, tc.err, err)
    72  
    73  			if len(tc.logContains) == 0 {
    74  				require.Empty(t, buf)
    75  			}
    76  			for _, content := range tc.logContains {
    77  				require.Contains(t, buf.String(), content)
    78  			}
    79  		})
    80  	}
    81  }