Skip to content

Commit ca59136

Browse files
authored
More logs in sse api (#3325)
add logs in sse api
1 parent 104dc0c commit ca59136

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

services/sse-api/src/sse_api/routes/hub_cache.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import dataclasses
55
import json
66
import logging
7+
import time
78
from asyncio import CancelledError
89
from collections.abc import AsyncGenerator, AsyncIterable
910

@@ -22,27 +23,40 @@
2223

2324
# Also: how do we manage multiple SSE servers / uvicorn workers / load-balancing?
2425

26+
SSE_EVENTS_COUNTER_LOG_INTERVAL_SECONDS = 300
27+
2528

2629
def create_hub_cache_endpoint(hub_cache_watcher: HubCacheWatcher) -> Endpoint:
2730
async def hub_cache_endpoint(request: Request) -> Response:
28-
logging.info("/hub-cache")
29-
3031
all = get_request_parameter(request, "all", default="false").lower() == "true"
3132
# ^ the values that trigger the initialization are "true", "True" and any other case-insensitive variant
3233

3334
uuid, event = hub_cache_watcher.subscribe()
35+
logging.info(f"/hub-cache {all=} {uuid=}")
3436
if all:
3537
init_task = hub_cache_watcher.run_initialization(uuid)
3638

3739
async def event_generator() -> AsyncGenerator[ServerSentEvent, None]:
40+
_last_time = time.time()
41+
_count_since_last_time = 0
3842
try:
3943
while True:
44+
if time.time() - _last_time > SSE_EVENTS_COUNTER_LOG_INTERVAL_SECONDS:
45+
logging.info(
46+
f"SSE events sent in the last {SSE_EVENTS_COUNTER_LOG_INTERVAL_SECONDS}s: {_count_since_last_time} ({uuid=})"
47+
)
48+
_last_time = time.time()
49+
_count_since_last_time = 0
4050
new_value = await event.wait_value()
4151
event.clear()
4252
if new_value is not None:
4353
logging.debug(f"Sending new value: {new_value}")
54+
_count_since_last_time += 1
4455
yield ServerSentEvent(data=json.dumps(dataclasses.asdict(new_value)), event="message")
4556
finally:
57+
logging.info(
58+
f"SSE events sent in the last {int(time.time() - _last_time)}s: {_count_since_last_time} ({uuid=})"
59+
)
4660
hub_cache_watcher.unsubscribe(uuid)
4761
if all:
4862
await init_task

0 commit comments

Comments
 (0)