Skip to content

Commit

Permalink
Update payload size when updating socket path (#807)
Browse files Browse the repository at this point in the history
* Update payload size when updating socket path

* Add a test case
  • Loading branch information
vickenty authored Jan 4, 2024
1 parent ff9f020 commit 1e2cfcf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 9 additions & 7 deletions datadog/dogstatsd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,20 +319,16 @@ def __init__(
self._enabled = False

# Connection
self._max_payload_size = max_buffer_len
self._max_buffer_len = max_buffer_len
self.socket_timeout = socket_timeout
if socket_path is not None:
self.socket_path = socket_path # type: Optional[text]
self.host = None
self.port = None
if not self._max_payload_size:
self._max_payload_size = UDS_OPTIMAL_PAYLOAD_LENGTH
else:
self.socket_path = None
self.host = self.resolve_host(host, use_default_route)
self.port = int(port)
if not self._max_payload_size:
self._max_payload_size = UDP_OPTIMAL_PAYLOAD_LENGTH

self.telemetry_socket_path = telemetry_socket_path
self.telemetry_host = None
Expand Down Expand Up @@ -420,8 +416,14 @@ def socket_path(self):

@socket_path.setter
def socket_path(self, path):
self._socket_path = path
self._transport = "udp" if path is None else "uds"
with self._socket_lock:
self._socket_path = path
if path is None:
self._transport = "udp"
self._max_payload_size = self._max_buffer_len or UDP_OPTIMAL_PAYLOAD_LENGTH
else:
self._transport = "uds"
self._max_payload_size = self._max_buffer_len or UDS_OPTIMAL_PAYLOAD_LENGTH

def enable_background_sender(self, sender_queue_size=0, sender_queue_timeout=0):
"""
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/dogstatsd/test_statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -1951,3 +1951,9 @@ def test_telemetry_api(self):
self.assertEqual(statsd.packets_dropped, 0)
self.assertEqual(statsd.packets_dropped_queue, 0)
self.assertEqual(statsd.packets_dropped_writer, 0)

def test_max_payload_size(self):
statsd = DogStatsd(socket_path=None, port=8125)
self.assertEqual(statsd._max_payload_size, UDP_OPTIMAL_PAYLOAD_LENGTH)
statsd.socket_path = "/foo"
self.assertEqual(statsd._max_payload_size, UDS_OPTIMAL_PAYLOAD_LENGTH)

0 comments on commit 1e2cfcf

Please sign in to comment.