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

Refactor: reduce duplication, condense method implementation #14501

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
46 changes: 18 additions & 28 deletions elastic/datadog_checks/elastic/elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Licensed under Simplified BSD License (see LICENSE)
import re
import time
from collections import defaultdict
from collections import defaultdict, namedtuple
from copy import deepcopy

import requests
Expand All @@ -26,6 +26,13 @@

REGEX = r'(?<!\\)\.' # This regex string is used to traverse through nested dictionaries for JSON responses

DatadogESHealth = namedtuple('DatadogESHealth', ['status', 'reverse_status', 'tag'])
ES_HEALTH_TO_DD_STATUS = {
Comment on lines +29 to +30
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To reviewer: I'm not happy with these names, alternatives welcome!

'green': DatadogESHealth(AgentCheck.OK, AgentCheck.CRITICAL, 'OK'),
'yellow': DatadogESHealth(AgentCheck.WARNING, AgentCheck.WARNING, 'WARN'),
'red': DatadogESHealth(AgentCheck.CRITICAL, AgentCheck.OK, 'ALERT'),
}


class AuthenticationError(requests.exceptions.HTTPError):
"""Authentication Error, unable to reach server"""
Expand Down Expand Up @@ -202,14 +209,8 @@ def _join_url(self, url, admin_forwarder=False):
return urljoin(self._config.url, url)

def _get_index_metrics(self, admin_forwarder, version, base_tags):
cat_url = '/_cat/indices?format=json&bytes=b'
index_url = self._join_url(cat_url, admin_forwarder)
index_resp = self._get_data(index_url)
index_stats_metrics = index_stats_for_version(version)
health_stat = {'green': 0, 'yellow': 1, 'red': 2}
reversed_health_stat = {'red': 0, 'yellow': 1, 'green': 2}
index_resp = self._get_data(self._join_url('/_cat/indices?format=json&bytes=b', admin_forwarder))
for idx in index_resp:
tags = base_tags + ['index_name:' + idx['index']]
# we need to remap metric names because the ones from elastic
# contain dots and that would confuse `_process_metric()` (sic)
index_data = {
Expand All @@ -222,21 +223,20 @@ def _get_index_metrics(self, admin_forwarder, version, base_tags):
'health': idx.get('health'),
}

# Convert the health status value
# Convert Elastic health to Datadog status.
if index_data['health'] is not None:
status = index_data['health'].lower()
index_data['health'] = health_stat[status]
index_data['health_reverse'] = reversed_health_stat[status]
dd_health = ES_HEALTH_TO_DD_STATUS[index_data['health'].lower()]
index_data['health'] = dd_health.status
index_data['health_reverse'] = dd_health.reverse_status
iliakur marked this conversation as resolved.
Show resolved Hide resolved

# Ensure that index_data does not contain None values
for key, value in list(iteritems(index_data)):
if value is None:
del index_data[key]
self.log.debug("The index %s has no metric data for %s", idx['index'], key)

for metric in index_stats_metrics:
# metric description
desc = index_stats_metrics[metric]
tags = base_tags + ['index_name:' + idx['index']]
for metric, desc in iteritems(index_stats_for_version(version)):
self._process_metric(index_data, metric, *desc, tags=tags)

def _get_urls(self, version):
Expand Down Expand Up @@ -400,24 +400,15 @@ def _process_health_data(self, data, version, base_tags, service_check_tags):
self._process_metric(data, metric, *desc, tags=base_tags)

# Process the service check
if cluster_status == 'green':
status = AgentCheck.OK
data['tag'] = "OK"
elif cluster_status == 'yellow':
status = AgentCheck.WARNING
data['tag'] = "WARN"
else:
status = AgentCheck.CRITICAL
data['tag'] = "ALERT"

dd_health = ES_HEALTH_TO_DD_STATUS.get(cluster_status, ES_HEALTH_TO_DD_STATUS['red'])
msg = (
"{tag} on cluster \"{cluster_name}\" "
"| active_shards={active_shards} "
"| initializing_shards={initializing_shards} "
"| relocating_shards={relocating_shards} "
"| unassigned_shards={unassigned_shards} "
"| timed_out={timed_out}".format(
tag=data.get('tag'),
tag=dd_health.tag,
cluster_name=data.get('cluster_name'),
active_shards=data.get('active_shards'),
initializing_shards=data.get('initializing_shards'),
Expand All @@ -426,8 +417,7 @@ def _process_health_data(self, data, version, base_tags, service_check_tags):
timed_out=data.get('timed_out'),
)
)

self.service_check(self.SERVICE_CHECK_CLUSTER_STATUS, status, message=msg, tags=service_check_tags)
self.service_check(self.SERVICE_CHECK_CLUSTER_STATUS, dd_health.status, message=msg, tags=service_check_tags)

def _process_policy_data(self, data, version, base_tags):
for policy, policy_data in iteritems(data):
Expand Down