Skip to content

add some randomness to actual time between requests #426

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

Closed
wants to merge 4 commits into from
Closed
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
3 changes: 3 additions & 0 deletions docs/configuration.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,9 @@ A higher value also impacts the time until data is indexed and searchable in Ela
This setting is useful to limit memory consumption if you experience a sudden spike of traffic.
It has to be provided in *<<config-format-duration, duration format>>*.

NOTE: The actual time will vary between 90-110% of the given value,
to avoid stampedes of instances that start at the same time.

[float]
[[config-processors]]
==== `processors`
Expand Down
8 changes: 6 additions & 2 deletions elasticapm/transport/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import gzip
import logging
import random
import threading
import time
import timeit
Expand Down Expand Up @@ -86,11 +87,13 @@ def init_buffer():

buffer = init_buffer()
buffer_written = False
# add some randomness to timeout to avoid stampedes of several workers that are booted at the same time
max_flush_time = self._max_flush_time * random.uniform(0.9, 1.1) if self._max_flush_time else None

while True:
since_last_flush = timeit.default_timer() - self._last_flush
# take max flush time into account to calculate timeout
timeout = max(0, self._max_flush_time - since_last_flush) if self._max_flush_time else None
timeout = max(0, max_flush_time - since_last_flush) if max_flush_time else None
timed_out = False
try:
event_type, data, flush = self._event_queue.get(block=True, timeout=timeout)
Expand Down Expand Up @@ -120,7 +123,7 @@ def init_buffer():
logger.debug(
"flushing due to time since last flush %.3fs > max_flush_time %.3fs",
since_last_flush,
self._max_flush_time,
max_flush_time,
)
flush = True
elif self._max_buffer_size and queue_size > self._max_buffer_size:
Expand All @@ -134,6 +137,7 @@ def init_buffer():
self._last_flush = timeit.default_timer()
buffer = init_buffer()
buffer_written = False
max_flush_time = self._max_flush_time * random.uniform(0.9, 1.1) if self._max_flush_time else None
self._flushed.set()

def _flush(self, buffer):
Expand Down
2 changes: 1 addition & 1 deletion tests/transports/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def test_flush_time(mock_send, caplog):
time.sleep(0.2)
transport.close()
record = caplog.records[0]
assert "0.1" in record.message
assert "due to time since last flush" in record.message
assert mock_send.call_count == 0


Expand Down