Skip to content

feat(agent): add structured zap logging to metal agent#164

Merged
Defilan merged 4 commits intodefilantech:mainfrom
matiasinsaurralde:metal-agent-logging
Feb 26, 2026
Merged

feat(agent): add structured zap logging to metal agent#164
Defilan merged 4 commits intodefilantech:mainfrom
matiasinsaurralde:metal-agent-logging

Conversation

@matiasinsaurralde
Copy link
Contributor

@matiasinsaurralde matiasinsaurralde commented Feb 21, 2026

What

  • Replace Metal Agent fmt.Printf logging with structured zap logs in startup, runtime, and shutdown paths.
  • Add --log-level parsing and production zap logger initialization in cmd/metal-agent/main.go.
  • Inject a shared sugared logger into agent subsystems (watcher, executor, registry) with no-op fallbacks for optional constructor usage.
  • Add contextual fields (component, subsystem, namespace/name/port/pid/error) to improve observability and troubleshooting.

Why

Metal agent logs were previously plain text and inconsistent across components. This change standardizes logging, enables configurable verbosity, and improves operational debugging with structured context.

How

  • Introduced a production zap logger in cmd/metal-agent/main.go via a small newLogger(level) helper.
  • Added parseLogLevel to map --log-level (debug|info|warn|error) into zapcore levels, defaulting to info.
  • Replaced user-facing fmt.Printf startup/runtime/shutdown logs with structured SugaredLogger calls (Infow, Warnw, Errorw, Debugw) and contextual fields.
  • Extended MetalAgentConfig to accept an injected logger and propagated scoped child loggers into watcher, executor, and registry (component/subsystem fields).
  • Kept constructors backward-compatible by allowing optional logger injection with a zap.NewNop().Sugar() fallback when none is provided.
  • Added a TODO in cmd/metal-agent/main.go to wire this logger into controller-runtime (ctrl.SetLogger) so controller-runtime/client logs use the same config.

Checklist

  • Tests added/updated
  • make test passes locally
  • make lint passes locally
  • Commit messages follow conventional commits
  • All commits are signed off (git commit -s) per DCO
  • Documentation updated (if user-facing change)

Copy link
Member

@Defilan Defilan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, Matias! Structured logging for the Metal Agent is overdue. Really appreciate the fix. A couple of things before merge:

  1. Remove LogLevel from MetalAgentConfig as it's never read inside the agent package (level is consumed in main.go before injection)
  2. Please add a table-driven test for parseLogLevel()
  3. Optional: consider making logger a required param instead of variadic. Update tests to pass zap.NewNop().Sugar()

We'll squash-merge so the lint-fix commit folds in automatically.

Signed-off-by: Matías Insaurralde <matias@insaurral.de>
Signed-off-by: Matías Insaurralde <matias@insaurral.de>
Signed-off-by: Matías Insaurralde <matias@insaurral.de>
Signed-off-by: Matías Insaurralde <matias@insaurral.de>
Copy link
Member

@Defilan Defilan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@Defilan Defilan merged commit e9d143c into defilantech:main Feb 26, 2026
15 checks passed
This was referenced Feb 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants