Skip to content

Commit b07e75e

Browse files
authored
Change default "extra" contents of activity/workflow logs (#490)
Fixes #489
1 parent 13d18ca commit b07e75e

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

temporalio/activity.py

+19-4
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,14 @@ class LoggerAdapter(logging.LoggerAdapter):
427427
activity_info_on_message: Boolean for whether a string representation of
428428
a dict of some activity info will be appended to each message.
429429
Default is True.
430-
activity_info_on_extra: Boolean for whether an ``activity_info`` value
431-
will be added to the ``extra`` dictionary, making it present on the
432-
``LogRecord.__dict__`` for use by others.
430+
activity_info_on_extra: Boolean for whether a ``temporal_activity``
431+
dictionary value will be added to the ``extra`` dictionary with some
432+
activity info, making it present on the ``LogRecord.__dict__`` for
433+
use by others. Default is True.
434+
full_activity_info_on_extra: Boolean for whether an ``activity_info``
435+
value will be added to the ``extra`` dictionary with the entire
436+
activity info, making it present on the ``LogRecord.__dict__`` for
437+
use by others. Default is False.
433438
"""
434439

435440
def __init__(
@@ -439,17 +444,27 @@ def __init__(
439444
super().__init__(logger, extra or {})
440445
self.activity_info_on_message = True
441446
self.activity_info_on_extra = True
447+
self.full_activity_info_on_extra = False
442448

443449
def process(
444450
self, msg: Any, kwargs: MutableMapping[str, Any]
445451
) -> Tuple[Any, MutableMapping[str, Any]]:
446452
"""Override to add activity details."""
447-
if self.activity_info_on_message or self.activity_info_on_extra:
453+
if (
454+
self.activity_info_on_message
455+
or self.activity_info_on_extra
456+
or self.full_activity_info_on_extra
457+
):
448458
context = _current_context.get(None)
449459
if context:
450460
if self.activity_info_on_message:
451461
msg = f"{msg} ({context.logger_details})"
452462
if self.activity_info_on_extra:
463+
# Extra can be absent or None, this handles both
464+
extra = kwargs.get("extra", None) or {}
465+
extra["temporal_activity"] = context._logger_details
466+
kwargs["extra"] = extra
467+
if self.full_activity_info_on_extra:
453468
# Extra can be absent or None, this handles both
454469
extra = kwargs.get("extra", None) or {}
455470
extra["activity_info"] = context.info()

temporalio/workflow.py

+19-4
Original file line numberDiff line numberDiff line change
@@ -1085,9 +1085,14 @@ class LoggerAdapter(logging.LoggerAdapter):
10851085
workflow_info_on_message: Boolean for whether a string representation of
10861086
a dict of some workflow info will be appended to each message.
10871087
Default is True.
1088-
workflow_info_on_extra: Boolean for whether a ``workflow_info`` value
1089-
will be added to the ``extra`` dictionary, making it present on the
1090-
``LogRecord.__dict__`` for use by others.
1088+
workflow_info_on_extra: Boolean for whether a ``temporal_workflow``
1089+
dictionary value will be added to the ``extra`` dictionary with some
1090+
workflow info, making it present on the ``LogRecord.__dict__`` for
1091+
use by others. Default is True.
1092+
full_workflow_info_on_extra: Boolean for whether a ``workflow_info``
1093+
value will be added to the ``extra`` dictionary with the entire
1094+
workflow info, making it present on the ``LogRecord.__dict__`` for
1095+
use by others. Default is False.
10911096
log_during_replay: Boolean for whether logs should occur during replay.
10921097
Default is False.
10931098
"""
@@ -1099,18 +1104,28 @@ def __init__(
10991104
super().__init__(logger, extra or {})
11001105
self.workflow_info_on_message = True
11011106
self.workflow_info_on_extra = True
1107+
self.full_workflow_info_on_extra = False
11021108
self.log_during_replay = False
11031109

11041110
def process(
11051111
self, msg: Any, kwargs: MutableMapping[str, Any]
11061112
) -> Tuple[Any, MutableMapping[str, Any]]:
11071113
"""Override to add workflow details."""
1108-
if self.workflow_info_on_message or self.workflow_info_on_extra:
1114+
if (
1115+
self.workflow_info_on_message
1116+
or self.workflow_info_on_extra
1117+
or self.full_workflow_info_on_extra
1118+
):
11091119
runtime = _Runtime.maybe_current()
11101120
if runtime:
11111121
if self.workflow_info_on_message:
11121122
msg = f"{msg} ({runtime.logger_details})"
11131123
if self.workflow_info_on_extra:
1124+
# Extra can be absent or None, this handles both
1125+
extra = kwargs.get("extra", None) or {}
1126+
extra["temporal_workflow"] = runtime.logger_details
1127+
kwargs["extra"] = extra
1128+
if self.full_workflow_info_on_extra:
11141129
# Extra can be absent or None, this handles both
11151130
extra = kwargs.get("extra", None) or {}
11161131
extra["workflow_info"] = runtime.workflow_info()

tests/worker/test_activity.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ async def say_hello(name: str) -> str:
814814
assert records[-1].message.startswith(
815815
"Called with arg: Temporal ({'activity_id': '"
816816
)
817-
assert records[-1].__dict__["activity_info"].activity_type == "say_hello"
817+
assert records[-1].__dict__["temporal_activity"]["activity_type"] == "say_hello"
818818

819819

820820
async def test_activity_worker_shutdown(client: Client, worker: ExternalWorker):

tests/worker/test_workflow.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1883,6 +1883,7 @@ async def test_workflow_logging(client: Client, env: WorkflowEnvironment):
18831883
workflow.logger.base_logger.addHandler(handler)
18841884
prev_level = workflow.logger.base_logger.level
18851885
workflow.logger.base_logger.setLevel(logging.INFO)
1886+
workflow.logger.full_workflow_info_on_extra = True
18861887

18871888
def find_log(starts_with: str) -> Optional[logging.LogRecord]:
18881889
for record in cast(List[logging.LogRecord], log_queue.queue):
@@ -1915,9 +1916,12 @@ def find_log(starts_with: str) -> Optional[logging.LogRecord]:
19151916
record = find_log("Signal: signal 1")
19161917
assert (
19171918
record
1918-
and record.__dict__["workflow_info"].workflow_type == "LoggingWorkflow"
1919+
and record.__dict__["temporal_workflow"]["workflow_type"]
1920+
== "LoggingWorkflow"
19191921
and record.funcName == "my_signal"
19201922
)
1923+
# Since we enabled full info, make sure it's there
1924+
assert isinstance(record.__dict__["workflow_info"], workflow.Info)
19211925

19221926
# Clear queue and start a new one with more signals
19231927
log_queue.queue.clear()

0 commit comments

Comments
 (0)