Implement context based logging

Signed-off-by: Daniel-Pivonka <dpivonka@redhat.com>
This commit is contained in:
Daniel-Pivonka 2019-08-09 13:11:21 -04:00 committed by mergify[bot]
parent 3111e7712a
commit aa74f8c87f
3 changed files with 23 additions and 13 deletions

View File

@ -93,6 +93,7 @@ func (s *nonBlockingGRPCServer) serve(endpoint string, ids csi.IdentityServer, c
opts := []grpc.ServerOption{ opts := []grpc.ServerOption{
grpc_middleware.WithUnaryServerChain( grpc_middleware.WithUnaryServerChain(
contextIDInjector,
logGRPC, logGRPC,
panicHandler, panicHandler,
), ),

View File

@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"runtime/debug" "runtime/debug"
"strings" "strings"
"sync/atomic"
"github.com/container-storage-interface/spec/lib/go/csi" "github.com/container-storage-interface/spec/lib/go/csi"
"github.com/kubernetes-csi/csi-lib-utils/protosanitizer" "github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
@ -105,14 +106,22 @@ func RunControllerandNodePublishServer(endpoint string, d *CSIDriver, cs csi.Con
s.Wait() s.Wait()
} }
var id uint64
func contextIDInjector(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
id = atomic.AddUint64(&id, 1)
ctx = context.WithValue(ctx, "ID", id)
return handler(ctx, req)
}
func logGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { func logGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
klog.V(3).Infof("GRPC call: %s", info.FullMethod) klog.V(3).Infof("ID: %d GRPC call: %s", ctx.Value("ID"), info.FullMethod)
klog.V(5).Infof("GRPC request: %s", protosanitizer.StripSecrets(req)) klog.V(5).Infof("ID: %d GRPC request: %s", ctx.Value("ID"), protosanitizer.StripSecrets(req))
resp, err := handler(ctx, req) resp, err := handler(ctx, req)
if err != nil { if err != nil {
klog.Errorf("GRPC error: %v", err) klog.Errorf("ID: %d GRPC error: %v", ctx.Value("ID"), err)
} else { } else {
klog.V(5).Infof("GRPC response: %s", protosanitizer.StripSecrets(resp)) klog.V(5).Infof("ID: %d GRPC response: %s", ctx.Value("ID"), protosanitizer.StripSecrets(resp))
} }
return resp, err return resp, err
} }

View File

@ -243,7 +243,7 @@ func (ns *NodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
defer targetPathLocker.Unlock(idLk, targetPath) defer targetPathLocker.Unlock(idLk, targetPath)
// Check if that target path exists properly // Check if that target path exists properly
notMnt, err := ns.createTargetMountPath(targetPath, isBlock) notMnt, err := ns.createTargetMountPath(ctx, targetPath, isBlock)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -253,12 +253,12 @@ func (ns *NodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
} }
// Publish Path // Publish Path
err = ns.mountVolume(stagingPath, req) err = ns.mountVolume(ctx, stagingPath, req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
klog.Infof("rbd: successfully mounted stagingPath %s to targetPath %s", stagingPath, targetPath) klog.Infof("ID: %d rbd: successfully mounted stagingPath %s to targetPath %s", ctx.Value("ID"), stagingPath, targetPath)
return &csi.NodePublishVolumeResponse{}, nil return &csi.NodePublishVolumeResponse{}, nil
} }
@ -318,15 +318,15 @@ func (ns *NodeServer) mountVolumeToStagePath(req *csi.NodeStageVolumeRequest, st
return err return err
} }
func (ns *NodeServer) mountVolume(stagingPath string, req *csi.NodePublishVolumeRequest) error { func (ns *NodeServer) mountVolume(ctx context.Context, stagingPath string, req *csi.NodePublishVolumeRequest) error {
// Publish Path // Publish Path
fsType := req.GetVolumeCapability().GetMount().GetFsType() fsType := req.GetVolumeCapability().GetMount().GetFsType()
readOnly := req.GetReadonly() readOnly := req.GetReadonly()
mountFlags := req.GetVolumeCapability().GetMount().GetMountFlags() mountFlags := req.GetVolumeCapability().GetMount().GetMountFlags()
isBlock := req.GetVolumeCapability().GetBlock() != nil isBlock := req.GetVolumeCapability().GetBlock() != nil
targetPath := req.GetTargetPath() targetPath := req.GetTargetPath()
klog.V(4).Infof("target %v\nisBlock %v\nfstype %v\nstagingPath %v\nreadonly %v\nmountflags %v\n", klog.V(4).Infof("ID: %d target %v\nisBlock %v\nfstype %v\nstagingPath %v\nreadonly %v\nmountflags %v\n",
targetPath, isBlock, fsType, stagingPath, readOnly, mountFlags) ctx.Value("ID"), targetPath, isBlock, fsType, stagingPath, readOnly, mountFlags)
mountFlags = append(mountFlags, "bind") mountFlags = append(mountFlags, "bind")
if readOnly { if readOnly {
mountFlags = append(mountFlags, "ro") mountFlags = append(mountFlags, "ro")
@ -338,7 +338,7 @@ func (ns *NodeServer) mountVolume(stagingPath string, req *csi.NodePublishVolume
return nil return nil
} }
func (ns *NodeServer) createTargetMountPath(mountPath string, isBlock bool) (bool, error) { func (ns *NodeServer) createTargetMountPath(ctx context.Context, mountPath string, isBlock bool) (bool, error) {
// Check if that mount path exists properly // Check if that mount path exists properly
notMnt, err := mount.IsNotMountPoint(ns.mounter, mountPath) notMnt, err := mount.IsNotMountPoint(ns.mounter, mountPath)
if err != nil { if err != nil {
@ -347,11 +347,11 @@ func (ns *NodeServer) createTargetMountPath(mountPath string, isBlock bool) (boo
// #nosec // #nosec
pathFile, e := os.OpenFile(mountPath, os.O_CREATE|os.O_RDWR, 0750) pathFile, e := os.OpenFile(mountPath, os.O_CREATE|os.O_RDWR, 0750)
if e != nil { if e != nil {
klog.V(4).Infof("Failed to create mountPath:%s with error: %v", mountPath, err) klog.V(4).Infof("ID: %d Failed to create mountPath:%s with error: %v", ctx.Value("ID"), mountPath, err)
return notMnt, status.Error(codes.Internal, e.Error()) return notMnt, status.Error(codes.Internal, e.Error())
} }
if err = pathFile.Close(); err != nil { if err = pathFile.Close(); err != nil {
klog.V(4).Infof("Failed to close mountPath:%s with error: %v", mountPath, err) klog.V(4).Infof("ID: %d Failed to close mountPath:%s with error: %v", ctx.Value("ID"), mountPath, err)
return notMnt, status.Error(codes.Internal, err.Error()) return notMnt, status.Error(codes.Internal, err.Error())
} }
} else { } else {