From bf43a119d276b1e613ca0bb0193d9e8ba05593c2 Mon Sep 17 00:00:00 2001 From: Mariusz Karpiarz Date: Thu, 4 Feb 2021 13:32:11 +0000 Subject: [PATCH] Add the NOTNUMBOOL mutator The "NOTNUMBOOL" mutator returns 1.0 when quantity is 0 and 0.0 otherwise. Change-Id: I16f2b05dcd089b214e18762e1f87d0dbcc2cb7cc --- cloudkitty/collector/__init__.py | 5 +++-- cloudkitty/utils/__init__.py | 3 +++ doc/source/admin/configuration/collector.rst | 18 ++++++++++++++++++ .../notnumbool-mutator-ab056e86f2bc843d.yaml | 6 ++++++ 4 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 releasenotes/notes/notnumbool-mutator-ab056e86f2bc843d.yaml diff --git a/cloudkitty/collector/__init__.py b/cloudkitty/collector/__init__.py index 4759d8ff..19e17891 100644 --- a/cloudkitty/collector/__init__.py +++ b/cloudkitty/collector/__init__.py @@ -89,10 +89,11 @@ METRIC_BASE_SCHEMA = { Required('metadata', default=list): [ All(str, Length(min=1)) ], - # Mutate collected value. May be any of (NONE, NUMBOOL, FLOOR, CEIL). + # Mutate collected value. May be any of: + # (NONE, NUMBOOL, NOTNUMBOOL, FLOOR, CEIL). # Defaults to NONE Required('mutate', default='NONE'): - In(['NONE', 'NUMBOOL', 'FLOOR', 'CEIL']), + In(['NONE', 'NUMBOOL', 'NOTNUMBOOL', 'FLOOR', 'CEIL']), # Collector-specific args. Should be overriden by schema provided for # the given collector Optional('extra_args'): dict, diff --git a/cloudkitty/utils/__init__.py b/cloudkitty/utils/__init__.py index 74aa3cc3..d345d2d9 100644 --- a/cloudkitty/utils/__init__.py +++ b/cloudkitty/utils/__init__.py @@ -257,6 +257,9 @@ def mutate(value, mode='NONE'): if mode == 'NUMBOOL': return float(value != 0.0) + if mode == 'NOTNUMBOOL': + return float(value == 0.0) + if mode == 'FLOOR': return math.floor(value) diff --git a/doc/source/admin/configuration/collector.rst b/doc/source/admin/configuration/collector.rst index 5ea614c1..e77c1c27 100644 --- a/doc/source/admin/configuration/collector.rst +++ b/doc/source/admin/configuration/collector.rst @@ -186,6 +186,9 @@ Four values are accepted for this parameter: * ``NUMBOOL``: If the collected qty equals 0, leave it at 0. Else, set it to 1. +* ``NOTNUMBOOL``: If the collected qty equals 0, set it to 1. Else, set it to + 0. + .. warning:: Quantity mutation is done **after** conversion. Example:: @@ -214,6 +217,21 @@ then defined based on the instance metadata. Example: metadata: - flavor_id +The ``NOTNUMBOOL`` mutator is useful for status-like metrics where 0 denotes +the billable state. For example the following Prometheus metric has value of 0 +when the instance is in ACTIVE state but 4 if the instance is in ERROR state: + +.. code-block:: yaml + + metrics: + openstack_nova_server_status: + unit: instance + mutate: NOTNUMBOOL + groupby: + - id + metadata: + - flavor_id + Display name ~~~~~~~~~~~~ diff --git a/releasenotes/notes/notnumbool-mutator-ab056e86f2bc843d.yaml b/releasenotes/notes/notnumbool-mutator-ab056e86f2bc843d.yaml new file mode 100644 index 00000000..69cbc6d8 --- /dev/null +++ b/releasenotes/notes/notnumbool-mutator-ab056e86f2bc843d.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + The new "NOTNUMBOOL" mutator has been added. This mutator is, essentially, + an opposite of the "NUMBOOL" mutator as it returns 1.0 when quantity is 0 + and 0.0 otherwise.