Skip to content

Commit

Permalink
Add request_id to logrus
Browse files Browse the repository at this point in the history
  • Loading branch information
dynastymasra committed Feb 14, 2020
1 parent b430346 commit a4583b6
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion grpc/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ func ValidateMetadataKeyStreamInterceptor(keys ...string) grpc.StreamServerInter
}
}

/**
//LogrusUnaryInterceptor gRPC interceptor to log unary request duration status
this function will set field request_id from metadata
*/
func LogrusUnaryInterceptor(logger *logrus.Entry) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
startTime := time.Now()
Expand All @@ -79,6 +82,7 @@ func LogrusUnaryInterceptor(logger *logrus.Entry) grpc.UnaryServerInterceptor {
log := logger.WithFields(logrus.Fields{
"full_method": info.FullMethod,
"request_time": startTime.Format(time.RFC3339),
"request_id": metautils.ExtractIncoming(ctx).Get(cookbook.RequestID),
})

responseTime := time.Now()
Expand All @@ -93,7 +97,10 @@ func LogrusUnaryInterceptor(logger *logrus.Entry) grpc.UnaryServerInterceptor {
}
}

//LogrusStreamInterceptor gRPC interceptor to log stream request duration status
/**
LogrusStreamInterceptor gRPC interceptor to log stream request duration status
this function will set field request_id from metadata
*/
func LogrusStreamInterceptor(logger *logrus.Entry) grpc.StreamServerInterceptor {
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
startTime := time.Now()
Expand All @@ -103,6 +110,7 @@ func LogrusStreamInterceptor(logger *logrus.Entry) grpc.StreamServerInterceptor
log := logger.WithFields(logrus.Fields{
"full_method": info.FullMethod,
"request_time": startTime.Format(time.RFC3339),
"request_id": metautils.ExtractIncoming(stream.Context()).Get(cookbook.RequestID),
})

responseTime := time.Now()
Expand Down

0 comments on commit a4583b6

Please sign in to comment.