Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let addLastRunError to report user errors #180040

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions x-pack/plugins/alerting/server/lib/last_run_status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const getRuleResultService = ({
const ruleResultService = new RuleResultService();
const { addLastRunError, addLastRunWarning, setLastRunOutcomeMessage } =
ruleResultService.getLastRunSetters();
errors.forEach((error) => addLastRunError(error));
errors.forEach((error) => addLastRunError(error.message));
warnings.forEach((warning) => addLastRunWarning(warning));
setLastRunOutcomeMessage(outcomeMessage);
return ruleResultService;
Expand Down Expand Up @@ -250,7 +250,7 @@ describe('lastRunFromState', () => {
metrics: getMetrics({ hasReachedAlertLimit: true }),
},
getRuleResultService({
errors: ['MOCK_ERROR'],
errors: [{ message: 'MOCK_ERROR', userError: false }],
outcomeMessage: 'Rule execution reported an error',
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function executionStatusFromState({
// These errors are reported by ruleResultService.addLastRunError, therefore they are landed in successful execution map
error = {
reason: RuleExecutionStatusErrorReasons.Unknown,
message: errorsFromLastRun.join(','),
message: errorsFromLastRun.map((lastRunError) => lastRunError.message).join(','),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ describe('RuleResultService', () => {

test('should return errors array with added error', () => {
lastRunSetters.addLastRunError('First error');
expect(ruleResultService.getLastRunErrors()).toEqual(['First error']);
expect(ruleResultService.getLastRunErrors()).toEqual([
{ message: 'First error', userError: false },
]);
});

test('should return errors array with added user error', () => {
lastRunSetters.addLastRunError('First error', true);
expect(ruleResultService.getLastRunErrors()).toEqual([
{ message: 'First error', userError: true },
]);
});

test('should return warnings array with added warning', () => {
Expand All @@ -49,7 +58,12 @@ describe('RuleResultService', () => {
lastRunSetters.addLastRunWarning('warning');
lastRunSetters.setLastRunOutcomeMessage('outcome message');
const expectedLastRun: RuleResultServiceResults = {
errors: ['error'],
errors: [
{
message: 'error',
userError: false,
},
],
warnings: ['warning'],
outcomeMessage: 'outcome message',
};
Expand All @@ -63,7 +77,16 @@ describe('RuleResultService', () => {
lastRunSetters.setLastRunOutcomeMessage('second outcome message');
lastRunSetters.setLastRunOutcomeMessage('last outcome message');
const expectedLastRun = {
errors: ['first error', 'second error'],
errors: [
{
message: 'first error',
userError: false,
},
{
message: 'second error',
userError: false,
},
],
warnings: [],
outcomeMessage: 'last outcome message',
};
Expand Down
15 changes: 10 additions & 5 deletions x-pack/plugins/alerting/server/monitoring/rule_result_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@
import { PublicLastRunSetters } from '../types';

export interface RuleResultServiceResults {
errors: string[];
errors: LastRunError[];
warnings: string[];
outcomeMessage: string;
}

interface LastRunError {
message: string;
userError?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional nit:

Suggested change
userError?: boolean;
userError: boolean;

}

export class RuleResultService {
private errors: string[] = [];
private errors: LastRunError[] = [];
private warnings: string[] = [];
private outcomeMessage: string = '';

public getLastRunErrors(): string[] {
public getLastRunErrors(): LastRunError[] {
return this.errors;
}

Expand Down Expand Up @@ -46,8 +51,8 @@ export class RuleResultService {
};
}

private addLastRunError(error: string) {
this.errors.push(error);
private addLastRunError(message: string, userError: boolean = false) {
this.errors.push({ message, userError });
}

private addLastRunWarning(warning: string) {
Expand Down
32 changes: 30 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 @@ -3311,7 +3311,10 @@ describe('Task Runner', () => {
});

ruleResultService.getLastRunResults.mockImplementation(() => ({
errors: ['an error occurred'],
errors: [
{ message: 'an error occurred', userError: false },
{ message: 'second error occurred', userError: true },
],
warnings: [],
outcomeMessage: '',
}));
Expand All @@ -3325,13 +3328,38 @@ describe('Task Runner', () => {
testAlertingEventLogCalls({
status: 'error',
softErrorFromLastRun: true,
errorMessage: 'an error occurred',
errorMessage: 'an error occurred,second error occurred',
errorReason: 'unknown',
});

expect(getErrorSource(runnerResult.taskRunError as Error)).toBe(TaskErrorSource.FRAMEWORK);
});

test('returns user error if all the errors are user error', async () => {
rulesClient.getAlertFromRaw.mockReturnValue(mockedRuleTypeSavedObject as Rule);
encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValue(mockedRawRuleSO);

const taskRunner = new TaskRunner({
ruleType,
taskInstance: mockedTaskInstance,
context: taskRunnerFactoryInitializerParams,
inMemoryMetrics,
});

ruleResultService.getLastRunResults.mockImplementation(() => ({
errors: [
{ message: 'an error occurred', userError: true },
{ message: 'second error occurred', userError: true },
],
warnings: [],
outcomeMessage: '',
}));

const runnerResult = await taskRunner.run();

expect(getErrorSource(runnerResult.taskRunError as Error)).toBe(TaskErrorSource.USER);
});

function testAlertingEventLogCalls({
ruleContext = alertingEventLoggerInitializer,
activeAlerts = 0,
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/alerting/server/task_runner/task_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,10 +715,11 @@ export class TaskRunner<

const { errors: errorsFromLastRun } = this.ruleResult.getLastRunResults();
if (errorsFromLastRun.length > 0) {
const isUserError = !errorsFromLastRun.some((lastRunError) => !lastRunError.userError);
return {
taskRunError: createTaskRunError(
new Error(errorsFromLastRun.join(',')),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: do we need to map here as well?

Suggested change
new Error(errorsFromLastRun.join(',')),
new Error(errorsFromLastRun.map((lastRunError) => lastRunError.message).join(',')),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, fixed.

TaskErrorSource.FRAMEWORK
isUserError ? TaskErrorSource.USER : TaskErrorSource.FRAMEWORK
),
};
}
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/alerting/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ export interface PublicMetricsSetters {
}

export interface PublicLastRunSetters {
addLastRunError: (outcome: string) => void;
addLastRunError: (message: string, userError?: boolean) => void;
addLastRunWarning: (outcomeMsg: string) => void;
setLastRunOutcomeMessage: (warning: string) => void;
}
Expand Down