From a99933b5a4cdfde6ff2e626e12401e3f0124fbf2 Mon Sep 17 00:00:00 2001 From: seph Date: Tue, 7 Nov 2023 18:26:40 -0500 Subject: [PATCH] Add some logs to the remote flare consumer (#1441) --- cmd/launcher/launcher.go | 2 +- .../consumers/flareconsumer/flareconsumer.go | 18 ++++++++++-------- .../flareconsumer/flareconsumer_test.go | 5 +++-- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/cmd/launcher/launcher.go b/cmd/launcher/launcher.go index 74dfb8557..1494b337f 100644 --- a/cmd/launcher/launcher.go +++ b/cmd/launcher/launcher.go @@ -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( diff --git a/ee/control/consumers/flareconsumer/flareconsumer.go b/ee/control/consumers/flareconsumer/flareconsumer.go index e765a8286..e7fc93445 100644 --- a/ee/control/consumers/flareconsumer/flareconsumer.go +++ b/ee/control/consumers/flareconsumer/flareconsumer.go @@ -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" @@ -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) } @@ -41,11 +38,10 @@ 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)) }, @@ -53,11 +49,13 @@ func New(logger log.Logger, knapsack types.Knapsack) *FlareConsumer { } func (fc *FlareConsumer) Do(data io.Reader) error { + // slog needs a ctx + 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()), ) @@ -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) diff --git a/ee/control/consumers/flareconsumer/flareconsumer_test.go b/ee/control/consumers/flareconsumer/flareconsumer_test.go index 2eb6e0d26..15f2606fb 100644 --- a/ee/control/consumers/flareconsumer/flareconsumer_test.go +++ b/ee/control/consumers/flareconsumer/flareconsumer_test.go @@ -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" @@ -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