Log tracer errors (#6066)

Example (would log several times if not for the helper fn. Would emit no
logs due to mulithreading previously)

![image](https://github.com/hwchase17/langchain/assets/130414180/070d25ae-1f06-4487-9617-0a6f66f3f01e)
This commit is contained in:
Zander Chase 2023-06-12 17:13:49 -07:00 committed by GitHub
parent b023f0c0f2
commit 2f0088039d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,6 +16,16 @@ from langchain.env import get_runtime_environment
from langchain.schema import BaseMessage, messages_to_dict from langchain.schema import BaseMessage, messages_to_dict
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
_LOGGED = set()
def log_error_once(method: str, exception: Exception) -> None:
"""Log an error once."""
global _LOGGED
if (method, type(exception)) in _LOGGED:
return
_LOGGED.add((method, type(exception)))
logger.error(exception)
class LangChainTracer(BaseTracer): class LangChainTracer(BaseTracer):
@ -76,11 +86,21 @@ class LangChainTracer(BaseTracer):
extra = run_dict.get("extra", {}) extra = run_dict.get("extra", {})
extra["runtime"] = get_runtime_environment() extra["runtime"] = get_runtime_environment()
run_dict["extra"] = extra run_dict["extra"] = extra
run = self.client.create_run(**run_dict, session_name=self.session_name) try:
run = self.client.create_run(**run_dict, session_name=self.session_name)
except Exception as e:
# Errors are swallowed by the thread executor so we need to log them here
log_error_once("post", e)
raise
def _update_run_single(self, run: Run) -> None: def _update_run_single(self, run: Run) -> None:
"""Update a run.""" """Update a run."""
self.client.update_run(run.id, **run.dict()) try:
self.client.update_run(run.id, **run.dict())
except Exception as e:
# Errors are swallowed by the thread executor so we need to log them here
log_error_once("patch", e)
raise
def _on_llm_start(self, run: Run) -> None: def _on_llm_start(self, run: Run) -> None:
"""Persist an LLM run.""" """Persist an LLM run."""