From f089b7605568136c060f3b1b18a1167e7882c77c Mon Sep 17 00:00:00 2001 From: james pickett Date: Thu, 1 Feb 2024 11:50:21 -0800 Subject: [PATCH 1/5] embed flags interface into knapsack --- ee/agent/flags/keys/keys.go | 3 +- ee/agent/knapsack/knapsack.go | 369 +--------------------------------- 2 files changed, 6 insertions(+), 366 deletions(-) diff --git a/ee/agent/flags/keys/keys.go b/ee/agent/flags/keys/keys.go index c6aa5a91f..40a3553cf 100644 --- a/ee/agent/flags/keys/keys.go +++ b/ee/agent/flags/keys/keys.go @@ -6,8 +6,7 @@ type FlagKey string // When adding a new FlagKey: // 1. Define the FlagKey identifier, and the string key value it corresponds to, in the block below // 2. Add a getter and setter to the Flags interface (flags.go) -// 3. Implement the getter and setter in the Knapsack, which delegates the call to the FlagController -// 4. Implement the getter and setter in the FlagController, providing defaults, limits, and overrides +// 3. Implement the getter and setter in the FlagController, providing defaults, limits, and overrides // 4. Implement tests for any new APIs, sanitizers, limits, overrides. // 5. Update mocks -- in ee/agent/types, run `mockery --name Knapsack` and `mockery --name Flags`. const ( diff --git a/ee/agent/knapsack/knapsack.go b/ee/agent/knapsack/knapsack.go index 85425a229..149bc8410 100644 --- a/ee/agent/knapsack/knapsack.go +++ b/ee/agent/knapsack/knapsack.go @@ -2,13 +2,11 @@ package knapsack import ( "context" - "time" "log/slog" "github.com/go-kit/kit/log" "github.com/kolide/kit/ulid" - "github.com/kolide/launcher/ee/agent/flags/keys" "github.com/kolide/launcher/ee/agent/storage" "github.com/kolide/launcher/ee/agent/types" "github.com/kolide/launcher/ee/tuf" @@ -17,11 +15,15 @@ import ( "go.etcd.io/bbolt" ) +// type alias Flags, so that we can embed it inside knapsack, as `flags` and not `Flags` +type flags types.Flags + // Knapsack is an inventory of data and useful services which are used throughout // launcher code and are typically valid for the lifetime of the launcher application instance. type knapsack struct { stores map[storage.Store]types.KVStore - flags types.Flags + // Embed flags so we get all the flag interfaces + flags // BboltDB is the underlying bbolt database. // Ideally, we can eventually remove this. This is only here because some parts of the codebase @@ -78,7 +80,6 @@ func (k *knapsack) BboltDB() *bbolt.DB { } // Stores interface methods - func (k *knapsack) Stores() map[storage.Store]types.KVStore { return k.stores } @@ -145,43 +146,6 @@ func (k *knapsack) getKVStore(storeType storage.Store) types.KVStore { return store } -// Flags interface methods - -func (k *knapsack) RegisterChangeObserver(observer types.FlagsChangeObserver, flagKeys ...keys.FlagKey) { - k.flags.RegisterChangeObserver(observer, flagKeys...) -} - -func (k *knapsack) AutoloadedExtensions() []string { - return k.flags.AutoloadedExtensions() -} - -func (k *knapsack) SetKolideServerURL(url string) error { - return k.flags.SetKolideServerURL(url) -} -func (k *knapsack) KolideServerURL() string { - return k.flags.KolideServerURL() -} - -func (k *knapsack) KolideHosted() bool { - return k.flags.KolideHosted() -} - -func (k *knapsack) EnrollSecret() string { - return k.flags.EnrollSecret() -} - -func (k *knapsack) EnrollSecretPath() string { - return k.flags.EnrollSecretPath() -} - -func (k *knapsack) RootDirectory() string { - return k.flags.RootDirectory() -} - -func (k *knapsack) OsquerydPath() string { - return k.flags.OsquerydPath() -} - func (k *knapsack) LatestOsquerydPath(ctx context.Context) string { latestBin, err := tuf.CheckOutLatest(ctx, "osqueryd", k.RootDirectory(), k.UpdateDirectory(), k.UpdateChannel(), log.NewNopLogger()) if err != nil { @@ -190,326 +154,3 @@ func (k *knapsack) LatestOsquerydPath(ctx context.Context) string { return latestBin.Path } - -func (k *knapsack) CertPins() [][]byte { - return k.flags.CertPins() -} - -func (k *knapsack) RootPEM() string { - return k.flags.RootPEM() -} - -func (k *knapsack) SetLoggingInterval(interval time.Duration) error { - return k.flags.SetLoggingInterval(interval) -} -func (k *knapsack) LoggingInterval() time.Duration { - return k.flags.LoggingInterval() -} - -func (k *knapsack) EnableInitialRunner() bool { - return k.flags.EnableInitialRunner() -} - -func (k *knapsack) Transport() string { - return k.flags.Transport() -} - -func (k *knapsack) LogMaxBytesPerBatch() int { - return k.flags.LogMaxBytesPerBatch() -} - -func (k *knapsack) SetDesktopEnabled(enabled bool) error { - return k.flags.SetDesktopEnabled(enabled) -} -func (k *knapsack) DesktopEnabled() bool { - return k.flags.DesktopEnabled() -} - -func (k *knapsack) SetDesktopUpdateInterval(interval time.Duration) error { - return k.flags.SetDesktopUpdateInterval(interval) -} -func (k *knapsack) DesktopUpdateInterval() time.Duration { - return k.flags.DesktopUpdateInterval() -} - -func (k *knapsack) SetDesktopMenuRefreshInterval(interval time.Duration) error { - return k.flags.SetDesktopMenuRefreshInterval(interval) -} -func (k *knapsack) DesktopMenuRefreshInterval() time.Duration { - return k.flags.DesktopMenuRefreshInterval() -} - -func (k *knapsack) SetDebugServerData(debug bool) error { - return k.flags.SetDebugServerData(debug) -} -func (k *knapsack) DebugServerData() bool { - return k.flags.DebugServerData() -} - -func (k *knapsack) SetForceControlSubsystems(force bool) error { - return k.flags.SetForceControlSubsystems(force) -} -func (k *knapsack) ForceControlSubsystems() bool { - return k.flags.ForceControlSubsystems() -} - -func (k *knapsack) SetControlServerURL(url string) error { - return k.flags.SetControlServerURL(url) -} -func (k *knapsack) ControlServerURL() string { - return k.flags.ControlServerURL() -} - -func (k *knapsack) SetControlRequestInterval(interval time.Duration) error { - return k.flags.SetControlRequestInterval(interval) -} -func (k *knapsack) SetControlRequestIntervalOverride(interval, duration time.Duration) { - k.flags.SetControlRequestIntervalOverride(interval, duration) -} -func (k *knapsack) ControlRequestInterval() time.Duration { - return k.flags.ControlRequestInterval() -} - -func (k *knapsack) SetDisableControlTLS(disabled bool) error { - return k.flags.SetDisableControlTLS(disabled) -} -func (k *knapsack) DisableControlTLS() bool { - return k.flags.DisableControlTLS() -} - -func (k *knapsack) SetInsecureControlTLS(disabled bool) error { - return k.flags.SetInsecureControlTLS(disabled) -} -func (k *knapsack) InsecureControlTLS() bool { - return k.flags.InsecureControlTLS() -} - -func (k *knapsack) SetInsecureTLS(insecure bool) error { - return k.flags.SetInsecureTLS(insecure) -} -func (k *knapsack) InsecureTLS() bool { - return k.flags.InsecureTLS() -} - -func (k *knapsack) SetInsecureTransportTLS(insecure bool) error { - return k.flags.SetInsecureTransportTLS(insecure) -} -func (k *knapsack) InsecureTransportTLS() bool { - return k.flags.InsecureTransportTLS() -} - -func (k *knapsack) IAmBreakingEELicense() bool { - return k.flags.IAmBreakingEELicense() -} - -func (k *knapsack) SetDebug(debug bool) error { - return k.flags.SetDebug(debug) -} -func (k *knapsack) Debug() bool { - return k.flags.Debug() -} - -func (k *knapsack) DebugLogFile() string { - return k.flags.DebugLogFile() -} - -func (k *knapsack) SetOsqueryVerbose(verbose bool) error { - return k.flags.SetOsqueryVerbose(verbose) -} -func (k *knapsack) OsqueryVerbose() bool { - return k.flags.OsqueryVerbose() -} - -func (k *knapsack) SetWatchdogEnabled(enable bool) error { - return k.flags.SetWatchdogEnabled(enable) -} -func (k *knapsack) WatchdogEnabled() bool { - return k.flags.WatchdogEnabled() -} - -func (k *knapsack) SetWatchdogDelaySec(sec int) error { - return k.flags.SetWatchdogDelaySec(sec) -} -func (k *knapsack) WatchdogDelaySec() int { - return k.flags.WatchdogDelaySec() -} - -func (k *knapsack) SetWatchdogMemoryLimitMB(limit int) error { - return k.flags.SetWatchdogMemoryLimitMB(limit) -} -func (k *knapsack) WatchdogMemoryLimitMB() int { - return k.flags.WatchdogMemoryLimitMB() -} - -func (k *knapsack) SetWatchdogUtilizationLimitPercent(limit int) error { - return k.flags.SetWatchdogUtilizationLimitPercent(limit) -} -func (k *knapsack) WatchdogUtilizationLimitPercent() int { - return k.flags.WatchdogUtilizationLimitPercent() -} - -func (k *knapsack) OsqueryFlags() []string { - return k.flags.OsqueryFlags() -} - -func (k *knapsack) OsqueryTlsConfigEndpoint() string { - return k.flags.OsqueryTlsConfigEndpoint() -} -func (k *knapsack) OsqueryTlsEnrollEndpoint() string { - return k.flags.OsqueryTlsEnrollEndpoint() -} -func (k *knapsack) OsqueryTlsLoggerEndpoint() string { - return k.flags.OsqueryTlsLoggerEndpoint() -} -func (k *knapsack) OsqueryTlsDistributedReadEndpoint() string { - return k.flags.OsqueryTlsDistributedReadEndpoint() -} -func (k *knapsack) OsqueryTlsDistributedWriteEndpoint() string { - return k.flags.OsqueryTlsDistributedWriteEndpoint() -} - -func (k *knapsack) SetAutoupdate(enabled bool) error { - return k.flags.SetAutoupdate(enabled) -} -func (k *knapsack) Autoupdate() bool { - return k.flags.Autoupdate() -} - -func (k *knapsack) SetNotaryServerURL(url string) error { - return k.flags.SetNotaryServerURL(url) -} -func (k *knapsack) NotaryServerURL() string { - return k.flags.NotaryServerURL() -} - -func (k *knapsack) SetTufServerURL(url string) error { - return k.flags.SetTufServerURL(url) -} -func (k *knapsack) TufServerURL() string { - return k.flags.TufServerURL() -} - -func (k *knapsack) SetMirrorServerURL(url string) error { - return k.flags.SetMirrorServerURL(url) -} -func (k *knapsack) MirrorServerURL() string { - return k.flags.MirrorServerURL() -} - -func (k *knapsack) SetAutoupdateInterval(interval time.Duration) error { - return k.flags.SetAutoupdateInterval(interval) -} -func (k *knapsack) AutoupdateInterval() time.Duration { - return k.flags.AutoupdateInterval() -} - -func (k *knapsack) SetUpdateChannel(channel string) error { - return k.flags.SetUpdateChannel(channel) -} -func (k *knapsack) UpdateChannel() string { - return k.flags.UpdateChannel() -} - -func (k *knapsack) SetNotaryPrefix(prefix string) error { - return k.flags.SetNotaryPrefix(prefix) -} -func (k *knapsack) NotaryPrefix() string { - return k.flags.NotaryPrefix() -} - -func (k *knapsack) SetAutoupdateInitialDelay(delay time.Duration) error { - return k.flags.SetAutoupdateInitialDelay(delay) -} -func (k *knapsack) AutoupdateInitialDelay() time.Duration { - return k.flags.AutoupdateInitialDelay() -} - -func (k *knapsack) SetUpdateDirectory(directory string) error { - return k.flags.SetUpdateDirectory(directory) -} -func (k *knapsack) UpdateDirectory() string { - return k.flags.UpdateDirectory() -} - -func (k *knapsack) SetUseTUFAutoupdater(enabled bool) error { - return k.flags.SetUseTUFAutoupdater(enabled) -} -func (k *knapsack) UseTUFAutoupdater() bool { - return k.flags.UseTUFAutoupdater() -} - -func (k *knapsack) SetExportTraces(enabled bool) error { - return k.flags.SetExportTraces(enabled) -} -func (k *knapsack) SetExportTracesOverride(value bool, duration time.Duration) { - k.flags.SetExportTracesOverride(value, duration) -} -func (k *knapsack) ExportTraces() bool { - return k.flags.ExportTraces() -} - -func (k *knapsack) SetTraceSamplingRate(rate float64) error { - return k.flags.SetTraceSamplingRate(rate) -} -func (k *knapsack) SetTraceSamplingRateOverride(value float64, duration time.Duration) { - k.flags.SetTraceSamplingRateOverride(value, duration) -} -func (k *knapsack) TraceSamplingRate() float64 { - return k.flags.TraceSamplingRate() -} - -func (k *knapsack) SetTraceIngestServerURL(url string) error { - return k.flags.SetTraceIngestServerURL(url) -} -func (k *knapsack) TraceIngestServerURL() string { - return k.flags.TraceIngestServerURL() -} - -func (k *knapsack) SetDisableTraceIngestTLS(enabled bool) error { - return k.flags.SetDisableTraceIngestTLS(enabled) -} -func (k *knapsack) DisableTraceIngestTLS() bool { - return k.flags.DisableTraceIngestTLS() -} - -func (k *knapsack) SetTraceBatchTimeout(duration time.Duration) error { - return k.flags.SetTraceBatchTimeout(duration) -} -func (k *knapsack) TraceBatchTimeout() time.Duration { - return k.flags.TraceBatchTimeout() -} - -func (k *knapsack) SetLogIngestServerURL(url string) error { - return k.flags.SetLogIngestServerURL(url) -} -func (k *knapsack) LogIngestServerURL() string { - return k.flags.LogIngestServerURL() -} - -func (k *knapsack) SetLogShippingLevel(level string) error { - return k.flags.SetLogShippingLevel(level) -} -func (k *knapsack) SetLogShippingLevelOverride(value string, duration time.Duration) { - k.flags.SetLogShippingLevelOverride(value, duration) -} -func (k *knapsack) LogShippingLevel() string { - return k.flags.LogShippingLevel() -} - -func (k *knapsack) SetInModernStandby(enabled bool) error { - return k.flags.SetInModernStandby(enabled) -} -func (k *knapsack) InModernStandby() bool { - return k.flags.InModernStandby() -} - -func (k *knapsack) SetOsqueryHealthcheckStartupDelay(delay time.Duration) error { - return k.flags.SetOsqueryHealthcheckStartupDelay(delay) -} -func (k *knapsack) OsqueryHealthcheckStartupDelay() time.Duration { - return k.flags.OsqueryHealthcheckStartupDelay() -} - -func (k *knapsack) LocalDevelopmentPath() string { - return k.flags.LocalDevelopmentPath() -} From 73ff740b70fd08655fa4ecb806f78c77924eb770 Mon Sep 17 00:00:00 2001 From: james pickett Date: Fri, 2 Feb 2024 09:44:13 -0800 Subject: [PATCH 2/5] reset slogger in main after adding handler --- cmd/launcher/launcher.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cmd/launcher/launcher.go b/cmd/launcher/launcher.go index a1260dadb..684135ae6 100644 --- a/cmd/launcher/launcher.go +++ b/cmd/launcher/launcher.go @@ -197,6 +197,8 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl AddSource: true, Level: slog.LevelDebug, })) + // need to reset slogger to include new handler + slogger = k.Slogger() } // create a rungroup for all the actors we create to allow for easy start/stop @@ -221,6 +223,9 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl logger = teelogger.New(logger, logShipper) logger = log.With(logger, "caller", log.Caller(5)) k.AddSlogHandler(logShipper.SlogHandler()) + // need to reset slogger to include new handler + slogger = k.Slogger() + ctx = ctxlog.NewContext(ctx, logger) // Set the logger back in the ctx k.SetTraceSamplingRateOverride(1.0, initialDebugDuration) From f57710af6d89fe50d84d562a523db4548f9bc77a Mon Sep 17 00:00:00 2001 From: james pickett Date: Fri, 2 Feb 2024 13:49:30 -0800 Subject: [PATCH 3/5] avoid re-assignment by overwriting memory address of logger --- cmd/launcher/launcher.go | 6 ------ ee/agent/knapsack/knapsack.go | 5 ++--- pkg/log/multislogger/multislogger.go | 16 +++++----------- 3 files changed, 7 insertions(+), 20 deletions(-) diff --git a/cmd/launcher/launcher.go b/cmd/launcher/launcher.go index 684135ae6..c22021a8d 100644 --- a/cmd/launcher/launcher.go +++ b/cmd/launcher/launcher.go @@ -185,8 +185,6 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl fcOpts := []flags.Option{flags.WithCmdLineOpts(opts)} flagController := flags.NewFlagController(logger, stores[storage.AgentFlagsStore], fcOpts...) k := knapsack.New(stores, flagController, db, multiSlogger, systemMultiSlogger) - // reassign slogger to knapsack slogger to get launcher run id added to slogger - slogger = k.Slogger() go runOsqueryVersionCheck(ctx, slogger, k.LatestOsquerydPath(ctx)) go timemachine.AddExclusions(ctx, k) @@ -197,8 +195,6 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl AddSource: true, Level: slog.LevelDebug, })) - // need to reset slogger to include new handler - slogger = k.Slogger() } // create a rungroup for all the actors we create to allow for easy start/stop @@ -223,8 +219,6 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl logger = teelogger.New(logger, logShipper) logger = log.With(logger, "caller", log.Caller(5)) k.AddSlogHandler(logShipper.SlogHandler()) - // need to reset slogger to include new handler - slogger = k.Slogger() ctx = ctxlog.NewContext(ctx, logger) // Set the logger back in the ctx diff --git a/ee/agent/knapsack/knapsack.go b/ee/agent/knapsack/knapsack.go index 149bc8410..ddd39b22e 100644 --- a/ee/agent/knapsack/knapsack.go +++ b/ee/agent/knapsack/knapsack.go @@ -51,7 +51,7 @@ func New(stores map[storage.Store]types.KVStore, flags types.Flags, db *bbolt.DB } if k.slogger != nil { - k.slogger.Logger = k.slogger.Logger.With("launcher_run_id", k.launcherRunId) + *k.slogger.Logger = *k.slogger.Logger.With("launcher_run_id", k.launcherRunId) } return k @@ -68,8 +68,7 @@ func (k *knapsack) SystemSlogger() *slog.Logger { func (k *knapsack) AddSlogHandler(handler ...slog.Handler) { k.slogger.AddHandler(handler...) - k.slogger.Logger = k.slogger.Logger.With("launcher_run_id", k.launcherRunId) - + *k.slogger.Logger = *k.slogger.Logger.With("launcher_run_id", k.launcherRunId) // also send system logs to the same handlers k.systemSlogger.AddHandler(handler...) } diff --git a/pkg/log/multislogger/multislogger.go b/pkg/log/multislogger/multislogger.go index 28f17e5d3..40fc9e944 100644 --- a/pkg/log/multislogger/multislogger.go +++ b/pkg/log/multislogger/multislogger.go @@ -2,7 +2,6 @@ package multislogger import ( "context" - "io" "log/slog" slogmulti "github.com/samber/slog-multi" @@ -41,27 +40,22 @@ type MultiSlogger struct { func New(h ...slog.Handler) *MultiSlogger { ms := new(MultiSlogger) - if len(h) == 0 { - // if we don't have any handlers passed in, we'll just discard the logs - // do not add the discard handler to the handlers so it will not be - // included when a handler is added - ms.Logger = slog.New(slog.NewTextHandler(io.Discard, nil)) - return ms - } + // setting to fanout with no handlers is noop + ms.Logger = slog.New(slogmulti.Fanout()) ms.AddHandler(h...) return ms } // AddHandler adds a handler to the multislogger, this creates a branch new -// slog.Logger under the the hood, mean any attributes added with -// Logger.With will be lost +// slog.Logger under the the hood, and overwrites old Logger memory address, +// this means any attributes added with Logger.With will be lost func (m *MultiSlogger) AddHandler(handler ...slog.Handler) { m.handlers = append(m.handlers, handler...) // we have to rebuild the handler everytime because the slogmulti package we're // using doesn't support adding handlers after the Fanout handler has been created - m.Logger = slog.New( + *m.Logger = *slog.New( slogmulti. Pipe(slogmulti.NewHandleInlineMiddleware(utcTimeMiddleware)). Pipe(slogmulti.NewHandleInlineMiddleware(ctxValuesMiddleWare)). From b0278dc7805488f3f0a6dc93ca744da72410cf18 Mon Sep 17 00:00:00 2001 From: james pickett Date: Fri, 2 Feb 2024 15:03:06 -0800 Subject: [PATCH 4/5] move launcher run id setting up to multi slogger --- ee/agent/knapsack/knapsack.go | 9 --------- pkg/log/multislogger/multislogger.go | 24 +++++++++++++++++++----- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/ee/agent/knapsack/knapsack.go b/ee/agent/knapsack/knapsack.go index ddd39b22e..23093b4f1 100644 --- a/ee/agent/knapsack/knapsack.go +++ b/ee/agent/knapsack/knapsack.go @@ -6,7 +6,6 @@ import ( "log/slog" "github.com/go-kit/kit/log" - "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" @@ -33,7 +32,6 @@ type knapsack struct { db *bbolt.DB slogger, systemSlogger *multislogger.MultiSlogger - launcherRunId string // This struct is a work in progress, and will be iteratively added to as needs arise. // Some potential future additions include: @@ -47,11 +45,6 @@ func New(stores map[storage.Store]types.KVStore, flags types.Flags, db *bbolt.DB stores: stores, slogger: slogger, systemSlogger: systemSlogger, - launcherRunId: ulid.New(), - } - - if k.slogger != nil { - *k.slogger.Logger = *k.slogger.Logger.With("launcher_run_id", k.launcherRunId) } return k @@ -68,8 +61,6 @@ func (k *knapsack) SystemSlogger() *slog.Logger { func (k *knapsack) AddSlogHandler(handler ...slog.Handler) { k.slogger.AddHandler(handler...) - *k.slogger.Logger = *k.slogger.Logger.With("launcher_run_id", k.launcherRunId) - // also send system logs to the same handlers k.systemSlogger.AddHandler(handler...) } diff --git a/pkg/log/multislogger/multislogger.go b/pkg/log/multislogger/multislogger.go index 40fc9e944..146d9fa8d 100644 --- a/pkg/log/multislogger/multislogger.go +++ b/pkg/log/multislogger/multislogger.go @@ -4,6 +4,7 @@ import ( "context" "log/slog" + "github.com/kolide/kit/ulid" slogmulti "github.com/samber/slog-multi" ) @@ -32,16 +33,18 @@ var ctxValueKeysToAdd = []contextKey{ type MultiSlogger struct { *slog.Logger - handlers []slog.Handler + handlers []slog.Handler + launcherRunId string } // 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 := new(MultiSlogger) - - // setting to fanout with no handlers is noop - ms.Logger = slog.New(slogmulti.Fanout()) + ms := &MultiSlogger{ + // setting to fanout with no handlers is noop + Logger: slog.New(slogmulti.Fanout()), + launcherRunId: ulid.New(), + } ms.AddHandler(h...) return ms @@ -59,10 +62,21 @@ func (m *MultiSlogger) AddHandler(handler ...slog.Handler) { slogmulti. Pipe(slogmulti.NewHandleInlineMiddleware(utcTimeMiddleware)). Pipe(slogmulti.NewHandleInlineMiddleware(ctxValuesMiddleWare)). + Pipe(slogmulti.NewHandleInlineMiddleware(m.launcherRunIdMiddleware)). Handler(slogmulti.Fanout(m.handlers...)), ) } +// launcherRunIdMiddleware adds the launcher run id to the log record +func (ms *MultiSlogger) launcherRunIdMiddleware(ctx context.Context, record slog.Record, next func(context.Context, slog.Record) error) error { + record.AddAttrs(slog.Attr{ + Key: "launcher_run_id", + Value: slog.StringValue(ms.launcherRunId), + }) + + return next(ctx, record) +} + func utcTimeMiddleware(ctx context.Context, record slog.Record, next func(context.Context, slog.Record) error) error { record.Time = record.Time.UTC() return next(ctx, record) From 5176fde26bf7d51f25b2202a868f262e2cadc68e Mon Sep 17 00:00:00 2001 From: james pickett Date: Fri, 2 Feb 2024 15:05:24 -0800 Subject: [PATCH 5/5] just use with instead of middle ware --- pkg/log/multislogger/multislogger.go | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/pkg/log/multislogger/multislogger.go b/pkg/log/multislogger/multislogger.go index 146d9fa8d..0025f0a63 100644 --- a/pkg/log/multislogger/multislogger.go +++ b/pkg/log/multislogger/multislogger.go @@ -62,19 +62,8 @@ func (m *MultiSlogger) AddHandler(handler ...slog.Handler) { slogmulti. Pipe(slogmulti.NewHandleInlineMiddleware(utcTimeMiddleware)). Pipe(slogmulti.NewHandleInlineMiddleware(ctxValuesMiddleWare)). - Pipe(slogmulti.NewHandleInlineMiddleware(m.launcherRunIdMiddleware)). Handler(slogmulti.Fanout(m.handlers...)), - ) -} - -// launcherRunIdMiddleware adds the launcher run id to the log record -func (ms *MultiSlogger) launcherRunIdMiddleware(ctx context.Context, record slog.Record, next func(context.Context, slog.Record) error) error { - record.AddAttrs(slog.Attr{ - Key: "launcher_run_id", - Value: slog.StringValue(ms.launcherRunId), - }) - - return next(ctx, record) + ).With("launcher_run_id", m.launcherRunId) } func utcTimeMiddleware(ctx context.Context, record slog.Record, next func(context.Context, slog.Record) error) error {