Skip to content

Commit

Permalink
fix(ref): use explicit child/scoped loggers, avoid variable shadowing
Browse files Browse the repository at this point in the history
I even know about variable shadowing, but here we are. In any case,
while Go 1.22 should fix the loop variable shadowing issue, I find it
much cleaner and clearer to use explicitly scoped, named loggers where
possible.

Fixes a few issues where log statements would continuously grow and add
more keys on in each iteration of loops because I was reusing the same
logger reference in each iteration. Ex (note the `module` field added on
from all of the various module run loops):

```
Apr 08 04:10:52 testbox-arch mango[1744]: {"time":"2024-04-08T04:10:52.970064415Z","level":"DEBUG","msg":"No module variables","hostname":{"system":"testbox-arch","inventory":"testbox-arch"},"worker":"manager","manager":{"inventory":"/opt/mango/inventory","hostname":"testbox-arch"},"manager":{"enrolled":true,"runID":"01HTXYDQ54N7BNNFQSEC42NC9M"},"module":{"id":"/opt/mango/inventory/modules/test-requires-3"},"module":{"id":"/opt/mango/inventory/modules/test-requires-4"},"module":{"id":"/opt/mango/inventory/modules/test-requires-5"},"module":{"id":"/opt/mango/inventory/modules/test-reload"},"module":{"id":"/opt/mango/inventory/modules/test-env-vars"},"module":{"id":"/opt/mango/inventory/modules/test-template"},"module":{"id":"/opt/mango/inventory/modules/test-requires-1"},"module":{"id":"/opt/mango/inventory/modules/test-requires-2"}}
```
  • Loading branch information
tjhop committed Apr 9, 2024
1 parent 6ea9131 commit 7375f23
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 63 deletions.
10 changes: 5 additions & 5 deletions cmd/mango/mango.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,14 +537,14 @@ func main() {
}
}

logger = logger.With(
mainLogger := logger.With(
slog.Group("hostname",
slog.String("system", utils.GetHostname()),
slog.String("inventory", me),
),
)

logger.LogAttrs(
mainLogger.LogAttrs(
rootCtx,
slog.LevelInfo,
"Mango build information",
Expand All @@ -556,9 +556,9 @@ func main() {
),
)

// set logger as default
slog.SetDefault(logger)
// set mainLogger as default
slog.SetDefault(mainLogger)

// run mango daemon
mango(rootCtx, logger, inventoryPath, me)
mango(rootCtx, mainLogger, inventoryPath, me)
}
4 changes: 2 additions & 2 deletions internal/inventory/directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (i *Inventory) ParseDirectives(ctx context.Context, logger *slog.Logger) er
"inventory": i.inventoryPath,
"component": "directives",
}
logger = logger.With(
iLogger := logger.With(
slog.Group(
"inventory",
slog.String("component", "directives"),
Expand All @@ -40,7 +40,7 @@ func (i *Inventory) ParseDirectives(ctx context.Context, logger *slog.Logger) er
path := filepath.Join(i.inventoryPath, "directives")
files, err := utils.GetFilesInDirectory(path)
if err != nil {
logger.LogAttrs(
iLogger.LogAttrs(
ctx,
slog.LevelError,
"Failed to parse directives",
Expand Down
16 changes: 8 additions & 8 deletions internal/inventory/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (i *Inventory) ParseGroups(ctx context.Context, logger *slog.Logger) error
"inventory": i.inventoryPath,
"component": "groups",
}
logger = logger.With(
iLogger := logger.With(
slog.Group(
"inventory",
slog.String("component", "groups"),
Expand All @@ -52,7 +52,7 @@ func (i *Inventory) ParseGroups(ctx context.Context, logger *slog.Logger) error
path := filepath.Join(i.inventoryPath, "groups")
groupDirs, err := utils.GetFilesInDirectory(path)
if err != nil {
logger.LogAttrs(
iLogger.LogAttrs(
ctx,
slog.LevelError,
"Failed to get files in directory",
Expand All @@ -73,7 +73,7 @@ func (i *Inventory) ParseGroups(ctx context.Context, logger *slog.Logger) error
groupPath := filepath.Join(path, groupDir.Name())
groupFiles, err := utils.GetFilesInDirectory(groupPath)
if err != nil {
logger.LogAttrs(
iLogger.LogAttrs(
ctx,
slog.LevelError,
"Failed to parse group files",
Expand All @@ -100,7 +100,7 @@ func (i *Inventory) ParseGroups(ctx context.Context, logger *slog.Logger) error

for line := range lines {
if line.Err != nil {
logger.LogAttrs(
iLogger.LogAttrs(
ctx,
slog.LevelError,
"Failed to read globs for group",
Expand All @@ -120,7 +120,7 @@ func (i *Inventory) ParseGroups(ctx context.Context, logger *slog.Logger) error

for line := range lines {
if line.Err != nil {
logger.LogAttrs(
iLogger.LogAttrs(
ctx,
slog.LevelError,
"Failed to read regexs for group",
Expand All @@ -140,7 +140,7 @@ func (i *Inventory) ParseGroups(ctx context.Context, logger *slog.Logger) error

for line := range lines {
if line.Err != nil {
logger.LogAttrs(
iLogger.LogAttrs(
ctx,
slog.LevelError,
"Failed to read roles for group",
Expand All @@ -160,7 +160,7 @@ func (i *Inventory) ParseGroups(ctx context.Context, logger *slog.Logger) error

for line := range lines {
if line.Err != nil {
logger.LogAttrs(
iLogger.LogAttrs(
ctx,
slog.LevelError,
"Failed to read modules for group",
Expand All @@ -176,7 +176,7 @@ func (i *Inventory) ParseGroups(ctx context.Context, logger *slog.Logger) error
case "variables":
group.variables = filepath.Join(groupPath, "variables")
default:
logger.LogAttrs(
iLogger.LogAttrs(
ctx,
slog.LevelWarn,
"Skipping file while parsing inventory",
Expand Down
12 changes: 6 additions & 6 deletions internal/inventory/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (i *Inventory) ParseHosts(ctx context.Context, logger *slog.Logger) error {
"inventory": i.inventoryPath,
"component": "hosts",
}
logger = logger.With(
iLogger := logger.With(
slog.Group(
"inventory",
slog.String("component", "hosts"),
Expand All @@ -45,7 +45,7 @@ func (i *Inventory) ParseHosts(ctx context.Context, logger *slog.Logger) error {
path := filepath.Join(i.inventoryPath, "hosts")
hostDirs, err := utils.GetFilesInDirectory(path)
if err != nil {
logger.LogAttrs(
iLogger.LogAttrs(
ctx,
slog.LevelError,
"Failed to get files in directory",
Expand All @@ -66,7 +66,7 @@ func (i *Inventory) ParseHosts(ctx context.Context, logger *slog.Logger) error {
hostPath := filepath.Join(path, hostDir.Name())
hostFiles, err := utils.GetFilesInDirectory(hostPath)
if err != nil {
logger.LogAttrs(
iLogger.LogAttrs(
ctx,
slog.LevelError,
"Failed to parse host files",
Expand All @@ -93,7 +93,7 @@ func (i *Inventory) ParseHosts(ctx context.Context, logger *slog.Logger) error {

for line := range lines {
if line.Err != nil {
logger.LogAttrs(
iLogger.LogAttrs(
ctx,
slog.LevelError,
"Failed to read roles for host",
Expand All @@ -113,7 +113,7 @@ func (i *Inventory) ParseHosts(ctx context.Context, logger *slog.Logger) error {

for line := range lines {
if line.Err != nil {
logger.LogAttrs(
iLogger.LogAttrs(
ctx,
slog.LevelError,
"Failed to read modules for host",
Expand All @@ -129,7 +129,7 @@ func (i *Inventory) ParseHosts(ctx context.Context, logger *slog.Logger) error {
case "variables":
host.variables = filepath.Join(hostPath, "variables")
default:
logger.LogAttrs(
iLogger.LogAttrs(
ctx,
slog.LevelWarn,
"Skipping file while parsing inventory",
Expand Down
8 changes: 4 additions & 4 deletions internal/inventory/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (i *Inventory) ParseModules(ctx context.Context, logger *slog.Logger) error
"inventory": i.inventoryPath,
"component": "modules",
}
logger = logger.With(
iLogger := logger.With(
slog.Group(
"inventory",
slog.String("component", "modules"),
Expand All @@ -47,7 +47,7 @@ func (i *Inventory) ParseModules(ctx context.Context, logger *slog.Logger) error
path := filepath.Join(i.inventoryPath, "modules")
modDirs, err := utils.GetFilesInDirectory(path)
if err != nil {
logger.LogAttrs(
iLogger.LogAttrs(
ctx,
slog.LevelError,
"Failed to get files in directory",
Expand All @@ -68,7 +68,7 @@ func (i *Inventory) ParseModules(ctx context.Context, logger *slog.Logger) error
modPath := filepath.Join(path, modDir.Name())
modFiles, err := utils.GetFilesInDirectory(modPath)
if err != nil {
logger.LogAttrs(
iLogger.LogAttrs(
ctx,
slog.LevelError,
"Failed to parse module files",
Expand Down Expand Up @@ -97,7 +97,7 @@ func (i *Inventory) ParseModules(ctx context.Context, logger *slog.Logger) error
case "requires":
mod.Requires = filepath.Join(modPath, "requires")
default:
logger.LogAttrs(
iLogger.LogAttrs(
ctx,
slog.LevelWarn,
"Skipping file while parsing inventory",
Expand Down
10 changes: 5 additions & 5 deletions internal/inventory/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (i *Inventory) ParseRoles(ctx context.Context, logger *slog.Logger) error {
"inventory": i.inventoryPath,
"component": "roles",
}
logger = logger.With(
iLogger := logger.With(
slog.Group(
"inventory",
slog.String("component", "roles"),
Expand All @@ -40,7 +40,7 @@ func (i *Inventory) ParseRoles(ctx context.Context, logger *slog.Logger) error {
path := filepath.Join(i.inventoryPath, "roles")
roleDirs, err := utils.GetFilesInDirectory(path)
if err != nil {
logger.LogAttrs(
iLogger.LogAttrs(
ctx,
slog.LevelError,
"Failed to parse roles",
Expand All @@ -61,7 +61,7 @@ func (i *Inventory) ParseRoles(ctx context.Context, logger *slog.Logger) error {
rolePath := filepath.Join(path, roleDir.Name())
roleFiles, err := utils.GetFilesInDirectory(rolePath)
if err != nil {
logger.LogAttrs(
iLogger.LogAttrs(
ctx,
slog.LevelError,
"Failed to parse role files",
Expand All @@ -88,7 +88,7 @@ func (i *Inventory) ParseRoles(ctx context.Context, logger *slog.Logger) error {

for line := range lines {
if line.Err != nil {
logger.LogAttrs(
iLogger.LogAttrs(
ctx,
slog.LevelError,
"Failed to read modules in role",
Expand All @@ -106,7 +106,7 @@ func (i *Inventory) ParseRoles(ctx context.Context, logger *slog.Logger) error {
role.modules = mods

default:
logger.LogAttrs(
iLogger.LogAttrs(
ctx,
slog.LevelWarn,
"Skipping file while parsing inventory",
Expand Down
8 changes: 4 additions & 4 deletions internal/manager/directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,18 @@ func (mgr *Manager) RunDirectives(ctx context.Context, logger *slog.Logger) {
logger.InfoContext(ctx, "Directive run started")
defer logger.InfoContext(ctx, "Directive run finished")
for _, d := range mgr.directives {
logger = logger.With(
dLogger := logger.With(
slog.Group(
"directive",
slog.String("id", d.String()),
),
)

logger.InfoContext(ctx, "Directive started")
defer logger.InfoContext(ctx, "Directive finished")
dLogger.InfoContext(ctx, "Directive started")
defer dLogger.InfoContext(ctx, "Directive finished")

if err := mgr.RunDirective(ctx, d); err != nil {
logger.LogAttrs(
dLogger.LogAttrs(
ctx,
slog.LevelError,
"Directive failed",
Expand Down
6 changes: 3 additions & 3 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@ func (mgr *Manager) ReloadAndRunAll(ctx context.Context, logger *slog.Logger, in
ctx = context.WithValue(ctx, contextKeyInventoryPath, inv.GetInventoryPath())
ctx = context.WithValue(ctx, contextKeyHostname, inv.GetHostname())

logger = logger.With(
mLogger := logger.With(
slog.Group(
"manager",
slog.Bool(string(contextKeyEnrolled), enrolled),
slog.String(string(contextKeyRunID), runID.String()),
),
)

mgr.Reload(ctx, logger, inv)
mgr.RunAll(ctx, logger)
mgr.Reload(ctx, mLogger, inv)
mgr.RunAll(ctx, mLogger)
}

// Reload accepts a struct that fulfills the inventory.Store interface and
Expand Down
20 changes: 10 additions & 10 deletions internal/manager/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (mgr *Manager) ReloadModules(ctx context.Context, logger *slog.Logger) {
modGraph := graph.New(moduleHash, graph.Directed(), graph.PreventCycles())
for _, mod := range rawMods {
newMod := Module{m: mod}
logger = logger.With(
modLogger := logger.With(
slog.Group(
"module",
slog.String("id", mod.String()),
Expand All @@ -54,14 +54,14 @@ func (mgr *Manager) ReloadModules(ctx context.Context, logger *slog.Logger) {
// if the module has a variables file set, source it and store
// the expanded variables
if mod.Variables != "" {
newMod.Variables = mgr.ReloadVariables(ctx, logger, []string{mod.Variables}, shell.MakeVariableMap(mgr.hostVariables))
newMod.Variables = mgr.ReloadVariables(ctx, modLogger, []string{mod.Variables}, shell.MakeVariableMap(mgr.hostVariables))
} else {
logger.DebugContext(ctx, "No module variables")
modLogger.DebugContext(ctx, "No module variables")
}

err := modGraph.AddVertex(newMod)
if err != nil {
logger.LogAttrs(
modLogger.LogAttrs(
ctx,
slog.LevelError,
"Failed to add module to DAG",
Expand Down Expand Up @@ -203,7 +203,7 @@ func (mgr *Manager) RunModules(ctx context.Context, logger *slog.Logger) {
}

for _, v := range order {
logger = logger.With(
vLogger := logger.With(
slog.Group(
"module",
slog.String("id", v),
Expand All @@ -212,19 +212,19 @@ func (mgr *Manager) RunModules(ctx context.Context, logger *slog.Logger) {

mod, err := mgr.modules.Vertex(v)
if err != nil {
logger.LogAttrs(
vLogger.LogAttrs(
ctx,
slog.LevelError,
"Failed to retreive module from DAG vertex",
slog.String("err", err.Error()),
)
}

logger.InfoContext(ctx, "Module started")
defer logger.InfoContext(ctx, "Module finished")
vLogger.InfoContext(ctx, "Module started")
defer vLogger.InfoContext(ctx, "Module finished")

if err := mgr.RunModule(ctx, logger, mod); err != nil {
logger.LogAttrs(
if err := mgr.RunModule(ctx, vLogger, mod); err != nil {
vLogger.LogAttrs(
ctx,
slog.LevelError,
"Module failed",
Expand Down
Loading

0 comments on commit 7375f23

Please sign in to comment.