Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added handling of evaluation errors for 'if' conditions #434

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,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