Skip to content

Commit f9e8fa9

Browse files
authored
Merge pull request #1647 from dennis-trapp/compound-formatted
add _formatted to metric agg
2 parents e65fb2a + 83fc93e commit f9e8fa9

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Fix `schema.yaml` to support Kibana 8.17 - [#1631](https://github.com/jertel/elastalert2/pull/1631) - @vpiserchia
1313
- [Helm] Clarified documentation around rootRulesFolder - @jertel
1414
- [IRIS] Fix `iris.py` to overcome a description overwriting bug - [#1643](https://github.com/jertel/elastalert2/pull/1643) - @jmolletAMNH
15+
- Add `metric_<metric_key>_formatted` and `metric_agg_value_formatted` to metric aggregation when using compound query keys - [#1647](https://github.com/jertel/elastalert2/pull/1647) - @dennis-trapp
1516

1617
# 2.23.0
1718

elastalert/ruletypes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,10 @@ def check_matches_recursive(self, timestamp, query_key, aggregation_data, compou
11461146
# add compound key to payload to allow alerts to trigger for every unique occurence
11471147
compound_value = [match_data[key] for key in self.rules['compound_query_key']]
11481148
match_data[self.rules['query_key']] = ",".join([str(value) for value in compound_value])
1149+
metric_format_string = self.rules.get('metric_format_string', None)
1150+
if metric_format_string:
1151+
match_data[self.metric_key +'_formatted'] = format_string(metric_format_string, metric_val)
1152+
match_data['metric_agg_value_formatted'] = format_string(metric_format_string, metric_val)
11491153
self.add_match(match_data)
11501154

11511155
def crossed_thresholds(self, metric_value):

tests/rules_test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,38 @@ def test_metric_aggregation_complex_query_key():
12561256
assert rule.matches[1]['sub_qk'] == 'sub_qk_val2'
12571257

12581258

1259+
def test_metric_aggregation_complex_query_key_formatted():
1260+
rules = {'buffer_time': datetime.timedelta(minutes=5),
1261+
'timestamp_field': '@timestamp',
1262+
'metric_agg_type': 'avg',
1263+
'metric_agg_key': 'some_val',
1264+
'metric_format_string': '{:.2f}',
1265+
'compound_query_key': ['qk', 'sub_qk'],
1266+
'query_key': 'qk,sub_qk',
1267+
'max_threshold': 0.8}
1268+
1269+
query = {"bucket_aggs": {"buckets": [
1270+
{"metric_some_val_avg": {"value": 1000.9133333}, "key": "sub_qk_val1"},
1271+
{"metric_some_val_avg": {"value": 10.9}, "key": "sub_qk_val2"},
1272+
{"metric_some_val_avg": {"value": 0.89999}, "key": "sub_qk_val3"}]
1273+
}, "key": "qk_val"}
1274+
1275+
rule = MetricAggregationRule(rules)
1276+
rule.check_matches(datetime.datetime.now(), 'qk_val', query)
1277+
assert len(rule.matches) == 3
1278+
assert rule.matches[0]['qk'] == 'qk_val'
1279+
assert rule.matches[1]['qk'] == 'qk_val'
1280+
assert rule.matches[0]['sub_qk'] == 'sub_qk_val1'
1281+
assert rule.matches[1]['sub_qk'] == 'sub_qk_val2'
1282+
assert rule.matches[1]['sub_qk'] == 'sub_qk_val2'
1283+
assert rule.matches[0]['metric_agg_value_formatted'] == '1000.91'
1284+
assert rule.matches[0]["metric_some_val_avg_formatted"] == "1000.91"
1285+
assert rule.matches[1]['metric_agg_value_formatted'] == '10.90'
1286+
assert rule.matches[1]["metric_some_val_avg_formatted"] == "10.90"
1287+
assert rule.matches[2]['metric_agg_value_formatted'] == '0.90'
1288+
assert rule.matches[2]["metric_some_val_avg_formatted"] == "0.90"
1289+
1290+
12591291
def test_metric_aggregation_complex_query_key_bucket_interval():
12601292
rules = {'buffer_time': datetime.timedelta(minutes=5),
12611293
'timestamp_field': '@timestamp',

0 commit comments

Comments
 (0)