From abcb04c89a27f275e99144f8fb8ba72631d669e9 Mon Sep 17 00:00:00 2001 From: James Munson Date: Tue, 19 Nov 2024 14:42:55 -0700 Subject: [PATCH] fix(logging): handle RPC call to set V1 log level Signed-off-by: James Munson --- pkg/client/instance.go | 8 +++--- pkg/instance/log.go | 59 +++++++++++++++++++++++++++++------------- 2 files changed, 45 insertions(+), 22 deletions(-) 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..e407988ca 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" @@ -19,22 +22,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 == "TRACE" { + spdkLevel = "DEBUG" + } 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 +79,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 +106,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 +126,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 }