Skip to content

Commit

Permalink
Merge pull request #489 from permitio/rk/bugfix-pipe-opa-logs
Browse files Browse the repository at this point in the history
Fix hanging inline OPA on very long log lines
  • Loading branch information
roekatz authored Aug 23, 2023
2 parents dcdbf50 + f48b48d commit 18dbe29
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion packages/opal-client/opal_client/engine/logger.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import json
import logging
from enum import Enum
Expand Down Expand Up @@ -32,8 +33,17 @@ async def pipe_opa_logs(stream, logs_format: EngineLogFormat):
if logs_format == EngineLogFormat.NONE:
return

line = b""
while True:
line = await stream.readline()
try:
line += await stream.readuntil(b"\n")
except asyncio.exceptions.IncompleteReadError as e:
line += e.partial
except asyncio.exceptions.LimitOverrunError as e:
# No new line yet but buffer limit exceeded, read what's available and try again
line += await stream.readexactly(e.consumed)
continue

if not line:
break
try:
Expand All @@ -54,6 +64,8 @@ async def pipe_opa_logs(stream, logs_format: EngineLogFormat):
log_entire_dict(level, msg, log_line)
except json.JSONDecodeError:
logger.info(line)
finally:
line = b""


def log_event_name(level: str, msg: Optional[str]) -> bool:
Expand Down

0 comments on commit 18dbe29

Please sign in to comment.