diff options
author | Rafael Weingärtner <rafael@apache.org> | 2019-11-14 14:47:19 -0300 |
---|---|---|
committer | Rafael Weingärtner <rafael@apache.org> | 2019-11-26 11:11:30 -0300 |
commit | b422e9dd4b5ae2254416e59fb849e67f6447d23b (patch) | |
tree | d9367df376559536e26685b21ae14882e2499465 /ceilometer/polling | |
parent | 9ed26c570a2603553bdf8304fe130a76ae66ae89 (diff) | |
download | ceilometer-b422e9dd4b5ae2254416e59fb849e67f6447d23b.tar.gz |
Dynamic pollsters: enable operation on attributes
This PR enables the use of python operation to transform the attributes that
are extracted in the JSON response that the Dynamic pollster handle. By
enabling operators to define transformations on the fly, we can provide even
more flexibility for the dynamic pollsters' system.
One example of use case is the RadosGW that uses <project_id$project_id> as the
username. With this implementation, one can create configurations in the
dynamic pollster to clean that variable. It is as simple as defining
`resource_id_attribute: "user | value.split('$')[0].strip()"`
The operations are separated by `|` symbol. The first element of the expression
is the key to be retrieved from the JSON object. The other elements are
operations that can be applied to the `value` variable. The value variable is
the variable we use to hold the data.
Depends-On: https://review.opendev.org/#/c/693088/
Change-Id: I9ee209410491b3f04259e1a5c62ac20461070ae1
Signed-off-by: Rafael Weingärtner <rafael@apache.org>
Diffstat (limited to 'ceilometer/polling')
-rw-r--r-- | ceilometer/polling/dynamic_pollster.py | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/ceilometer/polling/dynamic_pollster.py b/ceilometer/polling/dynamic_pollster.py index f501c1b5..6d4662a6 100644 --- a/ceilometer/polling/dynamic_pollster.py +++ b/ceilometer/polling/dynamic_pollster.py @@ -264,5 +264,28 @@ class DynamicPollster(plugin_base.PollsterBase): def retrieve_attribute_nested_value(self, json_object, attribute_key): LOG.debug("Retrieving the nested keys [%s] from [%s].", attribute_key, json_object) + + keys_and_operations = attribute_key.split("|") + attribute_key = keys_and_operations[0].strip() + nested_keys = attribute_key.split(".") - return reduce(operator.getitem, nested_keys, json_object) + value = reduce(operator.getitem, nested_keys, json_object) + + # We have operations to be executed against the value extracted + if len(keys_and_operations) > 1: + for operation in keys_and_operations[1::]: + # The operation must be performed onto the 'value' variable + if 'value' not in operation: + raise declarative.DynamicPollsterDefinitionException( + "The attribute field operation [%s] must use the [" + "value] variable." % operation, + self.pollster_definitions) + + LOG.debug("Executing operation [%s] against value[%s].", + operation, value) + + value = eval(operation.strip()) + + LOG.debug("Result [%s] of operation [%s] against value [%s].", + value, operation) + return value |