Skip to content

Fixing indent issue with kwargs["extra"] #789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions temporalio/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1361,9 +1361,9 @@ def process(
if self.workflow_info_on_extra:
extra.setdefault("temporal_workflow", {}).update(update_details)

kwargs["extra"] = {**extra, **(kwargs.get("extra") or {})}
if msg_extra:
msg = f"{msg} ({msg_extra})"
kwargs["extra"] = {**extra, **(kwargs.get("extra") or {})}
if msg_extra:
msg = f"{msg} ({msg_extra})"
return (msg, kwargs)

def isEnabledFor(self, level: int) -> bool:
Expand Down
77 changes: 55 additions & 22 deletions tests/worker/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1948,8 +1948,29 @@ def find_log(self, starts_with: str) -> Optional[logging.LogRecord]:
return None


async def test_workflow_logging(client: Client, env: WorkflowEnvironment):
workflow.logger.full_workflow_info_on_extra = True
@pytest.mark.parametrize(
"with_workflow_info",
[True, False],
)
async def test_workflow_logging(
client: Client, env: WorkflowEnvironment, with_workflow_info: bool
):
orig_on_message = workflow.logger.workflow_info_on_message
orig_on_extra = workflow.logger.workflow_info_on_extra
orig_full_on_extra = workflow.logger.full_workflow_info_on_extra

try:
workflow.logger.workflow_info_on_message = with_workflow_info
workflow.logger.workflow_info_on_extra = with_workflow_info
workflow.logger.full_workflow_info_on_extra = with_workflow_info
await _do_workflow_logging_test(client, with_workflow_info)
finally:
workflow.logger.workflow_info_on_message = orig_on_message
workflow.logger.workflow_info_on_extra = orig_on_extra
workflow.logger.full_workflow_info_on_extra = orig_full_on_extra


async def _do_workflow_logging_test(client: Client, with_workflow_info: bool):
with LogCapturer().logs_captured(
workflow.logger.base_logger, activity.logger.base_logger
) as capturer:
Expand All @@ -1976,30 +1997,42 @@ async def test_workflow_logging(client: Client, env: WorkflowEnvironment):
assert "signal 2" == await handle.query(LoggingWorkflow.last_signal)

# Confirm logs were produced
assert capturer.find_log("Signal: signal 1 ({'attempt':")
assert capturer.find_log("Signal: signal 1")
assert capturer.find_log("Signal: signal 2")
assert capturer.find_log("Update: update 1")
assert capturer.find_log("Update: update 2")
assert not capturer.find_log("Signal: signal 3")
# Also make sure it has some workflow info and correct funcName
record = capturer.find_log("Signal: signal 1")
assert (
record
and record.__dict__["temporal_workflow"]["workflow_type"]
== "LoggingWorkflow"
and record.funcName == "my_signal"
)
# Since we enabled full info, make sure it's there
assert isinstance(record.__dict__["workflow_info"], workflow.Info)
# Check the log emitted by the update execution.
record = capturer.find_log("Update: update 1")
assert (
record
and record.__dict__["temporal_workflow"]["update_id"] == "update-1"
and record.__dict__["temporal_workflow"]["update_name"] == "my_update"
and "'update_id': 'update-1'" in record.message
and "'update_name': 'my_update'" in record.message
)

if with_workflow_info:
record = capturer.find_log("Signal: signal 1 ({'attempt':")
assert (
record
and record.__dict__["temporal_workflow"]["workflow_type"]
== "LoggingWorkflow"
and record.funcName == "my_signal"
)
# Since we enabled full info, make sure it's there
assert isinstance(record.__dict__["workflow_info"], workflow.Info)

# Check the log emitted by the update execution.
record = capturer.find_log("Update: update 1")
assert (
record
and record.__dict__["temporal_workflow"]["update_id"] == "update-1"
and record.__dict__["temporal_workflow"]["update_name"] == "my_update"
and "'update_id': 'update-1'" in record.message
and "'update_name': 'my_update'" in record.message
)
else:
record = capturer.find_log("Signal: signal 1")
assert record and "temporal_workflow" not in record.__dict__
assert record and "workflow_info" not in record.__dict__

record = capturer.find_log("Update: update 1")
assert record and "temporal_workflow" not in record.__dict__
assert record and "workflow_info" not in record.__dict__
assert "'update_id': 'update-1'" not in record.message
assert "'update_name': 'my_update'" not in record.message

# Clear queue and start a new one with more signals
capturer.log_queue.queue.clear()
Expand Down