Skip to content

Commit

Permalink
Use since instead of tail
Browse files Browse the repository at this point in the history
  • Loading branch information
dkeysil committed Feb 7, 2024
1 parent 92a060d commit 38568b5
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 25 deletions.
4 changes: 2 additions & 2 deletions clients/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,12 +799,12 @@ func (d *dockerClient) ListDigestReferences(ctx context.Context) (imgs []string,
}

// GetContainerLogs gets the container logs.
func (d *dockerClient) GetContainerLogs(ctx context.Context, containerID, tail string, truncate int) (string, error) {
func (d *dockerClient) GetContainerLogs(ctx context.Context, containerID, since string, truncate int) (string, error) {
r, err := d.cli.ContainerLogs(ctx, containerID, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Timestamps: true,
Tail: tail,
Since: since,
})
if err != nil {
return "", err
Expand Down
2 changes: 1 addition & 1 deletion clients/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type DockerClient interface {
EnsureLocalImage(ctx context.Context, name, ref string) error
EnsureLocalImages(ctx context.Context, timeoutPerPull time.Duration, imagePulls []docker.ImagePull) []error
ListDigestReferences(ctx context.Context) ([]string, error)
GetContainerLogs(ctx context.Context, containerID, tail string, truncate int) (string, error)
GetContainerLogs(ctx context.Context, containerID, since string, truncate int) (string, error)
GetContainerFromRemoteAddr(ctx context.Context, hostPort string) (*types.Container, error)
SetImagePullCooldown(threshold int, cooldownDuration time.Duration)
Events(ctx context.Context, since time.Time) (<-chan events.Message, <-chan error)
Expand Down
8 changes: 4 additions & 4 deletions clients/mocks/mock_clients.go

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

8 changes: 3 additions & 5 deletions services/components/lifecycle/bot_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package lifecycle
import (
"context"
"fmt"
"strconv"
"time"

"github.com/ethereum/go-ethereum/accounts/keystore"
Expand All @@ -17,7 +16,7 @@ import (

// BotLogger manages bots logging.
type BotLogger interface {
SendBotLogs(ctx context.Context) error
SendBotLogs(ctx context.Context, snapshotInterval time.Duration) error
}

type botLogger struct {
Expand Down Expand Up @@ -47,12 +46,11 @@ func NewBotLogger(

// adjust these better with auto-upgrade later
const (
defaultAgentLogSendInterval = time.Minute
defaultAgentLogTailLines = 50
defaultAgentLogAvgMaxCharsPerLine = 200
)

func (bl *botLogger) SendBotLogs(ctx context.Context) error {
func (bl *botLogger) SendBotLogs(ctx context.Context, snapshotInterval time.Duration) error {
var (
sendLogs agentlogs.Agents
keepLogs agentlogs.Agents
Expand All @@ -69,7 +67,7 @@ func (bl *botLogger) SendBotLogs(ctx context.Context) error {
}
logs, err := bl.dockerClient.GetContainerLogs(
ctx, container.ID,
strconv.Itoa(defaultAgentLogTailLines),
fmt.Sprintf("%ds", int64(snapshotInterval.Seconds())),
defaultAgentLogAvgMaxCharsPerLine*defaultAgentLogTailLines,
)
if err != nil {
Expand Down
20 changes: 10 additions & 10 deletions services/components/lifecycle/bot_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package lifecycle
import (
"context"
"errors"
"strconv"
"testing"
"time"

"github.com/docker/docker/api/types"
"github.com/ethereum/go-ethereum/accounts/keystore"
Expand Down Expand Up @@ -87,18 +87,18 @@ func (s *BotLoggerSuite) TestSendBotLogs() {
}
s.dockerClient.EXPECT().GetContainerLogs(
ctx, "bot1",
strconv.Itoa(defaultAgentLogTailLines),
"60s",
defaultAgentLogAvgMaxCharsPerLine*defaultAgentLogTailLines,
).Return("some log", nil).Times(1)

s.dockerClient.EXPECT().GetContainerLogs(
ctx, "bot2",
strconv.Itoa(defaultAgentLogTailLines),
"60s",
defaultAgentLogAvgMaxCharsPerLine*defaultAgentLogTailLines,
).Return("some log", nil).Times(1)

s.botClient.EXPECT().LoadBotContainers(ctx).Return(mockContainers, nil)
s.r.NoError(botLogger.SendBotLogs(ctx))
s.r.NoError(botLogger.SendBotLogs(ctx, time.Minute))
}

// should fail if there is an error loading
Expand All @@ -115,7 +115,7 @@ func (s *BotLoggerSuite) TestLoadBotContainersError() {
mockContainers := []types.Container{}

s.botClient.EXPECT().LoadBotContainers(ctx).Return(mockContainers, errors.New("test"))
s.r.EqualError(botLogger.SendBotLogs(ctx), "failed to load the bot containers: test")
s.r.EqualError(botLogger.SendBotLogs(ctx, time.Minute), "failed to load the bot containers: test")
}

// Should not send agent logs if fails
Expand Down Expand Up @@ -154,17 +154,17 @@ func (s *BotLoggerSuite) TestGetContainerLogsError() {

s.dockerClient.EXPECT().GetContainerLogs(
ctx, "bot1",
strconv.Itoa(defaultAgentLogTailLines),
"60s",
defaultAgentLogAvgMaxCharsPerLine*defaultAgentLogTailLines,
).Return("", errors.New("test")).Times(1)

s.dockerClient.EXPECT().GetContainerLogs(
ctx, "bot2",
strconv.Itoa(defaultAgentLogTailLines),
"60s",
defaultAgentLogAvgMaxCharsPerLine*defaultAgentLogTailLines,
).Return("some log", nil).Times(1)

s.r.NoError(botLogger.SendBotLogs(ctx))
s.r.NoError(botLogger.SendBotLogs(ctx, time.Minute))
}

// Fails sending agent logs
Expand Down Expand Up @@ -192,9 +192,9 @@ func (s *BotLoggerSuite) TestFailsToSendLogs() {

s.dockerClient.EXPECT().GetContainerLogs(
ctx, "bot1",
strconv.Itoa(defaultAgentLogTailLines),
"60s",
defaultAgentLogAvgMaxCharsPerLine*defaultAgentLogTailLines,
).Return("some log", nil).Times(1)

s.r.EqualError(botLogger.SendBotLogs(ctx), "failed to send agent logs: test")
s.r.EqualError(botLogger.SendBotLogs(ctx, time.Minute), "failed to send agent logs: test")
}
2 changes: 1 addition & 1 deletion services/supervisor/agent_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func (sup *SupervisorService) syncAgentLogs() {
interval := time.Duration(sup.botLifecycleConfig.Config.AgentLogsConfig.SendIntervalSeconds) * time.Second
ticker := time.NewTicker(interval)
for range ticker.C {
err := sup.botLifecycle.BotLogger.SendBotLogs(sup.ctx)
err := sup.botLifecycle.BotLogger.SendBotLogs(sup.ctx, interval)
sup.lastAgentLogsRequest.Set()
sup.lastAgentLogsRequestError.Set(err)
if err != nil {
Expand Down
8 changes: 6 additions & 2 deletions services/supervisor/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/forta-network/forta-node/config"
"github.com/forta-network/forta-node/services/components/containers"
mock_containers "github.com/forta-network/forta-node/services/components/containers/mocks"
mock_lifecycle "github.com/forta-network/forta-node/services/components/lifecycle/mocks"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -53,8 +54,9 @@ type Suite struct {
globalClient *mock_clients.MockDockerClient
releaseClient *mrelease.MockClient

msgClient *mock_clients.MockMessageClient
botClient *mock_containers.MockBotClient
msgClient *mock_clients.MockMessageClient
botClient *mock_containers.MockBotClient
botLifeCycleManager *mock_lifecycle.MockBotLifecycleManager

supervisor *SupervisorService

Expand Down Expand Up @@ -104,6 +106,7 @@ func (s *Suite) SetupTest() {
s.globalClient = mock_clients.NewMockDockerClient(ctrl)
s.releaseClient = mrelease.NewMockClient(ctrl)
s.botClient = mock_containers.NewMockBotClient(ctrl)
s.botLifeCycleManager = mock_lifecycle.NewMockBotLifecycleManager(ctrl)

s.msgClient = mock_clients.NewMockMessageClient(ctrl)

Expand Down Expand Up @@ -132,6 +135,7 @@ func (s *Suite) SetupTest() {
supervisor.config.Config.AgentLogsConfig.SendIntervalSeconds = 1
supervisor.botLifecycleConfig.Config = supervisor.config.Config
supervisor.botLifecycle.BotClient = s.botClient
supervisor.botLifecycle.BotManager = s.botLifeCycleManager
s.supervisor = supervisor
}

Expand Down

0 comments on commit 38568b5

Please sign in to comment.