From 2a51a37bcb94c13350a8fd6066027c5f73abee1d Mon Sep 17 00:00:00 2001 From: Ersin Erdal <92688503+ersin-erdal@users.noreply.github.com> Date: Tue, 10 May 2022 14:49:30 +0200 Subject: [PATCH] Add debug log on task run end (#131639) * Add debug log on task run end --- .../server/task_running/task_runner.test.ts | 48 +++++++++++++++++++ .../server/task_running/task_runner.ts | 4 +- 2 files changed, 51 insertions(+), 1 deletion(-) 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] }); } }