From e87ecd09fd55c63c965e10f919b8493add08078a Mon Sep 17 00:00:00 2001 From: seph Date: Tue, 7 Nov 2023 09:11:30 -0500 Subject: [PATCH 1/3] Add some logs to flare --- cmd/launcher/launcher.go | 2 +- .../consumers/flareconsumer/flareconsumer.go | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 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..0f8e555d0 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, "Recieved 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) From 0358aad036ac05322904fd6d5794b869524082b2 Mon Sep 17 00:00:00 2001 From: seph Date: Tue, 7 Nov 2023 14:20:41 -0500 Subject: [PATCH 2/3] lint and ci --- ee/control/consumers/flareconsumer/flareconsumer.go | 2 +- ee/control/consumers/flareconsumer/flareconsumer_test.go | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/ee/control/consumers/flareconsumer/flareconsumer.go b/ee/control/consumers/flareconsumer/flareconsumer.go index 0f8e555d0..e7fc93445 100644 --- a/ee/control/consumers/flareconsumer/flareconsumer.go +++ b/ee/control/consumers/flareconsumer/flareconsumer.go @@ -79,7 +79,7 @@ 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, "Recieved remote flare request", + fc.knapsack.Slogger().Log(ctx, slog.LevelInfo, "received remote flare request", "note", flareData.Note, ) diff --git a/ee/control/consumers/flareconsumer/flareconsumer_test.go b/ee/control/consumers/flareconsumer/flareconsumer_test.go index 2eb6e0d26..a1e3ca77e 100644 --- a/ee/control/consumers/flareconsumer/flareconsumer_test.go +++ b/ee/control/consumers/flareconsumer/flareconsumer_test.go @@ -5,7 +5,6 @@ import ( "io" "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 +35,7 @@ func TestFlareConsumer(t *testing.T) { t.Parallel() mockSack := knapsackMock.NewKnapsack(t) - f := New(log.NewNopLogger(), mockSack) + f := New(mockSack) f.flarer = tt.flarer(t) f.newFlareStream = func(note, uploadRequestURL string) (io.WriteCloser, error) { // whatever, it implements write closer From 19c703baa3502372a28faed5a1adba1f876f614d Mon Sep 17 00:00:00 2001 From: seph Date: Tue, 7 Nov 2023 15:17:16 -0500 Subject: [PATCH 3/3] test --- ee/control/consumers/flareconsumer/flareconsumer_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ee/control/consumers/flareconsumer/flareconsumer_test.go b/ee/control/consumers/flareconsumer/flareconsumer_test.go index a1e3ca77e..15f2606fb 100644 --- a/ee/control/consumers/flareconsumer/flareconsumer_test.go +++ b/ee/control/consumers/flareconsumer/flareconsumer_test.go @@ -3,6 +3,7 @@ package flareconsumer import ( "bytes" "io" + "log/slog" "testing" "github.com/kolide/launcher/ee/control/consumers/flareconsumer/mocks" @@ -35,6 +36,7 @@ func TestFlareConsumer(t *testing.T) { t.Parallel() mockSack := knapsackMock.NewKnapsack(t) + 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) {