Skip to content

Commit f49aab6

Browse files
authored
Add timer resolution warning on init (#1481)
1 parent 0140658 commit f49aab6

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

newrelic/config.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,11 @@ def _load_configuration(config_file=None, environment=None, ignore_errors=True,
983983

984984
initialize_logging(log_file, log_level)
985985

986+
# Check the resolution of the system timers we will be using
987+
# and log a warning if it isn't precise enough.
988+
989+
_check_timer_resolution()
990+
986991
# Now process the remainder of the global configuration
987992
# settings.
988993

@@ -1074,6 +1079,44 @@ def _load_configuration(config_file=None, environment=None, ignore_errors=True,
10741079
_raise_configuration_error(section=None, option="transaction_tracer.generator_trace")
10751080

10761081

1082+
def _check_timer_resolution():
1083+
"""Check the resolution of the system timer we will be using. If it isn't precise enough then log warnings."""
1084+
1085+
from time import get_clock_info
1086+
1087+
timer = "time" # Hard code this for now, in the future we may want to make timer selection dynamic
1088+
min_recommended_timer_resolution = 1e-4 # 0.1 milliseconds
1089+
1090+
# Attempt to get the resolution of the selected timer. If this fails, log a warning and exit early.
1091+
try:
1092+
resolution = get_clock_info(timer).resolution
1093+
except Exception:
1094+
_logger.warning("Unable to determine resolution of system timer.")
1095+
return
1096+
1097+
# Check the resolution level of the timer and log appropriate messages for it.
1098+
resolution_log_level = logging.DEBUG
1099+
if resolution > min_recommended_timer_resolution:
1100+
resolution_log_level = logging.WARNING
1101+
_logger.warning(
1102+
"The resolution of time.%s() on this system is not precise enough and may result in "
1103+
"inaccurate timing measurements. This can cause widely varying response times and "
1104+
"trace durations to be reported by the New Relic agent.",
1105+
timer,
1106+
)
1107+
1108+
# On Windows, Python 3.13+ uses a higher resolution timer implementation. Add a specific recommendation for this.
1109+
if sys.platform == "win32" and sys.version_info < (3, 13):
1110+
_logger.warning(
1111+
"On Windows, consider using Python 3.13 or later to take advantage of the higher resolution timer implementations."
1112+
)
1113+
1114+
# Log the used timer's resolution at the appropriate log level.
1115+
# If the resolution is too low, this will be a warning.
1116+
# Otherwise, it will be a debug message.
1117+
_logger.log(resolution_log_level, "Timer implementation: time.%s(). Resolution: %s seconds.", timer, resolution)
1118+
1119+
10771120
# Generic error reporting functions.
10781121

10791122

0 commit comments

Comments
 (0)