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

Including time_taken in the statsd metrics #324

Merged
merged 2 commits into from
Jul 4, 2021
Merged
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
38 changes: 22 additions & 16 deletions elastalert/elastalert.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,7 @@ def run_rule(self, rule, endtime, starttime=None):
rule['previous_endtime'] = endtime

time_taken = time.time() - run_start

# Write to ES that we've run this rule against this time period
body = {'rule_name': rule['name'],
'endtime': endtime,
Expand All @@ -986,6 +987,27 @@ def run_rule(self, rule, endtime, starttime=None):
'time_taken': time_taken}
self.writeback('elastalert_status', body)

# Write metrics about the run to statsd
if self.statsd:
Copy link
Contributor Author

@JeffAshton JeffAshton Jul 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved from below to where time_taken was readily available.

image

This feels aligned with the elastalert_status work that run_rule is already doing above. Would also allow for measuring of elastalert-test-rule.

try:
self.statsd.gauge(
'rule.time_taken', time_taken,
tags={"elastalert_instance": self.statsd_instance_tag, "rule_name": rule['name']})
self.statsd.gauge(
'query.hits', self.thread_data.num_hits,
tags={"elastalert_instance": self.statsd_instance_tag, "rule_name": rule['name']})
self.statsd.gauge(
'already_seen.hits', self.thread_data.num_dupes,
tags={"elastalert_instance": self.statsd_instance_tag, "rule_name": rule['name']})
self.statsd.gauge(
'query.matches', num_matches,
tags={"elastalert_instance": self.statsd_instance_tag, "rule_name": rule['name']})
self.statsd.gauge(
'query.alerts_sent', self.thread_data.alerts_sent,
tags={"elastalert_instance": self.statsd_instance_tag, "rule_name": rule['name']})
except BaseException as e:
elastalert_logger.error("unable to send metrics:\n%s" % str(e))

return num_matches

def init_rule(self, new_rule, new=True):
Expand Down Expand Up @@ -1319,22 +1341,6 @@ def handle_rule_execution(self, rule):
self.thread_data.alerts_sent))
rule_duration = seconds(endtime - rule.get('original_starttime'))
elastalert_logger.info("%s range %s" % (rule['name'], rule_duration))
if self.statsd:
try:
self.statsd.gauge(
'query.hits', self.thread_data.num_hits,
tags={"elastalert_instance": self.statsd_instance_tag, "rule_name": rule['name']})
self.statsd.gauge(
'already_seen.hits', self.thread_data.num_dupes,
tags={"elastalert_instance": self.statsd_instance_tag, "rule_name": rule['name']})
self.statsd.gauge(
'query.matches', num_matches,
tags={"elastalert_instance": self.statsd_instance_tag, "rule_name": rule['name']})
self.statsd.gauge(
'query.alerts_sent', self.thread_data.alerts_sent,
tags={"elastalert_instance": self.statsd_instance_tag, "rule_name": rule['name']})
except BaseException as e:
elastalert_logger.error("unable to send metrics:\n%s" % str(e))

self.thread_data.alerts_sent = 0

Expand Down