Clean up logging namespaces in daemon a bit

This commit is contained in:
Brian Picciano 2024-12-13 13:52:39 +01:00
parent 5e8049c8d0
commit aac7b30cf0
5 changed files with 38 additions and 21 deletions

View File

@ -42,7 +42,7 @@ var subCmdDaemon = subCmd{
networkLoader, err := network.NewLoader( networkLoader, err := network.NewLoader(
ctx, ctx,
ctx.logger.WithNamespace("loader"), ctx.logger.WithNamespace("network-loader"),
envBinDirPath, envBinDirPath,
nil, nil,
) )
@ -50,18 +50,22 @@ var subCmdDaemon = subCmd{
return fmt.Errorf("instantiating network loader: %w", err) return fmt.Errorf("instantiating network loader: %w", err)
} }
daemonLogger := ctx.logger.WithNamespace("daemon")
daemonInst, err := daemon.New( daemonInst, err := daemon.New(
ctx, ctx.logger, networkLoader, daemonConfig, ctx,
daemonLogger,
networkLoader,
daemonConfig,
) )
if err != nil { if err != nil {
return fmt.Errorf("starting daemon: %w", err) return fmt.Errorf("starting daemon: %w", err)
} }
defer func() { defer func() {
ctx.logger.Info(ctx, "Stopping child processes") daemonLogger.Info(ctx, "Stopping daemon")
if err := daemonInst.Shutdown(); err != nil { if err := daemonInst.Shutdown(); err != nil {
ctx.logger.Error(ctx, "Shutting down daemon cleanly failed, there may be orphaned child processes", err) ctx.logger.Error(ctx, "Shutting down daemon cleanly failed, there may be orphaned child processes", err)
} }
ctx.logger.Info(ctx, "Child processes successfully stopped") daemonLogger.Info(ctx, "Daemon successfully stopped")
}() }()
{ {

View File

@ -13,7 +13,6 @@ import (
"sort" "sort"
"sync" "sync"
"dev.mediocregopher.com/mediocre-go-lib.git/mctx"
"dev.mediocregopher.com/mediocre-go-lib.git/mlog" "dev.mediocregopher.com/mediocre-go-lib.git/mlog"
) )
@ -71,8 +70,6 @@ func New(
} }
for _, creationParams := range loadableNetworks { for _, creationParams := range loadableNetworks {
ctx = mctx.WithAnnotator(ctx, creationParams)
var ( var (
id = creationParams.ID id = creationParams.ID
networkConfig = pickNetworkConfig(daemonConfig, creationParams) networkConfig = pickNetworkConfig(daemonConfig, creationParams)
@ -80,7 +77,7 @@ func New(
n, err := networkLoader.Load( n, err := networkLoader.Load(
ctx, ctx,
logger.WithNamespace("network"), networkLogger(logger, creationParams),
creationParams, creationParams,
&network.Opts{ &network.Opts{
Config: networkConfig, Config: networkConfig,
@ -119,20 +116,19 @@ func (d *Daemon) CreateNetwork(
var ( var (
creationParams = bootstrap.NewCreationParams(name, domain) creationParams = bootstrap.NewCreationParams(name, domain)
networkConfig = pickNetworkConfig(d.daemonConfig, creationParams) networkConfig = pickNetworkConfig(d.daemonConfig, creationParams)
networkLogger = networkLogger(d.logger, creationParams)
) )
ctx = mctx.WithAnnotator(ctx, creationParams)
if joined, err := alreadyJoined(ctx, d.networks, creationParams); err != nil { if joined, err := alreadyJoined(ctx, d.networks, creationParams); err != nil {
return fmt.Errorf("checking if already joined to network: %w", err) return fmt.Errorf("checking if already joined to network: %w", err)
} else if joined { } else if joined {
return ErrAlreadyJoined return ErrAlreadyJoined
} }
d.logger.Info(ctx, "Creating network") networkLogger.Info(ctx, "Creating network")
n, err := d.networkLoader.Create( n, err := d.networkLoader.Create(
ctx, ctx,
d.logger.WithNamespace("network"), networkLogger,
creationParams, creationParams,
ipNet, ipNet,
hostName, hostName,
@ -144,7 +140,7 @@ func (d *Daemon) CreateNetwork(
return fmt.Errorf("creating network: %w", err) return fmt.Errorf("creating network: %w", err)
} }
d.logger.Info(ctx, "Network created successfully") networkLogger.Info(ctx, "Network created successfully")
d.networks[creationParams.ID] = joinedNetwork{n, networkConfig} d.networks[creationParams.ID] = joinedNetwork{n, networkConfig}
return nil return nil
} }
@ -164,20 +160,21 @@ func (d *Daemon) JoinNetwork(
creationParams = newBootstrap.Bootstrap.NetworkCreationParams creationParams = newBootstrap.Bootstrap.NetworkCreationParams
networkID = creationParams.ID networkID = creationParams.ID
networkConfig = pickNetworkConfig(d.daemonConfig, creationParams) networkConfig = pickNetworkConfig(d.daemonConfig, creationParams)
networkLogger = networkLogger(
d.logger, newBootstrap.Bootstrap.NetworkCreationParams,
)
) )
ctx = mctx.WithAnnotator(ctx, newBootstrap.Bootstrap.NetworkCreationParams)
if joined, err := alreadyJoined(ctx, d.networks, creationParams); err != nil { if joined, err := alreadyJoined(ctx, d.networks, creationParams); err != nil {
return fmt.Errorf("checking if already joined to network: %w", err) return fmt.Errorf("checking if already joined to network: %w", err)
} else if joined { } else if joined {
return ErrAlreadyJoined return ErrAlreadyJoined
} }
d.logger.Info(ctx, "Joining network") networkLogger.Info(ctx, "Joining network")
n, err := d.networkLoader.Join( n, err := d.networkLoader.Join(
ctx, ctx,
d.logger.WithNamespace("network"), networkLogger,
newBootstrap, newBootstrap,
&network.Opts{ &network.Opts{
Config: networkConfig, Config: networkConfig,
@ -189,7 +186,7 @@ func (d *Daemon) JoinNetwork(
) )
} }
d.logger.Info(ctx, "Network joined successfully") networkLogger.Info(ctx, "Network joined successfully")
d.networks[networkID] = joinedNetwork{n, networkConfig} d.networks[networkID] = joinedNetwork{n, networkConfig}
return nil return nil
} }

View File

@ -5,8 +5,16 @@ import (
"fmt" "fmt"
"isle/bootstrap" "isle/bootstrap"
"isle/daemon/network" "isle/daemon/network"
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
) )
func networkLogger(
logger *mlog.Logger, creationParams bootstrap.CreationParams,
) *mlog.Logger {
return logger.WithNamespace("networks").WithNamespace(creationParams.Domain)
}
func pickNetwork( func pickNetwork(
ctx context.Context, ctx context.Context,
networkLoader network.Loader, networkLoader network.Loader,

View File

@ -223,7 +223,7 @@ func (l *loader) Load(
return load( return load(
ctx, ctx,
logger.WithNamespace("network"), logger,
l.envBinDirPath, l.envBinDirPath,
l.nebulaDeviceNamer, l.nebulaDeviceNamer,
networkStateDir, networkStateDir,
@ -256,7 +256,7 @@ func (l *loader) Join(
return join( return join(
ctx, ctx,
logger.WithNamespace("network"), logger,
l.envBinDirPath, l.envBinDirPath,
l.nebulaDeviceNamer, l.nebulaDeviceNamer,
joiningBootstrap, joiningBootstrap,
@ -289,7 +289,7 @@ func (l *loader) Create(
return create( return create(
ctx, ctx,
logger.WithNamespace("network"), logger,
l.envBinDirPath, l.envBinDirPath,
l.nebulaDeviceNamer, l.nebulaDeviceNamer,
networkStateDir, networkStateDir,

View File

@ -0,0 +1,8 @@
---
type: task
---
# Fix dnsmasq Logging Capture
The dnsmasq process's logging is not being captured into mlog, but instead is
being written directly to stdout/stderr.