Skip to content

Commit

Permalink
Adhere to code style (DataDog#404)
Browse files Browse the repository at this point in the history
* Adhere to code style

* fix

* better

* .
  • Loading branch information
ofek authored Apr 12, 2019
1 parent cfeb0ac commit 0c0d1fb
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 54 deletions.
5 changes: 1 addition & 4 deletions riak_repl/datadog_checks/riak_repl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
from .__about__ import __version__
from .riak_repl import RiakReplCheck

__all__ = [
'__version__',
'RiakReplCheck'
]
__all__ = ['__version__', 'RiakReplCheck']
38 changes: 13 additions & 25 deletions riak_repl/datadog_checks/riak_repl/riak_repl.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import requests
import unicodedata

import requests
from six import iteritems

from datadog_checks.base import AgentCheck
Expand Down Expand Up @@ -33,15 +33,10 @@ class RiakReplCheck(AgentCheck):
"realtime_send_kbps",
"realtime_recv_kbps",
"fullsync_send_kbps",
"fullsync_recv_kbps"
"fullsync_recv_kbps",
]

REALTIME_QUEUE_STATS = [
"percent_bytes_used",
"bytes",
"max_bytes",
"overload_drops"
]
REALTIME_QUEUE_STATS = ["percent_bytes_used", "bytes", "max_bytes", "overload_drops"]

FULLSYNC_COORDINATOR = [
"queued",
Expand All @@ -54,7 +49,7 @@ class RiakReplCheck(AgentCheck):
"soft_retry_exits",
"busy_nodes",
"fullsyncs_completed",
"last_fullsync_duration"
"last_fullsync_duration",
]

def check(self, instance):
Expand All @@ -69,20 +64,17 @@ def check(self, instance):
try:
r = requests.get(url, timeout=timeout)
except requests.exceptions.Timeout:
raise CheckException('URL: {0} timed out after {1} \
seconds.'.format(url, timeout))
raise CheckException('URL: {} timed out after {} seconds.'.format(url, timeout))
except requests.exceptions.ConnectionError as e:
raise CheckException(e)

if r.status_code != 200:
raise CheckException('Invalid Status Code, {0} returned a status \
of {1}.'.format(url, r.status_code))
raise CheckException('Invalid Status Code, {} returned a status of {}.'.format(url, r.status_code))

try:
stats = json.loads(r.text)
except ValueError:
raise CheckException('{0} returned an unserializable \
payload'.format(url))
raise CheckException('{} returned an unserializable payload'.format(url))

for key, val in iteritems(stats):
if key in self.REPL_STATS:
Expand All @@ -91,32 +83,28 @@ def check(self, instance):
if stats['realtime_enabled'] is not None:
for key, val in iteritems(stats['realtime_queue_stats']):
if key in self.REALTIME_QUEUE_STATS:
self.safe_submit_metric("riak_repl.realtime_queue_stats."
+ key, val, tags=tags)
self.safe_submit_metric("riak_repl.realtime_queue_stats." + key, val, tags=tags)

for c in stats['connected_clusters']:
cluster = c.replace("-", "_")
if c not in stats['fullsync_coordinator']:
continue
for key, val in iteritems(stats['fullsync_coordinator'][c]):
if key in self.FULLSYNC_COORDINATOR:
self.safe_submit_metric("riak_repl.fullsync_coordinator."
+ cluster + "." + key,
val, tags=tags)
self.safe_submit_metric("riak_repl.fullsync_coordinator." + cluster + "." + key, val, tags=tags)

def safe_submit_metric(self, name, value, tags=None):
tags = [] if tags is None else tags
try:
self.gauge(name, float(value), tags=tags)
return
except ValueError:
self.log.debug("metric name {0} cannot be converted to a \
float: {1}".format(name, value))
self.log.debug("metric name {} cannot be converted to a float: {}".format(name, value))

try:
self.gauge(name, unicodedata.numeric(value), tags=tags)
return
except (TypeError, ValueError):
self.log.debug("metric name {0} cannot be converted to a \
float even using unicode tools:\
{1}".format(name, value))
self.log.debug(
"metric name {} cannot be converted to a float even using unicode tools: {}".format(name, value)
)
7 changes: 0 additions & 7 deletions riak_repl/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@
long_description=long_description,
long_description_content_type='text/markdown',
keywords='datadog agent riak_repl check',

# The project's main homepage.
url='https://github.com/DataDog/integrations-extras',

# Author details
author='Britt Treece',
author_email='[email protected]',

# License
license='BSD-3-Clause',

# See https://pypi.org/classifiers
classifiers=[
'Development Status :: 5 - Production/Stable',
Expand All @@ -47,13 +43,10 @@
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
],

# The package we're going to ship
packages=['datadog_checks', 'datadog_checks.riak_repl'],

# Run-time dependencies
install_requires=[CHECKS_BASE_REQ],

# Extra files to ship with the wheel package
include_package_data=True,
)
11 changes: 6 additions & 5 deletions riak_repl/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import pytest
import os

import pytest

from datadog_checks.dev.conditions import CheckDockerLogs, CheckEndpoints
from datadog_checks.dev.docker import docker_run

from .common import HERE, URL


@pytest.fixture(scope="session")
@pytest.fixture(scope='session')
def dd_environment():
compose_file = os.path.join(HERE, "docker", "docker-compose.yml")
log_patterns = ['Full-sync with site "riak-west-1" completed',
'Full-sync with site "riak-east-1" completed']
compose_file = os.path.join(HERE, 'docker', 'docker-compose.yml')
log_patterns = ['Full-sync with site "riak-west-1" completed', 'Full-sync with site "riak-east-1" completed']

with docker_run(
compose_file=compose_file,
conditions=[CheckEndpoints(URL, wait=5), CheckDockerLogs(compose_file, log_patterns, matches='all')],
Expand Down
6 changes: 3 additions & 3 deletions riak_repl/tests/test_check.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from datadog_checks.riak_repl import RiakReplCheck
from datadog_checks.errors import CheckException
from datadog_checks.riak_repl import RiakReplCheck

from .common import INSTANCE

Expand Down Expand Up @@ -70,13 +70,13 @@ def test_service_check(aggregator, dd_environment):
"riak_repl.fullsync_coordinator.riak_west_1.retry_exits",
"riak_repl.fullsync_coordinator.riak_west_1.soft_retry_exits",
"riak_repl.fullsync_coordinator.riak_west_1.busy_nodes",
"riak_repl.fullsync_coordinator.riak_west_1.fullsyncs_completed"
"riak_repl.fullsync_coordinator.riak_west_1.fullsyncs_completed",
]
}

c = RiakReplCheck('riak_repl', init_config, {}, None)

c.check(INSTANCE)

for key in init_config['keys']:
aggregator.assert_metric(key, tags=[])

Expand Down
12 changes: 2 additions & 10 deletions riak_repl/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
minversion = 2.0
basepython = py37
envlist =
py{27,37}-{unit,integration}, flake8
py{27,37}-{unit,integration}

[testenv]
dd_check_style = true
platform = linux|darwin|win32
deps =
datadog-checks-base[deps]
Expand All @@ -16,12 +17,3 @@ commands =
pip install -r requirements.in
unit: pytest -v -m "not integration" {posargs}
integration: pytest -v -m"integration" {posargs}

[testenv:flake8]
skip_install = true
deps = flake8
commands = flake8 .

[flake8]
exclude = .eggs,.tox
max-line-length = 120

0 comments on commit 0c0d1fb

Please sign in to comment.