Skip to content

Commit

Permalink
Added handling of evaluation errors for 'if' conditions
Browse files Browse the repository at this point in the history
Details:

* The 'if' conditions in the default metric definition file should not
  cause issues, but because the file can be modified by users, the current
  approach of ending with a Python traceback has been improved to now
  handle possible exceptions when evaluating the expression.

Signed-off-by: Andreas Maier <[email protected]>
  • Loading branch information
andy-maier committed Nov 28, 2023
1 parent 7fbf189 commit bfd6c22
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
3 changes: 3 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ Released: not yet
* Added adapter name and port index as two new labels 'adapter' and 'port' to
metric group 'partition-attached-network-interface'. (issue #347)

* Added handling of evaluation errors for 'if' conditions in metric definition
files.

**Cleanup:**

**Known issues:**
Expand Down
15 changes: 11 additions & 4 deletions zhmc_prometheus_exporter/zhmc_prometheus_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,11 +516,13 @@ def eval_condition(condition, hmc_version, se_version):
hmc_version (string): Expression variable: HMC version as a string.
se_version (string): Expression variable: SE/CPC version as a string.
May be None.
Returns:
bool: Evaluated condition
"""
org_condition = condition
hmc_version = split_version(hmc_version, 3)
if se_version:
se_version = split_version(se_version, 3)
Expand All @@ -530,10 +532,15 @@ def eval_condition(condition, hmc_version, se_version):
break
condition = "{}{}{}".format(
m.group(1), split_version(m.group(2), 3), m.group(3))
# pylint: disable=eval-used
condition = eval(condition, None,
dict(hmc_version=hmc_version, se_version=se_version))
return condition
try:
# pylint: disable=eval-used
return eval(condition, None,
dict(hmc_version=hmc_version, se_version=se_version))
except Exception as exc: # pylint: disable=broad-exception-caught
warnings.warn("Ignoring item because its condition {!r} does not "
"properly evaluate: {}: {}".
format(org_condition, exc.__class__.__name__, exc))
return False


# Metrics context creation & deletion and retrieval derived from
Expand Down

0 comments on commit bfd6c22

Please sign in to comment.