Skip to content

Commit

Permalink
Make Debug GRPC call generic
Browse files Browse the repository at this point in the history
Use oneof in GetDebug{Request, Response} and SetDubug{Request, Response}
in order to make them easily extensible in the future.

Signed-off-by: Anastasios Papagiannis <[email protected]>
  • Loading branch information
tpapagian committed Sep 4, 2024
1 parent 8e4feb4 commit 5ebad69
Show file tree
Hide file tree
Showing 9 changed files with 673 additions and 426 deletions.
341 changes: 206 additions & 135 deletions api/v1/tetragon/sensors.pb.go

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions api/v1/tetragon/sensors.proto
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,22 @@ message GetDebugRequest{
}
message GetDebugResponse{
ConfigFlag flag = 1;
LogLevel level = 2;
oneof arg {
LogLevel level = 2;
}
}

message SetDebugRequest{
ConfigFlag flag = 1;
LogLevel level = 2;
oneof arg {
LogLevel level = 2;
}
}
message SetDebugResponse{
ConfigFlag flag = 1;
LogLevel level = 2;
oneof arg {
LogLevel level = 2;
}
}

service FineGuidanceSensors {
Expand Down
6 changes: 4 additions & 2 deletions cmd/tetra/loglevel/loglevel.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ func New() *cobra.Command {
defer c.Close()

currentLogLevel, err := c.Client.SetDebug(c.Ctx, &tetragon.SetDebugRequest{
Flag: tetragon.ConfigFlag_CONFIG_FLAG_LOG_LEVEL,
Level: tetragon.LogLevel(levelParsed),
Flag: tetragon.ConfigFlag_CONFIG_FLAG_LOG_LEVEL,
Arg: &tetragon.SetDebugRequest_Level{
Level: tetragon.LogLevel(levelParsed),
},
})
if err != nil {
return fmt.Errorf("failed to set log level: %w", err)
Expand Down

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,10 @@ func (s *Server) GetDebug(_ context.Context, req *tetragon.GetDebugRequest) (*te
case tetragon.ConfigFlag_CONFIG_FLAG_LOG_LEVEL:
logger.GetLogger().Debugf("Client requested current log level: %s", logger.GetLogLevel().String())
return &tetragon.GetDebugResponse{
Flag: tetragon.ConfigFlag_CONFIG_FLAG_LOG_LEVEL,
Level: tetragon.LogLevel(logger.GetLogLevel()),
Flag: tetragon.ConfigFlag_CONFIG_FLAG_LOG_LEVEL,
Arg: &tetragon.GetDebugResponse_Level{
Level: tetragon.LogLevel(logger.GetLogLevel()),
},
}, nil
default:
logger.GetLogger().WithField("request", req).Warnf("Client requested unknown config flag %d", req.GetFlag())
Expand All @@ -407,8 +409,10 @@ func (s *Server) SetDebug(_ context.Context, req *tetragon.SetDebugRequest) (*te
logger.SetLogLevel(changedLogLevel)
logger.GetLogger().WithField("request", req).Warnf("Log level changed from %s to %s", currentLogLevel, changedLogLevel.String())
return &tetragon.SetDebugResponse{
Flag: tetragon.ConfigFlag_CONFIG_FLAG_LOG_LEVEL,
Level: tetragon.LogLevel(changedLogLevel),
Flag: tetragon.ConfigFlag_CONFIG_FLAG_LOG_LEVEL,
Arg: &tetragon.SetDebugResponse_Level{
Level: tetragon.LogLevel(changedLogLevel),
},
}, nil
default:
logger.GetLogger().WithField("request", req).Warnf("Client requested change of unknown config flag %d", req.GetFlag())
Expand Down
22 changes: 16 additions & 6 deletions pkg/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func TestGetDebug(t *testing.T) {
t.Errorf("Expected flag in response to be %d, but got %d", tetragon.ConfigFlag_CONFIG_FLAG_LOG_LEVEL, resp.Flag)
}
expectedLogLevel := logger.GetLogLevel()
if resp.Level != tetragon.LogLevel(expectedLogLevel) {
t.Errorf("Expected log level in response to be %s, but got %s", expectedLogLevel.String(), resp.Level.String())
if resp.GetLevel() != tetragon.LogLevel(expectedLogLevel) {
t.Errorf("Expected log level in response to be %s, but got %s", expectedLogLevel.String(), resp.GetLevel().String())
}

// Test unknown flag
Expand All @@ -45,7 +45,12 @@ func TestGetDebug(t *testing.T) {

func TestSetDebug(t *testing.T) {
srv := &Server{}
req := &tetragon.SetDebugRequest{Flag: tetragon.ConfigFlag_CONFIG_FLAG_LOG_LEVEL, Level: tetragon.LogLevel(logrus.InfoLevel)}
req := &tetragon.SetDebugRequest{
Flag: tetragon.ConfigFlag_CONFIG_FLAG_LOG_LEVEL,
Arg: &tetragon.SetDebugRequest_Level{
Level: tetragon.LogLevel(logrus.InfoLevel),
},
}
resp, err := srv.SetDebug(context.Background(), req)
if err != nil {
t.Errorf("Expected SetDebug to succeed, got error %v", err)
Expand All @@ -54,8 +59,8 @@ func TestSetDebug(t *testing.T) {
t.Errorf("Expected flag in response to be %d, but got %d", tetragon.ConfigFlag_CONFIG_FLAG_LOG_LEVEL, resp.Flag)
}
expectedLogLevel := logrus.InfoLevel
if resp.Level != tetragon.LogLevel(expectedLogLevel) {
t.Errorf("Expected log level in response to be %s, but got %s", expectedLogLevel.String(), resp.Level.String())
if resp.GetLevel() != tetragon.LogLevel(expectedLogLevel) {
t.Errorf("Expected log level in response to be %s, but got %s", expectedLogLevel.String(), resp.GetLevel().String())
}

// Test unknown flag
Expand All @@ -70,7 +75,12 @@ func TestSetDebug(t *testing.T) {

// Test changing log level
prevLogLevel := logger.GetLogLevel()
req = &tetragon.SetDebugRequest{Flag: tetragon.ConfigFlag_CONFIG_FLAG_LOG_LEVEL, Level: tetragon.LogLevel(logrus.DebugLevel)}
req = &tetragon.SetDebugRequest{
Flag: tetragon.ConfigFlag_CONFIG_FLAG_LOG_LEVEL,
Arg: &tetragon.SetDebugRequest_Level{
Level: tetragon.LogLevel(logrus.DebugLevel),
},
}
_, err = srv.SetDebug(context.Background(), req)
if err != nil {
t.Errorf("Expected SetDebug to succeed, got error %v", err)
Expand Down
Loading

0 comments on commit 5ebad69

Please sign in to comment.