From 579871121588d836f1ef624bf8e486f3d1daa724 Mon Sep 17 00:00:00 2001 From: Mike Garabedian Date: Wed, 17 Mar 2021 08:42:22 -0400 Subject: [PATCH] Utilize time precision function from datadog_checks_base (#8841) * Use precision time function from base package * Update base check requirements --- .../datadog_checks/dns_check/dns_check.py | 20 +++------------- dns_check/setup.py | 2 +- dns_check/tests/test_dns_check.py | 8 +++---- sqlserver/datadog_checks/sqlserver/metrics.py | 7 +++--- sqlserver/datadog_checks/sqlserver/utils.py | 14 ----------- sqlserver/setup.py | 2 +- .../datadog_checks/tcp_check/tcp_check.py | 24 ++++--------------- tcp_check/setup.py | 2 +- 8 files changed, 19 insertions(+), 60 deletions(-) diff --git a/dns_check/datadog_checks/dns_check/dns_check.py b/dns_check/datadog_checks/dns_check/dns_check.py index d5ff603bb337e..7adcbdf42e8ae 100644 --- a/dns_check/datadog_checks/dns_check/dns_check.py +++ b/dns_check/datadog_checks/dns_check/dns_check.py @@ -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): @@ -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) @@ -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) diff --git a/dns_check/setup.py b/dns_check/setup.py index d16246d221fd0..654ea3e0dae67 100644 --- a/dns_check/setup.py +++ b/dns_check/setup.py @@ -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', diff --git a/dns_check/tests/test_dns_check.py b/dns_check/tests/test_dns_check.py index d113f9f264165..f12389d2556c8 100644 --- a/dns_check/tests/test_dns_check.py +++ b/dns_check/tests/test_dns_check.py @@ -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']) @@ -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]) @@ -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( @@ -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]) diff --git a/sqlserver/datadog_checks/sqlserver/metrics.py b/sqlserver/datadog_checks/sqlserver/metrics.py index 6e43ce4061171..d7cc7ad83d86d 100644 --- a/sqlserver/datadog_checks/sqlserver/metrics.py +++ b/sqlserver/datadog_checks/sqlserver/metrics.py @@ -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' @@ -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: diff --git a/sqlserver/datadog_checks/sqlserver/utils.py b/sqlserver/datadog_checks/sqlserver/utils.py index 6dfcb6efc2e8b..d40f2c4efa0d2 100644 --- a/sqlserver/datadog_checks/sqlserver/utils.py +++ b/sqlserver/datadog_checks/sqlserver/utils.py @@ -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(): diff --git a/sqlserver/setup.py b/sqlserver/setup.py index f3ba896391099..41cead2f51725 100644 --- a/sqlserver/setup.py +++ b/sqlserver/setup.py @@ -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', diff --git a/tcp_check/datadog_checks/tcp_check/tcp_check.py b/tcp_check/datadog_checks/tcp_check/tcp_check.py index 7e19983fb0fdb..ee892494fe829 100644 --- a/tcp_check/datadog_checks/tcp_check/tcp_check.py +++ b/tcp_check/datadog_checks/tcp_check/tcp_check.py @@ -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): @@ -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() @@ -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( diff --git a/tcp_check/setup.py b/tcp_check/setup.py index f4caee3bf464f..a0cd71d5c319d 100644 --- a/tcp_check/setup.py +++ b/tcp_check/setup.py @@ -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',