Skip to content

Commit 6a4ea4a

Browse files
authored
Include update info in workflow logging output (#1595)
1 parent 8c68e0c commit 6a4ea4a

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

packages/test/src/test-integration-update.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from '@temporalio/client';
1111
import * as wf from '@temporalio/workflow';
1212
import { temporal } from '@temporalio/proto';
13+
import { LogEntry } from '@temporalio/worker';
1314
import { helpers, makeTestFunction } from './helpers-integration';
1415
import { signalUpdateOrderingWorkflow } from './workflows/signal-update-ordering';
1516
import { signalsActivitiesTimersPromiseOrdering } from './workflows/signals-timers-activities-order';
@@ -19,6 +20,8 @@ import { loadHistory, waitUntil } from './helpers';
1920
// polling/retry strategies result in the expected behavior
2021
const LONG_POLL_EXPIRATION_INTERVAL_SECONDS = 5.0;
2122

23+
const recordedLogs: { [workflowId: string]: LogEntry[] } = {};
24+
2225
const test = makeTestFunction({
2326
workflowsPath: __filename,
2427
workflowEnvironmentOpts: {
@@ -29,6 +32,7 @@ const test = makeTestFunction({
2932
],
3033
},
3134
},
35+
recordedLogs,
3236
});
3337

3438
export const update = wf.defineUpdate<string[], [string]>('update');
@@ -1016,3 +1020,35 @@ test('Can complete update after workflow returns - pre-1.11.0 compatibility', as
10161020
await runReplayHistory({}, hist);
10171021
t.pass();
10181022
});
1023+
1024+
const logUpdate = wf.defineUpdate<[string, string], [string]>('log-update');
1025+
export async function workflowWithLogInUpdate(): Promise<void> {
1026+
const updateHandler = (msg: string): [string, string] => {
1027+
const updateInfo = wf.currentUpdateInfo();
1028+
if (!updateInfo) {
1029+
throw new Error('expected updateInfo to be defined');
1030+
}
1031+
wf.log.info(msg);
1032+
return [updateInfo.id, updateInfo.name];
1033+
};
1034+
wf.setHandler(logUpdate, updateHandler);
1035+
await wf.condition(() => false);
1036+
}
1037+
1038+
test('Workflow Worker logs update info when logging within update handler', async (t) => {
1039+
const { createWorker, startWorkflow } = helpers(t);
1040+
const worker = await createWorker();
1041+
await worker.runUntil(async () => {
1042+
const wfHandle = await startWorkflow(workflowWithLogInUpdate);
1043+
const logMsg = 'log msg';
1044+
const [updateId, updateName] = await wfHandle.executeUpdate(logUpdate, { args: [logMsg] });
1045+
t.true(
1046+
recordedLogs[wfHandle.workflowId].some(
1047+
(logEntry) =>
1048+
logEntry.meta?.updateName === updateName &&
1049+
logEntry.meta?.updateId === updateId &&
1050+
logEntry.message === logMsg
1051+
)
1052+
);
1053+
});
1054+
});

packages/workflow/src/logs.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { type Sink, type Sinks, proxySinks } from './sinks';
55
import { isCancellation } from './errors';
66
import { WorkflowInfo, ContinueAsNew } from './interfaces';
77
import { assertInWorkflowContext } from './global-attributes';
8+
import { currentUpdateInfo, inWorkflowContext } from './workflow';
89

910
export interface WorkflowLogger extends Sink {
1011
trace(message: string, attrs?: Record<string, unknown>): void;
@@ -117,11 +118,20 @@ export function executeWithLifecycleLogging(fn: () => Promise<unknown>): Promise
117118
* Note that this function may be called from outside of the Workflow context (eg. by the worker itself).
118119
*/
119120
export function workflowLogAttributes(info: WorkflowInfo): Record<string, unknown> {
120-
return {
121+
const attributes: { [key: string]: string } = {
121122
namespace: info.namespace,
122123
taskQueue: info.taskQueue,
123124
workflowId: info.workflowId,
124125
runId: info.runId,
125126
workflowType: info.workflowType,
126127
};
128+
if (inWorkflowContext()) {
129+
const updateInfo = currentUpdateInfo();
130+
if (updateInfo) {
131+
// Add update info if it exists
132+
attributes['updateId'] = updateInfo.id;
133+
attributes['updateName'] = updateInfo.name;
134+
}
135+
}
136+
return attributes;
127137
}

0 commit comments

Comments
 (0)