Skip to content

Fix missing logger argument in cpu_stats error message - #213

Merged
ualex73 merged 1 commit into
ualex73:masterfrom
weirdtangent:fix-cpu-stats-logger-args
Aug 18, 2026
Merged

Fix missing logger argument in cpu_stats error message#213
ualex73 merged 1 commit into
ualex73:masterfrom
weirdtangent:fix-cpu-stats-logger-args

Conversation

@weirdtangent

@weirdtangent weirdtangent commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Problem

In helpers.py, the except KeyError handler for CPU stats logs the raw payload with a three-placeholder format string but passes only two arguments:

_LOGGER.error(
    "[%s] %s: Raw 'cpu_stats' %s", self._name, raw["cpu_stats"]
)

self._instance is missing. Every sibling _LOGGER call in the same block passes (self._instance, self._name, ...), so this looks like a simple omission.

Effect

Because the format string can't be rendered, logging raises TypeError: not enough arguments for format string while emitting the record. Instead of one diagnostic line, the log gets --- Logging error ---, the TypeError traceback, a During handling of the above exception... chain, and a full Call stack: unwound back to sys.exit(main()) — roughly 50 lines per occurrence.

The practical impact is that the message intended to help diagnose a missing cpu_stats key is itself unreadable, and the failure is noisiest exactly when something is already wrong.

This is reachable in normal operation: Docker can return a cpu_stats block without system_cpu_usage for a container that has just started or restarted, which triggers the KeyError path.

Fix

Pass self._instance, matching the surrounding calls. No behaviour change beyond the message rendering correctly.

Testing

Verified in production. The KeyError branch has executed several times since the patch was deployed, and now emits exactly the two intended lines:

[<instance>] <container>: Cannot determine CPU usage for container ('system_cpu_usage')
[<instance>] <container>: Raw 'cpu_stats' {'cpu_usage': {'total_usage': 0, 'usage_in_kernelmode': 0, 'usage_in_usermode': 0}, 'throttling_data': {'periods': 0, 'throttled_periods': 0, 'throttled_time': 0}}

Before the fix, the same branch produced --- Logging error ---, a TypeError: not enough arguments for format string, a During handling of the above exception, another exception occurred: chain, and a full Call stack: unwound to sys.exit(main()) — roughly 50 lines in place of those two.

The trigger in my case is a monitored container that has just restarted: Docker returns a cpu_stats block with zeroed cpu_usage and no system_cpu_usage key, as shown above.

🤖 Generated with Claude Code

The _LOGGER.error() call logging the raw cpu_stats payload uses a
three-placeholder format string but passes only two arguments, omitting
self._instance. Every sibling call in this except block passes
(self._instance, self._name, ...).

When the KeyError path is hit, logging raises
'TypeError: not enough arguments for format string' internally, so
instead of one diagnostic line the user gets '--- Logging error ---'
plus two tracebacks and a full call stack.
@ualex73
ualex73 merged commit c3f2368 into ualex73:master Aug 18, 2026
2 checks passed
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