From 1dbc37d8b9eee52c0e63abcf09f1a7f07dce554e Mon Sep 17 00:00:00 2001 From: Ersin Erdal Date: Thu, 28 Mar 2024 00:15:53 +0100 Subject: [PATCH] Report Error to TM --- .../server/lib/rule_execution_status.test.ts | 2 +- .../server/lib/rule_execution_status.ts | 9 +++++ .../server/task_runner/task_runner.test.ts | 12 +++++- .../server/task_runner/task_runner.ts | 39 ++++++++++++------- 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/x-pack/plugins/alerting/server/lib/rule_execution_status.test.ts b/x-pack/plugins/alerting/server/lib/rule_execution_status.test.ts index a9c28588072e6..1742554065f06 100644 --- a/x-pack/plugins/alerting/server/lib/rule_execution_status.test.ts +++ b/x-pack/plugins/alerting/server/lib/rule_execution_status.test.ts @@ -185,7 +185,7 @@ describe('RuleExecutionStatus', () => { ruleResultService, }); expect(status.status).toBe('error'); - expect(status.error).toBe(undefined); + expect(status.error).toEqual({ message: 'an error', reason: 'unknown' }); expect(status.warning).toBe(undefined); }); }); diff --git a/x-pack/plugins/alerting/server/lib/rule_execution_status.ts b/x-pack/plugins/alerting/server/lib/rule_execution_status.ts index 1c63a4ef43e00..4a8869fac452e 100644 --- a/x-pack/plugins/alerting/server/lib/rule_execution_status.ts +++ b/x-pack/plugins/alerting/server/lib/rule_execution_status.ts @@ -15,6 +15,7 @@ import { RawRuleExecutionStatus, RawRule, Rule, + RuleExecutionStatusErrorReasons, } from '../types'; import { getReasonFromError } from './error_with_reason'; import { getEsErrorMessage } from './errors'; @@ -44,6 +45,8 @@ export function executionStatusFromState({ // Check for warning states let warning = null; + let error = null; + // We only have a single warning field so prioritizing the alert circuit breaker over the actions circuit breaker if (stateWithMetrics.metrics.hasReachedAlertLimit) { status = RuleExecutionStatusValues[5]; @@ -70,6 +73,11 @@ export function executionStatusFromState({ const { errors: errorsFromLastRun } = ruleResultService.getLastRunResults(); if (errorsFromLastRun.length > 0) { status = RuleExecutionStatusValues[2]; + // These errors are reported by ruleResultService.addLastRunError, therefore they are landed in successful execution map + error = { + reason: RuleExecutionStatusErrorReasons.Unknown, + message: errorsFromLastRun.join(','), + }; } return { @@ -77,6 +85,7 @@ export function executionStatusFromState({ lastExecutionDate: lastExecutionDate ?? new Date(), status, ...(warning ? { warning } : {}), + ...(error ? { error } : {}), }, metrics: stateWithMetrics.metrics, }; diff --git a/x-pack/plugins/alerting/server/task_runner/task_runner.test.ts b/x-pack/plugins/alerting/server/task_runner/task_runner.test.ts index 3f6ca14084006..8b01d74b8018c 100644 --- a/x-pack/plugins/alerting/server/task_runner/task_runner.test.ts +++ b/x-pack/plugins/alerting/server/task_runner/task_runner.test.ts @@ -3308,12 +3308,12 @@ describe('Task Runner', () => { }); ruleResultService.getLastRunResults.mockImplementation(() => ({ - errors: ['an error occured'], + errors: ['an error occurred'], warnings: [], outcomeMessage: '', })); - await taskRunner.run(); + const runnerResult = await taskRunner.run(); expect(inMemoryMetrics.increment).toHaveBeenCalledTimes(2); expect(inMemoryMetrics.increment.mock.calls[0][0]).toBe(IN_MEMORY_METRICS.RULE_EXECUTIONS); @@ -3322,7 +3322,11 @@ describe('Task Runner', () => { testAlertingEventLogCalls({ status: 'error', softErrorFromLastRun: true, + errorMessage: 'an error occurred', + errorReason: 'unknown', }); + + expect(getErrorSource(runnerResult.taskRunError as Error)).toBe(TaskErrorSource.USER); }); function testAlertingEventLogCalls({ @@ -3386,6 +3390,10 @@ describe('Task Runner', () => { status: { lastExecutionDate: new Date('1970-01-01T00:00:00.000Z'), status, + error: { + message: errorMessage, + reason: errorReason, + }, }, }) ); 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 3a7d5dcec6c7d..8f28393bf3810 100644 --- a/x-pack/plugins/alerting/server/task_runner/task_runner.ts +++ b/x-pack/plugins/alerting/server/task_runner/task_runner.ts @@ -13,12 +13,19 @@ import { Logger } from '@kbn/core/server'; import { ConcreteTaskInstance, createTaskRunError, + TaskErrorSource, throwUnrecoverableError, } from '@kbn/task-manager-plugin/server'; import { nanosToMillis } from '@kbn/event-log-plugin/server'; import { getErrorSource } from '@kbn/task-manager-plugin/server/task_running'; import { ExecutionHandler, RunResult } from './execution_handler'; -import { TaskRunnerContext } from './types'; +import { + RuleTaskInstance, + RuleTaskRunResult, + RuleTaskStateAndMetrics, + RunRuleParams, + TaskRunnerContext, +} from './types'; import { getExecutorServices } from './get_executor_services'; import { ElasticsearchError, @@ -56,12 +63,6 @@ import { import { NormalizedRuleType, UntypedNormalizedRuleType } from '../rule_type_registry'; import { getEsErrorMessage } from '../lib/errors'; import { IN_MEMORY_METRICS, InMemoryMetrics } from '../monitoring'; -import { - RuleTaskInstance, - RuleTaskRunResult, - RuleTaskStateAndMetrics, - RunRuleParams, -} from './types'; import { IExecutionStatusAndMetrics } from '../lib/rule_execution_status'; import { RuleRunMetricsStore } from '../lib/rule_run_metrics_store'; import { AlertingEventLogger } from '../lib/alerting_event_logger/alerting_event_logger'; @@ -72,7 +73,7 @@ import { ILastRun, lastRunFromState, lastRunToRaw } from '../lib/last_run_status import { RunningHandler } from './running_handler'; import { RuleResultService } from '../monitoring/rule_result_service'; import { MaintenanceWindow } from '../application/maintenance_window/types'; -import { getMaintenanceWindows, filterMaintenanceWindowsIds } from './get_maintenance_windows'; +import { filterMaintenanceWindowsIds, getMaintenanceWindows } from './get_maintenance_windows'; import { RuleTypeRunner } from './rule_type_runner'; import { initializeAlertsClient } from '../alerts_client'; @@ -706,11 +707,23 @@ export class TaskRunner< }; const getTaskRunError = (state: Result) => { - return isErr(state) - ? { - taskRunError: createTaskRunError(state.error, getErrorSource(state.error)), - } - : {}; + if (isErr(state)) { + return { + taskRunError: createTaskRunError(state.error, getErrorSource(state.error)), + }; + } + + const { errors: errorsFromLastRun } = this.ruleResult.getLastRunResults(); + if (errorsFromLastRun.length > 0) { + return { + taskRunError: createTaskRunError( + new Error(errorsFromLastRun.join(',')), + TaskErrorSource.USER + ), + }; + } + + return {}; }; return {