Skip to content

Commit

Permalink
Use tail to define number of logs but preserve slice cutting with som…
Browse files Browse the repository at this point in the history
…e limit
  • Loading branch information
dkeysil committed Feb 7, 2024
1 parent 514e0eb commit 13fcbdd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
15 changes: 12 additions & 3 deletions clients/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"path"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions services/components/lifecycle/bot_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand Down
10 changes: 5 additions & 5 deletions services/components/lifecycle/bot_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions services/components/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package registry

import (
"errors"
"sync"
"testing"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -20,6 +21,7 @@ func TestLoadAssignedBots(t *testing.T) {
botReg := &botRegistry{
scannerAddress: common.HexToAddress(utils.ZeroAddress),
registryStore: regStore,
mu: &sync.RWMutex{},
}

cfgs := []config.AgentConfig{{}}
Expand Down

0 comments on commit 13fcbdd

Please sign in to comment.