Skip to content

Commit

Permalink
Remove queuing for StatsD client (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineAugusti committed Oct 22, 2020
1 parent 0dd6046 commit 4eaf73a
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 23 deletions.
9 changes: 2 additions & 7 deletions notifications_utils/clients/statsd/statsd_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import random
import time
from collections import deque
from socket import AF_INET, SOCK_DGRAM, gethostbyname, socket

import cachetools.func
Expand All @@ -19,7 +18,6 @@ def __init__(self, host, port, prefix):
self._port = port
self._prefix = prefix
self._sock = socket(AF_INET, SOCK_DGRAM)
self._queue = deque()

def _resolve(self, addr):
return gethostbyname(addr)
Expand All @@ -35,16 +33,13 @@ def _cached_host(self):
return None

def _send(self, data):
self._queue.append(data)
try:
host = self._cached_host()
# If we can't resolve DNS, then host is `None`
# Don't send to statsd, data will be sent later
# Don't send to statsd
if host is None:
return

for elem in self._queue:
self._sock.sendto(elem.encode('ascii'), (host, self._port))
self._sock.sendto(data.encode('ascii'), (host, self._port))
except Exception as e:
current_app.logger.warning('Error sending statsd metric: {}'.format(str(e)))
pass
Expand Down
2 changes: 1 addition & 1 deletion notifications_utils/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = '43.1.0'
__version__ = '43.1.1'
# GDS version '34.0.1'
15 changes: 0 additions & 15 deletions tests/test_statsd_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,6 @@ def test_should_not_attempt_to_send_if_cache_contains_none(app, mocker):
assert mock_sock.called is False


def test_records_points_when_cache_contains_none(app, mocker):
stats_client = NotifyStatsClient('localhost', 8125, '')
mock_sock = mocker.patch.object(stats_client, "_sock")
mock_cached_host = mocker.patch.object(stats_client, '_cached_host', side_effect=[None, '1.2.3.4'])

stats_client._send('data')
mock_cached_host.assert_called_once_with()
assert mock_sock.called is False
assert len(stats_client._queue) == 1

stats_client._send('foo')
assert mock_sock.called is False
assert len(stats_client._queue) == 2


def test_should_manage_dns(app, mocker):
stats_client = NotifyStatsClient('exporter.apps.internal', 8125, '')

Expand Down

0 comments on commit 4eaf73a

Please sign in to comment.