From d7c9a8c2c5237699394e9a063204f236597a8a4a Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Thu, 21 Nov 2024 07:39:23 -0500 Subject: [PATCH 01/12] Alerting plugin changes --- .../alerts_client/alerts_client.test.ts | 82 +++++++++++++++---- .../server/alerts_client/alerts_client.ts | 7 +- .../alerts_service/alerts_service.test.ts | 29 +++++++ .../server/alerts_service/alerts_service.ts | 4 + x-pack/plugins/alerting/server/plugin.ts | 1 + .../task_runner/ad_hoc_task_runner.test.ts | 2 +- .../task_runner_alerts_client.test.ts | 2 +- 7 files changed, 108 insertions(+), 19 deletions(-) diff --git a/x-pack/plugins/alerting/server/alerts_client/alerts_client.test.ts b/x-pack/plugins/alerting/server/alerts_client/alerts_client.test.ts index 36bf830e47f7..903c8764d801 100644 --- a/x-pack/plugins/alerting/server/alerts_client/alerts_client.test.ts +++ b/x-pack/plugins/alerting/server/alerts_client/alerts_client.test.ts @@ -347,6 +347,7 @@ describe('Alerts Client', () => { rule: alertRuleData, kibanaVersion: '8.9.0', spaceId: 'space1', + isServerless: false, dataStreamAdapter: getDataStreamAdapter({ useDataStreamForAlerts }), }; maintenanceWindowsService.getMaintenanceWindows.mockReturnValue({ @@ -543,10 +544,58 @@ describe('Alerts Client', () => { }); describe('persistAlerts()', () => { - test('should index new alerts', async () => { - const alertsClient = new AlertsClient<{}, {}, {}, 'default', 'recovered'>( - alertsClientParams - ); + test('should index new alerts with refresh: wait_for in stateful', async () => { + const alertsClient = new AlertsClient<{}, {}, {}, 'default', 'recovered'>({ + ...alertsClientParams, + isServerless: false, + }); + + await alertsClient.initializeExecution(defaultExecutionOpts); + + // Report 2 new alerts + const alertExecutorService = alertsClient.factory(); + alertExecutorService.create('1').scheduleActions('default'); + alertExecutorService.create('2').scheduleActions('default'); + + await alertsClient.processAlerts(processAlertsOpts); + alertsClient.logAlerts(logAlertsOpts); + + await alertsClient.persistAlerts(); + + const { alertsToReturn } = alertsClient.getAlertsToSerialize(); + const uuid1 = alertsToReturn['1'].meta?.uuid; + const uuid2 = alertsToReturn['2'].meta?.uuid; + + expect(clusterClient.bulk).toHaveBeenCalledWith({ + index: '.alerts-test.alerts-default', + refresh: 'wait_for', + require_alias: !useDataStreamForAlerts, + body: [ + { + create: { _id: uuid1, ...(useDataStreamForAlerts ? {} : { require_alias: true }) }, + }, + // new alert doc + getNewIndexedAlertDoc({ [ALERT_UUID]: uuid1 }), + { + create: { _id: uuid2, ...(useDataStreamForAlerts ? {} : { require_alias: true }) }, + }, + // new alert doc + getNewIndexedAlertDoc({ [ALERT_UUID]: uuid2, [ALERT_INSTANCE_ID]: '2' }), + ], + }); + expect(maintenanceWindowsService.getMaintenanceWindows).toHaveBeenCalledWith({ + eventLogger: alertingEventLogger, + request: fakeRequest, + ruleTypeCategory: 'test', + spaceId: 'space1', + }); + }); + + test('should index new alerts with refresh: true in stateless', async () => { + const alertsClient = new AlertsClient<{}, {}, {}, 'default', 'recovered'>({ + ...alertsClientParams, + isServerless: true, + }); await alertsClient.initializeExecution(defaultExecutionOpts); @@ -659,7 +708,7 @@ describe('Alerts Client', () => { expect(clusterClient.bulk).toHaveBeenCalledWith({ index: '.alerts-test.alerts-default', - refresh: true, + refresh: 'wait_for', require_alias: !useDataStreamForAlerts, body: [ { @@ -732,7 +781,7 @@ describe('Alerts Client', () => { expect(clusterClient.bulk).toHaveBeenCalledWith({ index: '.alerts-test.alerts-default', - refresh: true, + refresh: 'wait_for', require_alias: !useDataStreamForAlerts, body: [ { @@ -867,7 +916,7 @@ describe('Alerts Client', () => { expect(clusterClient.bulk).toHaveBeenCalledWith({ index: '.alerts-test.alerts-default', - refresh: true, + refresh: 'wait_for', require_alias: !useDataStreamForAlerts, body: [ { @@ -940,7 +989,7 @@ describe('Alerts Client', () => { expect(clusterClient.bulk).toHaveBeenCalledWith({ index: '.alerts-test.alerts-default', - refresh: true, + refresh: 'wait_for', require_alias: !useDataStreamForAlerts, body: [ { @@ -1039,7 +1088,7 @@ describe('Alerts Client', () => { expect(clusterClient.bulk).toHaveBeenCalledWith({ index: '.alerts-test.alerts-default', - refresh: true, + refresh: 'wait_for', require_alias: !useDataStreamForAlerts, body: [ { @@ -1196,7 +1245,7 @@ describe('Alerts Client', () => { expect(clusterClient.bulk).toHaveBeenCalledWith({ index: '.alerts-test.alerts-default', - refresh: true, + refresh: 'wait_for', require_alias: !useDataStreamForAlerts, body: [ { @@ -1314,7 +1363,7 @@ describe('Alerts Client', () => { expect(clusterClient.bulk).toHaveBeenCalledWith({ index: '.alerts-test.alerts-default', - refresh: true, + refresh: 'wait_for', require_alias: !useDataStreamForAlerts, body: [ { @@ -1518,7 +1567,7 @@ describe('Alerts Client', () => { expect(clusterClient.bulk).toHaveBeenCalledWith({ index: '.alerts-test.alerts-default', - refresh: true, + refresh: 'wait_for', require_alias: !useDataStreamForAlerts, body: [ { @@ -1602,6 +1651,7 @@ describe('Alerts Client', () => { shouldWrite: false, }, }, + isServerless: false, request: fakeRequest, namespace: 'default', rule: alertRuleData, @@ -2451,7 +2501,7 @@ describe('Alerts Client', () => { expect(clusterClient.bulk).toHaveBeenCalledWith({ index: '.alerts-test.alerts-default', - refresh: true, + refresh: 'wait_for', require_alias: !useDataStreamForAlerts, body: [ { @@ -2725,7 +2775,7 @@ describe('Alerts Client', () => { expect(clusterClient.bulk).toHaveBeenCalledWith({ index: '.alerts-test.alerts-default', - refresh: true, + refresh: 'wait_for', require_alias: !useDataStreamForAlerts, body: [ { @@ -2826,7 +2876,7 @@ describe('Alerts Client', () => { expect(clusterClient.bulk).toHaveBeenCalledWith({ index: '.alerts-test.alerts-default', - refresh: true, + refresh: 'wait_for', require_alias: !useDataStreamForAlerts, body: [ { @@ -2923,7 +2973,7 @@ describe('Alerts Client', () => { expect(clusterClient.bulk).toHaveBeenCalledWith({ index: '.alerts-test.alerts-default', - refresh: true, + refresh: 'wait_for', require_alias: !useDataStreamForAlerts, body: [ { diff --git a/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts b/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts index 1bfa1de9e96d..0c2340ba7cd2 100644 --- a/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts +++ b/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts @@ -73,6 +73,7 @@ export interface AlertsClientParams extends CreateAlertsClientParams { elasticsearchClientPromise: Promise; kibanaVersion: string; dataStreamAdapter: DataStreamAdapter; + isServerless: boolean; } interface AlertsAffectedByMaintenanceWindows { @@ -109,6 +110,7 @@ export class AlertsClient< private runTimestampString: string | undefined; private rule: AlertRule; private ruleType: UntypedNormalizedRuleType; + private readonly isServerless: boolean; private indexTemplateAndPattern: IIndexPatternString; @@ -143,6 +145,7 @@ export class AlertsClient< this._isUsingDataStreams = this.options.dataStreamAdapter.isUsingDataStreams(); this.ruleInfoMessage = `for ${this.ruleType.id}:${this.options.rule.id} '${this.options.rule.name}'`; this.logTags = { tags: [this.ruleType.id, this.options.rule.id, 'alerts-client'] }; + this.isServerless = options.isServerless; } public async initializeExecution(opts: InitializeExecutionOpts) { @@ -555,7 +558,9 @@ export class AlertsClient< try { const response = await esClient.bulk({ - refresh: true, + // On serverless we can force a refresh to we don't wait for the longer refresh interval + // When too many refresh calls are done in a short period of time, they are throttled by stateless Elasticsearch + refresh: this.isServerless ? true : 'wait_for', index: this.indexTemplateAndPattern.alias, require_alias: !this.isUsingDataStreams(), body: bulkBody, diff --git a/x-pack/plugins/alerting/server/alerts_service/alerts_service.test.ts b/x-pack/plugins/alerting/server/alerts_service/alerts_service.test.ts index 4b4f632d4cb2..5b32cf507c81 100644 --- a/x-pack/plugins/alerting/server/alerts_service/alerts_service.test.ts +++ b/x-pack/plugins/alerting/server/alerts_service/alerts_service.test.ts @@ -275,6 +275,7 @@ describe('Alerts Service', () => { kibanaVersion: '8.8.0', dataStreamAdapter, elasticsearchAndSOAvailability$, + isServerless: false, }); await retryUntil( @@ -308,6 +309,7 @@ describe('Alerts Service', () => { kibanaVersion: '8.8.0', dataStreamAdapter, elasticsearchAndSOAvailability$: test$, + isServerless: false, }); await retryUntil( @@ -350,6 +352,7 @@ describe('Alerts Service', () => { kibanaVersion: '8.8.0', dataStreamAdapter, elasticsearchAndSOAvailability$, + isServerless: false, }); await retryUntil('error log called', async () => logger.error.mock.calls.length > 0); @@ -372,6 +375,7 @@ describe('Alerts Service', () => { kibanaVersion: '8.8.0', dataStreamAdapter, elasticsearchAndSOAvailability$, + isServerless: false, }); await retryUntil('error log called', async () => logger.error.mock.calls.length > 0); @@ -453,6 +457,7 @@ describe('Alerts Service', () => { kibanaVersion: '8.8.0', dataStreamAdapter, elasticsearchAndSOAvailability$, + isServerless: false, }); await retryUntil( @@ -495,6 +500,7 @@ describe('Alerts Service', () => { kibanaVersion: '8.8.0', dataStreamAdapter, elasticsearchAndSOAvailability$, + isServerless: false, }); await retryUntil( @@ -1516,6 +1522,7 @@ describe('Alerts Service', () => { kibanaVersion: '8.8.0', dataStreamAdapter, elasticsearchAndSOAvailability$, + isServerless: true, }); await retryUntil( @@ -1561,6 +1568,7 @@ describe('Alerts Service', () => { maintenanceWindowsService, namespace: 'default', spaceId: 'default', + isServerless: true, rule: { consumer: 'bar', executionId: '5f6aa57d-3e22-484e-bae8-cbed868f4d28', @@ -1586,6 +1594,7 @@ describe('Alerts Service', () => { kibanaVersion: '8.8.0', dataStreamAdapter, elasticsearchAndSOAvailability$, + isServerless: false, }); await retryUntil( @@ -1629,6 +1638,7 @@ describe('Alerts Service', () => { kibanaVersion: '8.8.0', dataStreamAdapter, elasticsearchAndSOAvailability$, + isServerless: false, }); alertsService.register(TestRegistrationContext); @@ -1693,6 +1703,7 @@ describe('Alerts Service', () => { maintenanceWindowsService, namespace: 'default', spaceId: 'default', + isServerless: false, rule: { consumer: 'bar', executionId: '5f6aa57d-3e22-484e-bae8-cbed868f4d28', @@ -1736,6 +1747,7 @@ describe('Alerts Service', () => { kibanaVersion: '8.8.0', dataStreamAdapter, elasticsearchAndSOAvailability$, + isServerless: false, }); alertsService.register(TestRegistrationContext); @@ -1824,6 +1836,7 @@ describe('Alerts Service', () => { maintenanceWindowsService, namespace: 'default', spaceId: 'default', + isServerless: false, rule: { consumer: 'bar', executionId: '5f6aa57d-3e22-484e-bae8-cbed868f4d28', @@ -1870,6 +1883,7 @@ describe('Alerts Service', () => { kibanaVersion: '8.8.0', dataStreamAdapter, elasticsearchAndSOAvailability$, + isServerless: false, }); alertsService.register(TestRegistrationContext); @@ -1911,6 +1925,7 @@ describe('Alerts Service', () => { maintenanceWindowsService, namespace: 'default', spaceId: 'default', + isServerless: false, rule: { consumer: 'bar', executionId: '5f6aa57d-3e22-484e-bae8-cbed868f4d28', @@ -1960,6 +1975,7 @@ describe('Alerts Service', () => { kibanaVersion: '8.8.0', dataStreamAdapter, elasticsearchAndSOAvailability$, + isServerless: false, }); alertsService.register(TestRegistrationContext); @@ -2013,6 +2029,7 @@ describe('Alerts Service', () => { maintenanceWindowsService, namespace: 'default', spaceId: 'default', + isServerless: false, rule: { consumer: 'bar', executionId: '5f6aa57d-3e22-484e-bae8-cbed868f4d28', @@ -2067,6 +2084,7 @@ describe('Alerts Service', () => { kibanaVersion: '8.8.0', dataStreamAdapter, elasticsearchAndSOAvailability$, + isServerless: false, }); alertsService.register(TestRegistrationContext); @@ -2134,6 +2152,7 @@ describe('Alerts Service', () => { kibanaVersion: '8.8.0', dataStreamAdapter, elasticsearchAndSOAvailability$, + isServerless: false, }); alertsService.register(TestRegistrationContext); @@ -2205,6 +2224,7 @@ describe('Alerts Service', () => { kibanaVersion: '8.8.0', dataStreamAdapter, elasticsearchAndSOAvailability$, + isServerless: false, }); alertsService.register(TestRegistrationContext); @@ -2283,6 +2303,7 @@ describe('Alerts Service', () => { kibanaVersion: '8.8.0', dataStreamAdapter, elasticsearchAndSOAvailability$, + isServerless: false, }); alertsService.register(TestRegistrationContext); @@ -2341,6 +2362,7 @@ describe('Alerts Service', () => { kibanaVersion: '8.8.0', dataStreamAdapter, elasticsearchAndSOAvailability$, + isServerless: false, }); await retryUntil( @@ -2362,6 +2384,7 @@ describe('Alerts Service', () => { kibanaVersion: '8.8.0', dataStreamAdapter, elasticsearchAndSOAvailability$, + isServerless: false, }); await retryUntil( @@ -2383,6 +2406,7 @@ describe('Alerts Service', () => { kibanaVersion: '8.8.0', dataStreamAdapter, elasticsearchAndSOAvailability$, + isServerless: false, }); await retryUntil( @@ -2412,6 +2436,7 @@ describe('Alerts Service', () => { kibanaVersion: '8.8.0', dataStreamAdapter, elasticsearchAndSOAvailability$, + isServerless: false, }); await retryUntil( @@ -2444,6 +2469,7 @@ describe('Alerts Service', () => { kibanaVersion: '8.8.0', dataStreamAdapter, elasticsearchAndSOAvailability$, + isServerless: false, }); await retryUntil( @@ -2481,6 +2507,7 @@ describe('Alerts Service', () => { kibanaVersion: '8.8.0', dataStreamAdapter, elasticsearchAndSOAvailability$, + isServerless: false, }); await retryUntil( @@ -2516,6 +2543,7 @@ describe('Alerts Service', () => { timeoutMs: 10, dataStreamAdapter, elasticsearchAndSOAvailability$, + isServerless: false, }); await retryUntil('error logger called', async () => logger.error.mock.calls.length > 0); @@ -2533,6 +2561,7 @@ describe('Alerts Service', () => { timeoutMs: 10, dataStreamAdapter, elasticsearchAndSOAvailability$, + isServerless: false, }); await retryUntil('debug logger called', async () => logger.debug.mock.calls.length > 0); diff --git a/x-pack/plugins/alerting/server/alerts_service/alerts_service.ts b/x-pack/plugins/alerting/server/alerts_service/alerts_service.ts index 4f0ead6c5420..6a6dbbdb58df 100644 --- a/x-pack/plugins/alerting/server/alerts_service/alerts_service.ts +++ b/x-pack/plugins/alerting/server/alerts_service/alerts_service.ts @@ -59,6 +59,7 @@ interface AlertsServiceParams { timeoutMs?: number; dataStreamAdapter: DataStreamAdapter; elasticsearchAndSOAvailability$: Observable; + isServerless: boolean; } export interface CreateAlertsClientParams extends LegacyAlertsClientParams { @@ -120,6 +121,7 @@ export type PublicFrameworkAlertsService = PublicAlertsService & { export class AlertsService implements IAlertsService { private initialized: boolean; + private isServerless: boolean; private isInitializing: boolean = false; private resourceInitializationHelper: ResourceInstallationHelper; private registeredContexts: Map = new Map(); @@ -129,6 +131,7 @@ export class AlertsService implements IAlertsService { constructor(private readonly options: AlertsServiceParams) { this.initialized = false; + this.isServerless = options.isServerless; this.dataStreamAdapter = options.dataStreamAdapter; // Kick off initialization of common assets and save the promise @@ -245,6 +248,7 @@ export class AlertsService implements IAlertsService { spaceId: opts.spaceId, kibanaVersion: this.options.kibanaVersion, dataStreamAdapter: this.dataStreamAdapter, + isServerless: this.isServerless, }); } diff --git a/x-pack/plugins/alerting/server/plugin.ts b/x-pack/plugins/alerting/server/plugin.ts index bf1f02d3a491..5327162fc960 100644 --- a/x-pack/plugins/alerting/server/plugin.ts +++ b/x-pack/plugins/alerting/server/plugin.ts @@ -330,6 +330,7 @@ export class AlertingPlugin { .getStartServices() .then(([{ elasticsearch }]) => elasticsearch.client.asInternalUser), elasticsearchAndSOAvailability$, + isServerless: !!plugins.serverless, }); } } diff --git a/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.test.ts b/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.test.ts index 0e0d7983e59f..def73bf34e11 100644 --- a/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.test.ts +++ b/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.test.ts @@ -459,7 +459,7 @@ describe('Ad Hoc Task Runner', () => { expect(clusterClient.bulk).toHaveBeenCalledWith({ index: '.alerts-test.alerts-default', - refresh: true, + refresh: 'wait_for', require_alias: !useDataStreamForAlerts, body: [ { diff --git a/x-pack/plugins/alerting/server/task_runner/task_runner_alerts_client.test.ts b/x-pack/plugins/alerting/server/task_runner/task_runner_alerts_client.test.ts index 958ecaf8d270..4ffc41cc3bf0 100644 --- a/x-pack/plugins/alerting/server/task_runner/task_runner_alerts_client.test.ts +++ b/x-pack/plugins/alerting/server/task_runner/task_runner_alerts_client.test.ts @@ -590,7 +590,7 @@ describe('Task Runner', () => { expect(clusterClient.bulk).toHaveBeenCalledWith({ index: '.alerts-test.alerts-default', - refresh: true, + refresh: 'wait_for', require_alias: !useDataStreamForAlerts, body: [ { From 3aa0ce40e4c59e370ef916b40a669ac0e51dc93c Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Thu, 21 Nov 2024 08:51:50 -0500 Subject: [PATCH 02/12] Fix type checks --- .../alerting/server/task_runner/ad_hoc_task_runner.test.ts | 1 + .../server/task_runner/task_runner_alerts_client.test.ts | 2 ++ 2 files changed, 3 insertions(+) diff --git a/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.test.ts b/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.test.ts index def73bf34e11..c02b83630fb6 100644 --- a/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.test.ts +++ b/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.test.ts @@ -131,6 +131,7 @@ const alertsService = new AlertsService({ elasticsearchClientPromise: Promise.resolve(clusterClient), dataStreamAdapter: getDataStreamAdapter({ useDataStreamForAlerts }), elasticsearchAndSOAvailability$, + isServerless: false, }); const backfillClient = backfillClientMock.create(); const dataPlugin = dataPluginMock.createStartContract(); diff --git a/x-pack/plugins/alerting/server/task_runner/task_runner_alerts_client.test.ts b/x-pack/plugins/alerting/server/task_runner/task_runner_alerts_client.test.ts index 4ffc41cc3bf0..02d94740ff75 100644 --- a/x-pack/plugins/alerting/server/task_runner/task_runner_alerts_client.test.ts +++ b/x-pack/plugins/alerting/server/task_runner/task_runner_alerts_client.test.ts @@ -412,6 +412,7 @@ describe('Task Runner', () => { elasticsearchClientPromise: Promise.resolve(clusterClient), dataStreamAdapter: getDataStreamAdapter({ useDataStreamForAlerts }), elasticsearchAndSOAvailability$, + isServerless: false, }); elasticsearchAndSOAvailability$.next(true); @@ -544,6 +545,7 @@ describe('Task Runner', () => { elasticsearchClientPromise: Promise.resolve(clusterClient), dataStreamAdapter: getDataStreamAdapter({ useDataStreamForAlerts }), elasticsearchAndSOAvailability$, + isServerless: false, }); elasticsearchAndSOAvailability$.next(true); From 5ed3e9e0e3a00c114c9b5e187df2b22273d65217 Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Thu, 21 Nov 2024 09:25:10 -0500 Subject: [PATCH 03/12] Add support in rule registry to refresh based on stateless vs stateful --- .../lib/initialize_alerts_client.test.ts | 4 ++++ x-pack/plugins/alerting/server/plugin.ts | 1 + .../task_runner/ad_hoc_task_runner.test.ts | 1 + .../server/task_runner/ad_hoc_task_runner.ts | 1 + .../task_runner/rule_type_runner.test.ts | 20 +++++++++++++++++++ .../server/task_runner/rule_type_runner.ts | 1 + .../server/task_runner/task_runner.test.ts | 1 + .../server/task_runner/task_runner.ts | 1 + .../task_runner_alerts_client.test.ts | 1 + .../task_runner/task_runner_cancel.test.ts | 1 + .../task_runner/task_runner_factory.test.ts | 1 + .../alerting/server/task_runner/types.ts | 2 ++ x-pack/plugins/alerting/server/types.ts | 1 + .../utils/rule_executor.test_helpers.ts | 1 + 14 files changed, 37 insertions(+) diff --git a/x-pack/plugins/alerting/server/alerts_client/lib/initialize_alerts_client.test.ts b/x-pack/plugins/alerting/server/alerts_client/lib/initialize_alerts_client.test.ts index c97fb9f31f82..78339ed5906b 100644 --- a/x-pack/plugins/alerting/server/alerts_client/lib/initialize_alerts_client.test.ts +++ b/x-pack/plugins/alerting/server/alerts_client/lib/initialize_alerts_client.test.ts @@ -100,6 +100,7 @@ describe('initializeAlertsClient', () => { ruleLogPrefix: `${RULE_TYPE_ID}:${RULE_ID}: '${RULE_NAME}'`, ruleRunMetricsStore, spaceId: 'default', + isServerless: false, }, executionId: 'abc', logger, @@ -159,6 +160,7 @@ describe('initializeAlertsClient', () => { ruleLogPrefix: `${RULE_TYPE_ID}:${RULE_ID}: '${RULE_NAME}'`, ruleRunMetricsStore, spaceId: 'default', + isServerless: false, }, executionId: 'abc', logger, @@ -219,6 +221,7 @@ describe('initializeAlertsClient', () => { ruleLogPrefix: `${RULE_TYPE_ID}:${RULE_ID}: '${RULE_NAME}'`, ruleRunMetricsStore, spaceId: 'default', + isServerless: false, }, executionId: 'abc', logger, @@ -288,6 +291,7 @@ describe('initializeAlertsClient', () => { ruleLogPrefix: `${RULE_TYPE_ID}:${RULE_ID}: '${RULE_NAME}'`, ruleRunMetricsStore, spaceId: 'default', + isServerless: false, }, executionId: 'abc', logger, diff --git a/x-pack/plugins/alerting/server/plugin.ts b/x-pack/plugins/alerting/server/plugin.ts index 5327162fc960..94f459ef2250 100644 --- a/x-pack/plugins/alerting/server/plugin.ts +++ b/x-pack/plugins/alerting/server/plugin.ts @@ -629,6 +629,7 @@ export class AlertingPlugin { supportsEphemeralTasks: plugins.taskManager.supportsEphemeralTasks(), uiSettings: core.uiSettings, usageCounter: this.usageCounter, + isServerless: !!plugins.serverless, }); this.eventLogService!.registerSavedObjectProvider(RULE_SAVED_OBJECT_TYPE, (request) => { diff --git a/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.test.ts b/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.test.ts index c02b83630fb6..cfc87f8497ea 100644 --- a/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.test.ts +++ b/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.test.ts @@ -177,6 +177,7 @@ const taskRunnerFactoryInitializerParams: TaskRunnerFactoryInitializerParamsType supportsEphemeralTasks: false, uiSettings: uiSettingsService, usageCounter: mockUsageCounter, + isServerless: false, }; const mockedTaskInstance: ConcreteTaskInstance = { diff --git a/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.ts b/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.ts index d12615103067..c9932820ff80 100644 --- a/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.ts +++ b/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.ts @@ -185,6 +185,7 @@ export class AdHocTaskRunner implements CancellableTask { ruleLogPrefix: ruleLabel, ruleRunMetricsStore, spaceId: adHocRunData.spaceId, + isServerless: this.context.isServerless, }; const alertsClient = await initializeAlertsClient< RuleTypeParams, diff --git a/x-pack/plugins/alerting/server/task_runner/rule_type_runner.test.ts b/x-pack/plugins/alerting/server/task_runner/rule_type_runner.test.ts index 1d218e50f927..68c01770fd6f 100644 --- a/x-pack/plugins/alerting/server/task_runner/rule_type_runner.test.ts +++ b/x-pack/plugins/alerting/server/task_runner/rule_type_runner.test.ts @@ -189,6 +189,7 @@ describe('RuleTypeRunner', () => { ruleLogPrefix: `${RULE_TYPE_ID}:${RULE_ID}: '${RULE_NAME}'`, ruleRunMetricsStore, spaceId: 'default', + isServerless: false, }, alertsClient, executionId: 'abc', @@ -231,6 +232,7 @@ describe('RuleTypeRunner', () => { startedAtOverridden: false, previousStartedAt: null, spaceId: 'default', + isServerless: false, rule: { id: RULE_ID, name: mockedRule.name, @@ -295,6 +297,7 @@ describe('RuleTypeRunner', () => { ruleLogPrefix: `${RULE_TYPE_ID}:${RULE_ID}: '${RULE_NAME}'`, ruleRunMetricsStore, spaceId: 'default', + isServerless: false, }, alertsClient, executionId: 'abc', @@ -337,6 +340,7 @@ describe('RuleTypeRunner', () => { startedAtOverridden: true, previousStartedAt: null, spaceId: 'default', + isServerless: false, rule: { id: RULE_ID, name: mockedRule.name, @@ -404,6 +408,7 @@ describe('RuleTypeRunner', () => { ruleLogPrefix: `${RULE_TYPE_ID}:${RULE_ID}: '${RULE_NAME}'`, ruleRunMetricsStore, spaceId: 'default', + isServerless: false, }, alertsClient, executionId: 'abc', @@ -465,6 +470,7 @@ describe('RuleTypeRunner', () => { ruleLogPrefix: `${RULE_TYPE_ID}:${RULE_ID}: '${RULE_NAME}'`, ruleRunMetricsStore, spaceId: 'default', + isServerless: false, }, alertsClient, executionId: 'abc', @@ -507,6 +513,7 @@ describe('RuleTypeRunner', () => { startedAtOverridden: false, previousStartedAt: null, spaceId: 'default', + isServerless: false, rule: { id: RULE_ID, name: mockedRule.name, @@ -567,6 +574,7 @@ describe('RuleTypeRunner', () => { ruleLogPrefix: `${RULE_TYPE_ID}:${RULE_ID}: '${RULE_NAME}'`, ruleRunMetricsStore, spaceId: 'default', + isServerless: false, }, alertsClient, executionId: 'abc', @@ -609,6 +617,7 @@ describe('RuleTypeRunner', () => { startedAtOverridden: false, previousStartedAt: null, spaceId: 'default', + isServerless: false, rule: { id: RULE_ID, name: mockedRule.name, @@ -669,6 +678,7 @@ describe('RuleTypeRunner', () => { ruleLogPrefix: `${RULE_TYPE_ID}:${RULE_ID}: '${RULE_NAME}'`, ruleRunMetricsStore, spaceId: 'default', + isServerless: false, }, alertsClient, executionId: 'abc', @@ -706,6 +716,7 @@ describe('RuleTypeRunner', () => { ruleLogPrefix: `${RULE_TYPE_ID}:${RULE_ID}: '${RULE_NAME}'`, ruleRunMetricsStore, spaceId: 'default', + isServerless: false, }, alertsClient, executionId: 'abc', @@ -748,6 +759,7 @@ describe('RuleTypeRunner', () => { startedAtOverridden: false, previousStartedAt: null, spaceId: 'default', + isServerless: false, rule: { id: RULE_ID, name: mockedRule.name, @@ -819,6 +831,7 @@ describe('RuleTypeRunner', () => { ruleLogPrefix: `${RULE_TYPE_ID}:${RULE_ID}: '${RULE_NAME}'`, ruleRunMetricsStore, spaceId: 'default', + isServerless: false, }, alertsClient, executionId: 'abc', @@ -861,6 +874,7 @@ describe('RuleTypeRunner', () => { startedAtOverridden: false, previousStartedAt: null, spaceId: 'default', + isServerless: false, rule: { id: RULE_ID, name: mockedRule.name, @@ -932,6 +946,7 @@ describe('RuleTypeRunner', () => { ruleLogPrefix: `${RULE_TYPE_ID}:${RULE_ID}: '${RULE_NAME}'`, ruleRunMetricsStore, spaceId: 'default', + isServerless: false, }, alertsClient, executionId: 'abc', @@ -975,6 +990,7 @@ describe('RuleTypeRunner', () => { startedAtOverridden: false, previousStartedAt: null, spaceId: 'default', + isServerless: false, rule: { id: RULE_ID, name: mockedRule.name, @@ -1036,6 +1052,7 @@ describe('RuleTypeRunner', () => { ruleLogPrefix: `${RULE_TYPE_ID}:${RULE_ID}: '${RULE_NAME}'`, ruleRunMetricsStore, spaceId: 'default', + isServerless: false, }, alertsClient, executionId: 'abc', @@ -1079,6 +1096,7 @@ describe('RuleTypeRunner', () => { startedAtOverridden: false, previousStartedAt: null, spaceId: 'default', + isServerless: false, rule: { id: RULE_ID, name: mockedRule.name, @@ -1140,6 +1158,7 @@ describe('RuleTypeRunner', () => { ruleLogPrefix: `${RULE_TYPE_ID}:${RULE_ID}: '${RULE_NAME}'`, ruleRunMetricsStore, spaceId: 'default', + isServerless: false, }, alertsClient, executionId: 'abc', @@ -1183,6 +1202,7 @@ describe('RuleTypeRunner', () => { startedAtOverridden: false, previousStartedAt: null, spaceId: 'default', + isServerless: false, rule: { id: RULE_ID, name: mockedRule.name, diff --git a/x-pack/plugins/alerting/server/task_runner/rule_type_runner.ts b/x-pack/plugins/alerting/server/task_runner/rule_type_runner.ts index c10871613ea2..5a9a88d85674 100644 --- a/x-pack/plugins/alerting/server/task_runner/rule_type_runner.ts +++ b/x-pack/plugins/alerting/server/task_runner/rule_type_runner.ts @@ -285,6 +285,7 @@ export class RuleTypeRunner< ...(context.queryDelaySec ? { queryDelay: context.queryDelaySec } : {}), ...(startedAtOverridden ? { forceNow: startedAt } : {}), }), + isServerless: context.isServerless, }) ) ); 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 a79dfe8f59c7..da23b9bdce4e 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 @@ -196,6 +196,7 @@ describe('Task Runner', () => { supportsEphemeralTasks: false, uiSettings: uiSettingsService, usageCounter: mockUsageCounter, + isServerless: false, }; const ephemeralTestParams: Array< 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 89432e182202..cd351054f993 100644 --- a/x-pack/plugins/alerting/server/task_runner/task_runner.ts +++ b/x-pack/plugins/alerting/server/task_runner/task_runner.ts @@ -317,6 +317,7 @@ export class TaskRunner< ruleLogPrefix: ruleLabel, ruleRunMetricsStore, spaceId, + isServerless: this.context.isServerless, }; const alertsClient = await withAlertingSpan('alerting:initialize-alerts-client', () => initializeAlertsClient< diff --git a/x-pack/plugins/alerting/server/task_runner/task_runner_alerts_client.test.ts b/x-pack/plugins/alerting/server/task_runner/task_runner_alerts_client.test.ts index 02d94740ff75..ed8e4a1259e6 100644 --- a/x-pack/plugins/alerting/server/task_runner/task_runner_alerts_client.test.ts +++ b/x-pack/plugins/alerting/server/task_runner/task_runner_alerts_client.test.ts @@ -225,6 +225,7 @@ describe('Task Runner', () => { supportsEphemeralTasks: false, uiSettings: uiSettingsService, usageCounter: mockUsageCounter, + isServerless: false, }; describe(`using ${label} for alert indices`, () => { diff --git a/x-pack/plugins/alerting/server/task_runner/task_runner_cancel.test.ts b/x-pack/plugins/alerting/server/task_runner/task_runner_cancel.test.ts index c96a3f95c636..066cff2944f2 100644 --- a/x-pack/plugins/alerting/server/task_runner/task_runner_cancel.test.ts +++ b/x-pack/plugins/alerting/server/task_runner/task_runner_cancel.test.ts @@ -158,6 +158,7 @@ describe('Task Runner Cancel', () => { supportsEphemeralTasks: false, uiSettings: uiSettingsService, usageCounter: mockUsageCounter, + isServerless: false, }; beforeEach(() => { diff --git a/x-pack/plugins/alerting/server/task_runner/task_runner_factory.test.ts b/x-pack/plugins/alerting/server/task_runner/task_runner_factory.test.ts index ed4b5a080223..7aa0105f7c06 100644 --- a/x-pack/plugins/alerting/server/task_runner/task_runner_factory.test.ts +++ b/x-pack/plugins/alerting/server/task_runner/task_runner_factory.test.ts @@ -129,6 +129,7 @@ describe('Task Runner Factory', () => { supportsEphemeralTasks: true, uiSettings: uiSettingsService, usageCounter: mockUsageCounter, + isServerless: false, }; beforeEach(() => { diff --git a/x-pack/plugins/alerting/server/task_runner/types.ts b/x-pack/plugins/alerting/server/task_runner/types.ts index a92ee2a89d65..b401155ab06d 100644 --- a/x-pack/plugins/alerting/server/task_runner/types.ts +++ b/x-pack/plugins/alerting/server/task_runner/types.ts @@ -146,6 +146,7 @@ export interface RuleTypeRunnerContext { ruleLogPrefix: string; ruleRunMetricsStore: RuleRunMetricsStore; spaceId: string; + isServerless: boolean; } export interface RuleRunnerErrorStackTraceLog { @@ -180,4 +181,5 @@ export interface TaskRunnerContext { supportsEphemeralTasks: boolean; uiSettings: UiSettingsServiceStart; usageCounter?: UsageCounter; + isServerless: boolean; } diff --git a/x-pack/plugins/alerting/server/types.ts b/x-pack/plugins/alerting/server/types.ts index b660d348b9e0..c7ef8f563843 100644 --- a/x-pack/plugins/alerting/server/types.ts +++ b/x-pack/plugins/alerting/server/types.ts @@ -133,6 +133,7 @@ export interface RuleExecutorOptions< namespace?: string; flappingSettings: RulesSettingsFlappingProperties; getTimeRange: (timeWindow?: string) => GetTimeRangeResult; + isServerless: boolean; } export interface RuleParamsAndRefs { diff --git a/x-pack/plugins/rule_registry/server/utils/rule_executor.test_helpers.ts b/x-pack/plugins/rule_registry/server/utils/rule_executor.test_helpers.ts index 357703c7b752..ca7291e2fc2e 100644 --- a/x-pack/plugins/rule_registry/server/utils/rule_executor.test_helpers.ts +++ b/x-pack/plugins/rule_registry/server/utils/rule_executor.test_helpers.ts @@ -99,4 +99,5 @@ export const createDefaultAlertExecutorOptions = < const date = new Date(Date.now()).toISOString(); return { dateStart: date, dateEnd: date }; }, + isServerless: false, }); From 39adad8f42f69354b31d00d260fe748c2278dc8e Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Thu, 21 Nov 2024 09:56:19 -0500 Subject: [PATCH 04/12] Pass isServerless for rule preview --- .../rule_preview/api/preview_rules/route.ts | 4 +++- .../detection_engine/rule_preview/api/register_routes.ts | 6 ++++-- x-pack/plugins/security_solution/server/plugin.ts | 3 ++- x-pack/plugins/security_solution/server/routes/index.ts | 6 ++++-- .../server/rule_types/es_query/rule_type.test.ts | 1 + .../server/rule_types/index_threshold/rule_type.test.ts | 4 ++++ 6 files changed, 18 insertions(+), 6 deletions(-) diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/rule_preview/api/preview_rules/route.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/rule_preview/api/preview_rules/route.ts index bc805867f69e..8e8093d96c3a 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/rule_preview/api/preview_rules/route.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/rule_preview/api/preview_rules/route.ts @@ -83,7 +83,8 @@ export const previewRulesRoute = ( securityRuleTypeOptions: CreateSecurityRuleTypeWrapperProps, previewRuleDataClient: IRuleDataClient, getStartServices: StartServicesAccessor, - logger: Logger + logger: Logger, + isServerless: boolean ) => { router.versioned .post({ @@ -320,6 +321,7 @@ export const previewRulesRoute = ( const date = startedAt.toISOString(); return { dateStart: date, dateEnd: date }; }, + isServerless, })) as { state: TState; loggedRequests: RulePreviewLoggedRequest[] }); const errors = loggedStatusChanges diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/rule_preview/api/register_routes.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/rule_preview/api/register_routes.ts index 4fedc1ea2784..71f5a9b5ee79 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/rule_preview/api/register_routes.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/rule_preview/api/register_routes.ts @@ -24,7 +24,8 @@ export const registerRulePreviewRoutes = ( securityRuleTypeOptions: CreateSecurityRuleTypeWrapperProps, previewRuleDataClient: IRuleDataClient, getStartServices: StartServicesAccessor, - logger: Logger + logger: Logger, + isServerless: boolean ) => { previewRulesRoute( router, @@ -35,6 +36,7 @@ export const registerRulePreviewRoutes = ( securityRuleTypeOptions, previewRuleDataClient, getStartServices, - logger + logger, + isServerless ); }; diff --git a/x-pack/plugins/security_solution/server/plugin.ts b/x-pack/plugins/security_solution/server/plugin.ts index e2ec9d0e1b53..81dce5372024 100644 --- a/x-pack/plugins/security_solution/server/plugin.ts +++ b/x-pack/plugins/security_solution/server/plugin.ts @@ -391,7 +391,8 @@ export class Plugin implements ISecuritySolutionPlugin { core.getStartServices, securityRuleTypeOptions, previewRuleDataClient, - this.telemetryReceiver + this.telemetryReceiver, + this.pluginContext.env.packageInfo.buildFlavor === 'serverless' ); registerEndpointRoutes(router, this.endpointContext); diff --git a/x-pack/plugins/security_solution/server/routes/index.ts b/x-pack/plugins/security_solution/server/routes/index.ts index f7d5ce7afb3d..6bde6327f729 100644 --- a/x-pack/plugins/security_solution/server/routes/index.ts +++ b/x-pack/plugins/security_solution/server/routes/index.ts @@ -77,7 +77,8 @@ export const initRoutes = ( getStartServices: StartServicesAccessor, securityRuleTypeOptions: CreateSecurityRuleTypeWrapperProps, previewRuleDataClient: IRuleDataClient, - previewTelemetryReceiver: ITelemetryReceiver + previewTelemetryReceiver: ITelemetryReceiver, + isServerless: boolean ) => { registerFleetIntegrationsRoutes(router); registerLegacyRuleActionsRoutes(router, logger); @@ -95,7 +96,8 @@ export const initRoutes = ( securityRuleTypeOptions, previewRuleDataClient, getStartServices, - logger + logger, + isServerless, ); registerResolverRoutes(router, getStartServices, config); diff --git a/x-pack/plugins/stack_alerts/server/rule_types/es_query/rule_type.test.ts b/x-pack/plugins/stack_alerts/server/rule_types/es_query/rule_type.test.ts index afcccecfef94..7473ae361977 100644 --- a/x-pack/plugins/stack_alerts/server/rule_types/es_query/rule_type.test.ts +++ b/x-pack/plugins/stack_alerts/server/rule_types/es_query/rule_type.test.ts @@ -957,5 +957,6 @@ async function invokeExecutor({ const date = new Date(Date.now()).toISOString(); return { dateStart: date, dateEnd: date }; }, + isServerless: false, }); } diff --git a/x-pack/plugins/stack_alerts/server/rule_types/index_threshold/rule_type.test.ts b/x-pack/plugins/stack_alerts/server/rule_types/index_threshold/rule_type.test.ts index 546ab5239561..e94340cd049c 100644 --- a/x-pack/plugins/stack_alerts/server/rule_types/index_threshold/rule_type.test.ts +++ b/x-pack/plugins/stack_alerts/server/rule_types/index_threshold/rule_type.test.ts @@ -231,6 +231,7 @@ describe('ruleType', () => { logger, flappingSettings: DEFAULT_FLAPPING_SETTINGS, getTimeRange, + isServerless: false, }); expect(alertServices.alertsClient.report).toHaveBeenCalledWith({ @@ -327,6 +328,7 @@ describe('ruleType', () => { logger, flappingSettings: DEFAULT_FLAPPING_SETTINGS, getTimeRange, + isServerless: false, }); expect(customAlertServices.alertFactory.create).not.toHaveBeenCalled(); @@ -397,6 +399,7 @@ describe('ruleType', () => { logger, flappingSettings: DEFAULT_FLAPPING_SETTINGS, getTimeRange, + isServerless: false, }); expect(customAlertServices.alertFactory.create).not.toHaveBeenCalled(); @@ -466,6 +469,7 @@ describe('ruleType', () => { logger, flappingSettings: DEFAULT_FLAPPING_SETTINGS, getTimeRange, + isServerless: false, }); expect(data.timeSeriesQuery).toHaveBeenCalledWith( From c837ea7d14d0f3fd3221df89db2395cbd3cd3a5c Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Thu, 21 Nov 2024 15:19:19 +0000 Subject: [PATCH 05/12] [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' --- x-pack/plugins/security_solution/server/routes/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/security_solution/server/routes/index.ts b/x-pack/plugins/security_solution/server/routes/index.ts index 6bde6327f729..ff1166413b9f 100644 --- a/x-pack/plugins/security_solution/server/routes/index.ts +++ b/x-pack/plugins/security_solution/server/routes/index.ts @@ -97,7 +97,7 @@ export const initRoutes = ( previewRuleDataClient, getStartServices, logger, - isServerless, + isServerless ); registerResolverRoutes(router, getStartServices, config); From 2a2acf7fcdae5147a80a164075435d25b9dae256 Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Thu, 21 Nov 2024 11:11:14 -0500 Subject: [PATCH 06/12] Fix more test failures --- .../rules/custom_threshold/custom_threshold_executor.test.ts | 1 + .../notifications/legacy_rules_notification_rule_type.test.ts | 1 + x-pack/plugins/security_solution/server/routes/index.ts | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/observability_solution/observability/server/lib/rules/custom_threshold/custom_threshold_executor.test.ts b/x-pack/plugins/observability_solution/observability/server/lib/rules/custom_threshold/custom_threshold_executor.test.ts index 984a614a367f..b8dff520ff11 100644 --- a/x-pack/plugins/observability_solution/observability/server/lib/rules/custom_threshold/custom_threshold_executor.test.ts +++ b/x-pack/plugins/observability_solution/observability/server/lib/rules/custom_threshold/custom_threshold_executor.test.ts @@ -120,6 +120,7 @@ const mockOptions = { const date = STARTED_AT_MOCK_DATE.toISOString(); return { dateStart: date, dateEnd: date }; }, + isServerless: false, }; const setEvaluationResults = (response: Array>) => { diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/rule_actions_legacy/logic/notifications/legacy_rules_notification_rule_type.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/rule_actions_legacy/logic/notifications/legacy_rules_notification_rule_type.test.ts index 4adf71d258e0..1cb8d83dc965 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/rule_actions_legacy/logic/notifications/legacy_rules_notification_rule_type.test.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/rule_actions_legacy/logic/notifications/legacy_rules_notification_rule_type.test.ts @@ -191,6 +191,7 @@ describe('legacyRules_notification_rule_type', () => { const date = new Date('2019-12-14T16:40:33.400Z').toISOString(); return { dateStart: date, dateEnd: date }; }, + isServerless: false, }; rule = legacyRulesNotificationRuleType({ diff --git a/x-pack/plugins/security_solution/server/routes/index.ts b/x-pack/plugins/security_solution/server/routes/index.ts index 6bde6327f729..ff1166413b9f 100644 --- a/x-pack/plugins/security_solution/server/routes/index.ts +++ b/x-pack/plugins/security_solution/server/routes/index.ts @@ -97,7 +97,7 @@ export const initRoutes = ( previewRuleDataClient, getStartServices, logger, - isServerless, + isServerless ); registerResolverRoutes(router, getStartServices, config); From ba9f8ecb0fba97869d913c31fb6cb9e7c6c94ca5 Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Thu, 21 Nov 2024 12:15:23 -0500 Subject: [PATCH 07/12] Fix more tests --- .../metric_threshold/metric_threshold_executor.test.ts | 1 + .../slo/server/lib/rules/slo_burn_rate/executor.test.ts | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/x-pack/plugins/observability_solution/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.test.ts b/x-pack/plugins/observability_solution/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.test.ts index 9777f7ceda47..4227ff10ad90 100644 --- a/x-pack/plugins/observability_solution/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.test.ts +++ b/x-pack/plugins/observability_solution/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.test.ts @@ -102,6 +102,7 @@ const mockOptions = { const date = STARTED_AT_MOCK_DATE.toISOString(); return { dateStart: date, dateEnd: date }; }, + isServerless: false, }; const setEvaluationResults = (response: Array>) => { diff --git a/x-pack/plugins/observability_solution/slo/server/lib/rules/slo_burn_rate/executor.test.ts b/x-pack/plugins/observability_solution/slo/server/lib/rules/slo_burn_rate/executor.test.ts index 6b6c85f9120f..e5c07b797677 100644 --- a/x-pack/plugins/observability_solution/slo/server/lib/rules/slo_burn_rate/executor.test.ts +++ b/x-pack/plugins/observability_solution/slo/server/lib/rules/slo_burn_rate/executor.test.ts @@ -204,6 +204,7 @@ describe('BurnRateRuleExecutor', () => { state: {}, flappingSettings: DEFAULT_FLAPPING_SETTINGS, getTimeRange, + isServerless: false, }) ).rejects.toThrowError(); }); @@ -226,6 +227,7 @@ describe('BurnRateRuleExecutor', () => { state: {}, flappingSettings: DEFAULT_FLAPPING_SETTINGS, getTimeRange, + isServerless: false, }); expect(esClientMock.search).not.toHaveBeenCalled(); @@ -276,6 +278,7 @@ describe('BurnRateRuleExecutor', () => { state: {}, flappingSettings: DEFAULT_FLAPPING_SETTINGS, getTimeRange, + isServerless: false, }); expect(servicesMock.alertsClient?.report).not.toBeCalled(); @@ -323,6 +326,7 @@ describe('BurnRateRuleExecutor', () => { state: {}, flappingSettings: DEFAULT_FLAPPING_SETTINGS, getTimeRange, + isServerless: false, }); expect(servicesMock.alertsClient?.report).not.toBeCalled(); @@ -382,6 +386,7 @@ describe('BurnRateRuleExecutor', () => { state: {}, flappingSettings: DEFAULT_FLAPPING_SETTINGS, getTimeRange, + isServerless: false, }); expect(servicesMock.alertsClient?.report).toBeCalledWith({ @@ -531,6 +536,7 @@ describe('BurnRateRuleExecutor', () => { state: {}, flappingSettings: DEFAULT_FLAPPING_SETTINGS, getTimeRange, + isServerless: false, }); expect(servicesMock.alertsClient?.report).toBeCalledWith({ @@ -651,6 +657,7 @@ describe('BurnRateRuleExecutor', () => { state: {}, flappingSettings: DEFAULT_FLAPPING_SETTINGS, getTimeRange, + isServerless: false, }); expect(servicesMock.alertsClient!.report).toBeCalledWith({ From 2aa31e6d2ccb103706bf44b2462b3c4ee12a8b8d Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Fri, 22 Nov 2024 13:04:56 -0500 Subject: [PATCH 08/12] New way to check if serverless --- x-pack/plugins/alerting/kibana.jsonc | 5 ++--- x-pack/plugins/alerting/server/plugin.ts | 18 +++++++++--------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/x-pack/plugins/alerting/kibana.jsonc b/x-pack/plugins/alerting/kibana.jsonc index 0b5f930dbb34..b99c0a26c901 100644 --- a/x-pack/plugins/alerting/kibana.jsonc +++ b/x-pack/plugins/alerting/kibana.jsonc @@ -35,12 +35,11 @@ "usageCollection", "security", "monitoringCollection", - "spaces", - "serverless" + "spaces" ], "extraPublicDirs": [ "common", "common/parse_duration" ] } -} \ No newline at end of file +} diff --git a/x-pack/plugins/alerting/server/plugin.ts b/x-pack/plugins/alerting/server/plugin.ts index 94f459ef2250..cde873afca0e 100644 --- a/x-pack/plugins/alerting/server/plugin.ts +++ b/x-pack/plugins/alerting/server/plugin.ts @@ -198,7 +198,6 @@ export interface AlertingPluginsSetup { data: DataPluginSetup; features: FeaturesPluginSetup; unifiedSearch: UnifiedSearchServerPluginSetup; - serverless?: ServerlessPluginSetup; } export interface AlertingPluginsStart { @@ -213,7 +212,6 @@ export interface AlertingPluginsStart { data: DataPluginStart; dataViews: DataViewsPluginStart; share: SharePluginStart; - serverless?: ServerlessPluginSetup; } export class AlertingPlugin { @@ -239,6 +237,7 @@ export class AlertingPlugin { private pluginStop$: Subject; private dataStreamAdapter?: DataStreamAdapter; private backfillClient?: BackfillClient; + private readonly isServerless: boolean; private nodeRoles: PluginInitializerContext['node']['roles']; private readonly connectorAdapterRegistry = new ConnectorAdapterRegistry(); @@ -256,6 +255,7 @@ export class AlertingPlugin { this.kibanaVersion = initializerContext.env.packageInfo.version; this.inMemoryMetrics = new InMemoryMetrics(initializerContext.logger.get('in_memory_metrics')); this.pluginStop$ = new ReplaySubject(1); + this.isServerless = initializerContext.env.packageInfo.buildFlavor === 'serverless'; } public setup( @@ -268,7 +268,7 @@ export class AlertingPlugin { const elasticsearchAndSOAvailability$ = getElasticsearchAndSOAvailability(core.status.core$); - const useDataStreamForAlerts = !!plugins.serverless; + const useDataStreamForAlerts = this.isServerless; this.dataStreamAdapter = getDataStreamAdapter({ useDataStreamForAlerts }); core.capabilities.registerProvider(() => { @@ -282,7 +282,7 @@ export class AlertingPlugin { }; }); - plugins.features.registerKibanaFeature(getRulesSettingsFeature(!!plugins.serverless)); + plugins.features.registerKibanaFeature(getRulesSettingsFeature(this.isServerless)); plugins.features.registerKibanaFeature(maintenanceWindowFeature); @@ -330,7 +330,7 @@ export class AlertingPlugin { .getStartServices() .then(([{ elasticsearch }]) => elasticsearch.client.asInternalUser), elasticsearchAndSOAvailability$, - isServerless: !!plugins.serverless, + isServerless: this.isServerless, }); } } @@ -411,7 +411,7 @@ export class AlertingPlugin { getAlertIndicesAlias: createGetAlertIndicesAliasFn(this.ruleTypeRegistry!), encryptedSavedObjects: plugins.encryptedSavedObjects, config$: plugins.unifiedSearch.autocomplete.getInitializerContextConfig().create(), - isServerless: !!plugins.serverless, + isServerless: this.isServerless, }); return { @@ -562,7 +562,7 @@ export class AlertingPlugin { logger: this.logger, savedObjectsService: core.savedObjects, securityService: core.security, - isServerless: !!plugins.serverless, + isServerless: this.isServerless, }); maintenanceWindowClientFactory.initialize({ @@ -620,7 +620,7 @@ export class AlertingPlugin { rulesSettingsService: new RulesSettingsService({ cacheInterval: this.config.rulesSettings.cacheInterval, getRulesSettingsClientWithRequest, - isServerless: !!plugins.serverless, + isServerless: this.isServerless, logger, }), savedObjects: core.savedObjects, @@ -629,7 +629,7 @@ export class AlertingPlugin { supportsEphemeralTasks: plugins.taskManager.supportsEphemeralTasks(), uiSettings: core.uiSettings, usageCounter: this.usageCounter, - isServerless: !!plugins.serverless, + isServerless: this.isServerless, }); this.eventLogService!.registerSavedObjectProvider(RULE_SAVED_OBJECT_TYPE, (request) => { From 0920460c14d12438b5d345a44ecd5ec055975995 Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Fri, 22 Nov 2024 13:10:17 -0500 Subject: [PATCH 09/12] Fix type check --- x-pack/plugins/alerting/server/plugin.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/x-pack/plugins/alerting/server/plugin.ts b/x-pack/plugins/alerting/server/plugin.ts index cde873afca0e..86d6ea94218c 100644 --- a/x-pack/plugins/alerting/server/plugin.ts +++ b/x-pack/plugins/alerting/server/plugin.ts @@ -61,7 +61,6 @@ import type { PluginSetup as UnifiedSearchServerPluginSetup } from '@kbn/unified import { PluginStart as DataPluginStart } from '@kbn/data-plugin/server'; import { MonitoringCollectionSetup } from '@kbn/monitoring-collection-plugin/server'; import { SharePluginStart } from '@kbn/share-plugin/server'; -import { ServerlessPluginSetup } from '@kbn/serverless/server'; import { RuleTypeRegistry } from './rule_type_registry'; import { TaskRunnerFactory } from './task_runner'; From 7c19b458e6f55866bfff6a5b0f39188ae95dc6c6 Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Mon, 25 Nov 2024 07:38:33 -0500 Subject: [PATCH 10/12] Add some testing logs --- .../alerting/server/alerts_client/alerts_client.ts | 5 +++++ .../utils/create_persistence_rule_type_wrapper.ts | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts b/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts index 0c2340ba7cd2..463333d9c337 100644 --- a/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts +++ b/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts @@ -557,6 +557,11 @@ export class AlertsClient< ); try { + // eslint-disable-next-line no-console + console.log( + '*** Refresh value when indexing alerts:', + this.isServerless ? true : 'wait_for' + ); const response = await esClient.bulk({ // On serverless we can force a refresh to we don't wait for the longer refresh interval // When too many refresh calls are done in a short period of time, they are throttled by stateless Elasticsearch diff --git a/x-pack/plugins/rule_registry/server/utils/create_persistence_rule_type_wrapper.ts b/x-pack/plugins/rule_registry/server/utils/create_persistence_rule_type_wrapper.ts index afdc63712ba8..bd2d0de30c6a 100644 --- a/x-pack/plugins/rule_registry/server/utils/create_persistence_rule_type_wrapper.ts +++ b/x-pack/plugins/rule_registry/server/utils/create_persistence_rule_type_wrapper.ts @@ -568,9 +568,17 @@ export const createPersistenceRuleTypeWrapper: CreatePersistenceRuleTypeWrapper currentTimeOverride, }); + // eslint-disable-next-line no-console + console.log( + '*** Rule registry - refresh value when indexing alerts:', + options.isServerless ? true : 'wait_for' + ); + const bulkResponse = await ruleDataClientWriter.bulk({ body: [...duplicateAlertUpdates, ...mapAlertsToBulkCreate(augmentedAlerts)], - refresh: true, + // On serverless we can force a refresh to we don't wait for the longer refresh interval + // When too many refresh calls are done in a short period of time, they are throttled by stateless Elasticsearch + refresh: options.isServerless ? true : 'wait_for', }); if (bulkResponse == null) { From d5397e7cb1589a904e9582f4d68d1e3ae6480844 Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Tue, 26 Nov 2024 12:12:49 -0500 Subject: [PATCH 11/12] Revert "Add some testing logs" This reverts commit 7c19b458e6f55866bfff6a5b0f39188ae95dc6c6. --- .../alerting/server/alerts_client/alerts_client.ts | 5 ----- .../utils/create_persistence_rule_type_wrapper.ts | 10 +--------- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts b/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts index 463333d9c337..0c2340ba7cd2 100644 --- a/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts +++ b/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts @@ -557,11 +557,6 @@ export class AlertsClient< ); try { - // eslint-disable-next-line no-console - console.log( - '*** Refresh value when indexing alerts:', - this.isServerless ? true : 'wait_for' - ); const response = await esClient.bulk({ // On serverless we can force a refresh to we don't wait for the longer refresh interval // When too many refresh calls are done in a short period of time, they are throttled by stateless Elasticsearch diff --git a/x-pack/plugins/rule_registry/server/utils/create_persistence_rule_type_wrapper.ts b/x-pack/plugins/rule_registry/server/utils/create_persistence_rule_type_wrapper.ts index bd2d0de30c6a..afdc63712ba8 100644 --- a/x-pack/plugins/rule_registry/server/utils/create_persistence_rule_type_wrapper.ts +++ b/x-pack/plugins/rule_registry/server/utils/create_persistence_rule_type_wrapper.ts @@ -568,17 +568,9 @@ export const createPersistenceRuleTypeWrapper: CreatePersistenceRuleTypeWrapper currentTimeOverride, }); - // eslint-disable-next-line no-console - console.log( - '*** Rule registry - refresh value when indexing alerts:', - options.isServerless ? true : 'wait_for' - ); - const bulkResponse = await ruleDataClientWriter.bulk({ body: [...duplicateAlertUpdates, ...mapAlertsToBulkCreate(augmentedAlerts)], - // On serverless we can force a refresh to we don't wait for the longer refresh interval - // When too many refresh calls are done in a short period of time, they are throttled by stateless Elasticsearch - refresh: options.isServerless ? true : 'wait_for', + refresh: true, }); if (bulkResponse == null) { From 35213a5c7232b425cb1064ca8bceb22137297e0d Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Tue, 26 Nov 2024 12:16:58 -0500 Subject: [PATCH 12/12] Add back refresh condition --- .../server/utils/create_persistence_rule_type_wrapper.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/rule_registry/server/utils/create_persistence_rule_type_wrapper.ts b/x-pack/plugins/rule_registry/server/utils/create_persistence_rule_type_wrapper.ts index afdc63712ba8..9900c889ae73 100644 --- a/x-pack/plugins/rule_registry/server/utils/create_persistence_rule_type_wrapper.ts +++ b/x-pack/plugins/rule_registry/server/utils/create_persistence_rule_type_wrapper.ts @@ -570,7 +570,9 @@ export const createPersistenceRuleTypeWrapper: CreatePersistenceRuleTypeWrapper const bulkResponse = await ruleDataClientWriter.bulk({ body: [...duplicateAlertUpdates, ...mapAlertsToBulkCreate(augmentedAlerts)], - refresh: true, + // On serverless we can force a refresh to we don't wait for the longer refresh interval + // When too many refresh calls are done in a short period of time, they are throttled by stateless Elasticsearch + refresh: options.isServerless ? true : 'wait_for', }); if (bulkResponse == null) {