Repository navigation
Report unusable rule literals as rule and value errors - #92
Open
JingHao-Leon wants to merge 2 commits into
Open
JingHao-Leon wants to merge 2 commits into
JingHao-Leon wants to merge 2 commits into
Conversation
An unsupported size unit and a literal that does not fit the measured value's type both escape as an unhandled ValueError today, so the historical runtime CLI answers with the reserved unexpected-exception status instead of a rule or value error.
An unsupported size unit reached bitmath.parse_string() unguarded and raised a raw ValueError, so the historical runtime CLI answered with the reserved unexpected-exception status 150 and a traceback. Resolve a sized literal through one helper that turns the parser's error into RuleSyntaxError, which the CLI already maps to the documented rule syntax status 140. The evaluator only guarded TypeError when coercing a literal to the measured value's type, so a decimal or non-numeric literal against a byte-valued resource also escaped as a ValueError; guard ValueError as well and report the already intended value error 110.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What broke
A monitoring rule whose literal cannot be used left the rule engine as an unhandled
ValueError. The historical runtime CLI answered with exit status 150 — the status reserved for "unexpected exception" — and logged a full Python traceback, instead of reporting the rule error with its documented status.Two literals were affected:
100 mb(the size parser expectsMB,MiB,kB,B, …);mem_usage, or a word against a percentage resource.mem_usage,mem_limit,io_read,io_write,net_rx,net_txandpidare measured as integers, so any decimal or word literal compared against them raisedValueErrorwhile the condition was evaluated. The same rule stored in the configuration file was already reported cleanly, so only the command line path was affected.This change makes the unusable literal a rule syntax error (140) when the expression is parsed, and a value error (110) when the literal does not fit the measured value's type.
Root cause
monit_docker/adapters/rules.py:95(also:92,:50and:47): the sized literal was handed tobitmath.parse_string()without handling theValueErrorit raises for an unsupported unit, so the failure left the parser as a raw exception.monit_docker/core/rules.py:69(and:71): the expected value was coerced withtype(actual)(condition.value), but onlyTypeErrorwas caught at:72. A literal that cannot be converted raisedValueError, which was not handled, so the failure left the evaluator as a raw exception.How verified
python -m unittest discover -s tests -v(Python 3.13.9, repository dependencies installed in a local virtual environment).Ran 407 tests in 39.960s / FAILED (failures=1, errors=5, skipped=18).Ran 413 tests in 40.045s / FAILED (failures=1, errors=5, skipped=18).sed -iand five tests that need Linux/proc, GNUstat -f -cor a TTY. Six new tests are added (four intests/test_rule_literals.py, two intests/test_monit_docker.py).python .github/scripts/check-test-collection.py --runner unittest tests.Collection verified: 413 declarations, 413 cases (unittest).python -m unittest tests.test_rule_literals tests.test_monit_dockerreportsFAILED (failures=2, errors=7).monit-docker monit --cmd-if 'mem_usage > 100 mb ? restart'exits140and printsunsupported unit in expression value: '100 mb'without a traceback.monit-docker monit --cmd-if 'mem_usage > 1.5 ? restart'exits110without a traceback.150with a Python traceback.CI
The pull request's workflow is waiting for a maintainer to approve it: the
Docker Hubrun(
dockerhub.yml) finished withaction_requiredand 0 jobs, so the test suite has not run onGitHub for this pull request. The numbers above come from running the same suite locally against
this branch.
The other workflows are not triggered by this diff:
grafana-screenshots.ymlandui-acceptance.ymlare path-filtered to files this change does not touch, andtests.ymlandupgrades.ymlareworkflow_call-only — they run as jobs ofdockerhub.ymlonce it isapproved.
Fixes #91
This change was prepared with the assistance of an AI coding agent.