Skip to content

Commit

Permalink
cloudwatchlogs_log_group_metric_filter: add support for unit and dime…
Browse files Browse the repository at this point in the history
…nsions
  • Loading branch information
jmisset-cb committed Sep 6, 2024
1 parent 70453b9 commit e915a8d
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- cloudwatchlogs_log_group_metric_filter - Add support for ``unit`` and ``dimensions`` options (https://github.com/ansible-collections/amazon.aws/pull/XXX)
60 changes: 47 additions & 13 deletions plugins/modules/cloudwatchlogs_log_group_metric_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,19 @@
default_value:
description:
- The value to emit when a filter pattern does not match a log event.
- The I(default_value) and I(dimensions) options are mutually exclusive.
type: float
unit:
description:
- The unit of the value. The various options are available `here <https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_MetricDatum.html>`.
type: str
dimensions:
description:
- A dimension is a name/value pair that is a part of the identity of a metric.
- You can assign up to 3 dimensions to a metric.
- Dimensions are only supported for JSON or space-delimited metric filters.
- The I(default_value) and I(dimensions) options are mutually exclusive.
type: dict
extends_documentation_fragment:
- amazon.aws.common.modules
- amazon.aws.region.modules
Expand All @@ -74,12 +86,26 @@
metric_name: box_free_space
metric_namespace: fluentd_metrics
metric_value: "$.value"
unit: Bytes
- name: delete metric filter on log group /fluentd/testcase
amazon.aws.cloudwatchlogs_log_group_metric_filter:
log_group_name: /fluentd/testcase
filter_name: BoxFreeStorage
state: absent
- name: set metric filter on log group /fluentd/testcase with dimensions
amazon.aws.cloudwatchlogs_log_group_metric_filter:
log_group_name: /fluentd/testcase
filter_name: BoxFreeStorage
filter_pattern: '{($.value = *) && ($.hostname = *)}'
state: present
metric_transformation:
metric_name: box_free_space
metric_namespace: fluentd_metrics
metric_value: "$.value"
dimensions:
hostname: $.hostname
"""

RETURN = r"""
Expand All @@ -106,31 +132,33 @@ def metricTransformationHandler(metricTransformations, originMetricTransformatio
if originMetricTransformations:
change = False
originMetricTransformations = camel_dict_to_snake_dict(originMetricTransformations)
for item in ["default_value", "metric_name", "metric_namespace", "metric_value"]:
for item in ["default_value", "metric_name", "metric_namespace", "metric_value", "unit", "dimensions"]:
if metricTransformations.get(item) != originMetricTransformations.get(item):
change = True
else:
change = True

defaultValue = metricTransformations.get("default_value")
if isinstance(defaultValue, int) or isinstance(defaultValue, float):
retval = [
{
"metricName": metricTransformations.get("metric_name"),
"metricNamespace": metricTransformations.get("metric_namespace"),
"metricValue": metricTransformations.get("metric_value"),
"defaultValue": defaultValue,
}
]
else:
retval = [
retval = [
{
"metricName": metricTransformations.get("metric_name"),
"metricNamespace": metricTransformations.get("metric_namespace"),
"metricValue": metricTransformations.get("metric_value"),
}
]

# Add optional values
defaultValue = metricTransformations.get("default_value")
if defaultValue is not None:
retval[0]["defaultValue"] = defaultValue

dimensions = metricTransformations.get("dimensions")
if dimensions is not None:
retval[0]["dimensions"] = dimensions

unit = metricTransformations.get("unit")
if unit is not None:
retval[0]["unit"] = unit

return retval, change


Expand All @@ -147,6 +175,8 @@ def main():
metric_namespace=dict(type="str"),
metric_value=dict(type="str"),
default_value=dict(type="float"),
unit=dict(type="str"),
dimensions=dict(type="dict"),
),
),
)
Expand Down Expand Up @@ -184,6 +214,10 @@ def main():
metricTransformation = [camel_dict_to_snake_dict(item) for item in [originMetricTransformations]]

elif state == "present":

if metric_transformation.get('default_value') is not None and metric_transformation.get('dimensions') is not None:
module.fail_json(msg="default_value and dimensions are mutually exclusive.")

metricTransformation, change = metricTransformationHandler(
metricTransformations=metric_transformation, originMetricTransformations=originMetricTransformations
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,95 @@
- out is changed
- out.metric_filters[0].metric_namespace == "made_with_ansible"

- name: Update metric transformation with dimensions on '{{ log_group_name }}'
amazon.aws.cloudwatchlogs_log_group_metric_filter:
log_group_name: "{{ log_group_name }}"
filter_name: "{{ filter_name }}"
filter_pattern: '{ ($.value = *) && ($.hostname = "box")}'
state: present
metric_transformation:
metric_name: box_free_space
metric_namespace: made_with_ansible
metric_value: $.value
dimensions:
hostname: $.hostname
register: out

- name: Assert that metric_filter is configured with dimensions
ansible.builtin.assert:
that:
- out is changed
- out.metric_filters[0].metric_namespace == "made_with_ansible"
- out.metric_filters[0].dimensions.hostname == "$.hostname"

- name: Update metric transformation with unit on '{{ log_group_name }}'
amazon.aws.cloudwatchlogs_log_group_metric_filter:
log_group_name: "{{ log_group_name }}"
filter_name: "{{ filter_name }}"
filter_pattern: '{ ($.value = *) && ($.hostname = "box")}'
state: present
metric_transformation:
metric_name: box_free_space
metric_namespace: made_with_ansible
metric_value: $.value
unit: Bytes
dimensions:
hostname: $.hostname
register: out

- name: Assert that metric_filter is configured with dimensions and unit
ansible.builtin.assert:
that:
- out is changed
- out.metric_filters[0].metric_namespace == "made_with_ansible"
- out.metric_filters[0].dimensions.hostname == "$.hostname"
- out.metric_filters[0].unit == "Bytes"

- name: Idempotency check on metric transformation on '{{ log_group_name }}'
amazon.aws.cloudwatchlogs_log_group_metric_filter:
log_group_name: "{{ log_group_name }}"
filter_name: "{{ filter_name }}"
filter_pattern: '{ ($.value = *) && ($.hostname = "box")}'
state: present
metric_transformation:
metric_name: box_free_space
metric_namespace: made_with_ansible
metric_value: $.value
unit: Bytes
dimensions:
hostname: $.hostname
register: out

- name: Assert that idempotent action with unit and dimensions does not register as changed
ansible.builtin.assert:
that:
- out is not changed
- out.metric_filters[0].metric_namespace == "made_with_ansible"
- out.metric_filters[0].unit == "Bytes"
- out.metric_filters[0].dimensions.hostname == "$.hostname"

- name: Update metric transformation with default_value and dimensions on '{{ log_group_name }}'
amazon.aws.cloudwatchlogs_log_group_metric_filter:
log_group_name: "{{ log_group_name }}"
filter_name: "{{ filter_name }}"
filter_pattern: '{ ($.value = *) && ($.hostname = "box")}'
state: present
metric_transformation:
metric_name: box_free_space
metric_namespace: made_with_ansible
metric_value: $.value
default_value: 3.1415
dimensions:
hostname: $.hostname
register: out
ignore_errors: true

- name: Update metric transformation with default_value and dimensions must fail
ansible.builtin.assert:
that:
- out is failed
- out.msg == "default_value and dimensions are mutually exclusive."

- name: checkmode delete metric filter on '{{ log_group_name }}'
amazon.aws.cloudwatchlogs_log_group_metric_filter:
log_group_name: "{{ log_group_name }}"
Expand Down

0 comments on commit e915a8d

Please sign in to comment.