...

Source file src/github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/client_interceptors.go

Documentation: github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus

     1  // Copyright 2017 Michal Witkowski. All Rights Reserved.
     2  // See LICENSE for licensing terms.
     3  
     4  package grpc_logrus
     5  
     6  import (
     7  	"context"
     8  	"path"
     9  	"time"
    10  
    11  	"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
    12  	"github.com/sirupsen/logrus"
    13  	"google.golang.org/grpc"
    14  )
    15  
    16  // UnaryClientInterceptor returns a new unary client interceptor that optionally logs the execution of external gRPC calls.
    17  func UnaryClientInterceptor(entry *logrus.Entry, opts ...Option) grpc.UnaryClientInterceptor {
    18  	o := evaluateClientOpt(opts)
    19  	return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
    20  		fields := newClientLoggerFields(ctx, method)
    21  		startTime := time.Now()
    22  		err := invoker(ctx, method, req, reply, cc, opts...)
    23  		newCtx := ctxlogrus.ToContext(ctx, entry.WithFields(fields))
    24  		logFinalClientLine(newCtx, o, startTime, err, "finished client unary call")
    25  		return err
    26  	}
    27  }
    28  
    29  // StreamClientInterceptor returns a new streaming client interceptor that optionally logs the execution of external gRPC calls.
    30  func StreamClientInterceptor(entry *logrus.Entry, opts ...Option) grpc.StreamClientInterceptor {
    31  	o := evaluateClientOpt(opts)
    32  	return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
    33  		fields := newClientLoggerFields(ctx, method)
    34  		startTime := time.Now()
    35  		clientStream, err := streamer(ctx, desc, cc, method, opts...)
    36  		newCtx := ctxlogrus.ToContext(ctx, entry.WithFields(fields))
    37  		logFinalClientLine(newCtx, o, startTime, err, "finished client streaming call")
    38  		return clientStream, err
    39  	}
    40  }
    41  
    42  func logFinalClientLine(ctx context.Context, o *options, startTime time.Time, err error, msg string) {
    43  	code := o.codeFunc(err)
    44  	level := o.levelFunc(code)
    45  	durField, durVal := o.durationFunc(time.Now().Sub(startTime))
    46  	fields := logrus.Fields{
    47  		"grpc.code": code.String(),
    48  		durField:    durVal,
    49  	}
    50  	o.messageFunc(ctx, msg, level, code, err, fields)
    51  }
    52  
    53  func newClientLoggerFields(ctx context.Context, fullMethodString string) logrus.Fields {
    54  	service := path.Dir(fullMethodString)[1:]
    55  	method := path.Base(fullMethodString)
    56  	return logrus.Fields{
    57  		SystemField:    "grpc",
    58  		KindField:      "client",
    59  		"grpc.service": service,
    60  		"grpc.method":  method,
    61  	}
    62  }
    63  

View as plain text