Skip to content

Commit

Permalink
fix(logging): handle RPC call to set V1 log level
Browse files Browse the repository at this point in the history
Signed-off-by: James Munson <[email protected]>
  • Loading branch information
james-munson authored and derekbit committed Nov 25, 2024
1 parent a95d431 commit cca5967
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 22 deletions.
8 changes: 4 additions & 4 deletions pkg/client/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
64 changes: 46 additions & 18 deletions pkg/instance/log.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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 {
Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down

0 comments on commit cca5967

Please sign in to comment.