diff --git a/x-pack/plugins/task_manager/server/task_running/task_runner.test.ts b/x-pack/plugins/task_manager/server/task_running/task_runner.test.ts index 73d30b022c626..5cce445e7bb4c 100644 --- a/x-pack/plugins/task_manager/server/task_running/task_runner.test.ts +++ b/x-pack/plugins/task_manager/server/task_running/task_runner.test.ts @@ -1517,6 +1517,54 @@ describe('TaskManagerRunner', () => { `Skipping reschedule for task bar \"${id}\" due to the task expiring` ); }); + + test('Prints debug logs on task start/end', async () => { + const { runner, logger } = await readyToRunStageSetup({ + definitions: { + bar: { + title: 'Bar!', + createTaskRunner: () => ({ + async run() { + return { state: {} }; + }, + }), + }, + }, + }); + await runner.run(); + + expect(logger.debug).toHaveBeenCalledTimes(2); + expect(logger.debug).toHaveBeenNthCalledWith(1, 'Running task bar "foo"', { + tags: ['task:start', 'foo', 'bar'], + }); + expect(logger.debug).toHaveBeenNthCalledWith(2, 'Task bar "foo" ended', { + tags: ['task:end', 'foo', 'bar'], + }); + }); + + test('Prints debug logs on task start/end even if it throws error', async () => { + const { runner, logger } = await readyToRunStageSetup({ + definitions: { + bar: { + title: 'Bar!', + createTaskRunner: () => ({ + async run() { + throw new Error(); + }, + }), + }, + }, + }); + await runner.run(); + + expect(logger.debug).toHaveBeenCalledTimes(2); + expect(logger.debug).toHaveBeenNthCalledWith(1, 'Running task bar "foo"', { + tags: ['task:start', 'foo', 'bar'], + }); + expect(logger.debug).toHaveBeenNthCalledWith(2, 'Task bar "foo" ended', { + tags: ['task:end', 'foo', 'bar'], + }); + }); }); interface TestOpts { diff --git a/x-pack/plugins/task_manager/server/task_running/task_runner.ts b/x-pack/plugins/task_manager/server/task_running/task_runner.ts index b6199f06300f1..d305a49bef55e 100644 --- a/x-pack/plugins/task_manager/server/task_running/task_runner.ts +++ b/x-pack/plugins/task_manager/server/task_running/task_runner.ts @@ -283,7 +283,7 @@ export class TaskManagerRunner implements TaskRunner { }` ); } - this.logger.debug(`Running task ${this}`); + this.logger.debug(`Running task ${this}`, { tags: ['task:start', this.id, this.taskType] }); const apmTrans = apm.startTransaction(this.taskType, TASK_MANAGER_RUN_TRANSACTION_TYPE, { childOf: this.instance.task.traceparent, @@ -324,6 +324,8 @@ export class TaskManagerRunner implements TaskRunner { ); if (apmTrans) apmTrans.end('failure'); return processedResult; + } finally { + this.logger.debug(`Task ${this} ended`, { tags: ['task:end', this.id, this.taskType] }); } }