From 9d1fe2c088ff9af2d0098fd4273e84b7b223ddd5 Mon Sep 17 00:00:00 2001 From: Ersin Erdal <92688503+ersin-erdal@users.noreply.github.com> Date: Tue, 7 Dec 2021 08:13:08 +0100 Subject: [PATCH] [RAC] Rename occurrences of alert_type to rule_type in Infra (#120455) [RAC] Rename occurrences of alert_type to rule_type in Infra --- .../infra/public/alerting/inventory/index.ts | 6 +- .../public/alerting/log_threshold/index.ts | 2 +- ...ert_type.ts => log_threshold_rule_type.ts} | 4 +- .../public/alerting/metric_anomaly/index.ts | 9 +- .../public/alerting/metric_threshold/index.ts | 6 +- x-pack/plugins/infra/public/plugin.ts | 14 +-- .../infra/server/lib/alerting/index.ts | 2 +- .../evaluate_condition.ts | 12 +- .../inventory_metric_threshold_executor.ts | 41 ++++--- ...r_inventory_metric_threshold_rule_type.ts} | 2 +- .../log_threshold_executor.test.ts | 24 ++-- .../log_threshold/log_threshold_executor.ts | 108 +++++++++--------- ...ts => register_log_threshold_rule_type.ts} | 4 +- .../metric_anomaly/metric_anomaly_executor.ts | 20 ++-- ...s => register_metric_anomaly_rule_type.ts} | 14 +-- .../{evaluate_alert.ts => evaluate_rule.ts} | 4 +- .../metric_threshold_executor.test.ts | 8 +- .../metric_threshold_executor.ts | 51 +++++---- ...=> register_metric_threshold_rule_type.ts} | 4 +- .../lib/alerting/register_alert_types.ts | 36 ------ .../lib/alerting/register_rule_types.ts | 35 ++++++ x-pack/plugins/infra/server/plugin.ts | 4 +- .../apis/metrics_ui/metric_threshold_alert.ts | 26 ++--- 23 files changed, 220 insertions(+), 216 deletions(-) rename x-pack/plugins/infra/public/alerting/log_threshold/{log_threshold_alert_type.ts => log_threshold_rule_type.ts} (92%) rename x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/{register_inventory_metric_threshold_alert_type.ts => register_inventory_metric_threshold_rule_type.ts} (98%) rename x-pack/plugins/infra/server/lib/alerting/log_threshold/{register_log_threshold_alert_type.ts => register_log_threshold_rule_type.ts} (97%) rename x-pack/plugins/infra/server/lib/alerting/metric_anomaly/{register_metric_anomaly_alert_type.ts => register_metric_anomaly_rule_type.ts} (95%) rename x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/{evaluate_alert.ts => evaluate_rule.ts} (98%) rename x-pack/plugins/infra/server/lib/alerting/metric_threshold/{register_metric_threshold_alert_type.ts => register_metric_threshold_rule_type.ts} (97%) delete mode 100644 x-pack/plugins/infra/server/lib/alerting/register_alert_types.ts create mode 100644 x-pack/plugins/infra/server/lib/alerting/register_rule_types.ts diff --git a/x-pack/plugins/infra/public/alerting/inventory/index.ts b/x-pack/plugins/infra/public/alerting/inventory/index.ts index 5e3c8a219ae47..4a724694bbad8 100644 --- a/x-pack/plugins/infra/public/alerting/inventory/index.ts +++ b/x-pack/plugins/infra/public/alerting/inventory/index.ts @@ -15,15 +15,15 @@ import { import { ObservabilityRuleTypeModel } from '../../../../observability/public'; -import { AlertTypeParams } from '../../../../alerting/common'; +import { AlertTypeParams as RuleTypeParams } from '../../../../alerting/common'; import { validateMetricThreshold } from './components/validation'; import { formatReason } from './rule_data_formatters'; -interface InventoryMetricAlertTypeParams extends AlertTypeParams { +interface InventoryMetricRuleTypeParams extends RuleTypeParams { criteria: InventoryMetricConditions[]; } -export function createInventoryMetricAlertType(): ObservabilityRuleTypeModel { +export function createInventoryMetricRuleType(): ObservabilityRuleTypeModel { return { id: METRIC_INVENTORY_THRESHOLD_ALERT_TYPE_ID, description: i18n.translate('xpack.infra.metrics.inventory.alertFlyout.alertDescription', { diff --git a/x-pack/plugins/infra/public/alerting/log_threshold/index.ts b/x-pack/plugins/infra/public/alerting/log_threshold/index.ts index 0f2746b446927..b6eff8ef3826e 100644 --- a/x-pack/plugins/infra/public/alerting/log_threshold/index.ts +++ b/x-pack/plugins/infra/public/alerting/log_threshold/index.ts @@ -5,5 +5,5 @@ * 2.0. */ -export * from './log_threshold_alert_type'; +export * from './log_threshold_rule_type'; export { AlertDropdown } from './components/alert_dropdown'; diff --git a/x-pack/plugins/infra/public/alerting/log_threshold/log_threshold_alert_type.ts b/x-pack/plugins/infra/public/alerting/log_threshold/log_threshold_rule_type.ts similarity index 92% rename from x-pack/plugins/infra/public/alerting/log_threshold/log_threshold_alert_type.ts rename to x-pack/plugins/infra/public/alerting/log_threshold/log_threshold_rule_type.ts index c6b2385f93c65..b0a8737a994a1 100644 --- a/x-pack/plugins/infra/public/alerting/log_threshold/log_threshold_alert_type.ts +++ b/x-pack/plugins/infra/public/alerting/log_threshold/log_threshold_rule_type.ts @@ -11,11 +11,11 @@ import { ObservabilityRuleTypeModel } from '../../../../observability/public'; import { LOG_DOCUMENT_COUNT_ALERT_TYPE_ID, PartialAlertParams, -} from '../../../common/alerting/logs/log_threshold/types'; +} from '../../../common/alerting/logs/log_threshold'; import { formatRuleData } from './rule_data_formatters'; import { validateExpression } from './validation'; -export function createLogThresholdAlertType(): ObservabilityRuleTypeModel { +export function createLogThresholdRuleType(): ObservabilityRuleTypeModel { return { id: LOG_DOCUMENT_COUNT_ALERT_TYPE_ID, description: i18n.translate('xpack.infra.logs.alertFlyout.alertDescription', { diff --git a/x-pack/plugins/infra/public/alerting/metric_anomaly/index.ts b/x-pack/plugins/infra/public/alerting/metric_anomaly/index.ts index f8dbd46a0776c..1fab0ea89fe5a 100644 --- a/x-pack/plugins/infra/public/alerting/metric_anomaly/index.ts +++ b/x-pack/plugins/infra/public/alerting/metric_anomaly/index.ts @@ -8,16 +8,15 @@ import { i18n } from '@kbn/i18n'; import React from 'react'; import { METRIC_ANOMALY_ALERT_TYPE_ID } from '../../../common/alerting/metrics'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { AlertTypeModel } from '../../../../triggers_actions_ui/public/types'; -import { AlertTypeParams } from '../../../../alerting/common'; +import { AlertTypeModel } from '../../../../triggers_actions_ui/public'; +import { AlertTypeParams as RuleTypeParams } from '../../../../alerting/common'; import { validateMetricAnomaly } from './components/validation'; -interface MetricAnomalyAlertTypeParams extends AlertTypeParams { +interface MetricAnomalyRuleTypeParams extends RuleTypeParams { hasInfraMLCapabilities: boolean; } -export function createMetricAnomalyAlertType(): AlertTypeModel { +export function createMetricAnomalyRuleType(): AlertTypeModel { return { id: METRIC_ANOMALY_ALERT_TYPE_ID, description: i18n.translate('xpack.infra.metrics.anomaly.alertFlyout.alertDescription', { diff --git a/x-pack/plugins/infra/public/alerting/metric_threshold/index.ts b/x-pack/plugins/infra/public/alerting/metric_threshold/index.ts index 679019eb2e520..d45d090e0ec92 100644 --- a/x-pack/plugins/infra/public/alerting/metric_threshold/index.ts +++ b/x-pack/plugins/infra/public/alerting/metric_threshold/index.ts @@ -10,18 +10,18 @@ import React from 'react'; import { ObservabilityRuleTypeModel } from '../../../../observability/public'; import { validateMetricThreshold } from './components/validation'; import { formatReason } from './rule_data_formatters'; -import { AlertTypeParams } from '../../../../alerting/common'; +import { AlertTypeParams as RuleTypeParams } from '../../../../alerting/common'; import { MetricExpressionParams, METRIC_THRESHOLD_ALERT_TYPE_ID, // eslint-disable-next-line @kbn/eslint/no-restricted-paths } from '../../../server/lib/alerting/metric_threshold/types'; -interface MetricThresholdAlertTypeParams extends AlertTypeParams { +interface MetricThresholdRuleTypeParams extends RuleTypeParams { criteria: MetricExpressionParams[]; } -export function createMetricThresholdAlertType(): ObservabilityRuleTypeModel { +export function createMetricThresholdRuleType(): ObservabilityRuleTypeModel { return { id: METRIC_THRESHOLD_ALERT_TYPE_ID, description: i18n.translate('xpack.infra.metrics.alertFlyout.alertDescription', { diff --git a/x-pack/plugins/infra/public/plugin.ts b/x-pack/plugins/infra/public/plugin.ts index 5565c90970ecd..bc3aff9f01637 100644 --- a/x-pack/plugins/infra/public/plugin.ts +++ b/x-pack/plugins/infra/public/plugin.ts @@ -31,19 +31,17 @@ export class Plugin implements InfraClientPluginClass { registerFeatures(pluginsSetup.home); } - const { createInventoryMetricAlertType } = await import('./alerting/inventory'); - const { createLogThresholdAlertType } = await import('./alerting/log_threshold'); - const { createMetricThresholdAlertType } = await import('./alerting/metric_threshold'); + const { createInventoryMetricRuleType } = await import('./alerting/inventory'); + const { createLogThresholdRuleType } = await import('./alerting/log_threshold'); + const { createMetricThresholdRuleType } = await import('./alerting/metric_threshold'); pluginsSetup.observability.observabilityRuleTypeRegistry.register( - createInventoryMetricAlertType() + createInventoryMetricRuleType() ); + pluginsSetup.observability.observabilityRuleTypeRegistry.register(createLogThresholdRuleType()); pluginsSetup.observability.observabilityRuleTypeRegistry.register( - createLogThresholdAlertType() - ); - pluginsSetup.observability.observabilityRuleTypeRegistry.register( - createMetricThresholdAlertType() + createMetricThresholdRuleType() ); pluginsSetup.observability.dashboard.register({ appName: 'infra_logs', diff --git a/x-pack/plugins/infra/server/lib/alerting/index.ts b/x-pack/plugins/infra/server/lib/alerting/index.ts index fb3cd7702c3df..570b44fd44db9 100644 --- a/x-pack/plugins/infra/server/lib/alerting/index.ts +++ b/x-pack/plugins/infra/server/lib/alerting/index.ts @@ -5,4 +5,4 @@ * 2.0. */ -export { registerAlertTypes } from './register_alert_types'; +export { registerRuleTypes } from './register_rule_types'; diff --git a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition.ts b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition.ts index 922b10e8bd2b0..364c6b5a0d23a 100644 --- a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition.ts +++ b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition.ts @@ -8,18 +8,18 @@ import { mapValues, last, first } from 'lodash'; import moment from 'moment'; import { ElasticsearchClient } from 'kibana/server'; -import { SnapshotCustomMetricInput } from '../../../../common/http_api/snapshot_api'; import { isTooManyBucketsPreviewException, TOO_MANY_BUCKETS_PREVIEW_EXCEPTION, } from '../../../../common/alerting/metrics'; -import { - InfraDatabaseSearchResponse, - CallWithRequestParams, -} from '../../adapters/framework/adapter_types'; +import { InfraDatabaseSearchResponse, CallWithRequestParams } from '../../adapters/framework'; import { Comparator, InventoryMetricConditions } from './types'; import { InventoryItemType, SnapshotMetricType } from '../../../../common/inventory_models/types'; -import { InfraTimerangeInput, SnapshotRequest } from '../../../../common/http_api/snapshot_api'; +import { + InfraTimerangeInput, + SnapshotRequest, + SnapshotCustomMetricInput, +} from '../../../../common/http_api'; import { InfraSource } from '../../sources'; import { UNGROUPED_FACTORY_KEY } from '../common/utils'; import { getNodes } from '../../../routes/snapshot/lib/get_nodes'; diff --git a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/inventory_metric_threshold_executor.ts b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/inventory_metric_threshold_executor.ts index 654d69eb7fabb..dda43178713c2 100644 --- a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/inventory_metric_threshold_executor.ts +++ b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/inventory_metric_threshold_executor.ts @@ -15,11 +15,14 @@ import { AlertStates } from './types'; import { ActionGroupIdsOf, ActionGroup, - AlertInstanceContext, - AlertInstanceState, + AlertInstanceContext as AlertContext, + AlertInstanceState as AlertState, RecoveredActionGroup, } from '../../../../../alerting/common'; -import { AlertInstance, AlertTypeState } from '../../../../../alerting/server'; +import { + AlertInstance as Alert, + AlertTypeState as RuleTypeState, +} from '../../../../../alerting/server'; import { SnapshotMetricType } from '../../../../common/inventory_models/types'; import { InfraBackendLibs } from '../../infra_types'; import { METRIC_FORMATTERS } from '../../../../common/formatters/snapshot_metric_formats'; @@ -39,34 +42,34 @@ type InventoryMetricThresholdAllowedActionGroups = ActionGroupIdsOf< typeof FIRED_ACTIONS | typeof WARNING_ACTIONS >; -export type InventoryMetricThresholdAlertTypeState = AlertTypeState; // no specific state used -export type InventoryMetricThresholdAlertInstanceState = AlertInstanceState; // no specific state used -export type InventoryMetricThresholdAlertInstanceContext = AlertInstanceContext; // no specific instance context used +export type InventoryMetricThresholdRuleTypeState = RuleTypeState; // no specific state used +export type InventoryMetricThresholdAlertState = AlertState; // no specific state used +export type InventoryMetricThresholdAlertContext = AlertContext; // no specific instance context used -type InventoryMetricThresholdAlertInstance = AlertInstance< - InventoryMetricThresholdAlertInstanceState, - InventoryMetricThresholdAlertInstanceContext, +type InventoryMetricThresholdAlert = Alert< + InventoryMetricThresholdAlertState, + InventoryMetricThresholdAlertContext, InventoryMetricThresholdAllowedActionGroups >; -type InventoryMetricThresholdAlertInstanceFactory = ( +type InventoryMetricThresholdAlertFactory = ( id: string, reason: string, threshold?: number | undefined, value?: number | undefined -) => InventoryMetricThresholdAlertInstance; +) => InventoryMetricThresholdAlert; export const createInventoryMetricThresholdExecutor = (libs: InfraBackendLibs) => libs.metricsRules.createLifecycleRuleExecutor< InventoryMetricThresholdParams & Record, - InventoryMetricThresholdAlertTypeState, - InventoryMetricThresholdAlertInstanceState, - InventoryMetricThresholdAlertInstanceContext, + InventoryMetricThresholdRuleTypeState, + InventoryMetricThresholdAlertState, + InventoryMetricThresholdAlertContext, InventoryMetricThresholdAllowedActionGroups >(async ({ services, params }) => { const { criteria, filterQuery, sourceId, nodeType, alertOnNoData } = params; if (criteria.length === 0) throw new Error('Cannot execute an alert with 0 conditions'); const { alertWithLifecycle, savedObjectsClient } = services; - const alertInstanceFactory: InventoryMetricThresholdAlertInstanceFactory = (id, reason) => + const alertFactory: InventoryMetricThresholdAlertFactory = (id, reason) => alertWithLifecycle({ id, fields: { @@ -82,8 +85,8 @@ export const createInventoryMetricThresholdExecutor = (libs: InfraBackendLibs) = } catch (e) { const actionGroupId = FIRED_ACTIONS.id; // Change this to an Error action group when able const reason = buildInvalidQueryAlertReason(params.filterQueryText); - const alertInstance = alertInstanceFactory('*', reason); - alertInstance.scheduleActions(actionGroupId, { + const alert = alertFactory('*', reason); + alert.scheduleActions(actionGroupId, { group: '*', alertState: stateToAlertMessage[AlertStates.ERROR], reason, @@ -191,8 +194,8 @@ export const createInventoryMetricThresholdExecutor = (libs: InfraBackendLibs) = ? WARNING_ACTIONS.id : FIRED_ACTIONS.id; - const alertInstance = alertInstanceFactory(`${group}`, reason); - alertInstance.scheduleActions( + const alert = alertFactory(`${group}`, reason); + alert.scheduleActions( /** * TODO: We're lying to the compiler here as explicitly calling `scheduleActions` on * the RecoveredActionGroup isn't allowed diff --git a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/register_inventory_metric_threshold_alert_type.ts b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/register_inventory_metric_threshold_rule_type.ts similarity index 98% rename from x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/register_inventory_metric_threshold_alert_type.ts rename to x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/register_inventory_metric_threshold_rule_type.ts index 77c85967e64f6..9776d1ab66915 100644 --- a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/register_inventory_metric_threshold_alert_type.ts +++ b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/register_inventory_metric_threshold_rule_type.ts @@ -58,7 +58,7 @@ const condition = schema.object({ ), }); -export async function registerMetricInventoryThresholdAlertType( +export async function registerMetricInventoryThresholdRuleType( alertingPlugin: PluginSetupContract, libs: InfraBackendLibs ) { diff --git a/x-pack/plugins/infra/server/lib/alerting/log_threshold/log_threshold_executor.test.ts b/x-pack/plugins/infra/server/lib/alerting/log_threshold/log_threshold_executor.test.ts index e5d8bab948581..b5cf05512b353 100644 --- a/x-pack/plugins/infra/server/lib/alerting/log_threshold/log_threshold_executor.test.ts +++ b/x-pack/plugins/infra/server/lib/alerting/log_threshold/log_threshold_executor.test.ts @@ -22,7 +22,7 @@ import { Criterion, UngroupedSearchQueryResponse, GroupedSearchQueryResponse, -} from '../../../../common/alerting/logs/log_threshold/types'; +} from '../../../../common/alerting/logs/log_threshold'; import { alertsMock } from '../../../../../alerting/server/mocks'; import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; @@ -407,7 +407,7 @@ describe('Log threshold executor', () => { describe('Results processors', () => { describe('Can process ungrouped results', () => { test('It handles the ALERT state correctly', () => { - const alertInstanceUpdaterMock = jest.fn(); + const alertUpdaterMock = jest.fn(); const alertParams = { ...baseAlertParams, criteria: [positiveCriteria[0]], @@ -423,12 +423,12 @@ describe('Log threshold executor', () => { results, alertParams, alertsMock.createAlertInstanceFactory, - alertInstanceUpdaterMock + alertUpdaterMock ); // First call, second argument - expect(alertInstanceUpdaterMock.mock.calls[0][1]).toBe(AlertStates.ALERT); + expect(alertUpdaterMock.mock.calls[0][1]).toBe(AlertStates.ALERT); // First call, third argument - expect(alertInstanceUpdaterMock.mock.calls[0][2]).toEqual([ + expect(alertUpdaterMock.mock.calls[0][2]).toEqual([ { actionGroup: 'logs.threshold.fired', context: { @@ -444,7 +444,7 @@ describe('Log threshold executor', () => { describe('Can process grouped results', () => { test('It handles the ALERT state correctly', () => { - const alertInstanceUpdaterMock = jest.fn(); + const alertUpdaterMock = jest.fn(); const alertParams = { ...baseAlertParams, criteria: [positiveCriteria[0]], @@ -487,13 +487,13 @@ describe('Log threshold executor', () => { results, alertParams, alertsMock.createAlertInstanceFactory, - alertInstanceUpdaterMock + alertUpdaterMock ); - expect(alertInstanceUpdaterMock.mock.calls.length).toBe(2); + expect(alertUpdaterMock.mock.calls.length).toBe(2); // First call, second argument - expect(alertInstanceUpdaterMock.mock.calls[0][1]).toBe(AlertStates.ALERT); + expect(alertUpdaterMock.mock.calls[0][1]).toBe(AlertStates.ALERT); // First call, third argument - expect(alertInstanceUpdaterMock.mock.calls[0][2]).toEqual([ + expect(alertUpdaterMock.mock.calls[0][2]).toEqual([ { actionGroup: 'logs.threshold.fired', context: { @@ -506,9 +506,9 @@ describe('Log threshold executor', () => { ]); // Second call, second argument - expect(alertInstanceUpdaterMock.mock.calls[1][1]).toBe(AlertStates.ALERT); + expect(alertUpdaterMock.mock.calls[1][1]).toBe(AlertStates.ALERT); // Second call, third argument - expect(alertInstanceUpdaterMock.mock.calls[1][2]).toEqual([ + expect(alertUpdaterMock.mock.calls[1][2]).toEqual([ { actionGroup: 'logs.threshold.fired', context: { diff --git a/x-pack/plugins/infra/server/lib/alerting/log_threshold/log_threshold_executor.ts b/x-pack/plugins/infra/server/lib/alerting/log_threshold/log_threshold_executor.ts index 6d2b074c45bb0..a41c70f5c2869 100644 --- a/x-pack/plugins/infra/server/lib/alerting/log_threshold/log_threshold_executor.ts +++ b/x-pack/plugins/infra/server/lib/alerting/log_threshold/log_threshold_executor.ts @@ -16,10 +16,10 @@ import { ElasticsearchClient } from 'kibana/server'; import { ActionGroup, ActionGroupIdsOf, - AlertInstance, - AlertInstanceContext, - AlertInstanceState, - AlertTypeState, + AlertInstance as Alert, + AlertInstanceContext as AlertContext, + AlertInstanceState as AlertState, + AlertTypeState as RuleTypeState, } from '../../../../../alerting/server'; import { AlertParams, @@ -40,7 +40,7 @@ import { RatioAlertParams, UngroupedSearchQueryResponse, UngroupedSearchQueryResponseRT, -} from '../../../../common/alerting/logs/log_threshold/types'; +} from '../../../../common/alerting/logs/log_threshold'; import { resolveLogSourceConfiguration } from '../../../../common/log_sources'; import { decodeOrThrow } from '../../../../common/runtime_types'; import { getIntervalInSeconds } from '../../../utils/get_interval_in_seconds'; @@ -54,22 +54,22 @@ import { } from './reason_formatters'; export type LogThresholdActionGroups = ActionGroupIdsOf; -export type LogThresholdAlertTypeParams = AlertParams; -export type LogThresholdAlertTypeState = AlertTypeState; // no specific state used -export type LogThresholdAlertInstanceState = AlertInstanceState; // no specific state used -export type LogThresholdAlertInstanceContext = AlertInstanceContext; // no specific instance context used - -type LogThresholdAlertInstance = AlertInstance< - LogThresholdAlertInstanceState, - LogThresholdAlertInstanceContext, +export type LogThresholdRuleTypeParams = AlertParams; +export type LogThresholdRuleTypeState = RuleTypeState; // no specific state used +export type LogThresholdAlertState = AlertState; // no specific state used +export type LogThresholdAlertContext = AlertContext; // no specific instance context used + +type LogThresholdAlert = Alert< + LogThresholdAlertState, + LogThresholdAlertContext, LogThresholdActionGroups >; -type LogThresholdAlertInstanceFactory = ( +type LogThresholdAlertFactory = ( id: string, reason: string, value: number, threshold: number -) => LogThresholdAlertInstance; +) => LogThresholdAlert; const COMPOSITE_GROUP_SIZE = 2000; @@ -88,15 +88,15 @@ const checkValueAgainstComparatorMap: { export const createLogThresholdExecutor = (libs: InfraBackendLibs) => libs.logsRules.createLifecycleRuleExecutor< - LogThresholdAlertTypeParams, - LogThresholdAlertTypeState, - LogThresholdAlertInstanceState, - LogThresholdAlertInstanceContext, + LogThresholdRuleTypeParams, + LogThresholdRuleTypeState, + LogThresholdAlertState, + LogThresholdAlertContext, LogThresholdActionGroups >(async ({ services, params }) => { const { alertWithLifecycle, savedObjectsClient, scopedClusterClient } = services; const { sources } = libs; - const alertInstanceFactory: LogThresholdAlertInstanceFactory = (id, reason, value, threshold) => + const alertFactory: LogThresholdAlertFactory = (id, reason, value, threshold) => alertWithLifecycle({ id, fields: { @@ -125,7 +125,7 @@ export const createLogThresholdExecutor = (libs: InfraBackendLibs) => indices, runtimeMappings, scopedClusterClient.asCurrentUser, - alertInstanceFactory + alertFactory ); } else { await executeRatioAlert( @@ -134,7 +134,7 @@ export const createLogThresholdExecutor = (libs: InfraBackendLibs) => indices, runtimeMappings, scopedClusterClient.asCurrentUser, - alertInstanceFactory + alertFactory ); } } catch (e) { @@ -148,7 +148,7 @@ async function executeAlert( indexPattern: string, runtimeMappings: estypes.MappingRuntimeFields, esClient: ElasticsearchClient, - alertInstanceFactory: LogThresholdAlertInstanceFactory + alertFactory: LogThresholdAlertFactory ) { const query = getESQuery(alertParams, timestampField, indexPattern, runtimeMappings); @@ -160,15 +160,15 @@ async function executeAlert( processGroupByResults( await getGroupedResults(query, esClient), alertParams, - alertInstanceFactory, - updateAlertInstance + alertFactory, + updateAlert ); } else { processUngroupedResults( await getUngroupedResults(query, esClient), alertParams, - alertInstanceFactory, - updateAlertInstance + alertFactory, + updateAlert ); } } @@ -179,7 +179,7 @@ async function executeRatioAlert( indexPattern: string, runtimeMappings: estypes.MappingRuntimeFields, esClient: ElasticsearchClient, - alertInstanceFactory: LogThresholdAlertInstanceFactory + alertFactory: LogThresholdAlertFactory ) { // Ratio alert params are separated out into two standard sets of alert params const numeratorParams: AlertParams = { @@ -211,8 +211,8 @@ async function executeRatioAlert( numeratorGroupedResults, denominatorGroupedResults, alertParams, - alertInstanceFactory, - updateAlertInstance + alertFactory, + updateAlert ); } else { const numeratorUngroupedResults = await getUngroupedResults(numeratorQuery, esClient); @@ -221,8 +221,8 @@ async function executeRatioAlert( numeratorUngroupedResults, denominatorUngroupedResults, alertParams, - alertInstanceFactory, - updateAlertInstance + alertFactory, + updateAlert ); } } @@ -241,20 +241,20 @@ const getESQuery = ( export const processUngroupedResults = ( results: UngroupedSearchQueryResponse, params: CountAlertParams, - alertInstanceFactory: LogThresholdAlertInstanceFactory, - alertInstaceUpdater: AlertInstanceUpdater + alertFactory: LogThresholdAlertFactory, + alertUpdater: AlertUpdater ) => { const { count, criteria } = params; const documentCount = results.hits.total.value; if (checkValueAgainstComparatorMap[count.comparator](documentCount, count.value)) { - const alertInstance = alertInstanceFactory( + const alert = alertFactory( UNGROUPED_FACTORY_KEY, getReasonMessageForUngroupedCountAlert(documentCount, count.value, count.comparator), documentCount, count.value ); - alertInstaceUpdater(alertInstance, AlertStates.ALERT, [ + alertUpdater(alert, AlertStates.ALERT, [ { actionGroup: FIRED_ACTIONS.id, context: { @@ -272,8 +272,8 @@ export const processUngroupedRatioResults = ( numeratorResults: UngroupedSearchQueryResponse, denominatorResults: UngroupedSearchQueryResponse, params: RatioAlertParams, - alertInstanceFactory: LogThresholdAlertInstanceFactory, - alertInstaceUpdater: AlertInstanceUpdater + alertFactory: LogThresholdAlertFactory, + alertUpdater: AlertUpdater ) => { const { count, criteria } = params; @@ -282,13 +282,13 @@ export const processUngroupedRatioResults = ( const ratio = getRatio(numeratorCount, denominatorCount); if (ratio !== undefined && checkValueAgainstComparatorMap[count.comparator](ratio, count.value)) { - const alertInstance = alertInstanceFactory( + const alert = alertFactory( UNGROUPED_FACTORY_KEY, getReasonMessageForUngroupedRatioAlert(ratio, count.value, count.comparator), ratio, count.value ); - alertInstaceUpdater(alertInstance, AlertStates.ALERT, [ + alertUpdater(alert, AlertStates.ALERT, [ { actionGroup: FIRED_ACTIONS.id, context: { @@ -345,8 +345,8 @@ const getReducedGroupByResults = ( export const processGroupByResults = ( results: GroupedSearchQueryResponse['aggregations']['groups']['buckets'], params: CountAlertParams, - alertInstanceFactory: LogThresholdAlertInstanceFactory, - alertInstaceUpdater: AlertInstanceUpdater + alertFactory: LogThresholdAlertFactory, + alertUpdater: AlertUpdater ) => { const { count, criteria } = params; @@ -356,7 +356,7 @@ export const processGroupByResults = ( const documentCount = group.documentCount; if (checkValueAgainstComparatorMap[count.comparator](documentCount, count.value)) { - const alertInstance = alertInstanceFactory( + const alert = alertFactory( group.name, getReasonMessageForGroupedCountAlert( documentCount, @@ -367,7 +367,7 @@ export const processGroupByResults = ( documentCount, count.value ); - alertInstaceUpdater(alertInstance, AlertStates.ALERT, [ + alertUpdater(alert, AlertStates.ALERT, [ { actionGroup: FIRED_ACTIONS.id, context: { @@ -386,8 +386,8 @@ export const processGroupByRatioResults = ( numeratorResults: GroupedSearchQueryResponse['aggregations']['groups']['buckets'], denominatorResults: GroupedSearchQueryResponse['aggregations']['groups']['buckets'], params: RatioAlertParams, - alertInstanceFactory: LogThresholdAlertInstanceFactory, - alertInstaceUpdater: AlertInstanceUpdater + alertFactory: LogThresholdAlertFactory, + alertUpdater: AlertUpdater ) => { const { count, criteria } = params; @@ -407,7 +407,7 @@ export const processGroupByRatioResults = ( ratio !== undefined && checkValueAgainstComparatorMap[count.comparator](ratio, count.value) ) { - const alertInstance = alertInstanceFactory( + const alert = alertFactory( numeratorGroup.name, getReasonMessageForGroupedRatioAlert( ratio, @@ -418,7 +418,7 @@ export const processGroupByRatioResults = ( ratio, count.value ); - alertInstaceUpdater(alertInstance, AlertStates.ALERT, [ + alertUpdater(alert, AlertStates.ALERT, [ { actionGroup: FIRED_ACTIONS.id, context: { @@ -434,24 +434,24 @@ export const processGroupByRatioResults = ( }); }; -type AlertInstanceUpdater = ( - alertInstance: AlertInstance, +type AlertUpdater = ( + alert: Alert, state: AlertStates, - actions?: Array<{ actionGroup: LogThresholdActionGroups; context: AlertInstanceContext }> + actions?: Array<{ actionGroup: LogThresholdActionGroups; context: AlertContext }> ) => void; -export const updateAlertInstance: AlertInstanceUpdater = (alertInstance, state, actions) => { +export const updateAlert: AlertUpdater = (alert, state, actions) => { if (actions && actions.length > 0) { const sharedContext = { timestamp: new Date().toISOString(), }; actions.forEach((actionSet) => { const { actionGroup, context } = actionSet; - alertInstance.scheduleActions(actionGroup, { ...sharedContext, ...context }); + alert.scheduleActions(actionGroup, { ...sharedContext, ...context }); }); } - alertInstance.replaceState({ + alert.replaceState({ alertState: state, }); }; diff --git a/x-pack/plugins/infra/server/lib/alerting/log_threshold/register_log_threshold_alert_type.ts b/x-pack/plugins/infra/server/lib/alerting/log_threshold/register_log_threshold_rule_type.ts similarity index 97% rename from x-pack/plugins/infra/server/lib/alerting/log_threshold/register_log_threshold_alert_type.ts rename to x-pack/plugins/infra/server/lib/alerting/log_threshold/register_log_threshold_rule_type.ts index 3d0bac3dd2bf5..05dc2682fc3b7 100644 --- a/x-pack/plugins/infra/server/lib/alerting/log_threshold/register_log_threshold_alert_type.ts +++ b/x-pack/plugins/infra/server/lib/alerting/log_threshold/register_log_threshold_rule_type.ts @@ -11,7 +11,7 @@ import { createLogThresholdExecutor, FIRED_ACTIONS } from './log_threshold_execu import { LOG_DOCUMENT_COUNT_ALERT_TYPE_ID, alertParamsRT, -} from '../../../../common/alerting/logs/log_threshold/types'; +} from '../../../../common/alerting/logs/log_threshold'; import { InfraBackendLibs } from '../../infra_types'; import { decodeOrThrow } from '../../../../common/runtime_types'; @@ -71,7 +71,7 @@ const denominatorConditionsActionVariableDescription = i18n.translate( } ); -export async function registerLogThresholdAlertType( +export async function registerLogThresholdRuleType( alertingPlugin: PluginSetupContract, libs: InfraBackendLibs ) { diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_anomaly/metric_anomaly_executor.ts b/x-pack/plugins/infra/server/lib/alerting/metric_anomaly/metric_anomaly_executor.ts index f7dbe95b4161c..a0eac87ed161e 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_anomaly/metric_anomaly_executor.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_anomaly/metric_anomaly_executor.ts @@ -8,20 +8,20 @@ import { i18n } from '@kbn/i18n'; import { first } from 'lodash'; import moment from 'moment'; +import { KibanaRequest } from 'kibana/server'; import { stateToAlertMessage } from '../common/messages'; import { MetricAnomalyParams } from '../../../../common/alerting/metrics'; import { MappedAnomalyHit } from '../../infra_ml'; import { AlertStates } from '../common/types'; import { ActionGroup, - AlertInstanceContext, - AlertInstanceState, + AlertInstanceContext as AlertContext, + AlertInstanceState as AlertState, } from '../../../../../alerting/common'; -import { AlertExecutorOptions } from '../../../../../alerting/server'; +import { AlertExecutorOptions as RuleExecutorOptions } from '../../../../../alerting/server'; import { getIntervalInSeconds } from '../../../utils/get_interval_in_seconds'; -import { MetricAnomalyAllowedActionGroups } from './register_metric_anomaly_alert_type'; +import { MetricAnomalyAllowedActionGroups } from './register_metric_anomaly_rule_type'; import { MlPluginSetup } from '../../../../../ml/server'; -import { KibanaRequest } from '../../../../../../../src/core/server'; import { InfraBackendLibs } from '../../infra_types'; import { evaluateCondition } from './evaluate_condition'; @@ -31,14 +31,14 @@ export const createMetricAnomalyExecutor = services, params, startedAt, - }: AlertExecutorOptions< + }: RuleExecutorOptions< /** * TODO: Remove this use of `any` by utilizing a proper type */ Record, Record, - AlertInstanceState, - AlertInstanceContext, + AlertState, + AlertContext, MetricAnomalyAllowedActionGroups >) => { if (!ml) { @@ -84,9 +84,9 @@ export const createMetricAnomalyExecutor = typical, influencers, } = first(data as MappedAnomalyHit[])!; - const alertInstance = services.alertInstanceFactory(`${nodeType}-${metric}`); + const alert = services.alertInstanceFactory(`${nodeType}-${metric}`); - alertInstance.scheduleActions(FIRED_ACTIONS_ID, { + alert.scheduleActions(FIRED_ACTIONS_ID, { alertState: stateToAlertMessage[AlertStates.ALERT], timestamp: moment(anomalyStartTime).toISOString(), anomalyScore, diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_anomaly/register_metric_anomaly_alert_type.ts b/x-pack/plugins/infra/server/lib/alerting/metric_anomaly/register_metric_anomaly_rule_type.ts similarity index 95% rename from x-pack/plugins/infra/server/lib/alerting/metric_anomaly/register_metric_anomaly_alert_type.ts rename to x-pack/plugins/infra/server/lib/alerting/metric_anomaly/register_metric_anomaly_rule_type.ts index 2e3660c901b4a..dd90297355742 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_anomaly/register_metric_anomaly_alert_type.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_anomaly/register_metric_anomaly_rule_type.ts @@ -9,9 +9,9 @@ import { schema } from '@kbn/config-schema'; import { i18n } from '@kbn/i18n'; import { MlPluginSetup } from '../../../../../ml/server'; import { - AlertType, - AlertInstanceState, - AlertInstanceContext, + AlertType as RuleType, + AlertInstanceState as AlertState, + AlertInstanceContext as AlertContext, } from '../../../../../alerting/server'; import { createMetricAnomalyExecutor, @@ -26,18 +26,18 @@ import { RecoveredActionGroupId } from '../../../../../alerting/common'; export type MetricAnomalyAllowedActionGroups = typeof FIRED_ACTIONS_ID; -export const registerMetricAnomalyAlertType = ( +export const registerMetricAnomalyRuleType = ( libs: InfraBackendLibs, ml?: MlPluginSetup -): AlertType< +): RuleType< /** * TODO: Remove this use of `any` by utilizing a proper type */ Record, never, // Only use if defining useSavedObjectReferences hook Record, - AlertInstanceState, - AlertInstanceContext, + AlertState, + AlertContext, MetricAnomalyAllowedActionGroups, RecoveredActionGroupId > => ({ diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_rule.ts similarity index 98% rename from x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts rename to x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_rule.ts index e8910572d4a09..47727314cc64f 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_rule.ts @@ -53,7 +53,7 @@ interface CompositeAggregationsResponse { }; } -export interface EvaluatedAlertParams { +export interface EvaluatedRuleParams { criteria: MetricExpressionParams[]; groupBy: string | undefined | string[]; filterQuery?: string; @@ -61,7 +61,7 @@ export interface EvaluatedAlertParams { shouldDropPartialBuckets?: boolean; } -export const evaluateAlert = ( +export const evaluateRule = ( esClient: ElasticsearchClient, params: Params, config: InfraSource['configuration'], diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.test.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.test.ts index 792e3a60747d0..5a75b18e47590 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.test.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.test.ts @@ -19,7 +19,10 @@ import { createLifecycleRuleExecutorMock } from '../../../../../rule_registry/se import { InfraSources } from '../../sources'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { elasticsearchClientMock } from 'src/core/server/elasticsearch/client/mocks'; -import { AlertInstanceContext, AlertInstanceState } from '../../../../../alerting/server'; +import { + AlertInstanceContext as AlertContext, + AlertInstanceState as AlertState, +} from '../../../../../alerting/server'; import { Aggregators, Comparator, @@ -763,8 +766,7 @@ const mockLibs: any = { const executor = createMetricThresholdExecutor(mockLibs); const alertsServices = alertsMock.createAlertServices(); -const services: AlertServicesMock & - LifecycleAlertServices = { +const services: AlertServicesMock & LifecycleAlertServices = { ...alertsServices, ...ruleRegistryMocks.createLifecycleAlertServices(alertsServices), }; diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.ts index c4e485af5bdb1..810055fc1771a 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.ts @@ -12,10 +12,13 @@ import { ALERT_REASON } from '@kbn/rule-data-utils'; import { ActionGroupIdsOf, RecoveredActionGroup, - AlertInstanceState, - AlertInstanceContext, + AlertInstanceState as AlertState, + AlertInstanceContext as AlertContext, } from '../../../../../alerting/common'; -import { AlertTypeState, AlertInstance } from '../../../../../alerting/server'; +import { + AlertTypeState as RuleTypeState, + AlertInstance as Alert, +} from '../../../../../alerting/server'; import { InfraBackendLibs } from '../../infra_types'; import { buildErrorAlertReason, @@ -28,47 +31,47 @@ import { import { UNGROUPED_FACTORY_KEY } from '../common/utils'; import { createFormatter } from '../../../../common/formatters'; import { AlertStates, Comparator } from './types'; -import { evaluateAlert, EvaluatedAlertParams } from './lib/evaluate_alert'; +import { evaluateRule, EvaluatedRuleParams } from './lib/evaluate_rule'; -export type MetricThresholdAlertTypeParams = Record; -export type MetricThresholdAlertTypeState = AlertTypeState & { +export type MetricThresholdRuleParams = Record; +export type MetricThresholdRuleTypeState = RuleTypeState & { groups: string[]; groupBy?: string | string[]; filterQuery?: string; }; -export type MetricThresholdAlertInstanceState = AlertInstanceState; // no specific instace state used -export type MetricThresholdAlertInstanceContext = AlertInstanceContext; // no specific instace state used +export type MetricThresholdAlertState = AlertState; // no specific instace state used +export type MetricThresholdAlertContext = AlertContext; // no specific instace state used type MetricThresholdAllowedActionGroups = ActionGroupIdsOf< typeof FIRED_ACTIONS | typeof WARNING_ACTIONS >; -type MetricThresholdAlertInstance = AlertInstance< - MetricThresholdAlertInstanceState, - MetricThresholdAlertInstanceContext, +type MetricThresholdAlert = Alert< + MetricThresholdAlertState, + MetricThresholdAlertContext, MetricThresholdAllowedActionGroups >; -type MetricThresholdAlertInstanceFactory = ( +type MetricThresholdAlertFactory = ( id: string, reason: string, threshold?: number | undefined, value?: number | undefined -) => MetricThresholdAlertInstance; +) => MetricThresholdAlert; export const createMetricThresholdExecutor = (libs: InfraBackendLibs) => libs.metricsRules.createLifecycleRuleExecutor< - MetricThresholdAlertTypeParams, - MetricThresholdAlertTypeState, - MetricThresholdAlertInstanceState, - MetricThresholdAlertInstanceContext, + MetricThresholdRuleParams, + MetricThresholdRuleTypeState, + MetricThresholdAlertState, + MetricThresholdAlertContext, MetricThresholdAllowedActionGroups >(async function (options) { const { services, params, state } = options; const { criteria } = params; if (criteria.length === 0) throw new Error('Cannot execute an alert with 0 conditions'); const { alertWithLifecycle, savedObjectsClient } = services; - const alertInstanceFactory: MetricThresholdAlertInstanceFactory = (id, reason) => + const alertFactory: MetricThresholdAlertFactory = (id, reason) => alertWithLifecycle({ id, fields: { @@ -94,8 +97,8 @@ export const createMetricThresholdExecutor = (libs: InfraBackendLibs) => const timestamp = moment().toISOString(); const actionGroupId = FIRED_ACTIONS.id; // Change this to an Error action group when able const reason = buildInvalidQueryAlertReason(params.filterQueryText); - const alertInstance = alertInstanceFactory(UNGROUPED_FACTORY_KEY, reason); - alertInstance.scheduleActions(actionGroupId, { + const alert = alertFactory(UNGROUPED_FACTORY_KEY, reason); + alert.scheduleActions(actionGroupId, { group: UNGROUPED_FACTORY_KEY, alertState: stateToAlertMessage[AlertStates.ERROR], reason, @@ -128,9 +131,9 @@ export const createMetricThresholdExecutor = (libs: InfraBackendLibs) => state.groups?.filter((g) => g !== UNGROUPED_FACTORY_KEY) ?? [] : []; - const alertResults = await evaluateAlert( + const alertResults = await evaluateRule( services.scopedClusterClient.asCurrentUser, - params as EvaluatedAlertParams, + params as EvaluatedRuleParams, config, prevGroups ); @@ -227,8 +230,8 @@ export const createMetricThresholdExecutor = (libs: InfraBackendLibs) => : nextState === AlertStates.WARNING ? WARNING_ACTIONS.id : FIRED_ACTIONS.id; - const alertInstance = alertInstanceFactory(`${group}`, reason); - alertInstance.scheduleActions(actionGroupId, { + const alert = alertFactory(`${group}`, reason); + alert.scheduleActions(actionGroupId, { group, alertState: stateToAlertMessage[nextState], reason, diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/register_metric_threshold_alert_type.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/register_metric_threshold_rule_type.ts similarity index 97% rename from x-pack/plugins/infra/server/lib/alerting/metric_threshold/register_metric_threshold_alert_type.ts rename to x-pack/plugins/infra/server/lib/alerting/metric_threshold/register_metric_threshold_rule_type.ts index 251531b4515a9..0a67dbdc3190f 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/register_metric_threshold_alert_type.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/register_metric_threshold_rule_type.ts @@ -9,7 +9,7 @@ import { schema } from '@kbn/config-schema'; import { i18n } from '@kbn/i18n'; import { ActionGroupIdsOf } from '../../../../../alerting/common'; import { AlertType, PluginSetupContract } from '../../../../../alerting/server'; -import { METRIC_EXPLORER_AGGREGATIONS } from '../../../../common/http_api/metrics_explorer'; +import { METRIC_EXPLORER_AGGREGATIONS } from '../../../../common/http_api'; import { createMetricThresholdExecutor, FIRED_ACTIONS, @@ -35,7 +35,7 @@ export type MetricThresholdAlertType = Omit & { ActionGroupIdsOf: MetricThresholdAllowedActionGroups; }; -export async function registerMetricThresholdAlertType( +export async function registerMetricThresholdRuleType( alertingPlugin: PluginSetupContract, libs: InfraBackendLibs ) { diff --git a/x-pack/plugins/infra/server/lib/alerting/register_alert_types.ts b/x-pack/plugins/infra/server/lib/alerting/register_alert_types.ts deleted file mode 100644 index d0af9ac4ce669..0000000000000 --- a/x-pack/plugins/infra/server/lib/alerting/register_alert_types.ts +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { PluginSetupContract } from '../../../../alerting/server'; -import { registerMetricThresholdAlertType } from './metric_threshold/register_metric_threshold_alert_type'; -import { registerMetricInventoryThresholdAlertType } from './inventory_metric_threshold/register_inventory_metric_threshold_alert_type'; -import { registerMetricAnomalyAlertType } from './metric_anomaly/register_metric_anomaly_alert_type'; - -import { registerLogThresholdAlertType } from './log_threshold/register_log_threshold_alert_type'; -import { InfraBackendLibs } from '../infra_types'; -import { MlPluginSetup } from '../../../../ml/server'; - -const registerAlertTypes = ( - alertingPlugin: PluginSetupContract, - libs: InfraBackendLibs, - ml?: MlPluginSetup -) => { - if (alertingPlugin) { - alertingPlugin.registerType(registerMetricAnomalyAlertType(libs, ml)); - - const registerFns = [ - registerLogThresholdAlertType, - registerMetricInventoryThresholdAlertType, - registerMetricThresholdAlertType, - ]; - registerFns.forEach((fn) => { - fn(alertingPlugin, libs); - }); - } -}; - -export { registerAlertTypes }; diff --git a/x-pack/plugins/infra/server/lib/alerting/register_rule_types.ts b/x-pack/plugins/infra/server/lib/alerting/register_rule_types.ts new file mode 100644 index 0000000000000..a60de8b9c0ff9 --- /dev/null +++ b/x-pack/plugins/infra/server/lib/alerting/register_rule_types.ts @@ -0,0 +1,35 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { PluginSetupContract } from '../../../../alerting/server'; +import { registerMetricThresholdRuleType } from './metric_threshold/register_metric_threshold_rule_type'; +import { registerMetricInventoryThresholdRuleType } from './inventory_metric_threshold/register_inventory_metric_threshold_rule_type'; +import { registerMetricAnomalyRuleType } from './metric_anomaly/register_metric_anomaly_rule_type'; +import { registerLogThresholdRuleType } from './log_threshold/register_log_threshold_rule_type'; +import { InfraBackendLibs } from '../infra_types'; +import { MlPluginSetup } from '../../../../ml/server'; + +const registerRuleTypes = ( + alertingPlugin: PluginSetupContract, + libs: InfraBackendLibs, + ml?: MlPluginSetup +) => { + if (alertingPlugin) { + alertingPlugin.registerType(registerMetricAnomalyRuleType(libs, ml)); + + const registerFns = [ + registerLogThresholdRuleType, + registerMetricInventoryThresholdRuleType, + registerMetricThresholdRuleType, + ]; + registerFns.forEach((fn) => { + fn(alertingPlugin, libs); + }); + } +}; + +export { registerRuleTypes }; diff --git a/x-pack/plugins/infra/server/plugin.ts b/x-pack/plugins/infra/server/plugin.ts index 4e655f200d94f..79998ae6d5690 100644 --- a/x-pack/plugins/infra/server/plugin.ts +++ b/x-pack/plugins/infra/server/plugin.ts @@ -27,7 +27,7 @@ import { KibanaFramework } from './lib/adapters/framework/kibana_framework_adapt import { InfraKibanaLogEntriesAdapter } from './lib/adapters/log_entries/kibana_log_entries_adapter'; import { KibanaMetricsAdapter } from './lib/adapters/metrics/kibana_metrics_adapter'; import { InfraElasticsearchSourceStatusAdapter } from './lib/adapters/source_status'; -import { registerAlertTypes } from './lib/alerting'; +import { registerRuleTypes } from './lib/alerting'; import { InfraFieldsDomain } from './lib/domains/fields_domain'; import { InfraLogEntriesDomain } from './lib/domains/log_entries_domain'; import { InfraMetricsDomain } from './lib/domains/metrics_domain'; @@ -161,7 +161,7 @@ export class InfraServerPlugin implements Plugin { ]); initInfraServer(this.libs); - registerAlertTypes(plugins.alerting, this.libs, plugins.ml); + registerRuleTypes(plugins.alerting, this.libs, plugins.ml); core.http.registerRouteHandlerContext( 'infra', diff --git a/x-pack/test/api_integration/apis/metrics_ui/metric_threshold_alert.ts b/x-pack/test/api_integration/apis/metrics_ui/metric_threshold_alert.ts index 4467afb7585ad..ecefef2fe930c 100644 --- a/x-pack/test/api_integration/apis/metrics_ui/metric_threshold_alert.ts +++ b/x-pack/test/api_integration/apis/metrics_ui/metric_threshold_alert.ts @@ -10,9 +10,9 @@ import { convertToKibanaClient } from '@kbn/test'; import { InfraSource } from '../../../../plugins/infra/common/source_configuration/source_configuration'; import { FtrProviderContext } from '../../ftr_provider_context'; import { - evaluateAlert, - EvaluatedAlertParams, -} from '../../../../plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert'; + evaluateRule, + EvaluatedRuleParams, +} from '../../../../plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_rule'; import { Aggregators, CountMetricExpressionParams, @@ -27,7 +27,7 @@ export default function ({ getService }: FtrProviderContext) { const esArchiver = getService('esArchiver'); const esClient = getService('es'); - const baseParams: EvaluatedAlertParams = { + const baseParams: EvaluatedRuleParams = { groupBy: void 0, filterQuery: void 0, criteria: [ @@ -100,7 +100,7 @@ export default function ({ getService }: FtrProviderContext) { }; const timeFrame = { end: DATES.ten_thousand_plus.max }; const kbnClient = convertToKibanaClient(esClient); - const results = await evaluateAlert(kbnClient, params, config, [], timeFrame); + const results = await evaluateRule(kbnClient, params, config, [], timeFrame); expect(results).to.eql([ { '*': { @@ -142,7 +142,7 @@ export default function ({ getService }: FtrProviderContext) { }; const timeFrame = { end: DATES.ten_thousand_plus.max }; const kbnClient = convertToKibanaClient(esClient); - const results = await evaluateAlert(kbnClient, params, config, [], timeFrame); + const results = await evaluateRule(kbnClient, params, config, [], timeFrame); expect(results).to.eql([ { web: { @@ -184,7 +184,7 @@ export default function ({ getService }: FtrProviderContext) { }; const timeFrame = { end: gauge.max }; const kbnClient = convertToKibanaClient(esClient); - const results = await evaluateAlert(kbnClient, params, configuration, [], timeFrame); + const results = await evaluateRule(kbnClient, params, configuration, [], timeFrame); expect(results).to.eql([ { '*': { @@ -208,7 +208,7 @@ export default function ({ getService }: FtrProviderContext) { const params = { ...baseParams }; const timeFrame = { end: gauge.max }; const kbnClient = convertToKibanaClient(esClient); - const results = await evaluateAlert(kbnClient, params, configuration, [], timeFrame); + const results = await evaluateRule(kbnClient, params, configuration, [], timeFrame); expect(results).to.eql([ { '*': { @@ -246,7 +246,7 @@ export default function ({ getService }: FtrProviderContext) { }; const timeFrame = { end: gauge.max }; const kbnClient = convertToKibanaClient(esClient); - const results = await evaluateAlert(kbnClient, params, configuration, [], timeFrame); + const results = await evaluateRule(kbnClient, params, configuration, [], timeFrame); expect(results).to.eql([ { dev: { @@ -287,7 +287,7 @@ export default function ({ getService }: FtrProviderContext) { }; const timeFrame = { end: gauge.max }; const kbnClient = convertToKibanaClient(esClient); - const results = await evaluateAlert(kbnClient, params, configuration, [], timeFrame); + const results = await evaluateRule(kbnClient, params, configuration, [], timeFrame); expect(results).to.eql([ { dev: { @@ -329,7 +329,7 @@ export default function ({ getService }: FtrProviderContext) { }; const timeFrame = { end: gauge.midpoint }; const kbnClient = convertToKibanaClient(esClient); - const results = await evaluateAlert( + const results = await evaluateRule( kbnClient, params, configuration, @@ -392,7 +392,7 @@ export default function ({ getService }: FtrProviderContext) { }; const timeFrame = { end: rate.max }; const kbnClient = convertToKibanaClient(esClient); - const results = await evaluateAlert(kbnClient, params, configuration, [], timeFrame); + const results = await evaluateRule(kbnClient, params, configuration, [], timeFrame); expect(results).to.eql([ { '*': { @@ -433,7 +433,7 @@ export default function ({ getService }: FtrProviderContext) { }; const timeFrame = { end: rate.max }; const kbnClient = convertToKibanaClient(esClient); - const results = await evaluateAlert(kbnClient, params, configuration, [], timeFrame); + const results = await evaluateRule(kbnClient, params, configuration, [], timeFrame); expect(results).to.eql([ { dev: {