Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some logs to the remote flare consumer #1441

Merged
merged 4 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func runLauncher(ctx context.Context, cancel func(), slogger, systemSlogger *mul
actionsQueue.RegisterActor(acceleratecontrolconsumer.AccelerateControlSubsystem, acceleratecontrolconsumer.New(k))

// register flare consumer
actionsQueue.RegisterActor(flareconsumer.FlareSubsystem, flareconsumer.New(logger, k))
actionsQueue.RegisterActor(flareconsumer.FlareSubsystem, flareconsumer.New(k))

// create notification consumer
notificationConsumer, err := notificationconsumer.NewNotifyConsumer(
Expand Down
18 changes: 10 additions & 8 deletions ee/control/consumers/flareconsumer/flareconsumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"

"github.com/kolide/launcher/pkg/agent/types"
"github.com/kolide/launcher/pkg/debug/checkups"
"github.com/kolide/launcher/pkg/debug/shipper"
Expand All @@ -26,7 +24,6 @@ type FlareConsumer struct {
lastFlareTime time.Time
flarer flarer
knapsack types.Knapsack
logger log.Logger
// newFlareStream is assigned to a field so it can be mocked in tests
newFlareStream func(note, uploadRequestURL string) (io.WriteCloser, error)
}
Expand All @@ -41,23 +38,24 @@ func (f *FlareRunner) RunFlare(ctx context.Context, k types.Knapsack, flareStrea
return checkups.RunFlare(ctx, k, flareStream, checkups.InSituEnvironment)
}

func New(logger log.Logger, knapsack types.Knapsack) *FlareConsumer {
func New(knapsack types.Knapsack) *FlareConsumer {
return &FlareConsumer{
flarer: &FlareRunner{},
knapsack: knapsack,
logger: logger,
newFlareStream: func(note, uploadRequestURL string) (io.WriteCloser, error) {
return shipper.New(knapsack, shipper.WithNote(note), shipper.WithUploadRequestURL(uploadRequestURL))
},
}
}

func (fc *FlareConsumer) Do(data io.Reader) error {
// slog needs a ctx
RebeccaMahany marked this conversation as resolved.
Show resolved Hide resolved
ctx := context.TODO()

timeSinceLastFlare := time.Since(fc.lastFlareTime)

if timeSinceLastFlare < minFlareInterval {
level.Info(fc.logger).Log(
"msg", "skipping flare, run too recently",
fc.knapsack.Slogger().Log(ctx, slog.LevelInfo, "skipping flare, run too recently",
"min_flare_interval", fmt.Sprintf("%v minutes", minFlareInterval.Minutes()),
"time_since_last_flare", fmt.Sprintf("%v minutes", timeSinceLastFlare.Minutes()),
)
Expand All @@ -81,6 +79,10 @@ func (fc *FlareConsumer) Do(data io.Reader) error {
return fmt.Errorf("failed to decode key-value json: %w", err)
}

fc.knapsack.Slogger().Log(ctx, slog.LevelInfo, "received remote flare request",
"note", flareData.Note,
)

flareStream, err := fc.newFlareStream(flareData.Note, flareData.UploadRequestURL)
if err != nil {
return fmt.Errorf("failed to create flare stream: %w", err)
Expand Down
5 changes: 3 additions & 2 deletions ee/control/consumers/flareconsumer/flareconsumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package flareconsumer
import (
"bytes"
"io"
"log/slog"
"testing"

"github.com/go-kit/kit/log"
"github.com/kolide/launcher/ee/control/consumers/flareconsumer/mocks"
knapsackMock "github.com/kolide/launcher/pkg/agent/types/mocks"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -36,7 +36,8 @@ func TestFlareConsumer(t *testing.T) {
t.Parallel()

mockSack := knapsackMock.NewKnapsack(t)
f := New(log.NewNopLogger(), mockSack)
mockSack.On("Slogger").Return(slog.New(slog.NewJSONHandler(io.Discard, nil))).Maybe()
f := New(mockSack)
f.flarer = tt.flarer(t)
f.newFlareStream = func(note, uploadRequestURL string) (io.WriteCloser, error) {
// whatever, it implements write closer
Expand Down
Loading