diff --git a/pkg/client/instance.go b/pkg/client/instance.go index c2f862385..ac47cf5ef 100644 --- a/pkg/client/instance.go +++ b/pkg/client/instance.go @@ -472,7 +472,7 @@ func (c *InstanceServiceClient) VersionGet() (*meta.VersionOutput, error) { }, nil } -// LogSetLevel sets the log level of the service. +// LogSetLevel sets the log level. func (c *InstanceServiceClient) LogSetLevel(dataEngine, service, level string) error { client := c.getControllerServiceClient() ctx, cancel := context.WithTimeout(context.Background(), types.GRPCServiceTimeout) @@ -490,7 +490,7 @@ func (c *InstanceServiceClient) LogSetLevel(dataEngine, service, level string) e return err } -// LogSetFlags sets the log flags of the service.x +// LogSetFlags sets the log flags. func (c *InstanceServiceClient) LogSetFlags(dataEngine, service, flags string) error { client := c.getControllerServiceClient() ctx, cancel := context.WithTimeout(context.Background(), types.GRPCServiceTimeout) @@ -508,7 +508,7 @@ func (c *InstanceServiceClient) LogSetFlags(dataEngine, service, flags string) e return err } -// LogGetLevel returns the log level of the service. +// LogGetLevel returns the log level. func (c *InstanceServiceClient) LogGetLevel(dataEngine, service string) (string, error) { client := c.getControllerServiceClient() ctx, cancel := context.WithTimeout(context.Background(), types.GRPCServiceTimeout) @@ -528,7 +528,7 @@ func (c *InstanceServiceClient) LogGetLevel(dataEngine, service string) (string, return resp.Level, nil } -// LogGetFlags returns the log flags of the service. +// LogGetFlags returns the log flags. func (c *InstanceServiceClient) LogGetFlags(dataEngine, service string) (string, error) { client := c.getControllerServiceClient() ctx, cancel := context.WithTimeout(context.Background(), types.GRPCServiceTimeout) diff --git a/pkg/instance/log.go b/pkg/instance/log.go index 19025f647..9d2a79bc5 100644 --- a/pkg/instance/log.go +++ b/pkg/instance/log.go @@ -1,7 +1,10 @@ package instance import ( + "strings" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" "golang.org/x/net/context" spdkclient "github.com/longhorn/longhorn-spdk-engine/pkg/client" @@ -11,6 +14,11 @@ import ( "google.golang.org/protobuf/types/known/emptypb" ) +const ( + NonSPDKLogLevelTrace = "TRACE" + SPDKLogLevelDebug = "DEBUG" +) + func (s *Server) LogSetLevel(ctx context.Context, req *rpc.LogSetLevelRequest) (resp *emptypb.Empty, err error) { ops, ok := s.ops[req.DataEngine] if !ok { @@ -19,22 +27,51 @@ func (s *Server) LogSetLevel(ctx context.Context, req *rpc.LogSetLevelRequest) ( return ops.LogSetLevel(ctx, req) } +func logSetLevel(level string) error { + // Set instance-manager log level. We expect a string such as "debug", "info", or "warn". + newLevel, err := logrus.ParseLevel(level) + if err != nil { + return err + } + + oldLevel := logrus.GetLevel() + if oldLevel != newLevel { + logrus.Warnf("Updating log level from %v to %v", oldLevel, newLevel) + logrus.SetLevel(newLevel) + } + + return nil +} + func (ops V1DataEngineInstanceOps) LogSetLevel(ctx context.Context, req *rpc.LogSetLevelRequest) (resp *emptypb.Empty, err error) { - /* TODO: Implement this */ + if err := logSetLevel(req.Level); err != nil { + return nil, err + } + return &emptypb.Empty{}, nil } func (ops V2DataEngineInstanceOps) LogSetLevel(ctx context.Context, req *rpc.LogSetLevelRequest) (resp *emptypb.Empty, err error) { + if err := logSetLevel(req.Level); err != nil { + return nil, err + } + + // Also set level for spdk_tgt. There is no "trace" level for SPDK. + spdkLevel := strings.ToUpper(req.Level) + if spdkLevel == NonSPDKLogLevelTrace { + spdkLevel = SPDKLogLevelDebug + } c, err := spdkclient.NewSPDKClient(ops.spdkServiceAddress) if err != nil { return nil, grpcstatus.Error(grpccodes.Internal, errors.Wrapf(err, "failed to create SPDK client").Error()) } defer c.Close() - err = c.LogSetLevel(req.Level) + err = c.LogSetLevel(spdkLevel) if err != nil { - return nil, grpcstatus.Error(grpccodes.Internal, errors.Wrapf(err, "failed to set log level").Error()) + return nil, grpcstatus.Error(grpccodes.Internal, errors.Wrapf(err, "failed to set SPDK log level").Error()) } + return &emptypb.Empty{}, nil } @@ -47,7 +84,7 @@ func (s *Server) LogSetFlags(ctx context.Context, req *rpc.LogSetFlagsRequest) ( } func (ops V1DataEngineInstanceOps) LogSetFlags(ctx context.Context, req *rpc.LogSetFlagsRequest) (resp *emptypb.Empty, err error) { - /* TODO: Implement this */ + // There is no V1 implementation. Log flags are not a thing as they are for SPDK. return &emptypb.Empty{}, nil } @@ -74,23 +111,14 @@ func (s *Server) LogGetLevel(ctx context.Context, req *rpc.LogGetLevelRequest) ( } func (ops V1DataEngineInstanceOps) LogGetLevel(ctx context.Context, req *rpc.LogGetLevelRequest) (resp *rpc.LogGetLevelResponse, err error) { - /* TODO: Implement this */ - return &rpc.LogGetLevelResponse{}, nil + return &rpc.LogGetLevelResponse{ + Level: logrus.GetLevel().String(), + }, nil } func (ops V2DataEngineInstanceOps) LogGetLevel(ctx context.Context, req *rpc.LogGetLevelRequest) (resp *rpc.LogGetLevelResponse, err error) { - c, err := spdkclient.NewSPDKClient(ops.spdkServiceAddress) - if err != nil { - return nil, grpcstatus.Error(grpccodes.Internal, errors.Wrapf(err, "failed to create SPDK client").Error()) - } - defer c.Close() - - level, err := c.LogGetLevel() - if err != nil { - return nil, grpcstatus.Error(grpccodes.Internal, errors.Wrapf(err, "failed to get log level").Error()) - } return &rpc.LogGetLevelResponse{ - Level: level, + Level: logrus.GetLevel().String(), }, nil } @@ -103,7 +131,7 @@ func (s *Server) LogGetFlags(ctx context.Context, req *rpc.LogGetFlagsRequest) ( } func (ops V1DataEngineInstanceOps) LogGetFlags(ctx context.Context, req *rpc.LogGetFlagsRequest) (resp *rpc.LogGetFlagsResponse, err error) { - /* TODO: Implement this */ + // No implementation necessary. return &rpc.LogGetFlagsResponse{}, nil }