Skip to content

Commit

Permalink
Utilize time precision function from datadog_checks_base (#8841)
Browse files Browse the repository at this point in the history
* Use precision time function from base package

* Update base check requirements
  • Loading branch information
mgarabed authored Mar 17, 2021
1 parent 6428c4d commit 5798711
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 60 deletions.
20 changes: 3 additions & 17 deletions dns_check/datadog_checks/dns_check/dns_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,10 @@

from __future__ import unicode_literals

import time

import dns.resolver
from six import PY3

from datadog_checks.base import AgentCheck
from datadog_checks.base.utils.platform import Platform

if PY3:
# use higher precision clock available in Python3
time_func = time.perf_counter
elif Platform.is_win32():
# for tiny time deltas, time.time on Windows reports the same value
# of the clock more than once, causing the computation of response_time
# to be often 0; let's use time.clock that is more precise.
time_func = time.clock
else:
time_func = time.time
from datadog_checks.base.utils.time import get_precise_time


class BadConfException(Exception):
Expand Down Expand Up @@ -69,7 +55,7 @@ def check(self, instance):
hostname, timeout, nameserver, record_type, resolver, resolves_as = self._load_conf(instance)

# Perform the DNS query, and report its duration as a gauge
t0 = time_func()
t0 = get_precise_time()

try:
self.log.debug('Querying "%s" record for hostname "%s"...', record_type, hostname)
Expand All @@ -86,7 +72,7 @@ def check(self, instance):
if resolves_as:
self._check_answer(answer, resolves_as)

response_time = time_func() - t0
response_time = get_precise_time() - t0

except dns.exception.Timeout:
self.log.error('DNS resolution of %s timed out', hostname)
Expand Down
2 changes: 1 addition & 1 deletion dns_check/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_dependencies():
return f.readlines()


CHECKS_BASE_REQ = 'datadog-checks-base>=11.2.0'
CHECKS_BASE_REQ = 'datadog-checks-base>=16.7.0'

setup(
name='datadog-dns_check',
Expand Down
8 changes: 4 additions & 4 deletions dns_check/tests/test_dns_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
RESULTS_TIMEOUT = 10


@mock.patch('datadog_checks.dns_check.dns_check.time_func', side_effect=MockTime.time)
@mock.patch('datadog_checks.dns_check.dns_check.get_precise_time', side_effect=MockTime.time)
@mock.patch.object(Resolver, 'query', side_effect=success_query_mock)
def test_success(mocked_query, mocked_time, aggregator):
integration = DNSCheck('dns_check', {}, common.CONFIG_SUCCESS['instances'])
Expand Down Expand Up @@ -65,7 +65,7 @@ def test_success(mocked_query, mocked_time, aggregator):
aggregator.assert_all_metrics_covered()


@mock.patch('datadog_checks.dns_check.dns_check.time_func', side_effect=MockTime.time)
@mock.patch('datadog_checks.dns_check.dns_check.get_precise_time', side_effect=MockTime.time)
@mock.patch.object(Resolver, 'query', side_effect=nxdomain_query_mock)
def test_success_nxdomain(mocked_query, mocked_time, aggregator):
integration = DNSCheck('dns_check', {}, [common.CONFIG_SUCCESS_NXDOMAIN])
Expand All @@ -79,7 +79,7 @@ def test_success_nxdomain(mocked_query, mocked_time, aggregator):
aggregator.assert_all_metrics_covered()


@mock.patch('datadog_checks.dns_check.dns_check.time_func', side_effect=MockTime.time)
@mock.patch('datadog_checks.dns_check.dns_check.get_precise_time', side_effect=MockTime.time)
@mock.patch.object(Resolver, 'query', side_effect=Timeout())
def test_default_timeout(mocked_query, mocked_time, aggregator):
integration = DNSCheck(
Expand All @@ -100,7 +100,7 @@ def test_default_timeout(mocked_query, mocked_time, aggregator):
aggregator.assert_all_metrics_covered()


@mock.patch('datadog_checks.dns_check.dns_check.time_func', side_effect=MockTime.time)
@mock.patch('datadog_checks.dns_check.dns_check.get_precise_time', side_effect=MockTime.time)
@mock.patch.object(Resolver, 'query', side_effect=Timeout())
def test_instance_timeout(mocked_query, mocked_time, aggregator):
integration = DNSCheck('dns_check', {}, [common.CONFIG_INSTANCE_TIMEOUT])
Expand Down
7 changes: 4 additions & 3 deletions sqlserver/datadog_checks/sqlserver/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
from functools import partial

from datadog_checks.base.errors import CheckException
from datadog_checks.base.utils.time import get_precise_time

from .utils import construct_use_statement, time_func
from .utils import construct_use_statement

# Queries
ALL_INSTANCES = 'ALL'
Expand Down Expand Up @@ -643,14 +644,14 @@ def fetch_all_values(cls, cursor, counters_list, logger):
for db in cls._DATABASES:
query = cls.QUERY_BASE.format(db=db)
logger.debug("%s: fetch_all executing query: %s", cls.__name__, query)
start = time_func()
start = get_precise_time()
try:
cursor.execute(query)
data = cursor.fetchall()
except Exception as e:
logger.warning("Error when trying to query db %s - skipping. Error: %s", db, e)
continue
elapsed = time_func() - start
elapsed = get_precise_time() - start

query_columns = [i[0] for i in cursor.description]
if columns:
Expand Down
14 changes: 0 additions & 14 deletions sqlserver/datadog_checks/sqlserver/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,12 @@
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
import os
import time

from six import PY3

from datadog_checks.base.utils.platform import Platform

CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
DRIVER_CONFIG_DIR = os.path.join(CURRENT_DIR, 'data', 'driver_config')

if PY3:
# use higher precision clock available in Python3
time_func = time.perf_counter
elif Platform.is_win32():
# for tiny time deltas, time.time on Windows reports the same value
# of the clock more than once, causing the computation of response_time
# to be often 0; let's use time.clock that is more precise.
time_func = time.clock
else:
time_func = time.time


def set_default_driver_conf():
if Platform.is_containerized():
Expand Down
2 changes: 1 addition & 1 deletion sqlserver/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_dependencies():
return f.readlines()


CHECKS_BASE_REQ = 'datadog-checks-base>=15.7.0'
CHECKS_BASE_REQ = 'datadog-checks-base>=16.7.0'

setup(
name='datadog-sqlserver',
Expand Down
24 changes: 5 additions & 19 deletions tcp_check/datadog_checks/tcp_check/tcp_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,10 @@
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)
import socket
import time
from contextlib import closing

from six import PY3

from datadog_checks.base import AgentCheck, ConfigurationError
from datadog_checks.base.utils.platform import Platform

if PY3:
# use higher precision clock available in Python3
time_func = time.perf_counter
elif Platform.is_win32():
# for tiny time deltas, time.time on Windows reports the same value
# of the clock more than once, causing the computation of response_time
# to be often 0; let's use time.clock that is more precise.
time_func = time.clock
else:
time_func = time.time
from datadog_checks.base.utils.time import get_precise_time


class TCPCheck(AgentCheck):
Expand Down Expand Up @@ -81,13 +67,13 @@ def resolve_ip(self):
def connect(self):
with closing(socket.socket(self.socket_type)) as sock:
sock.settimeout(self.timeout)
start = time_func()
start = get_precise_time()
sock.connect((self.addr, self.port))
response_time = time_func() - start
response_time = get_precise_time() - start
return response_time

def check(self, instance):
start = time_func() # Avoid initialisation warning
start = get_precise_time() # Avoid initialisation warning
self.log.debug("Connecting to %s %d", self.addr, self.port)
try:
response_time = self.connect()
Expand All @@ -100,7 +86,7 @@ def check(self, instance):
tags=self.tags,
)
except Exception as e:
length = int((time_func() - start) * 1000)
length = int((get_precise_time() - start) * 1000)
if isinstance(e, socket.error) and "timed out" in str(e):
# The connection timed out because it took more time than the system tcp stack allows
self.log.warning(
Expand Down
2 changes: 1 addition & 1 deletion tcp_check/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_dependencies():
return f.readlines()


CHECKS_BASE_REQ = 'datadog-checks-base>=11.2.0'
CHECKS_BASE_REQ = 'datadog-checks-base>=16.7.0'

setup(
name='datadog-tcp_check',
Expand Down

0 comments on commit 5798711

Please sign in to comment.