Skip to content

Commit

Permalink
support histograms, fix count submission (#1616)
Browse files Browse the repository at this point in the history
* fix counts

* document new param

* support histograms

* update metadata

* style

* fix

* add tests
  • Loading branch information
ofek authored May 30, 2018
1 parent 8baea47 commit b2a9e4c
Show file tree
Hide file tree
Showing 6 changed files with 553 additions and 299 deletions.
2 changes: 2 additions & 0 deletions envoy/conf.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ instances:
# supply a list of tags. The admin endpoint must be accessible.
# https://www.envoyproxy.io/docs/envoy/latest/operations/admin

# Add a `?usedonly` on the end if you wish to ignore
# unused metrics instead of reporting them as `0`.
- stats_url: http://localhost:80/stats

# tags:
Expand Down
29 changes: 14 additions & 15 deletions envoy/datadog_checks/envoy/envoy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from datadog_checks.checks import AgentCheck

from .errors import UnknownMetric, UnknownTags
from .parser import parse_metric
from .parser import parse_histogram, parse_metric


class Envoy(AgentCheck):
Expand Down Expand Up @@ -38,7 +38,7 @@ def check(self, instance):
timeout = int(instance.get('timeout', 20))

try:
request = requests.get(
response = requests.get(
stats_url, auth=auth, verify=verify_ssl, proxies=proxies, timeout=timeout
)
except requests.exceptions.Timeout:
Expand All @@ -52,29 +52,21 @@ def check(self, instance):
self.log.exception(msg)
return

if request.status_code != 200:
msg = 'Envoy endpoint `{}` responded with HTTP status code {}'.format(stats_url, request.status_code)
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)
self.log.warning(msg)
return

# Avoid repeated global lookups.
get_method = getattr

for line in request.content.decode().splitlines():
for line in response.content.decode().splitlines():
try:
envoy_metric, value = line.split(': ')
except ValueError:
continue

try:
value = int(value)
except (ValueError, TypeError):
self.log.debug(
'Unable to parse value `{}` as an integer for metric `{}`'.format(value, envoy_metric)
)
continue

try:
metric, tags, method = parse_metric(envoy_metric)
except UnknownMetric:
Expand All @@ -90,7 +82,14 @@ def check(self, instance):
self.unknown_tags[tag] += 1
continue

tags.extend(custom_tags)
get_method(self, method)(metric, value, tags=tags)
try:
value = int(value)
tags.extend(custom_tags)
get_method(self, method)(metric, value, tags=tags)

# If the value isn't an integer assume it's pre-computed histogram data.
except (ValueError, TypeError):
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)
Loading

0 comments on commit b2a9e4c

Please sign in to comment.