Skip to content

Commit

Permalink
Moved run ID management to knapsack (#1929)
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarfda authored Oct 31, 2024
1 parent c380438 commit 55714cd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
7 changes: 7 additions & 0 deletions cmd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl
flagController := flags.NewFlagController(slogger, stores[storage.AgentFlagsStore], fcOpts...)
k := knapsack.New(stores, flagController, db, multiSlogger, systemMultiSlogger)

// Generate a new run ID
newRunID := k.GetRunID()

// Apply the run ID to both logger and slogger
logger = log.With(logger, "run_id", newRunID)
slogger = slogger.With("run_id", newRunID)

// start counting uptime
processStartTime := time.Now().UTC()

Expand Down
14 changes: 14 additions & 0 deletions ee/agent/knapsack/knapsack.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ import (

"log/slog"

"github.com/kolide/kit/ulid"
"github.com/kolide/launcher/ee/agent/storage"
"github.com/kolide/launcher/ee/agent/types"
"github.com/kolide/launcher/ee/tuf"
"github.com/kolide/launcher/pkg/log/multislogger"
"go.etcd.io/bbolt"
)

// Package-level runID variable
var runID string

// type alias Flags, so that we can embed it inside knapsack, as `flags` and not `Flags`
type flags types.Flags

Expand Down Expand Up @@ -59,6 +63,16 @@ func New(stores map[storage.Store]types.KVStore, flags types.Flags, db *bbolt.DB
return k
}

// NewRunID sets the run ID in the knapsack
func (k *knapsack) GetRunID() string {
if runID == "" {
runID = ulid.New()
k.slogger.Logger = k.slogger.Logger.With("run_id", runID)
k.systemSlogger.Logger = k.systemSlogger.Logger.With("run_id", runID)
}
return runID
}

// Logging interface methods
func (k *knapsack) Slogger() *slog.Logger {
return k.slogger.Logger
Expand Down
9 changes: 3 additions & 6 deletions pkg/log/multislogger/multislogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log/slog"
"os"

"github.com/kolide/kit/ulid"
slogmulti "github.com/samber/slog-multi"
)

Expand Down Expand Up @@ -34,17 +33,15 @@ var ctxValueKeysToAdd = []contextKey{

type MultiSlogger struct {
*slog.Logger
handlers []slog.Handler
launcherRunId string
handlers []slog.Handler
}

// New creates a new multislogger if no handlers are passed in, it will
// create a logger that discards all logs
func New(h ...slog.Handler) *MultiSlogger {
ms := &MultiSlogger{
// setting to fanout with no handlers is noop
Logger: slog.New(slogmulti.Fanout()),
launcherRunId: ulid.New(),
Logger: slog.New(slogmulti.Fanout()),
}

ms.AddHandler(h...)
Expand All @@ -69,7 +66,7 @@ func (m *MultiSlogger) AddHandler(handler ...slog.Handler) {
Pipe(slogmulti.NewHandleInlineMiddleware(utcTimeMiddleware)).
Pipe(slogmulti.NewHandleInlineMiddleware(ctxValuesMiddleWare)).
Handler(slogmulti.Fanout(m.handlers...)),
).With("launcher_run_id", m.launcherRunId)
)
}

func utcTimeMiddleware(ctx context.Context, record slog.Record, next func(context.Context, slog.Record) error) error {
Expand Down

0 comments on commit 55714cd

Please sign in to comment.