Skip to content

Commit

Permalink
Lazy log formatting (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbriancon authored Feb 5, 2021
1 parent 498a43f commit 01fc156
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
13 changes: 10 additions & 3 deletions datadog/api/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ def _should_submit(cls):
# number of timeouts, then enter the backoff state, recording the time
# we started backing off
if not cls._backoff_timestamp and cls._timeout_counter >= cls._max_timeouts:
log.info("Max number of datadog timeouts exceeded, backing off for {0} seconds".format(cls._backoff_period))
log.info(
"Max number of datadog timeouts exceeded, backing off for %s seconds",
cls._backoff_period,
)
cls._backoff_timestamp = now
should_submit = False

Expand All @@ -268,13 +271,17 @@ def _should_submit(cls):
backed_off_time, backoff_time_left = cls._backoff_status()
if backoff_time_left < 0:
log.info(
"Exiting backoff state after {0} seconds, will try to submit metrics again".format(backed_off_time)
"Exiting backoff state after %s seconds, will try to submit metrics again",
backed_off_time,
)
cls._backoff_timestamp = None
cls._timeout_counter = 0
should_submit = True
else:
log.info("In backoff state, won't submit metrics for another {0} seconds".format(backoff_time_left))
log.info(
"In backoff state, won't submit metrics for another %s seconds",
backoff_time_left,
)
should_submit = False
else:
should_submit = True
Expand Down
10 changes: 8 additions & 2 deletions datadog/dogstatsd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,13 +605,19 @@ def _xmit_packet(self, packet, is_telemetry):
# dogstatsd is overflowing, drop the packets (mimicks the UDP behaviour)
pass
except (socket.herror, socket.gaierror) as se:
log.warning("Error submitting packet: {}, dropping the packet and closing the socket".format(se))
log.warning(
"Error submitting packet: %s, dropping the packet and closing the socket",
se,
)
self.close_socket()
except socket.error as se:
if se.errno == errno.EAGAIN:
log.debug("Socket send would block: %s, dropping the packet", se)
else:
log.warning("Error submitting packet: {}, dropping the packet and closing the socket".format(se))
log.warning(
"Error submitting packet: %s, dropping the packet and closing the socket",
se,
)
self.close_socket()
except Exception as e:
log.error("Unexpected error: %s", str(e))
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/dogstatsd/test_statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ def test_socket_error(self):
self.statsd.gauge('no error', 1)
mock_log.error.assert_not_called()
mock_log.warning.assert_called_once_with(
"Error submitting packet: Socket error, dropping the packet and closing the socket"
"Error submitting packet: %s, dropping the packet and closing the socket",
mock.ANY,
)

def test_socket_overflown(self):
Expand Down

0 comments on commit 01fc156

Please sign in to comment.