Skip to content

Commit

Permalink
PR feedback - configurable cache interval
Browse files Browse the repository at this point in the history
  • Loading branch information
ymao1 committed Sep 13, 2024
1 parent 60ce3e6 commit cf77b09
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 7 deletions.
3 changes: 3 additions & 0 deletions x-pack/plugins/alerting/server/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ describe('config validation', () => {
},
},
},
"rulesSettings": Object {
"cacheInterval": 60000,
},
}
`);
});
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/alerting/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { schema, TypeOf } from '@kbn/config-schema';
import { validateDurationSchema, parseDuration } from './lib';
import { DEFAULT_CACHE_INTERVAL_MS } from './rules_settings';

export const DEFAULT_MAX_ALERTS = 1000;
const ONE_DAY_IN_MS = 24 * 60 * 60 * 1000;
Expand Down Expand Up @@ -74,6 +75,9 @@ export const configSchema = schema.object({
enableFrameworkAlerts: schema.boolean({ defaultValue: true }),
cancelAlertsOnRuleTimeout: schema.boolean({ defaultValue: true }),
rules: rulesSchema,
rulesSettings: schema.object({
cacheInterval: schema.number({ defaultValue: DEFAULT_CACHE_INTERVAL_MS }),
}),
});

export type AlertingConfig = TypeOf<typeof configSchema>;
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/alerting/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@ export class AlertingPlugin {
maxEphemeralActionsPerRule: this.config.maxEphemeralActionsPerAlert,
ruleTypeRegistry: this.ruleTypeRegistry!,
rulesSettingsService: new RulesSettingsService({
cacheInterval: this.config.rulesSettings.cacheInterval,
getRulesSettingsClientWithRequest,
isServerless: !!plugins.serverless,
logger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import {
} from '../types';
import { withAlertingSpan } from '../task_runner/lib';

const CACHE_INTERVAL_MS = 60000; // 1 minute cache
export const DEFAULT_CACHE_INTERVAL_MS = 60000; // 1 minute cache

export interface RulesSettingsServiceConstructorOptions {
readonly isServerless: boolean;
cacheInterval?: number;
logger: Logger;
getRulesSettingsClientWithRequest(request: KibanaRequest): RulesSettingsClientApi;
}
Expand All @@ -32,10 +33,14 @@ interface Settings {
type LastUpdatedSettings = Settings & { lastUpdated: number };

export class RulesSettingsService {
private cacheIntervalMs = DEFAULT_CACHE_INTERVAL_MS;
private defaultQueryDelaySettings: RulesSettingsQueryDelayProperties;
private settings: Map<string, LastUpdatedSettings> = new Map();

constructor(private readonly options: RulesSettingsServiceConstructorOptions) {
if (options.cacheInterval) {
this.cacheIntervalMs = options.cacheInterval;
}
this.defaultQueryDelaySettings = options.isServerless
? DEFAULT_SERVERLESS_QUERY_DELAY_SETTINGS
: DEFAULT_QUERY_DELAY_SETTINGS;
Expand All @@ -49,7 +54,7 @@ export class RulesSettingsService {
const currentFlappingSettings = settingsFromLastUpdate.flappingSettings;
const currentQueryDelaySettings = settingsFromLastUpdate.queryDelaySettings;

if (now - lastUpdated >= CACHE_INTERVAL_MS) {
if (now - lastUpdated >= this.cacheIntervalMs) {
// cache expired, refetch settings
try {
return await this.fetchSettings(request, spaceId, now);
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/alerting/server/test_utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,6 @@ export function generateAlertingConfig(): AlertingConfig {
},
},
},
rulesSettings: { cacheInterval: 60000 },
};
}
1 change: 1 addition & 0 deletions x-pack/test/alerting_api_integration/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ export function createTestConfig(name: string, options: CreateTestConfigOptions)
{ id: 'test.capped', max: '1' },
])}`,
`--xpack.alerting.enableFrameworkAlerts=true`,
`--xpack.alerting.rulesSettings.cacheInterval=10000`,
`--xpack.actions.enabledActionTypes=${JSON.stringify(enabledActionTypes)}`,
`--xpack.actions.rejectUnauthorized=${rejectUnauthorized}`,
`--xpack.actions.microsoftGraphApiUrl=${servers.kibana.protocol}://${servers.kibana.hostname}:${servers.kibana.port}/api/_actions-FTS-external-service-simulators/exchange/users/test@/sendMail`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default function createAlertsAsDataInstallResourcesTest({ getService }: F
it(`should write alert docs during rule execution with flapping.enabled: ${enableFlapping}`, async () => {
await setFlappingSettings(enableFlapping);
// wait so cache expires
await setTimeoutAsync(60000);
await setTimeoutAsync(10000);

const pattern = {
alertA: [true, true, true], // stays active across executions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default function createAlertsAsDataFlappingTest({ getService }: FtrProvid
})
.expect(200);
// wait so cache expires
await setTimeoutAsync(60000);
await setTimeoutAsync(10000);

const pattern = {
alertA: [true, false, false, true, false, true, false, true, false].concat(
Expand Down Expand Up @@ -192,7 +192,7 @@ export default function createAlertsAsDataFlappingTest({ getService }: FtrProvid
})
.expect(200);
// wait so cache expires
await setTimeoutAsync(60000);
await setTimeoutAsync(10000);

const pattern = {
alertA: [true, false, false, true, false, true, false, true, false, true].concat(
Expand Down Expand Up @@ -322,7 +322,7 @@ export default function createAlertsAsDataFlappingTest({ getService }: FtrProvid
})
.expect(200);
// wait so cache expires
await setTimeoutAsync(60000);
await setTimeoutAsync(10000);

const pattern = {
alertA: [true, false, true, false, false, false, false, false, false],
Expand Down Expand Up @@ -382,7 +382,7 @@ export default function createAlertsAsDataFlappingTest({ getService }: FtrProvid
})
.expect(200);
// wait so cache expires
await setTimeoutAsync(60000);
await setTimeoutAsync(10000);

const pattern = {
alertA: [true, false, false, true, false, true, false, true, false].concat(
Expand Down

0 comments on commit cf77b09

Please sign in to comment.