Skip to content

Commit

Permalink
Update check signature (#8258)
Browse files Browse the repository at this point in the history
  • Loading branch information
coignetp authored Dec 29, 2020
1 parent be7b071 commit ff05e14
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
47 changes: 21 additions & 26 deletions envoy/datadog_checks/envoy/envoy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import requests
from six.moves.urllib.parse import urljoin

from datadog_checks.base import AgentCheck
from datadog_checks.base import AgentCheck, ConfigurationError

from .errors import UnknownMetric, UnknownTags
from .parser import parse_histogram, parse_metric
Expand All @@ -24,6 +24,13 @@ def __init__(self, name, init_config, instances):
self.unknown_metrics = defaultdict(int)
self.unknown_tags = defaultdict(int)

self.custom_tags = self.instance.get('tags', [])
self.caching_metrics = self.instance.get('cache_metrics', True)

self.stats_url = self.instance.get('stats_url')
if self.stats_url is None:
raise ConfigurationError('Envoy configuration setting `stats_url` is required')

included_metrics = set(
re.sub(r'^envoy\\?\.', '', s, 1)
for s in self.instance.get('included_metrics', self.instance.get('metric_whitelist', []))
Expand All @@ -43,38 +50,26 @@ def __init__(self, name, init_config, instances):

self.caching_metrics = None

def check(self, instance):
custom_tags = instance.get('tags', [])
try:
stats_url = instance['stats_url']
except KeyError:
msg = 'Envoy configuration setting `stats_url` is required'
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL, message=msg, tags=custom_tags)
self.log.error(msg)
return

if self.caching_metrics is None:
self.caching_metrics = instance.get('cache_metrics', True)

self._collect_metadata(stats_url)
def check(self, _):
self._collect_metadata()

try:
response = self.http.get(stats_url)
response = self.http.get(self.stats_url)
except requests.exceptions.Timeout:
timeout = self.http.options['timeout']
msg = 'Envoy endpoint `{}` timed out after {} seconds'.format(stats_url, timeout)
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL, message=msg, tags=custom_tags)
msg = 'Envoy endpoint `{}` timed out after {} seconds'.format(self.stats_url, timeout)
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL, message=msg, tags=self.custom_tags)
self.log.exception(msg)
return
except (requests.exceptions.RequestException, requests.exceptions.ConnectionError):
msg = 'Error accessing Envoy endpoint `{}`'.format(stats_url)
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL, message=msg, tags=custom_tags)
msg = 'Error accessing Envoy endpoint `{}`'.format(self.stats_url)
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL, message=msg, tags=self.custom_tags)
self.log.exception(msg)
return

if response.status_code != 200:
msg = 'Envoy endpoint `{}` responded with HTTP status code {}'.format(stats_url, response.status_code)
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL, message=msg, tags=custom_tags)
msg = 'Envoy endpoint `{}` responded with HTTP status code {}'.format(self.stats_url, response.status_code)
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL, message=msg, tags=self.custom_tags)
self.log.warning(msg)
return

Expand Down Expand Up @@ -105,7 +100,7 @@ def check(self, instance):
self.unknown_tags[tag] += 1
continue

tags.extend(custom_tags)
tags.extend(self.custom_tags)

try:
value = int(value)
Expand All @@ -116,7 +111,7 @@ def check(self, instance):
for metric, value in parse_histogram(metric, value):
self.gauge(metric, value, tags=tags)

self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.OK, tags=custom_tags)
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.OK, tags=self.custom_tags)

def included_metrics(self, metric):
if self.caching_metrics:
Expand Down Expand Up @@ -147,9 +142,9 @@ def included_metrics(self, metric):
return True

@AgentCheck.metadata_entrypoint
def _collect_metadata(self, stats_url):
def _collect_metadata(self):
# From http://domain/thing/stats to http://domain/thing/server_info
server_info_url = urljoin(stats_url, 'server_info')
server_info_url = urljoin(self.stats_url, 'server_info')
raw_version = None

try:
Expand Down
12 changes: 6 additions & 6 deletions envoy/tests/test_envoy.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,23 +158,23 @@ def test_metadata(datadog_agent):
check.log = mock.MagicMock()

with mock.patch('requests.get', side_effect=requests.exceptions.Timeout()):
check._collect_metadata(instance['stats_url'])
check._collect_metadata()
datadog_agent.assert_metadata_count(0)
check.log.warning.assert_called_with(
'Envoy endpoint `%s` timed out after %s seconds', 'http://localhost:8001/server_info', (10.0, 10.0)
)

datadog_agent.reset()
with mock.patch('requests.get', side_effect=IndexError()):
check._collect_metadata(instance['stats_url'])
check._collect_metadata()
datadog_agent.assert_metadata_count(0)
check.log.warning.assert_called_with(
'Error collecting Envoy version with url=`%s`. Error: %s', 'http://localhost:8001/server_info', ''
)

datadog_agent.reset()
with mock.patch('requests.get', side_effect=requests.exceptions.RequestException('Req Exception')):
check._collect_metadata(instance['stats_url'])
check._collect_metadata()
datadog_agent.assert_metadata_count(0)
check.log.warning.assert_called_with(
'Error collecting Envoy version with url=`%s`. Error: %s',
Expand All @@ -184,7 +184,7 @@ def test_metadata(datadog_agent):

datadog_agent.reset()
with mock.patch('requests.get', return_value=response('server_info')):
check._collect_metadata(instance['stats_url'])
check._collect_metadata()

major, minor, patch = ENVOY_VERSION.split('.')
version_metadata = {
Expand All @@ -200,7 +200,7 @@ def test_metadata(datadog_agent):

datadog_agent.reset()
with mock.patch('requests.get', return_value=response('server_info_before_1_9')):
check._collect_metadata(instance['stats_url'])
check._collect_metadata()

expected_version = '1.8.0'
major, minor, patch = expected_version.split('.')
Expand All @@ -217,7 +217,7 @@ def test_metadata(datadog_agent):

datadog_agent.reset()
with mock.patch('requests.get', return_value=response('server_info_invalid')):
check._collect_metadata(instance['stats_url'])
check._collect_metadata()

datadog_agent.assert_metadata('test:123', {})
datadog_agent.assert_metadata_count(0)
Expand Down

0 comments on commit ff05e14

Please sign in to comment.