Skip to content

Commit

Permalink
[8.x] Enhance task failure log to include error source in tags (#199406
Browse files Browse the repository at this point in the history
…) (#200082)

# Backport

This will backport the following commits from `main` to `8.x`:
- [Enhance task failure log to include error source in tags
(#199406)](#199406)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Mike
Côté","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-11-13T19:48:18Z","message":"Enhance
task failure log to include error source in tags (#199406)\n\nResolves
https://github.com/elastic/kibana/issues/199346\r\n\r\nIn this PR I'm
adding `user-error` and `framework-error` tags to the\r\nassociated task
failure logs.\r\n\r\n## To verify\r\n\r\nYou can either use the jest
test to observe the returned flags or set\r\nyour logging to JSON and
use the following code samples to test various\r\ntypes of
errors.\r\n\r\nkibana.yml to set logging to JSON\r\n```\r\nlogging:\r\n
appenders:\r\n json-layout:\r\n type: console\r\n layout:\r\n type:
json\r\n root:\r\n appenders: [json-layout]\r\n```\r\n\r\nCode samples
throwing various types of errors.\r\n```\r\ndiff --git
a/x-pack/plugins/alerting/server/task_runner/task_runner.ts
b/x-pack/plugins/alerting/server/task_runner/task_runner.ts\r\nindex
89432e18220..129b53f71b4 100644\r\n---
a/x-pack/plugins/alerting/server/task_runner/task_runner.ts\r\n+++
b/x-pack/plugins/alerting/server/task_runner/task_runner.ts\r\n@@ -649,6
+649,10 @@ export class TaskRunner<\r\n schedule: taskSchedule,\r\n } =
this.taskInstance;\r\n\r\n+ // throw createTaskRunError(new
Error('foo'), TaskErrorSource.USER);\r\n+ // throw
createTaskRunError(new Error('foo'), TaskErrorSource.FRAMEWORK);\r\n+ //
throw new Error('foo');\r\n+\r\n this.logger = createTaskRunnerLogger({
logger: this.logger, tags: [ruleId, this.ruleType.id] });\r\n\r\n let
stateWithMetrics: Result<RuleTaskStateAndMetrics,
Error>;\r\n```","sha":"544525d497d6c833c9a861061876412d3198c5e9","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Feature:Task
Manager","Team:ResponseOps","v9.0.0","backport:version","v8.17.0"],"title":"Enhance
task failure log to include error source in
tags","number":199406,"url":"https://github.com/elastic/kibana/pull/199406","mergeCommit":{"message":"Enhance
task failure log to include error source in tags (#199406)\n\nResolves
https://github.com/elastic/kibana/issues/199346\r\n\r\nIn this PR I'm
adding `user-error` and `framework-error` tags to the\r\nassociated task
failure logs.\r\n\r\n## To verify\r\n\r\nYou can either use the jest
test to observe the returned flags or set\r\nyour logging to JSON and
use the following code samples to test various\r\ntypes of
errors.\r\n\r\nkibana.yml to set logging to JSON\r\n```\r\nlogging:\r\n
appenders:\r\n json-layout:\r\n type: console\r\n layout:\r\n type:
json\r\n root:\r\n appenders: [json-layout]\r\n```\r\n\r\nCode samples
throwing various types of errors.\r\n```\r\ndiff --git
a/x-pack/plugins/alerting/server/task_runner/task_runner.ts
b/x-pack/plugins/alerting/server/task_runner/task_runner.ts\r\nindex
89432e18220..129b53f71b4 100644\r\n---
a/x-pack/plugins/alerting/server/task_runner/task_runner.ts\r\n+++
b/x-pack/plugins/alerting/server/task_runner/task_runner.ts\r\n@@ -649,6
+649,10 @@ export class TaskRunner<\r\n schedule: taskSchedule,\r\n } =
this.taskInstance;\r\n\r\n+ // throw createTaskRunError(new
Error('foo'), TaskErrorSource.USER);\r\n+ // throw
createTaskRunError(new Error('foo'), TaskErrorSource.FRAMEWORK);\r\n+ //
throw new Error('foo');\r\n+\r\n this.logger = createTaskRunnerLogger({
logger: this.logger, tags: [ruleId, this.ruleType.id] });\r\n\r\n let
stateWithMetrics: Result<RuleTaskStateAndMetrics,
Error>;\r\n```","sha":"544525d497d6c833c9a861061876412d3198c5e9"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/199406","number":199406,"mergeCommit":{"message":"Enhance
task failure log to include error source in tags (#199406)\n\nResolves
https://github.com/elastic/kibana/issues/199346\r\n\r\nIn this PR I'm
adding `user-error` and `framework-error` tags to the\r\nassociated task
failure logs.\r\n\r\n## To verify\r\n\r\nYou can either use the jest
test to observe the returned flags or set\r\nyour logging to JSON and
use the following code samples to test various\r\ntypes of
errors.\r\n\r\nkibana.yml to set logging to JSON\r\n```\r\nlogging:\r\n
appenders:\r\n json-layout:\r\n type: console\r\n layout:\r\n type:
json\r\n root:\r\n appenders: [json-layout]\r\n```\r\n\r\nCode samples
throwing various types of errors.\r\n```\r\ndiff --git
a/x-pack/plugins/alerting/server/task_runner/task_runner.ts
b/x-pack/plugins/alerting/server/task_runner/task_runner.ts\r\nindex
89432e18220..129b53f71b4 100644\r\n---
a/x-pack/plugins/alerting/server/task_runner/task_runner.ts\r\n+++
b/x-pack/plugins/alerting/server/task_runner/task_runner.ts\r\n@@ -649,6
+649,10 @@ export class TaskRunner<\r\n schedule: taskSchedule,\r\n } =
this.taskInstance;\r\n\r\n+ // throw createTaskRunError(new
Error('foo'), TaskErrorSource.USER);\r\n+ // throw
createTaskRunError(new Error('foo'), TaskErrorSource.FRAMEWORK);\r\n+ //
throw new Error('foo');\r\n+\r\n this.logger = createTaskRunnerLogger({
logger: this.logger, tags: [ruleId, this.ruleType.id] });\r\n\r\n let
stateWithMetrics: Result<RuleTaskStateAndMetrics,
Error>;\r\n```","sha":"544525d497d6c833c9a861061876412d3198c5e9"}},{"branch":"8.x","label":"v8.17.0","branchLabelMappingKey":"^v8.17.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Mike Côté <[email protected]>
  • Loading branch information
kibanamachine and mikecote authored Nov 14, 2024
1 parent da7fdf3 commit 19b154f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -930,9 +930,33 @@ describe('TaskManagerRunner', () => {
const loggerCall = logger.error.mock.calls[0][0];
const loggerMeta = logger.error.mock.calls[0][1];
expect(loggerCall as string).toMatchInlineSnapshot(`"Task bar \\"foo\\" failed: Error: rar"`);
expect(loggerMeta?.tags).toEqual(['bar', 'foo', 'task-run-failed']);
expect(loggerMeta?.tags).toEqual(['bar', 'foo', 'task-run-failed', 'framework-error']);
expect(loggerMeta?.error?.stack_trace).toBeDefined();
});
test('logs user errors as expected when task fails', async () => {
const { runner, logger } = await readyToRunStageSetup({
instance: {
params: { a: 'b' },
state: { hey: 'there' },
},
definitions: {
bar: {
title: 'Bar!',
createTaskRunner: () => ({
async run() {
throw createTaskRunError(new Error('rar'), TaskErrorSource.USER);
},
}),
},
},
});
await runner.run();

const loggerCall = logger.error.mock.calls[0][0];
const loggerMeta = logger.error.mock.calls[0][1];
expect(loggerCall as string).toMatchInlineSnapshot(`"Task bar \\"foo\\" failed: Error: rar"`);
expect(loggerMeta?.tags).toEqual(['bar', 'foo', 'task-run-failed', 'user-error']);
});
test('provides execution context on run', async () => {
const { runner } = await readyToRunStageSetup({
definitions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ import {
TaskStatus,
} from '../task';
import { TaskTypeDictionary } from '../task_type_dictionary';
import { isUnrecoverableError } from './errors';
import { isUnrecoverableError, isUserError } from './errors';
import { CLAIM_STRATEGY_MGET, type TaskManagerConfig } from '../config';
import { TaskValidator } from '../task_validator';
import { getRetryAt, getRetryDate, getTimeout } from '../lib/get_retry_at';
import { getNextRunAt } from '../lib/get_next_run_at';
import { TaskErrorSource } from '../../common/constants';

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

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

0 comments on commit 19b154f

Please sign in to comment.