1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Store automation and script traces (#56894)

* Store automation and script traces

* Pylint

* Deduplicate code

* Fix issues when no stored traces are available

* Store serialized data for restored traces

* Update WS API

* Update test

* Restore context

* Improve tests

* Add new test files

* Rename restore_traces to async_restore_traces

* Refactor trace.websocket_api

* Defer loading stored traces

* Lint

* Revert refactoring which is no longer needed

* Correct order when restoring traces

* Apply suggestion from code review

* Improve test coverage

* Apply suggestions from code review
This commit is contained in:
Erik Montnemery
2021-10-19 10:23:23 +02:00
committed by GitHub
parent 29c062fcc4
commit 961ee717ef
11 changed files with 1256 additions and 191 deletions

View File

@@ -17,7 +17,7 @@ class TraceElement:
def __init__(self, variables: TemplateVarsType, path: str) -> None:
"""Container for trace data."""
self._child_key: tuple[str, str] | None = None
self._child_key: str | None = None
self._child_run_id: str | None = None
self._error: Exception | None = None
self.path: str = path
@@ -40,7 +40,7 @@ class TraceElement:
"""Container for trace data."""
return str(self.as_dict())
def set_child_id(self, child_key: tuple[str, str], child_run_id: str) -> None:
def set_child_id(self, child_key: str, child_run_id: str) -> None:
"""Set trace id of a nested script run."""
self._child_key = child_key
self._child_run_id = child_run_id
@@ -62,9 +62,10 @@ class TraceElement:
"""Return dictionary version of this TraceElement."""
result: dict[str, Any] = {"path": self.path, "timestamp": self._timestamp}
if self._child_key is not None:
domain, item_id = self._child_key.split(".", 1)
result["child_id"] = {
"domain": self._child_key[0],
"item_id": self._child_key[1],
"domain": domain,
"item_id": item_id,
"run_id": str(self._child_run_id),
}
if self._variables:
@@ -91,8 +92,8 @@ trace_path_stack_cv: ContextVar[list[str] | None] = ContextVar(
)
# Copy of last variables
variables_cv: ContextVar[Any | None] = ContextVar("variables_cv", default=None)
# (domain, item_id) + Run ID
trace_id_cv: ContextVar[tuple[tuple[str, str], str] | None] = ContextVar(
# (domain.item_id, Run ID)
trace_id_cv: ContextVar[tuple[str, str] | None] = ContextVar(
"trace_id_cv", default=None
)
# Reason for stopped script execution
@@ -101,12 +102,12 @@ script_execution_cv: ContextVar[StopReason | None] = ContextVar(
)
def trace_id_set(trace_id: tuple[tuple[str, str], str]) -> None:
def trace_id_set(trace_id: tuple[str, str]) -> None:
"""Set id of the current trace."""
trace_id_cv.set(trace_id)
def trace_id_get() -> tuple[tuple[str, str], str] | None:
def trace_id_get() -> tuple[str, str] | None:
"""Get id if the current trace."""
return trace_id_cv.get()
@@ -182,7 +183,7 @@ def trace_clear() -> None:
script_execution_cv.set(StopReason())
def trace_set_child_id(child_key: tuple[str, str], child_run_id: str) -> None:
def trace_set_child_id(child_key: str, child_run_id: str) -> None:
"""Set child trace_id of TraceElement at the top of the stack."""
node = cast(TraceElement, trace_stack_top(trace_stack_cv))
if node: