From 4206746589572a56254d4f550edb1f4e8dd3d9c5 Mon Sep 17 00:00:00 2001 From: dkeysil Date: Wed, 7 Feb 2024 14:22:00 +0100 Subject: [PATCH] Use tail to define number of logs but preserve slice cutting with some limit --- clients/docker/client.go | 15 ++++++++++++--- services/components/lifecycle/bot_logger.go | 5 ++--- services/components/lifecycle/bot_logger_test.go | 10 +++++----- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/clients/docker/client.go b/clients/docker/client.go index f7c0042b..5761da3e 100644 --- a/clients/docker/client.go +++ b/clients/docker/client.go @@ -9,6 +9,7 @@ import ( "fmt" "io" "path" + "strconv" "strings" "time" @@ -798,13 +799,18 @@ func (d *dockerClient) ListDigestReferences(ctx context.Context) (imgs []string, return } +const ( + defaultAgentLogAvgMaxCharsPerLine = 200 +) + // GetContainerLogs gets the container logs. -func (d *dockerClient) GetContainerLogs(ctx context.Context, containerID, since string, truncate int) (string, error) { +func (d *dockerClient) GetContainerLogs(ctx context.Context, containerID, since string, tail int) (string, error) { r, err := d.cli.ContainerLogs(ctx, containerID, types.ContainerLogsOptions{ ShowStdout: true, ShowStderr: true, Timestamps: true, Since: since, + Tail: strconv.Itoa(tail), }) if err != nil { return "", err @@ -813,9 +819,12 @@ func (d *dockerClient) GetContainerLogs(ctx context.Context, containerID, since if err != nil { return "", err } - if truncate >= 0 && len(b) > truncate { - b = b[:truncate] + + // limit the log size + if tail >= 0 && len(b) > defaultAgentLogAvgMaxCharsPerLine*tail { + b = b[:defaultAgentLogAvgMaxCharsPerLine*tail] } + // remove strange 8-byte prefix in each line lines := strings.Split(string(b), "\n") for i, line := range lines { diff --git a/services/components/lifecycle/bot_logger.go b/services/components/lifecycle/bot_logger.go index 1ced64d9..94119267 100644 --- a/services/components/lifecycle/bot_logger.go +++ b/services/components/lifecycle/bot_logger.go @@ -50,8 +50,7 @@ func NewBotLogger( // adjust these better with auto-upgrade later const ( - defaultAgentLogTailLines = 50 - defaultAgentLogAvgMaxCharsPerLine = 200 + defaultAgentLogTailLines = 600 ) func (bl *botLogger) SendBotLogs(ctx context.Context, snapshotInterval time.Duration) error { @@ -72,7 +71,7 @@ func (bl *botLogger) SendBotLogs(ctx context.Context, snapshotInterval time.Dura logs, err := bl.dockerClient.GetContainerLogs( ctx, container.ID, fmt.Sprintf("%ds", int64(snapshotInterval.Seconds())), - defaultAgentLogAvgMaxCharsPerLine*defaultAgentLogTailLines, + defaultAgentLogTailLines, ) if err != nil { log.WithError(err).Warn("failed to get agent container logs") diff --git a/services/components/lifecycle/bot_logger_test.go b/services/components/lifecycle/bot_logger_test.go index 13b0acbb..c28634b0 100644 --- a/services/components/lifecycle/bot_logger_test.go +++ b/services/components/lifecycle/bot_logger_test.go @@ -96,13 +96,13 @@ func (s *BotLoggerSuite) TestSendBotLogs() { s.dockerClient.EXPECT().GetContainerLogs( ctx, "bot1", "60s", - defaultAgentLogAvgMaxCharsPerLine*defaultAgentLogTailLines, + defaultAgentLogTailLines, ).Return("some log", nil).Times(1) s.dockerClient.EXPECT().GetContainerLogs( ctx, "bot2", "60s", - defaultAgentLogAvgMaxCharsPerLine*defaultAgentLogTailLines, + defaultAgentLogTailLines, ).Return("some log", nil).Times(1) s.agentRegistry.EXPECT().GetConfigByID("bot1ID").Return(&config.AgentConfig{ChainID: 1}, nil).Times(1) @@ -166,13 +166,13 @@ func (s *BotLoggerSuite) TestGetContainerLogsError() { s.dockerClient.EXPECT().GetContainerLogs( ctx, "bot1", "60s", - defaultAgentLogAvgMaxCharsPerLine*defaultAgentLogTailLines, + defaultAgentLogTailLines, ).Return("", errors.New("test")).Times(1) s.dockerClient.EXPECT().GetContainerLogs( ctx, "bot2", "60s", - defaultAgentLogAvgMaxCharsPerLine*defaultAgentLogTailLines, + defaultAgentLogTailLines, ).Return("some log", nil).Times(1) s.agentRegistry.EXPECT().GetConfigByID("bot2ID").Return(&config.AgentConfig{ChainID: 2}, nil).Times(1) @@ -206,7 +206,7 @@ func (s *BotLoggerSuite) TestFailsToSendLogs() { s.dockerClient.EXPECT().GetContainerLogs( ctx, "bot1", "60s", - defaultAgentLogAvgMaxCharsPerLine*defaultAgentLogTailLines, + defaultAgentLogTailLines, ).Return("some log", nil).Times(1) s.agentRegistry.EXPECT().GetConfigByID("bot1ID").Return(&config.AgentConfig{ChainID: 1}, nil).Times(1)