Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add expiration time to observability buffer (#10291)
Przemysław Łada2194a228efa7
diff --git a/saleor/settings.py b/saleor/settings.pyindex e5572b2ac4..3e4f798763 100644--- a/saleor/settings.py+++ b/saleor/settings.py@@ -576,7 +576,7 @@ OBSERVABILITY_REPORT_ALL_API_CALLS = get_bool_from_env("OBSERVABILITY_REPORT_ALL_API_CALLS", False)OBSERVABILITY_MAX_PAYLOAD_SIZE = int(- os.environ.get("OBSERVABILITY_MAX_PAYLOAD_SIZE", 128 * 1024)+ os.environ.get("OBSERVABILITY_MAX_PAYLOAD_SIZE", 25 * 1000))OBSERVABILITY_BUFFER_SIZE_LIMIT = int(os.environ.get("OBSERVABILITY_BUFFER_SIZE_LIMIT", 1000)@@ -587,12 +587,20 @@ OBSERVABILITY_BUFFER_BATCH_SIZE = int(OBSERVABILITY_REPORT_PERIOD = timedelta(seconds=parse(os.environ.get("OBSERVABILITY_REPORT_PERIOD", "20 seconds")))+OBSERVABILITY_BUFFER_TIMEOUT = timedelta(+ seconds=parse(os.environ.get("OBSERVABILITY_BUFFER_TIMEOUT", "5 minutes"))+)if OBSERVABILITY_ACTIVE:CELERY_BEAT_SCHEDULE["observability-reporter"] = {"task": "saleor.plugins.webhook.tasks.observability_reporter_task","schedule": OBSERVABILITY_REPORT_PERIOD,"options": {"expires": OBSERVABILITY_REPORT_PERIOD.total_seconds()},}+ if OBSERVABILITY_BUFFER_TIMEOUT < OBSERVABILITY_REPORT_PERIOD * 2:+ warnings.warn(+ "OBSERVABILITY_REPORT_PERIOD is too big compared to "+ "OBSERVABILITY_BUFFER_TIMEOUT. That can lead to a loss of events."+ )# Change this value if your application is running behind a proxy,# e.g. HTTP_CF_Connecting_IP for Cloudflare or X_FORWARDED_FORdiff --git a/saleor/webhook/observability/buffers.py b/saleor/webhook/observability/buffers.pyindex 1fb3d431fa..d1b94eab23 100644--- a/saleor/webhook/observability/buffers.py+++ b/saleor/webhook/observability/buffers.py@@ -10,6 +10,7 @@ from redis import ConnectionPool, Redisfrom .exceptions import ConnectionNotConfiguredKEY_TYPE = str+DEFAULT_CONNECTION_TIMEOUT = 0.5_local = Local()@@ -23,13 +24,15 @@ class BaseBuffer:key: KEY_TYPE,max_size: int,batch_size: int,- connection_timeout=10.0,+ connection_timeout=DEFAULT_CONNECTION_TIMEOUT,+ timeout: int = 60,):self.broker_url = broker_urlself.key = keyself.max_size = max_sizeself.batch_size = batch_sizeself.connection_timeout = connection_timeout+ self.timeout = timeoutdef decode(self, value: bytes) -> Any:return pickle.loads(zlib.decompress(value))@@ -126,6 +129,7 @@ class RedisBuffer(BaseBuffer):client = self.clientclient.lpush(key, *events_data)client.ltrim(key, 0, max(0, self.max_size - 1))+ client.expire(key, self.timeout)return max(0, len(events) - self.max_size)def put_events(self, events: List[Any]) -> int:@@ -149,7 +153,7 @@ class RedisBuffer(BaseBuffer):trimmed[key] = self._put_events(key, events_dict[key], client=pipe)result = pipe.execute()for key in keys:- buffer_len, _ = result.pop(0), result.pop(0)+ buffer_len, _, _ = result.pop(0), result.pop(0), result.pop(0)trimmed[key] += max(0, buffer_len - self.max_size)return trimmed@@ -189,13 +193,20 @@ class RedisBuffer(BaseBuffer):return self.client.llen(self.key)-def get_buffer(key: KEY_TYPE) -> BaseBuffer:+def get_buffer(+ key: KEY_TYPE, connection_timeout=DEFAULT_CONNECTION_TIMEOUT+) -> BaseBuffer:if not settings.OBSERVABILITY_BROKER_URL:raise ConnectionNotConfigured("The observability broker url not set")broker_url = settings.OBSERVABILITY_BROKER_URLmax_size = settings.OBSERVABILITY_BUFFER_SIZE_LIMITbatch_size = settings.OBSERVABILITY_BUFFER_BATCH_SIZE- connection_timeout = settings.OBSERVABILITY_REPORT_PERIOD.total_seconds()+ timeout = int(settings.OBSERVABILITY_BUFFER_TIMEOUT.total_seconds())return RedisBuffer(- broker_url, key, max_size, batch_size, connection_timeout=connection_timeout+ broker_url,+ key,+ max_size,+ batch_size,+ connection_timeout=connection_timeout,+ timeout=timeout,)diff --git a/saleor/webhook/observability/tests/test_buffer.py b/saleor/webhook/observability/tests/test_buffer.pyindex d84a0cd9c4..be9162d709 100644--- a/saleor/webhook/observability/tests/test_buffer.py+++ b/saleor/webhook/observability/tests/test_buffer.py@@ -1,4 +1,8 @@+from datetime import timedelta+import pytest+from django.utils import timezone+from freezegun import freeze_timefrom ..buffers import RedisBuffer, get_bufferfrom ..exceptions import ConnectionNotConfigured@@ -94,6 +98,22 @@ def test_put_multi_key_events(patch_connection_pool):assert dropped == {key_a: 0, key_b: 0, key_c: MAX_SIZE}+def test_put_multi_key_events_when_buffer_full(patch_connection_pool):+ max_events = [{"event": "data"}] * MAX_SIZE+ key_a, events_a = "buffer_a", [{"event": "data"}] * 2+ key_b, events_b = "buffer_b", [{"event": "data"}] * MAX_SIZE+ key_c, events_c = "buffer_c", [{"event": "data"}] * MAX_SIZE * 2+ buffer_a = get_buffer(key_a)+ buffer_a.put_multi_key_events(+ {key_a: max_events, key_b: max_events, key_c: max_events}+ )++ dropped = buffer_a.put_multi_key_events(+ {key_a: events_a, key_b: events_b, key_c: events_c}+ )+ assert dropped == {key_a: 2, key_b: MAX_SIZE, key_c: MAX_SIZE * 2}++def test_pop_event(buffer):event = "buffer", {"event": "data"}buffer.put_event(event)@@ -129,3 +149,13 @@ def test_clear(buffer):assert buffer.size() == 2assert buffer.clear() == 2assert buffer.size() == 0+++def test_pop_expired_events(buffer):+ push_time = timezone.now()+ with freeze_time(push_time):+ events = [{"event": f"data{i}"} for i in range(MAX_SIZE)]+ buffer.put_events(events)+ with freeze_time(push_time + timedelta(seconds=buffer.timeout + 1)):+ popped_events = buffer.pop_events()+ assert popped_events == []