Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

feat: Avoided displaying sensitive logs while running tool with sidecar (#1274)

Chandrasekharan M314c05d43146
diff --git a/runner/src/unstract/runner/runner.py b/runner/src/unstract/runner/runner.py
index f85d897c3..870b2b069 100644
--- a/runner/src/unstract/runner/runner.py
+++ b/runner/src/unstract/runner/runner.py
@@ -42,11 +42,11 @@ class UnstractRunner:
execution_id: str,
organization_id: str,
file_execution_id: str,
+ container_name: str,
channel: str | None = None,
) -> None:
for line in container.logs(follow=True):
log_message = line
- self.logger.debug(f"[{container.name}] - {log_message}")
self.process_log_message(
log_message=log_message,
tool_instance_id=tool_instance_id,
@@ -54,6 +54,7 @@ class UnstractRunner:
execution_id=execution_id,
organization_id=organization_id,
file_execution_id=file_execution_id,
+ container_name=container_name,
)
def get_valid_log_message(self, log_message: str) -> dict[str, Any] | None:
@@ -79,23 +80,31 @@ class UnstractRunner:
execution_id: str,
organization_id: str,
file_execution_id: str,
+ container_name: str,
channel: str | None = None,
) -> dict[str, Any] | None:
log_dict = self.get_valid_log_message(log_message)
if not log_dict:
+ self.logger.debug(f"[{container_name}] {log_message}")
return None
log_type = log_dict.get("type")
log_level = log_dict.get("level")
- if log_type == LogType.LOG and log_level == LogLevel.ERROR:
- raise ToolRunException(log_dict.get("log"))
+
if not self.is_valid_log_type(log_type):
self.logger.warning(
f"Received invalid logType: {log_type} with log message: {log_dict}"
)
return None
- if log_type == LogType.RESULT:
+
+ if log_type == LogType.LOG:
+ if log_level == LogLevel.ERROR:
+ raise ToolRunException(log_dict.get("log"))
+ self.logger.debug(f"[{container_name}] {log_message}")
+ elif log_type == LogType.RESULT:
+ self.logger.debug(f"[{container_name}] Completed running")
return log_dict
- if log_type == LogType.UPDATE:
+ elif log_type == LogType.UPDATE:
+ self.logger.debug(f"[{container_name}] Pushing UI updates")
log_dict["component"] = tool_instance_id
if channel:
log_dict[LogFieldName.EXECUTION_ID] = execution_id
@@ -218,6 +227,7 @@ class UnstractRunner:
"MESSAGING_CHANNEL": messaging_channel,
"LOG_LEVEL": os.getenv(Env.LOG_LEVEL, "INFO"),
"CELERY_BROKER_URL": os.getenv(Env.CELERY_BROKER_URL),
+ "CONTAINER_NAME": container_name,
}
sidecar_config = self.client.get_container_run_config(
command=[],
@@ -291,7 +301,7 @@ class UnstractRunner:
"return $exit_code; "
"}"
)
- execute_cmd = f"run_tool 2>&1 | tee -a {shared_log_file}"
+ execute_cmd = f"run_tool > {shared_log_file} 2>&1"
# Combine all commands
shell_script = f"{mkdir_cmd} && {run_tool_fn}; {execute_cmd}"
@@ -305,8 +315,8 @@ class UnstractRunner:
file_execution_id: str,
settings: dict[str, Any],
envs: dict[str, Any],
+ container_name: str,
messaging_channel: str | None = None,
- container_name: str | None = None,
) -> Any | None:
"""RUN container With RUN Command.
@@ -417,6 +427,7 @@ class UnstractRunner:
execution_id=execution_id,
organization_id=organization_id,
file_execution_id=file_execution_id,
+ container_name=container_name,
)
self.logger.info(
f"Execution ID: {execution_id}, docker "
@@ -425,8 +436,7 @@ class UnstractRunner:
except ToolRunException as te:
self.logger.error(
- "Error while running docker container"
- f" {container_config.get('name')}: {te}",
+ f"Error while running docker container {container_config.get('name')}: {te}",
stack_info=True,
exc_info=True,
)
diff --git a/tool-sidecar/src/unstract/tool_sidecar/constants.py b/tool-sidecar/src/unstract/tool_sidecar/constants.py
index 110d4f392..f9a25c318 100644
--- a/tool-sidecar/src/unstract/tool_sidecar/constants.py
+++ b/tool-sidecar/src/unstract/tool_sidecar/constants.py
@@ -26,3 +26,4 @@ class Env:
ORGANIZATION_ID = "ORGANIZATION_ID"
FILE_EXECUTION_ID = "FILE_EXECUTION_ID"
MESSAGING_CHANNEL = "MESSAGING_CHANNEL"
+ CONTAINER_NAME = "CONTAINER_NAME"
diff --git a/tool-sidecar/src/unstract/tool_sidecar/log_processor.py b/tool-sidecar/src/unstract/tool_sidecar/log_processor.py
index df95734d2..6417eddd6 100644
--- a/tool-sidecar/src/unstract/tool_sidecar/log_processor.py
+++ b/tool-sidecar/src/unstract/tool_sidecar/log_processor.py
@@ -25,8 +25,6 @@ logger = logging.getLogger(__name__)
class LogProcessor:
- TERMINATION_MARKER = "TOOL_EXECUTION_COMPLETE"
-
def __init__(
self,
log_path: str,
@@ -39,6 +37,7 @@ class LogProcessor:
organization_id: str,
file_execution_id: str,
messaging_channel: str,
+ container_name: str = "",
):
"""Initialize the log processor with necessary connections and paths.
@@ -60,6 +59,7 @@ class LogProcessor:
self.organization_id = organization_id
self.file_execution_id = file_execution_id
self.messaging_channel = messaging_channel
+ self.container_name = container_name
def wait_for_log_file(self, timeout: int = 300) -> bool:
"""Wait for the log file to be created by the tool container.
@@ -93,34 +93,41 @@ class LogProcessor:
f"{LogFieldName.TOOL_TERMINATION_MARKER}"
)
return LogLineDTO(is_terminated=True)
+
log_dict = self.get_valid_log_message(line)
if not log_dict:
+ logger.info(f"[Tool: {self.container_name}] {line}")
return LogLineDTO()
+
log_type = log_dict.get("type")
log_level = log_dict.get("level")
- if log_type == LogType.LOG and log_level == LogLevel.ERROR:
- logger.error(f"Error in log: {log_dict.get('log')}")
- return LogLineDTO(error=log_dict.get("log"))
if not self.is_valid_log_type(log_type):
logger.warning(
f"Received invalid logType: {log_type} with log message: {log_dict}"
)
return LogLineDTO()
- if log_type == LogType.RESULT:
- logger.info(
- f"Tool {self.tool_instance_id} execution {self.execution_id} "
- "completed successfully with result."
- )
+
+ log_process_status = LogLineDTO()
+ if log_type == LogType.LOG:
+ if log_level == LogLevel.ERROR:
+ logger.error(f"[Tool: {self.container_name}] {log_dict.get('log')}")
+ log_process_status.error = log_dict.get("log")
+ else:
+ logger.info(f"[Tool: {self.container_name}] {log_dict.get('log')}")
+ elif log_type == LogType.RESULT:
+ logger.info(f"[Tool {self.container_name}] Completed running")
return LogLineDTO(with_result=True)
- if log_type == LogType.UPDATE:
+ elif log_type == LogType.UPDATE:
+ logger.info(f"[Tool: {self.container_name}] Pushing UI updates")
log_dict["component"] = self.tool_instance_id
+
log_dict[LogFieldName.EXECUTION_ID] = self.execution_id
log_dict[LogFieldName.ORGANIZATION_ID] = self.organization_id
log_dict[LogFieldName.TIMESTAMP] = self.get_log_timestamp(log_dict)
log_dict[LogFieldName.FILE_EXECUTION_ID] = self.file_execution_id
# Publish to channel of socket io
LogPublisher.publish(self.messaging_channel, log_dict)
- return LogLineDTO()
+ return log_process_status
def get_log_timestamp(self, log_dict: dict[str, Any]) -> float:
"""Obtains the timestamp from the log dictionary.
@@ -227,6 +234,7 @@ def main():
organization_id = os.getenv(Env.ORGANIZATION_ID)
file_execution_id = os.getenv(Env.FILE_EXECUTION_ID)
messaging_channel = os.getenv(Env.MESSAGING_CHANNEL)
+ container_name = os.getenv(Env.CONTAINER_NAME)
# Validate required parameters
required_params = {
@@ -261,6 +269,7 @@ def main():
execution_id=execution_id,
organization_id=organization_id,
file_execution_id=file_execution_id,
+ container_name=container_name,
)
processor.monitor_logs()