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

More logger replacement #1618

Merged
merged 3 commits into from
Feb 23, 2024
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
17 changes: 13 additions & 4 deletions cmd/launcher/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package main

import (
"context"
"log/slog"
"os"

"github.com/go-kit/kit/log"
"github.com/kolide/kit/logutil"
"github.com/kolide/launcher/ee/agent/flags"
"github.com/kolide/launcher/ee/agent/knapsack"
"github.com/kolide/launcher/ee/debug/checkups"
"github.com/kolide/launcher/pkg/launcher"
"github.com/kolide/launcher/pkg/log/multislogger"
)

func runDoctor(args []string) error {
Expand All @@ -27,8 +27,17 @@ func runDoctor(args []string) error {
}

fcOpts := []flags.Option{flags.WithCmdLineOpts(opts)}
logger := log.With(logutil.NewCLILogger(true), "caller", log.DefaultCaller)
flagController := flags.NewFlagController(logger, nil, fcOpts...)

slogLevel := slog.LevelInfo
if opts.Debug {
slogLevel = slog.LevelDebug
}

slogger := multislogger.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slogLevel,
})).Logger

flagController := flags.NewFlagController(slogger, nil, fcOpts...)
k := knapsack.New(nil, flagController, nil, nil, nil)

w := os.Stdout //tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', tabwriter.AlignRight)
Expand Down
28 changes: 20 additions & 8 deletions cmd/launcher/flare.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import (
"flag"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"strings"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/kolide/kit/ulid"
"github.com/kolide/launcher/ee/agent/flags"
"github.com/kolide/launcher/ee/agent/knapsack"
"github.com/kolide/launcher/ee/agent/storage/inmemory"
"github.com/kolide/launcher/ee/debug/checkups"
"github.com/kolide/launcher/ee/debug/shipper"
"github.com/kolide/launcher/pkg/launcher"
"github.com/kolide/launcher/pkg/log/multislogger"
"github.com/peterbourgon/ff/v3"
)

Expand Down Expand Up @@ -49,9 +49,17 @@ func runFlare(args []string) error {
return err
}

logger := log.NewLogfmtLogger(os.Stdout)
slogLevel := slog.LevelInfo
if opts.Debug {
slogLevel = slog.LevelDebug
}

slogger := multislogger.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slogLevel,
})).Logger

fcOpts := []flags.Option{flags.WithCmdLineOpts(opts)}
flagController := flags.NewFlagController(logger, inmemory.NewStore(), fcOpts...)
flagController := flags.NewFlagController(slogger, inmemory.NewStore(), fcOpts...)

k := knapsack.New(nil, flagController, nil, nil, nil)
ctx := context.Background()
Expand All @@ -70,7 +78,7 @@ func runFlare(args []string) error {
return err
}
flareDest = shipper
successMessage = "Flare uploaded successfully"
successMessage = "flare uploaded successfully"
case "local":
reportName := fmt.Sprintf("kolide_agent_flare_report_%s.zip", ulid.New())
reportPath := filepath.Join(*flOutputDir, reportName)
Expand All @@ -81,16 +89,20 @@ func runFlare(args []string) error {
}
defer flareFile.Close()
flareDest = flareFile
successMessage = "Flare saved locally"
successMessage = "flare saved locally"
default:
return fmt.Errorf(`invalid save option: %s, expected "local" or "upload"`, *flSave)

}

if err := checkups.RunFlare(ctx, k, flareDest, checkups.StandaloneEnviroment); err != nil {
return err
}

level.Info(logger).Log("msg", successMessage, "file", flareDest.Name())
slogger.Log(ctx, slog.LevelInfo,
"flare creation complete",
"status", successMessage,
"file", flareDest.Name(),
)

return nil
}
35 changes: 21 additions & 14 deletions cmd/launcher/internal/record_metadata.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package internal

import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"runtime"
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/groob/plist"
"github.com/kolide/kit/version"
"github.com/kolide/launcher/ee/agent/types"
Expand All @@ -21,8 +20,8 @@ type (
// subsystem. whenever new data is received, it will rewrite the metadata.json
// and metadata.plist files to our root install directory
metadataWriter struct {
logger log.Logger
k types.Knapsack
slogger *slog.Logger
k types.Knapsack
}
metadata struct {
DeviceId string `json:"device_id" plist:"device_id"`
Expand All @@ -33,21 +32,27 @@ type (
}
)

func NewMetadataWriter(logger log.Logger, k types.Knapsack) *metadataWriter {
func NewMetadataWriter(slogger *slog.Logger, k types.Knapsack) *metadataWriter {
return &metadataWriter{
logger: logger,
k: k,
slogger: slogger.With("component", "metadata_writer"),
k: k,
}
}

func (mw *metadataWriter) Ping() {
metadata := newMetadataTemplate()
if err := mw.populateLatestServerData(metadata); err != nil {
level.Debug(mw.logger).Log("msg", "unable to collect latest server data, metadata files will be incomplete", "err", err)
mw.slogger.Log(context.TODO(), slog.LevelDebug,
"unable to collect latest server data, metadata files will be incomplete",
"err", err,
)
}

if err := mw.recordMetadata(metadata); err != nil {
level.Debug(mw.logger).Log("msg", "unable to write out metadata files", "err", err)
mw.slogger.Log(context.TODO(), slog.LevelDebug,
"unable to write out metadata files",
"err", err,
)
}
}

Expand Down Expand Up @@ -106,17 +111,19 @@ func (mw *metadataWriter) recordMetadata(metadata *metadata) error {
func (mw *metadataWriter) getServerDataValue(store types.GetterSetterDeleterIteratorUpdater, key string) string {
val, err := store.Get([]byte(key))
if err != nil {
level.Debug(mw.logger).Log(
"msg", fmt.Sprintf("unable to collect value for %s from server_data, will re-attempt on next update", key),
mw.slogger.Log(context.TODO(), slog.LevelDebug,
"unable to collect value for key from server_data, will re-attempt on next update",
"key", key,
"err", err,
)

return ""
}

if string(val) == "" {
level.Debug(mw.logger).Log(
"msg", fmt.Sprintf("server_data was missing value for %s, will re-attempt on next update", key),
mw.slogger.Log(context.TODO(), slog.LevelDebug,
"server_data was missing value for key, will re-attempt on next update",
"key", key,
"err", err,
)

Expand Down
6 changes: 3 additions & 3 deletions cmd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl
}

fcOpts := []flags.Option{flags.WithCmdLineOpts(opts)}
flagController := flags.NewFlagController(logger, stores[storage.AgentFlagsStore], fcOpts...)
flagController := flags.NewFlagController(slogger, stores[storage.AgentFlagsStore], fcOpts...)
k := knapsack.New(stores, flagController, db, multiSlogger, systemMultiSlogger)

go runOsqueryVersionCheck(ctx, slogger, k.LatestOsquerydPath(ctx))
Expand Down Expand Up @@ -275,7 +275,7 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl
sigChannel := make(chan os.Signal, 1)

// Add a rungroup to catch things on the sigChannel
signalListener := newSignalListener(sigChannel, cancel, logger)
signalListener := newSignalListener(sigChannel, cancel, slogger)
runGroup.Add("sigChannel", signalListener.Execute, signalListener.Interrupt)

// For now, remediation is not performed -- we only log the hardware change.
Expand Down Expand Up @@ -450,7 +450,7 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl
controlService.RegisterSubscriber(authTokensSubsystemName, traceExporter)
}

if metadataWriter := internal.NewMetadataWriter(logger, k); metadataWriter == nil {
if metadataWriter := internal.NewMetadataWriter(slogger, k); metadataWriter == nil {
slogger.Log(ctx, slog.LevelDebug,
"unable to set up metadata writer",
"err", err,
Expand Down
9 changes: 6 additions & 3 deletions cmd/launcher/run_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package main
import (
"flag"
"fmt"
"log/slog"
"os"
"os/signal"
"path/filepath"
"syscall"

"github.com/go-kit/kit/log"
"github.com/kolide/kit/env"
"github.com/kolide/kit/fsutil"
"github.com/kolide/launcher/ee/agent"
"github.com/kolide/launcher/ee/agent/flags"
"github.com/kolide/launcher/ee/agent/knapsack"
"github.com/kolide/launcher/ee/agent/storage/inmemory"
"github.com/kolide/launcher/pkg/launcher"
"github.com/kolide/launcher/pkg/log/multislogger"
"github.com/kolide/launcher/pkg/osquery/runtime"
"github.com/kolide/launcher/pkg/osquery/table"
)
Expand Down Expand Up @@ -58,9 +59,11 @@ func runSocket(args []string) error {
if err != nil {
return err
}
logger := log.NewLogfmtLogger(os.Stdout)
slogger := multislogger.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
})).Logger
fcOpts := []flags.Option{flags.WithCmdLineOpts(cmdlineopts)}
flagController := flags.NewFlagController(logger, inmemory.NewStore(), fcOpts...)
flagController := flags.NewFlagController(slogger, inmemory.NewStore(), fcOpts...)
k := knapsack.New(nil, flagController, nil, nil, nil)
runner := runtime.New(k, opts...)
go runner.Run()
Expand Down
15 changes: 8 additions & 7 deletions cmd/launcher/signal_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,35 @@ package main

import (
"context"
"log/slog"
"os"
"os/signal"
"syscall"

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

// listens for interrupts
type signalListener struct {
sigChannel chan os.Signal
cancel context.CancelFunc
logger log.Logger
slogger *slog.Logger
interrupted bool
}

func newSignalListener(sigChannel chan os.Signal, cancel context.CancelFunc, logger log.Logger) *signalListener {
func newSignalListener(sigChannel chan os.Signal, cancel context.CancelFunc, slogger *slog.Logger) *signalListener {
return &signalListener{
sigChannel: sigChannel,
cancel: cancel,
logger: log.With(logger, "component", "signal_listener"),
slogger: slogger.With("component", "signal_listener"),
}
}

func (s *signalListener) Execute() error {
signal.Notify(s.sigChannel, os.Interrupt, syscall.SIGTERM)
sig := <-s.sigChannel
level.Info(s.logger).Log("msg", "beginning shutdown via signal", "signal_received", sig)
s.slogger.Log(context.TODO(), slog.LevelInfo,
"beginning shutdown via signal",
"signal_received", sig,
)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/launcher/signal_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"
"time"

"github.com/go-kit/kit/log"
"github.com/kolide/launcher/pkg/log/multislogger"
"github.com/stretchr/testify/require"
)

Expand All @@ -16,7 +16,7 @@ func TestInterrupt_Multiple(t *testing.T) {

sigChannel := make(chan os.Signal, 1)
_, cancel := context.WithCancel(context.TODO())
sigListener := newSignalListener(sigChannel, cancel, log.NewNopLogger())
sigListener := newSignalListener(sigChannel, cancel, multislogger.New().Logger)

// Let the signal listener run for a bit
go sigListener.Execute()
Expand Down
Loading
Loading