Skip to content

Commit

Permalink
Report Error to TM
Browse files Browse the repository at this point in the history
  • Loading branch information
ersin-erdal committed Mar 27, 2024
1 parent 08cd9cc commit 1dbc37d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Expand Down
9 changes: 9 additions & 0 deletions x-pack/plugins/alerting/server/lib/rule_execution_status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
RawRuleExecutionStatus,
RawRule,
Rule,
RuleExecutionStatusErrorReasons,
} from '../types';
import { getReasonFromError } from './error_with_reason';
import { getEsErrorMessage } from './errors';
Expand Down Expand Up @@ -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];
Expand All @@ -70,13 +73,19 @@ 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 {
status: {
lastExecutionDate: lastExecutionDate ?? new Date(),
status,
...(warning ? { warning } : {}),
...(error ? { error } : {}),
},
metrics: stateWithMetrics.metrics,
};
Expand Down
12 changes: 10 additions & 2 deletions x-pack/plugins/alerting/server/task_runner/task_runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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({
Expand Down Expand Up @@ -3386,6 +3390,10 @@ describe('Task Runner', () => {
status: {
lastExecutionDate: new Date('1970-01-01T00:00:00.000Z'),
status,
error: {
message: errorMessage,
reason: errorReason,
},
},
})
);
Expand Down
39 changes: 26 additions & 13 deletions x-pack/plugins/alerting/server/task_runner/task_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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';
Expand All @@ -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';

Expand Down Expand Up @@ -706,11 +707,23 @@ export class TaskRunner<
};

const getTaskRunError = (state: Result<RuleTaskStateAndMetrics, Error>) => {
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 {
Expand Down

0 comments on commit 1dbc37d

Please sign in to comment.