Skip to content

Commit 544525d

Browse files
authored
Enhance task failure log to include error source in tags (#199406)
Resolves #199346 In this PR I'm adding `user-error` and `framework-error` tags to the associated task failure logs. ## To verify You can either use the jest test to observe the returned flags or set your logging to JSON and use the following code samples to test various types of errors. kibana.yml to set logging to JSON ``` logging: appenders: json-layout: type: console layout: type: json root: appenders: [json-layout] ``` Code samples throwing various types of errors. ``` diff --git a/x-pack/plugins/alerting/server/task_runner/task_runner.ts b/x-pack/plugins/alerting/server/task_runner/task_runner.ts index 89432e1..129b53f 100644 --- a/x-pack/plugins/alerting/server/task_runner/task_runner.ts +++ b/x-pack/plugins/alerting/server/task_runner/task_runner.ts @@ -649,6 +649,10 @@ export class TaskRunner< schedule: taskSchedule, } = this.taskInstance; + // throw createTaskRunError(new Error('foo'), TaskErrorSource.USER); + // throw createTaskRunError(new Error('foo'), TaskErrorSource.FRAMEWORK); + // throw new Error('foo'); + this.logger = createTaskRunnerLogger({ logger: this.logger, tags: [ruleId, this.ruleType.id] }); let stateWithMetrics: Result<RuleTaskStateAndMetrics, Error>; ```
1 parent 6550193 commit 544525d

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

x-pack/plugins/task_manager/server/task_running/task_runner.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,9 +930,33 @@ describe('TaskManagerRunner', () => {
930930
const loggerCall = logger.error.mock.calls[0][0];
931931
const loggerMeta = logger.error.mock.calls[0][1];
932932
expect(loggerCall as string).toMatchInlineSnapshot(`"Task bar \\"foo\\" failed: Error: rar"`);
933-
expect(loggerMeta?.tags).toEqual(['bar', 'foo', 'task-run-failed']);
933+
expect(loggerMeta?.tags).toEqual(['bar', 'foo', 'task-run-failed', 'framework-error']);
934934
expect(loggerMeta?.error?.stack_trace).toBeDefined();
935935
});
936+
test('logs user errors as expected when task fails', async () => {
937+
const { runner, logger } = await readyToRunStageSetup({
938+
instance: {
939+
params: { a: 'b' },
940+
state: { hey: 'there' },
941+
},
942+
definitions: {
943+
bar: {
944+
title: 'Bar!',
945+
createTaskRunner: () => ({
946+
async run() {
947+
throw createTaskRunError(new Error('rar'), TaskErrorSource.USER);
948+
},
949+
}),
950+
},
951+
},
952+
});
953+
await runner.run();
954+
955+
const loggerCall = logger.error.mock.calls[0][0];
956+
const loggerMeta = logger.error.mock.calls[0][1];
957+
expect(loggerCall as string).toMatchInlineSnapshot(`"Task bar \\"foo\\" failed: Error: rar"`);
958+
expect(loggerMeta?.tags).toEqual(['bar', 'foo', 'task-run-failed', 'user-error']);
959+
});
936960
test('provides execution context on run', async () => {
937961
const { runner } = await readyToRunStageSetup({
938962
definitions: {

x-pack/plugins/task_manager/server/task_running/task_runner.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@ import {
5555
TaskStatus,
5656
} from '../task';
5757
import { TaskTypeDictionary } from '../task_type_dictionary';
58-
import { isUnrecoverableError } from './errors';
58+
import { isUnrecoverableError, isUserError } from './errors';
5959
import { CLAIM_STRATEGY_MGET, type TaskManagerConfig } from '../config';
6060
import { TaskValidator } from '../task_validator';
6161
import { getRetryAt, getRetryDate, getTimeout } from '../lib/get_retry_at';
6262
import { getNextRunAt } from '../lib/get_next_run_at';
63+
import { TaskErrorSource } from '../../common/constants';
6364

6465
export const EMPTY_RUN_RESULT: SuccessfulRunResult = { state: {} };
6566

@@ -397,8 +398,9 @@ export class TaskManagerRunner implements TaskRunner {
397398
if (apmTrans) apmTrans.end('success');
398399
return processedResult;
399400
} catch (err) {
401+
const errorSource = isUserError(err) ? TaskErrorSource.USER : TaskErrorSource.FRAMEWORK;
400402
this.logger.error(`Task ${this} failed: ${err}`, {
401-
tags: [this.taskType, this.instance.task.id, 'task-run-failed'],
403+
tags: [this.taskType, this.instance.task.id, 'task-run-failed', `${errorSource}-error`],
402404
error: { stack_trace: err.stack },
403405
});
404406
// in error scenario, we can not get the RunResult

0 commit comments

Comments
 (0)