github.com/cs3org/reva/v2@v2.27.7/internal/grpc/interceptors/log/log.go (about) 1 // Copyright 2018-2021 CERN 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package log 20 21 import ( 22 "context" 23 "time" 24 25 "github.com/cs3org/reva/v2/pkg/appctx" 26 ctxpkg "github.com/cs3org/reva/v2/pkg/ctx" 27 "github.com/rs/zerolog" 28 "google.golang.org/grpc" 29 "google.golang.org/grpc/codes" 30 "google.golang.org/grpc/peer" 31 "google.golang.org/grpc/status" 32 ) 33 34 // NewUnary returns a new unary interceptor 35 // that logs grpc calls. 36 func NewUnary() grpc.UnaryServerInterceptor { 37 interceptor := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { 38 start := time.Now() 39 res, err := handler(ctx, req) 40 code := status.Code(err) 41 end := time.Now() 42 diff := end.Sub(start).Nanoseconds() 43 var fromAddress string 44 if p, ok := peer.FromContext(ctx); ok { 45 fromAddress = p.Addr.Network() + "://" + p.Addr.String() 46 } 47 userAgent, ok := ctxpkg.ContextGetUserAgentString(ctx) 48 if !ok { 49 userAgent = "" 50 } 51 52 log := appctx.GetLogger(ctx) 53 var event *zerolog.Event 54 var msg string 55 if code != codes.OK { 56 event = log.Error() 57 msg = err.Error() 58 } else { 59 event = log.Debug() 60 msg = "unary" 61 } 62 63 event.Str("user-agent", userAgent). 64 Str("from", fromAddress). 65 Str("uri", info.FullMethod). 66 Str("start", start.Format("02/Jan/2006:15:04:05 -0700")). 67 Str("end", end.Format("02/Jan/2006:15:04:05 -0700")).Int("time_ns", int(diff)). 68 Str("code", code.String()). 69 Msg(msg) 70 71 return res, err 72 } 73 return interceptor 74 } 75 76 // NewStream returns a new server stream interceptor 77 // that adds trace information to the request. 78 func NewStream() grpc.StreamServerInterceptor { 79 interceptor := func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { 80 ctx := ss.Context() 81 start := time.Now() 82 err := handler(srv, ss) 83 end := time.Now() 84 code := status.Code(err) 85 diff := end.Sub(start).Nanoseconds() 86 var fromAddress string 87 if p, ok := peer.FromContext(ss.Context()); ok { 88 fromAddress = p.Addr.Network() + "://" + p.Addr.String() 89 } 90 userAgent, ok := ctxpkg.ContextGetUserAgentString(ctx) 91 if !ok { 92 userAgent = "" 93 } 94 95 log := appctx.GetLogger(ss.Context()) 96 var event *zerolog.Event 97 var msg string 98 if code != codes.OK { 99 event = log.Error() 100 msg = err.Error() 101 } else { 102 event = log.Debug() 103 msg = "stream" 104 } 105 106 event.Str("user-agent", userAgent). 107 Str("from", fromAddress). 108 Str("uri", info.FullMethod). 109 Str("start", start.Format("02/Jan/2006:15:04:05 -0700")). 110 Str("end", end.Format("02/Jan/2006:15:04:05 -0700")).Int("time_ns", int(diff)). 111 Str("code", code.String()). 112 Msg(msg) 113 114 return err 115 } 116 return interceptor 117 }