Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potential metric loss when open_buffer is combined with disable_buffering=False #820

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions datadog/dogstatsd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,14 +734,11 @@ def open_buffer(self, max_buffer_size=None):

self._config_lock.acquire()

# XXX Remove if `disable_buffering` default is changed to False
self._send = self._send_to_buffer

if max_buffer_size is not None:
log.warning("The parameter max_buffer_size is now deprecated and is not used anymore")

self._reset_buffer()

def close_buffer(self):
"""
Flush the buffer and switch back to single metric packets.
Expand All @@ -752,7 +749,6 @@ def close_buffer(self):
try:
self.flush()
finally:
# XXX Remove if `disable_buffering` default is changed to False
if self._disable_buffering:
self._send = self._send_to_server

Expand Down
18 changes: 18 additions & 0 deletions tests/integration/dogstatsd/test_statsd_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,21 @@ def test_fork_hooks(disable_background_sender, disable_buffering):

foo.close()
bar.close()


def test_buffering_with_context():
statsd = DogStatsd(
telemetry_min_flush_interval=0,
disable_buffering=False,
)

foo, bar = socket.socketpair(socket.AF_UNIX, socket.SOCK_DGRAM, 0)
statsd.socket = foo

statsd.increment("first")
with statsd: # should not erase previously buffered metrics
pass

bar.settimeout(5)
msg = bar.recv(8192)
assert msg == b"first:1|c\n"
Loading