-
Notifications
You must be signed in to change notification settings - Fork 297
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
Reducing the unused calls to elasticsearch_client #303
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -859,6 +859,14 @@ def enhance_filter(self, rule): | |||
filters.append({'query': query_str_filter}) | ||||
elastalert_logger.debug("Enhanced filter with {} terms: {}".format(listname, str(query_str_filter))) | ||||
|
||||
def get_elasticsearch_client(self, rule): | ||||
key = rule['name'] | ||||
es_client = self.es_clients.get(key) | ||||
if es_client is None: | ||||
es_client = elasticsearch_client(rule) | ||||
self.es_clients[key] = es_client | ||||
return es_client | ||||
|
||||
def run_rule(self, rule, endtime, starttime=None): | ||||
""" Run a rule for a given time period, including querying and alerting on results. | ||||
|
||||
|
@@ -868,7 +876,7 @@ def run_rule(self, rule, endtime, starttime=None): | |||
:return: The number of matches that the rule produced. | ||||
""" | ||||
run_start = time.time() | ||||
self.thread_data.current_es = self.es_clients.setdefault(rule['name'], elasticsearch_client(rule)) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I see where this will help improve efficiency. I think a unit test that proves the same elasticsearch_client instance is returned from the new getter function, when called twice with the same rule, would be good unit coverage. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is especially bad for us running in AWS because inside elastalert2/elastalert/auth.py Line 59 in d455e55
|
||||
self.thread_data.current_es = self.get_elasticsearch_client(rule) | ||||
|
||||
# If there are pending aggregate matches, try processing them | ||||
for x in range(len(rule['agg_matches'])): | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be condensed into a single line,
Not sure what your stylistic preference is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer readability over condensed code, so what you have looks fine.