Skip to content

Commit

Permalink
Merging in main
Browse files Browse the repository at this point in the history
  • Loading branch information
ymao1 committed Jan 23, 2023
2 parents 6b74e18 + 164f629 commit 19b8818
Show file tree
Hide file tree
Showing 50 changed files with 1,446 additions and 288 deletions.
41 changes: 41 additions & 0 deletions x-pack/plugins/alerting/server/alert/create_alert_factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('createAlertFactory()', () => {
alerts: {},
logger,
maxAlerts: 1000,
autoRecoverAlerts: true,
});
const result = alertFactory.create('1');
expect(result).toMatchInlineSnapshot(`
Expand All @@ -55,6 +56,7 @@ describe('createAlertFactory()', () => {
},
logger,
maxAlerts: 1000,
autoRecoverAlerts: true,
});
const result = alertFactory.create('1');
expect(result).toMatchInlineSnapshot(`
Expand All @@ -79,6 +81,7 @@ describe('createAlertFactory()', () => {
alerts,
logger,
maxAlerts: 1000,
autoRecoverAlerts: true,
});
alertFactory.create('1');
expect(alerts).toMatchInlineSnapshot(`
Expand All @@ -98,6 +101,7 @@ describe('createAlertFactory()', () => {
alerts: {},
logger,
maxAlerts: 3,
autoRecoverAlerts: true,
});

expect(alertFactory.hasReachedAlertLimit()).toBe(false);
Expand All @@ -117,6 +121,7 @@ describe('createAlertFactory()', () => {
alerts: {},
logger,
maxAlerts: 1000,
autoRecoverAlerts: true,
});
const result = alertFactory.create('1');
expect(result).toEqual({
Expand Down Expand Up @@ -158,6 +163,7 @@ describe('createAlertFactory()', () => {
logger,
canSetRecoveryContext: true,
maxAlerts: 1000,
autoRecoverAlerts: true,
});
const result = alertFactory.create('1');
expect(result).toEqual({
Expand All @@ -184,6 +190,7 @@ describe('createAlertFactory()', () => {
logger,
maxAlerts: 1000,
canSetRecoveryContext: true,
autoRecoverAlerts: true,
});
const result = alertFactory.create('1');
expect(result).toEqual({
Expand All @@ -209,6 +216,7 @@ describe('createAlertFactory()', () => {
logger,
maxAlerts: 1000,
canSetRecoveryContext: true,
autoRecoverAlerts: true,
});
const result = alertFactory.create('1');
expect(result).toEqual({
Expand All @@ -233,6 +241,7 @@ describe('createAlertFactory()', () => {
logger,
maxAlerts: 1000,
canSetRecoveryContext: false,
autoRecoverAlerts: true,
});
const result = alertFactory.create('1');
expect(result).toEqual({
Expand All @@ -259,6 +268,7 @@ describe('createAlertFactory()', () => {
alerts: {},
logger,
maxAlerts: 1000,
autoRecoverAlerts: true,
});

const limit = alertFactory.alertLimit.getValue();
Expand All @@ -276,6 +286,7 @@ describe('createAlertFactory()', () => {
alerts: {},
logger,
maxAlerts: 1000,
autoRecoverAlerts: true,
});

const limit = alertFactory.alertLimit.getValue();
Expand All @@ -290,6 +301,7 @@ describe('createAlertFactory()', () => {
alerts: {},
logger,
maxAlerts: 1000,
autoRecoverAlerts: true,
});

const limit = alertFactory.alertLimit.getValue();
Expand All @@ -298,6 +310,34 @@ describe('createAlertFactory()', () => {
alertFactory.alertLimit.setLimitReached(false);
alertFactory.alertLimit.checkLimitUsage();
});

test('returns empty array if recovered alerts exist but autoRecoverAlerts is false', () => {
const alertFactory = createAlertFactory({
alerts: {},
logger,
maxAlerts: 1000,
canSetRecoveryContext: true,
autoRecoverAlerts: false,
});
const result = alertFactory.create('1');
expect(result).toEqual({
meta: {
flappingHistory: [],
},
state: {},
context: {},
scheduledExecutionOptions: undefined,
id: '1',
});

const { getRecoveredAlerts: getRecoveredAlertsFn } = alertFactory.done();
const recoveredAlerts = getRecoveredAlertsFn!();
expect(Array.isArray(recoveredAlerts)).toBe(true);
expect(recoveredAlerts.length).toEqual(0);
expect(logger.debug).toHaveBeenCalledWith(
`Set autoRecoverAlerts to true on rule type to get access to recovered alerts.`
);
});
});

describe('getPublicAlertFactory', () => {
Expand All @@ -306,6 +346,7 @@ describe('getPublicAlertFactory', () => {
alerts: {},
logger,
maxAlerts: 1000,
autoRecoverAlerts: true,
});

expect(alertFactory.create).toBeDefined();
Expand Down
9 changes: 9 additions & 0 deletions x-pack/plugins/alerting/server/alert/create_alert_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface CreateAlertFactoryOpts<
alerts: Record<string, Alert<State, Context>>;
logger: Logger;
maxAlerts: number;
autoRecoverAlerts: boolean;
canSetRecoveryContext?: boolean;
}

Expand All @@ -63,6 +64,7 @@ export function createAlertFactory<
alerts,
logger,
maxAlerts,
autoRecoverAlerts,
canSetRecoveryContext = false,
}: CreateAlertFactoryOpts<State, Context>): AlertFactory<State, Context, ActionGroupIds> {
// Keep track of which alerts we started with so we can determine which have recovered
Expand Down Expand Up @@ -128,6 +130,12 @@ export function createAlertFactory<
);
return [];
}
if (!autoRecoverAlerts) {
logger.debug(
`Set autoRecoverAlerts to true on rule type to get access to recovered alerts.`
);
return [];
}

const { currentRecoveredAlerts } = processAlerts<
State,
Expand All @@ -140,6 +148,7 @@ export function createAlertFactory<
previouslyRecoveredAlerts: {},
hasReachedAlertLimit,
alertLimit: maxAlerts,
autoRecoverAlerts,
// setFlapping is false, as we only want to use this function to get the recovered alerts
setFlapping: false,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ describe('Legacy Alerts Client', () => {
logger,
maxAlerts: 1000,
canSetRecoveryContext: false,
autoRecoverAlerts: true,
});
});

Expand Down Expand Up @@ -227,6 +228,7 @@ describe('Legacy Alerts Client', () => {
previouslyRecoveredAlerts: {},
hasReachedAlertLimit: false,
alertLimit: 1000,
autoRecoverAlerts: true,
setFlapping: true,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export class LegacyAlertsClient<
alerts: this.alerts,
logger: this.options.logger,
maxAlerts: this.options.maxAlerts,
autoRecoverAlerts: this.options.ruleType.autoRecoverAlerts ?? true,
canSetRecoveryContext: this.options.ruleType.doesSetRecoveryContext ?? false,
});
}
Expand All @@ -121,6 +122,10 @@ export class LegacyAlertsClient<
previouslyRecoveredAlerts: this.recoveredAlertsFromPreviousExecution,
hasReachedAlertLimit: this.alertFactory!.hasReachedAlertLimit(),
alertLimit: this.options.maxAlerts,
autoRecoverAlerts:
this.options.ruleType.autoRecoverAlerts !== undefined
? this.options.ruleType.autoRecoverAlerts
: true,
setFlapping: true,
});

Expand Down
Loading

0 comments on commit 19b8818

Please sign in to comment.