From 149c3feb2f718520161cc0f9fa572a52d6c5a563 Mon Sep 17 00:00:00 2001 From: Coen Warmer Date: Tue, 8 Nov 2022 11:41:21 +0100 Subject: [PATCH 01/17] Add context.alertDetailsUrl to connector template for APM Latency Threshold rule --- x-pack/plugins/alerting/server/types.ts | 1 + .../server/routes/alerts/action_variables.ts | 10 +++ ...ter_transaction_duration_rule_type.test.ts | 13 +-- ...register_transaction_duration_rule_type.ts | 81 ++++++++++++------- 4 files changed, 70 insertions(+), 35 deletions(-) diff --git a/x-pack/plugins/alerting/server/types.ts b/x-pack/plugins/alerting/server/types.ts index 88f5d8d1562de..af0038c720c7a 100644 --- a/x-pack/plugins/alerting/server/types.ts +++ b/x-pack/plugins/alerting/server/types.ts @@ -81,6 +81,7 @@ export interface RuleExecutorServices< alertFactory: PublicAlertFactory; shouldWriteAlerts: () => boolean; shouldStopExecution: () => boolean; + getAlertUuid?: (alertId: string) => string | null; } export interface RuleExecutorOptions< diff --git a/x-pack/plugins/apm/server/routes/alerts/action_variables.ts b/x-pack/plugins/apm/server/routes/alerts/action_variables.ts index ce78dbc7bee6d..cad7fd822f7ed 100644 --- a/x-pack/plugins/apm/server/routes/alerts/action_variables.ts +++ b/x-pack/plugins/apm/server/routes/alerts/action_variables.ts @@ -65,6 +65,16 @@ export const apmActionVariables = { ), name: 'reason' as const, }, + alertDetailsUrl: { + description: i18n.translate( + 'xpack.apm.alerts.action_variables.alertDetailUrl', + { + defaultMessage: + 'Link to the view within Elastic that shows further details and context surrounding this alert', + } + ), + name: 'alertDetailsUrl' as const, + }, viewInAppUrl: { description: i18n.translate( 'xpack.apm.alerts.action_variables.viewInAppUrl', diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.test.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.test.ts index 2b159e7acc0d2..1066eb8a987d8 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.test.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.test.ts @@ -9,7 +9,7 @@ import { registerTransactionDurationRuleType } from './register_transaction_dura import { createRuleTypeMocks } from '../../test_utils'; describe('registerTransactionDurationRuleType', () => { - it('sends alert when value is greater than threashold', async () => { + it('sends alert when value is greater than threshold', async () => { const { services, dependencies, executor, scheduleActions } = createRuleTypeMocks(); @@ -56,14 +56,17 @@ describe('registerTransactionDurationRuleType', () => { await executor({ params }); expect(scheduleActions).toHaveBeenCalledTimes(1); expect(scheduleActions).toHaveBeenCalledWith('threshold_met', { - transactionType: 'request', - serviceName: 'opbeans-java', + alertDetailsUrl: expect.stringContaining( + 'http://localhost:5601/eyr/app/observability/alerts/' + ), environment: 'Not defined', - threshold: 3000000, - triggerValue: '5,500 ms', interval: `5m`, reason: 'Avg. latency is 5,500 ms in the last 5 mins for opbeans-java. Alert when > 3,000 ms.', + transactionType: 'request', + serviceName: 'opbeans-java', + threshold: 3000000, + triggerValue: '5,500 ms', viewInAppUrl: 'http://localhost:5601/eyr/app/apm/services/opbeans-java?transactionType=request&environment=ENVIRONMENT_ALL', }); diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts index b4c7a6212b62d..d41ae0393f37b 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts @@ -11,12 +11,15 @@ import { ALERT_EVALUATION_THRESHOLD, ALERT_EVALUATION_VALUE, ALERT_REASON, + ALERT_UUID, } from '@kbn/rule-data-utils'; import { firstValueFrom } from 'rxjs'; import { asDuration } from '@kbn/observability-plugin/common/utils/formatters'; import { termQuery } from '@kbn/observability-plugin/server'; import { createLifecycleRuleTypeFactory } from '@kbn/rule-registry-plugin/server'; import { ProcessorEvent } from '@kbn/observability-plugin/common'; +import { getAlertDetailsUrl } from '@kbn/infra-plugin/server/lib/alerting/common/utils'; +import { addSpaceIdToPath } from '@kbn/spaces-plugin/common'; import { getAlertUrlTransaction } from '../../../../../common/utils/formatters'; import { SearchAggregatedTransactionSetting } from '../../../../../common/aggregated_transactions'; import { @@ -94,25 +97,29 @@ export function registerTransactionDurationRuleType({ }, actionVariables: { context: [ + apmActionVariables.alertDetailsUrl, + apmActionVariables.environment, + apmActionVariables.interval, + apmActionVariables.reason, apmActionVariables.serviceName, apmActionVariables.transactionType, - apmActionVariables.environment, apmActionVariables.threshold, apmActionVariables.triggerValue, - apmActionVariables.interval, - apmActionVariables.reason, apmActionVariables.viewInAppUrl, ], }, producer: APM_SERVER_FEATURE_ID, minimumLicenseRequired: 'basic', isExportable: true, - executor: async ({ services, params }) => { + executor: async ({ params: ruleParams, services, spaceId }) => { const config = await firstValueFrom(config$); - const ruleParams = params; + + const { getAlertUuid, savedObjectsClient, scopedClusterClient } = + services; + const indices = await getApmIndices({ config, - savedObjectsClient: services.savedObjectsClient, + savedObjectsClient, }); // only query transaction events when set to 'never', @@ -185,7 +192,7 @@ export function registerTransactionDurationRuleType({ }; const response = await alertingEsClient({ - scopedClusterClient: services.scopedClusterClient, + scopedClusterClient, params: searchParams, }); @@ -197,22 +204,25 @@ export function registerTransactionDurationRuleType({ const thresholdMicroseconds = ruleParams.threshold * 1000; const triggeredBuckets = []; + for (const bucket of response.aggregations.series.buckets) { const [serviceName, environment, transactionType] = bucket.key; + const transactionDuration = 'avgLatency' in bucket // only true if ruleParams.aggregationType === 'avg' ? bucket.avgLatency.value : bucket.pctLatency.values[0].value; + if ( transactionDuration !== null && transactionDuration > thresholdMicroseconds ) { triggeredBuckets.push({ - serviceName, environment, + serviceName, + sourceFields: getServiceGroupFields(bucket), transactionType, transactionDuration, - sourceFields: getServiceGroupFields(bucket), }); } } @@ -224,36 +234,45 @@ export function registerTransactionDurationRuleType({ transactionDuration, sourceFields, } of triggeredBuckets) { + const environmentLabel = getEnvironmentLabel(environment); + const durationFormatter = getDurationFormatter(transactionDuration); const transactionDurationFormatted = durationFormatter(transactionDuration).formatted; - const reasonMessage = formatTransactionDurationReason({ + + const reason = formatTransactionDurationReason({ + aggregationType: String(ruleParams.aggregationType), + asDuration, measured: transactionDuration, serviceName, threshold: thresholdMicroseconds, - asDuration, - aggregationType: String(ruleParams.aggregationType), windowSize: ruleParams.windowSize, windowUnit: ruleParams.windowUnit, }); - const relativeViewInAppUrl = getAlertUrlTransaction( - serviceName, - getEnvironmentEsField(environment)?.[SERVICE_ENVIRONMENT], - transactionType + const viewInAppUrl = addSpaceIdToPath( + basePath.publicBaseUrl, + spaceId, + getAlertUrlTransaction( + serviceName, + getEnvironmentEsField(environment)?.[SERVICE_ENVIRONMENT], + transactionType + ) + ); + + const id = `${ApmRuleType.TransactionDuration}_${environmentLabel}`; + + const alertUuid = getAlertUuid?.(id) || ''; + + const alertDetailsUrl = getAlertDetailsUrl( + basePath, + spaceId, + alertUuid ); - const viewInAppUrl = basePath.publicBaseUrl - ? new URL( - basePath.prepend(relativeViewInAppUrl), - basePath.publicBaseUrl - ).toString() - : relativeViewInAppUrl; services .alertWithLifecycle({ - id: `${ApmRuleType.TransactionDuration}_${getEnvironmentLabel( - environment - )}`, + id, fields: { [SERVICE_NAME]: serviceName, ...getEnvironmentEsField(environment), @@ -261,18 +280,20 @@ export function registerTransactionDurationRuleType({ [PROCESSOR_EVENT]: ProcessorEvent.transaction, [ALERT_EVALUATION_VALUE]: transactionDuration, [ALERT_EVALUATION_THRESHOLD]: thresholdMicroseconds, - [ALERT_REASON]: reasonMessage, + [ALERT_REASON]: reason, + [ALERT_UUID]: alertUuid, ...sourceFields, }, }) .scheduleActions(ruleTypeConfig.defaultActionGroupId, { - transactionType, + alertDetailsUrl, + environment: environmentLabel, + interval: `${ruleParams.windowSize}${ruleParams.windowUnit}`, + reason, serviceName, - environment: getEnvironmentLabel(environment), threshold: thresholdMicroseconds, + transactionType, triggerValue: transactionDurationFormatted, - interval: `${ruleParams.windowSize}${ruleParams.windowUnit}`, - reason: reasonMessage, viewInAppUrl, }); } From 7e5977d6c8b71a139115624bf5b7690d1b01780d Mon Sep 17 00:00:00 2001 From: Coen Warmer Date: Tue, 8 Nov 2022 14:57:25 +0100 Subject: [PATCH 02/17] Add context.alertDetailsUrl to connector template for APM Error Count Threshold rule --- x-pack/plugins/alerting/server/types.ts | 10 +-- x-pack/plugins/apm/server/plugin.ts | 7 +- .../server/routes/alerts/action_variables.ts | 64 +++++++++---------- .../routes/alerts/register_apm_rule_types.ts | 8 ++- .../register_error_count_rule_type.ts | 61 ++++++++++++------ .../server/utils/create_lifecycle_executor.ts | 4 +- 6 files changed, 89 insertions(+), 65 deletions(-) diff --git a/x-pack/plugins/alerting/server/types.ts b/x-pack/plugins/alerting/server/types.ts index af0038c720c7a..481bdf257c204 100644 --- a/x-pack/plugins/alerting/server/types.ts +++ b/x-pack/plugins/alerting/server/types.ts @@ -74,14 +74,14 @@ export interface RuleExecutorServices< Context extends AlertInstanceContext = AlertInstanceContext, ActionGroupIds extends string = never > { - searchSourceClient: ISearchStartSearchSource; + alertFactory: PublicAlertFactory; savedObjectsClient: SavedObjectsClientContract; - uiSettingsClient: IUiSettingsClient; scopedClusterClient: IScopedClusterClient; - alertFactory: PublicAlertFactory; - shouldWriteAlerts: () => boolean; - shouldStopExecution: () => boolean; + searchSourceClient: ISearchStartSearchSource; + uiSettingsClient: IUiSettingsClient; getAlertUuid?: (alertId: string) => string | null; + shouldStopExecution: () => boolean; + shouldWriteAlerts: () => boolean; } export interface RuleExecutorOptions< diff --git a/x-pack/plugins/apm/server/plugin.ts b/x-pack/plugins/apm/server/plugin.ts index a6509c6779197..10aaff3d2eb7e 100644 --- a/x-pack/plugins/apm/server/plugin.ts +++ b/x-pack/plugins/apm/server/plugin.ts @@ -190,12 +190,13 @@ export class APMPlugin if (plugins.alerting) { registerApmRuleTypes({ - ruleDataClient, alerting: plugins.alerting, - ml: plugins.ml, + basePath: core.http.basePath, config$, + getAlertDetailsConfig: plugins.observability.getAlertDetailsConfig, logger: this.logger!.get('rule'), - basePath: core.http.basePath, + ml: plugins.ml, + ruleDataClient, }); } diff --git a/x-pack/plugins/apm/server/routes/alerts/action_variables.ts b/x-pack/plugins/apm/server/routes/alerts/action_variables.ts index cad7fd822f7ed..bf0453811b205 100644 --- a/x-pack/plugins/apm/server/routes/alerts/action_variables.ts +++ b/x-pack/plugins/apm/server/routes/alerts/action_variables.ts @@ -8,19 +8,15 @@ import { i18n } from '@kbn/i18n'; export const apmActionVariables = { - serviceName: { - description: i18n.translate( - 'xpack.apm.alerts.action_variables.serviceName', - { defaultMessage: 'The service the alert is created for' } - ), - name: 'serviceName' as const, - }, - transactionType: { + alertDetailsUrl: { description: i18n.translate( - 'xpack.apm.alerts.action_variables.transactionType', - { defaultMessage: 'The transaction type the alert is created for' } + 'xpack.apm.alerts.action_variables.alertDetailsUrl', + { + defaultMessage: + 'Link to the view within Elastic that shows further details and context surrounding this alert', + } ), - name: 'transactionType' as const, + name: 'alertDetailsUrl' as const, }, environment: { description: i18n.translate( @@ -29,23 +25,6 @@ export const apmActionVariables = { ), name: 'environment' as const, }, - threshold: { - description: i18n.translate('xpack.apm.alerts.action_variables.threshold', { - defaultMessage: - 'Any trigger value above this value will cause the alert to fire', - }), - name: 'threshold' as const, - }, - triggerValue: { - description: i18n.translate( - 'xpack.apm.alerts.action_variables.triggerValue', - { - defaultMessage: - 'The value that breached the threshold and triggered the alert', - } - ), - name: 'triggerValue' as const, - }, interval: { description: i18n.translate( 'xpack.apm.alerts.action_variables.intervalSize', @@ -65,15 +44,36 @@ export const apmActionVariables = { ), name: 'reason' as const, }, - alertDetailsUrl: { + serviceName: { description: i18n.translate( - 'xpack.apm.alerts.action_variables.alertDetailUrl', + 'xpack.apm.alerts.action_variables.serviceName', + { defaultMessage: 'The service the alert is created for' } + ), + name: 'serviceName' as const, + }, + threshold: { + description: i18n.translate('xpack.apm.alerts.action_variables.threshold', { + defaultMessage: + 'Any trigger value above this value will cause the alert to fire', + }), + name: 'threshold' as const, + }, + transactionType: { + description: i18n.translate( + 'xpack.apm.alerts.action_variables.transactionType', + { defaultMessage: 'The transaction type the alert is created for' } + ), + name: 'transactionType' as const, + }, + triggerValue: { + description: i18n.translate( + 'xpack.apm.alerts.action_variables.triggerValue', { defaultMessage: - 'Link to the view within Elastic that shows further details and context surrounding this alert', + 'The value that breached the threshold and triggered the alert', } ), - name: 'alertDetailsUrl' as const, + name: 'triggerValue' as const, }, viewInAppUrl: { description: i18n.translate( diff --git a/x-pack/plugins/apm/server/routes/alerts/register_apm_rule_types.ts b/x-pack/plugins/apm/server/routes/alerts/register_apm_rule_types.ts index e327970198f80..3286b8fb355f0 100644 --- a/x-pack/plugins/apm/server/routes/alerts/register_apm_rule_types.ts +++ b/x-pack/plugins/apm/server/routes/alerts/register_apm_rule_types.ts @@ -8,6 +8,7 @@ import { Observable } from 'rxjs'; import { IBasePath, Logger } from '@kbn/core/server'; import { PluginSetupContract as AlertingPluginSetupContract } from '@kbn/alerting-plugin/server'; +import { ObservabilityConfig } from '@kbn/observability-plugin/server'; import { IRuleDataClient } from '@kbn/rule-registry-plugin/server'; import { MlPluginSetup } from '@kbn/ml-plugin/server'; import { registerTransactionDurationRuleType } from './rule_types/transaction_duration/register_transaction_duration_rule_type'; @@ -17,12 +18,13 @@ import { APMConfig } from '../..'; import { registerTransactionErrorRateRuleType } from './rule_types/transaction_error_rate/register_transaction_error_rate_rule_type'; export interface RegisterRuleDependencies { - ruleDataClient: IRuleDataClient; - ml?: MlPluginSetup; alerting: AlertingPluginSetupContract; + basePath: IBasePath; config$: Observable; + getAlertDetailsConfig: () => ObservabilityConfig['unsafe']['alertDetails']; logger: Logger; - basePath: IBasePath; + ml?: MlPluginSetup; + ruleDataClient: IRuleDataClient; } export function registerApmRuleTypes(dependencies: RegisterRuleDependencies) { diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.ts index d9826aae392c8..6e02508ceea7e 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.ts @@ -11,10 +11,14 @@ import { ALERT_EVALUATION_THRESHOLD, ALERT_EVALUATION_VALUE, ALERT_REASON, + ALERT_UUID, } from '@kbn/rule-data-utils'; import { createLifecycleRuleTypeFactory } from '@kbn/rule-registry-plugin/server'; import { termQuery } from '@kbn/observability-plugin/server'; import { ProcessorEvent } from '@kbn/observability-plugin/common'; +import { getAlertDetailsUrl } from '@kbn/infra-plugin/server/lib/alerting/common/utils'; +import { addSpaceIdToPath } from '@kbn/spaces-plugin/common'; + import { ENVIRONMENT_NOT_DEFINED, getEnvironmentEsField, @@ -54,10 +58,11 @@ const ruleTypeConfig = RULE_TYPES_CONFIG[ApmRuleType.ErrorCount]; export function registerErrorCountRuleType({ alerting, + basePath, + config$, + getAlertDetailsConfig, logger, ruleDataClient, - config$, - basePath, }: RegisterRuleDependencies) { const createLifecycleRuleType = createLifecycleRuleTypeFactory({ ruleDataClient, @@ -75,24 +80,30 @@ export function registerErrorCountRuleType({ }, actionVariables: { context: [ - apmActionVariables.serviceName, + ...(getAlertDetailsConfig()?.apm.enabled + ? [apmActionVariables.alertDetailsUrl] + : []), apmActionVariables.environment, - apmActionVariables.threshold, - apmActionVariables.triggerValue, apmActionVariables.interval, apmActionVariables.reason, + apmActionVariables.serviceName, + apmActionVariables.threshold, + apmActionVariables.triggerValue, apmActionVariables.viewInAppUrl, ], }, producer: APM_SERVER_FEATURE_ID, minimumLicenseRequired: 'basic', isExportable: true, - executor: async ({ services, params: ruleParams }) => { + executor: async ({ params: ruleParams, services, spaceId }) => { const config = await firstValueFrom(config$); + const { getAlertUuid, savedObjectsClient, scopedClusterClient } = + services; + const indices = await getApmIndices({ config, - savedObjectsClient: services.savedObjectsClient, + savedObjectsClient, }); const searchParams = { @@ -138,7 +149,7 @@ export function registerErrorCountRuleType({ }; const response = await alertingEsClient({ - scopedClusterClient: services.scopedClusterClient, + scopedClusterClient, params: searchParams, }); @@ -166,23 +177,31 @@ export function registerErrorCountRuleType({ windowUnit: ruleParams.windowUnit, }); + const id = [ApmRuleType.ErrorCount, serviceName, environment] + .filter((name) => name) + .join('_'); + const relativeViewInAppUrl = getAlertUrlErrorCount( serviceName, getEnvironmentEsField(environment)?.[SERVICE_ENVIRONMENT] ); - const viewInAppUrl = basePath.publicBaseUrl - ? new URL( - basePath.prepend(relativeViewInAppUrl), - basePath.publicBaseUrl - ).toString() - : relativeViewInAppUrl; + const viewInAppUrl = addSpaceIdToPath( + basePath.publicBaseUrl, + spaceId, + relativeViewInAppUrl + ); + + const alertUuid = getAlertUuid(id); + const alertDetailsUrl = getAlertDetailsUrl( + basePath, + spaceId, + alertUuid + ); services .alertWithLifecycle({ - id: [ApmRuleType.ErrorCount, serviceName, environment] - .filter((name) => name) - .join('_'), + id, fields: { [SERVICE_NAME]: serviceName, ...getEnvironmentEsField(environment), @@ -190,16 +209,18 @@ export function registerErrorCountRuleType({ [ALERT_EVALUATION_VALUE]: errorCount, [ALERT_EVALUATION_THRESHOLD]: ruleParams.threshold, [ALERT_REASON]: alertReason, + [ALERT_UUID]: alertUuid, ...sourceFields, }, }) .scheduleActions(ruleTypeConfig.defaultActionGroupId, { - serviceName, + alertDetailsUrl, environment: getEnvironmentLabel(environment), - threshold: ruleParams.threshold, - triggerValue: errorCount, interval: `${ruleParams.windowSize}${ruleParams.windowUnit}`, reason: alertReason, + serviceName, + threshold: ruleParams.threshold, + triggerValue: errorCount, viewInAppUrl, }); }); diff --git a/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts b/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts index 6f7dafd3e495f..4165bbc2743d2 100644 --- a/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts +++ b/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts @@ -69,8 +69,8 @@ export interface LifecycleAlertServices< ActionGroupIds extends string = never > { alertWithLifecycle: LifecycleAlertService; - getAlertStartedDate: (alertInstanceId: string) => string | null; - getAlertUuid: (alertInstanceId: string) => string | null; + getAlertStartedDate: (alertId: string) => string | null; + getAlertUuid: (alertId: string) => string | null; } export type LifecycleRuleExecutor< From 23d6ac9427ddec0ad3a3d395eedb90cce522a201 Mon Sep 17 00:00:00 2001 From: Coen Warmer Date: Tue, 8 Nov 2022 15:32:59 +0100 Subject: [PATCH 03/17] Add context.alertDetailsUrl to connector template for APM Anomaly rule --- .../routes/alerts/register_apm_rule_types.ts | 2 +- .../anomaly/register_anomaly_rule_type.ts | 80 ++++++++++++------- ...register_transaction_duration_rule_type.ts | 5 +- 3 files changed, 56 insertions(+), 31 deletions(-) diff --git a/x-pack/plugins/apm/server/routes/alerts/register_apm_rule_types.ts b/x-pack/plugins/apm/server/routes/alerts/register_apm_rule_types.ts index 3286b8fb355f0..4edfc23f561e1 100644 --- a/x-pack/plugins/apm/server/routes/alerts/register_apm_rule_types.ts +++ b/x-pack/plugins/apm/server/routes/alerts/register_apm_rule_types.ts @@ -28,8 +28,8 @@ export interface RegisterRuleDependencies { } export function registerApmRuleTypes(dependencies: RegisterRuleDependencies) { - registerTransactionDurationRuleType(dependencies); registerAnomalyRuleType(dependencies); registerErrorCountRuleType(dependencies); + registerTransactionDurationRuleType(dependencies); registerTransactionErrorRateRuleType(dependencies); } diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts index d85b8df2798fe..8bd22a773f79f 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts @@ -12,12 +12,15 @@ import { ALERT_EVALUATION_VALUE, ALERT_REASON, ALERT_SEVERITY, + ALERT_UUID, } from '@kbn/rule-data-utils'; import { compact } from 'lodash'; import type { ESSearchResponse } from '@kbn/es-types'; import { KibanaRequest } from '@kbn/core/server'; import { termQuery } from '@kbn/observability-plugin/server'; +import { getAlertDetailsUrl } from '@kbn/infra-plugin/server/lib/alerting/common/utils'; import { createLifecycleRuleTypeFactory } from '@kbn/rule-registry-plugin/server'; +import { addSpaceIdToPath } from '@kbn/spaces-plugin/common'; import { ProcessorEvent } from '@kbn/observability-plugin/common'; import { ApmRuleType, @@ -65,12 +68,13 @@ const paramsSchema = schema.object({ const ruleTypeConfig = RULE_TYPES_CONFIG[ApmRuleType.Anomaly]; export function registerAnomalyRuleType({ - logger, - ruleDataClient, - config$, alerting, - ml, basePath, + config$, + getAlertDetailsConfig, + logger, + ml, + ruleDataClient, }: RegisterRuleDependencies) { const createLifecycleRuleType = createLifecycleRuleTypeFactory({ logger, @@ -88,32 +92,38 @@ export function registerAnomalyRuleType({ }, actionVariables: { context: [ - apmActionVariables.serviceName, - apmActionVariables.transactionType, + ...(getAlertDetailsConfig()?.apm.enabled + ? [apmActionVariables.alertDetailsUrl] + : []), apmActionVariables.environment, + apmActionVariables.reason, + apmActionVariables.serviceName, apmActionVariables.threshold, + apmActionVariables.transactionType, apmActionVariables.triggerValue, - apmActionVariables.reason, apmActionVariables.viewInAppUrl, ], }, producer: 'apm', minimumLicenseRequired: 'basic', isExportable: true, - executor: async ({ services, params }) => { + executor: async ({ params, services, spaceId }) => { if (!ml) { return {}; } + const { getAlertUuid, savedObjectsClient, scopedClusterClient } = + services; + const ruleParams = params; const request = {} as KibanaRequest; const { mlAnomalySearch } = ml.mlSystemProvider( request, - services.savedObjectsClient + savedObjectsClient ); const anomalyDetectors = ml.anomalyDetectorsProvider( request, - services.savedObjectsClient + savedObjectsClient ); const mlJobs = await getMLJobs( @@ -254,8 +264,8 @@ export function registerAnomalyRuleType({ const eventSourceFields = await getServiceGroupFieldsForAnomaly({ config$, - scopedClusterClient: services.scopedClusterClient, - savedObjectsClient: services.savedObjectsClient, + scopedClusterClient, + savedObjectsClient, serviceName, environment, transactionType, @@ -272,28 +282,38 @@ export function registerAnomalyRuleType({ windowUnit: params.windowUnit, }); + const id = [ + ApmRuleType.Anomaly, + serviceName, + environment, + transactionType, + ] + .filter((name) => name) + .join('_'); + const relativeViewInAppUrl = getAlertUrlTransaction( serviceName, getEnvironmentEsField(environment)?.[SERVICE_ENVIRONMENT], transactionType ); - const viewInAppUrl = basePath.publicBaseUrl - ? new URL( - basePath.prepend(relativeViewInAppUrl), - basePath.publicBaseUrl - ).toString() - : relativeViewInAppUrl; + const viewInAppUrl = addSpaceIdToPath( + basePath.publicBaseUrl, + spaceId, + relativeViewInAppUrl + ); + + const alertUuid = getAlertUuid?.(id) || ''; + + const alertDetailsUrl = getAlertDetailsUrl( + basePath, + spaceId, + alertUuid + ); + services .alertWithLifecycle({ - id: [ - ApmRuleType.Anomaly, - serviceName, - environment, - transactionType, - ] - .filter((name) => name) - .join('_'), + id, fields: { [SERVICE_NAME]: serviceName, ...getEnvironmentEsField(environment), @@ -303,16 +323,18 @@ export function registerAnomalyRuleType({ [ALERT_EVALUATION_VALUE]: score, [ALERT_EVALUATION_THRESHOLD]: threshold, [ALERT_REASON]: reasonMessage, + [ALERT_UUID]: alertUuid, ...eventSourceFields, }, }) .scheduleActions(ruleTypeConfig.defaultActionGroupId, { - serviceName, - transactionType, + alertDetailsUrl, environment: getEnvironmentLabel(environment), + reason: reasonMessage, + serviceName, threshold: selectedOption?.label, + transactionType, triggerValue: severityLevel, - reason: reasonMessage, viewInAppUrl, }); } diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts index d41ae0393f37b..a95dde7f3e4d6 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts @@ -79,6 +79,7 @@ export function registerTransactionDurationRuleType({ alerting, ruleDataClient, config$, + getAlertDetailsConfig, logger, basePath, }: RegisterRuleDependencies) { @@ -97,7 +98,9 @@ export function registerTransactionDurationRuleType({ }, actionVariables: { context: [ - apmActionVariables.alertDetailsUrl, + ...(getAlertDetailsConfig()?.apm.enabled + ? [apmActionVariables.alertDetailsUrl] + : []), apmActionVariables.environment, apmActionVariables.interval, apmActionVariables.reason, From e5726a3e3fe59413a68903908247b7fdbe02b78e Mon Sep 17 00:00:00 2001 From: Coen Warmer Date: Tue, 8 Nov 2022 15:43:42 +0100 Subject: [PATCH 04/17] Add context.alertDetailsUrl to connector template for APM Failed Transaction Rate Threshold rule --- ...register_transaction_duration_rule_type.ts | 20 ++--- ...gister_transaction_error_rate_rule_type.ts | 79 ++++++++++++------- 2 files changed, 61 insertions(+), 38 deletions(-) diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts index a95dde7f3e4d6..f52bd91eb7954 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts @@ -253,16 +253,6 @@ export function registerTransactionDurationRuleType({ windowUnit: ruleParams.windowUnit, }); - const viewInAppUrl = addSpaceIdToPath( - basePath.publicBaseUrl, - spaceId, - getAlertUrlTransaction( - serviceName, - getEnvironmentEsField(environment)?.[SERVICE_ENVIRONMENT], - transactionType - ) - ); - const id = `${ApmRuleType.TransactionDuration}_${environmentLabel}`; const alertUuid = getAlertUuid?.(id) || ''; @@ -273,6 +263,16 @@ export function registerTransactionDurationRuleType({ alertUuid ); + const viewInAppUrl = addSpaceIdToPath( + basePath.publicBaseUrl, + spaceId, + getAlertUrlTransaction( + serviceName, + getEnvironmentEsField(environment)?.[SERVICE_ENVIRONMENT], + transactionType + ) + ); + services .alertWithLifecycle({ id, diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_error_rate/register_transaction_error_rate_rule_type.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_error_rate/register_transaction_error_rate_rule_type.ts index 73f7ccda26401..70cf3b92c6cff 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_error_rate/register_transaction_error_rate_rule_type.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_error_rate/register_transaction_error_rate_rule_type.ts @@ -15,7 +15,9 @@ import { import { createLifecycleRuleTypeFactory } from '@kbn/rule-registry-plugin/server'; import { asPercent } from '@kbn/observability-plugin/common/utils/formatters'; import { termQuery } from '@kbn/observability-plugin/server'; +import { addSpaceIdToPath } from '@kbn/spaces-plugin/common'; import { ProcessorEvent } from '@kbn/observability-plugin/common'; +import { getAlertDetailsUrl } from '@kbn/infra-plugin/server/lib/alerting/common/utils'; import { ENVIRONMENT_NOT_DEFINED, getEnvironmentEsField, @@ -62,10 +64,11 @@ const ruleTypeConfig = RULE_TYPES_CONFIG[ApmRuleType.TransactionErrorRate]; export function registerTransactionErrorRateRuleType({ alerting, - ruleDataClient, - logger, - config$, basePath, + config$, + getAlertDetailsConfig, + logger, + ruleDataClient, }: RegisterRuleDependencies) { const createLifecycleRuleType = createLifecycleRuleTypeFactory({ ruleDataClient, @@ -83,24 +86,31 @@ export function registerTransactionErrorRateRuleType({ }, actionVariables: { context: [ - apmActionVariables.transactionType, - apmActionVariables.serviceName, + ...(getAlertDetailsConfig()?.apm.enabled + ? [apmActionVariables.alertDetailsUrl] + : []), apmActionVariables.environment, - apmActionVariables.threshold, - apmActionVariables.triggerValue, apmActionVariables.interval, apmActionVariables.reason, + apmActionVariables.serviceName, + apmActionVariables.threshold, + apmActionVariables.transactionType, + apmActionVariables.triggerValue, apmActionVariables.viewInAppUrl, ], }, producer: APM_SERVER_FEATURE_ID, minimumLicenseRequired: 'basic', isExportable: true, - executor: async ({ services, params: ruleParams }) => { + executor: async ({ services, spaceId, params: ruleParams }) => { const config = await firstValueFrom(config$); + + const { getAlertUuid, savedObjectsClient, scopedClusterClient } = + services; + const indices = await getApmIndices({ config, - savedObjectsClient: services.savedObjectsClient, + savedObjectsClient, }); // only query transaction events when set to 'never', @@ -178,7 +188,7 @@ export function registerTransactionErrorRateRuleType({ }; const response = await alertingEsClient({ - scopedClusterClient: services.scopedClusterClient, + scopedClusterClient, params: searchParams, }); @@ -219,6 +229,7 @@ export function registerTransactionErrorRateRuleType({ errorRate, sourceFields, } = result; + const reasonMessage = formatTransactionErrorRateReason({ threshold: ruleParams.threshold, measured: errorRate, @@ -228,27 +239,38 @@ export function registerTransactionErrorRateRuleType({ windowUnit: ruleParams.windowUnit, }); + const id = [ + ApmRuleType.TransactionErrorRate, + serviceName, + transactionType, + environment, + ] + .filter((name) => name) + .join('_'); + + const alertUuid = getAlertUuid?.(id) || ''; + + const alertDetailsUrl = getAlertDetailsUrl( + basePath, + spaceId, + alertUuid + ); + const relativeViewInAppUrl = getAlertUrlTransaction( serviceName, getEnvironmentEsField(environment)?.[SERVICE_ENVIRONMENT], transactionType ); - const viewInAppUrl = basePath.publicBaseUrl - ? new URL( - basePath.prepend(relativeViewInAppUrl), - basePath.publicBaseUrl - ).toString() - : relativeViewInAppUrl; + + const viewInAppUrl = addSpaceIdToPath( + basePath.publicBaseUrl, + spaceId, + relativeViewInAppUrl + ); + services .alertWithLifecycle({ - id: [ - ApmRuleType.TransactionErrorRate, - serviceName, - transactionType, - environment, - ] - .filter((name) => name) - .join('_'), + id, fields: { [SERVICE_NAME]: serviceName, ...getEnvironmentEsField(environment), @@ -261,13 +283,14 @@ export function registerTransactionErrorRateRuleType({ }, }) .scheduleActions(ruleTypeConfig.defaultActionGroupId, { - serviceName, - transactionType, + alertDetailsUrl, environment: getEnvironmentLabel(environment), - threshold: ruleParams.threshold, - triggerValue: asDecimalOrInteger(errorRate), interval: `${ruleParams.windowSize}${ruleParams.windowUnit}`, reason: reasonMessage, + serviceName, + threshold: ruleParams.threshold, + transactionType, + triggerValue: asDecimalOrInteger(errorRate), viewInAppUrl, }); }); From e5be4e5db84d4c181826928b31a23394c8722d4a Mon Sep 17 00:00:00 2001 From: Coen Warmer Date: Tue, 8 Nov 2022 16:08:33 +0100 Subject: [PATCH 05/17] Add mock for getAlertsDetailConfig --- .../apm/server/routes/alerts/test_utils/index.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/x-pack/plugins/apm/server/routes/alerts/test_utils/index.ts b/x-pack/plugins/apm/server/routes/alerts/test_utils/index.ts index 5302f632781f0..903c6e6cb0ab3 100644 --- a/x-pack/plugins/apm/server/routes/alerts/test_utils/index.ts +++ b/x-pack/plugins/apm/server/routes/alerts/test_utils/index.ts @@ -51,16 +51,17 @@ export const createRuleTypeMocks = () => { return { dependencies: { alerting, + basePath: { + prepend: (path: string) => `http://localhost:5601/eyr${path}`, + publicBaseUrl: 'http://localhost:5601/eyr', + serverBasePath: '/eyr', + } as IBasePath, config$: mockedConfig$, + getAlertDetailsConfig: jest.fn().mockReturnValue({ apm: true }), logger: loggerMock, ruleDataClient: ruleRegistryMocks.createRuleDataClient( '.alerts-observability.apm.alerts' ) as IRuleDataClient, - basePath: { - serverBasePath: '/eyr', - publicBaseUrl: 'http://localhost:5601/eyr', - prepend: (path: string) => `http://localhost:5601/eyr${path}`, - } as IBasePath, }, services, scheduleActions, From 7ac391c8c48d6f4bd9c9f1aed2b02471d74a3ba2 Mon Sep 17 00:00:00 2001 From: Coen Warmer Date: Wed, 9 Nov 2022 09:57:31 +0100 Subject: [PATCH 06/17] Update tests --- .../anomaly/register_anomaly_rule_type.test.ts | 3 +++ .../error_count/register_error_count_rule_type.test.ts | 9 +++++++++ .../register_transaction_error_rate_rule_type.test.ts | 3 +++ 3 files changed, 15 insertions(+) diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.test.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.test.ts index 875dce26c40fc..ecc93362f400d 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.test.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.test.ts @@ -194,6 +194,9 @@ describe('Transaction duration anomaly alert', () => { ); expect(scheduleActions).toHaveBeenCalledWith('threshold_met', { + alertDetailsUrl: expect.stringContaining( + 'http://localhost:5601/eyr/app/observability/alerts/' + ), serviceName: 'foo', transactionType: 'type-foo', environment: 'development', diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.test.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.test.ts index 342832b7e4099..705804cfa74f4 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.test.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.test.ts @@ -138,6 +138,9 @@ describe('Error count alert', () => { expect(scheduleActions).toHaveBeenCalledTimes(3); expect(scheduleActions).toHaveBeenCalledWith('threshold_met', { + alertDetailsUrl: expect.stringContaining( + 'http://localhost:5601/eyr/app/observability/alerts/' + ), serviceName: 'foo', environment: 'env-foo', threshold: 2, @@ -148,6 +151,9 @@ describe('Error count alert', () => { 'http://localhost:5601/eyr/app/apm/services/foo/errors?environment=env-foo', }); expect(scheduleActions).toHaveBeenCalledWith('threshold_met', { + alertDetailsUrl: expect.stringContaining( + 'http://localhost:5601/eyr/app/observability/alerts/' + ), serviceName: 'foo', environment: 'env-foo-2', threshold: 2, @@ -158,6 +164,9 @@ describe('Error count alert', () => { 'http://localhost:5601/eyr/app/apm/services/foo/errors?environment=env-foo-2', }); expect(scheduleActions).toHaveBeenCalledWith('threshold_met', { + alertDetailsUrl: expect.stringContaining( + 'http://localhost:5601/eyr/app/observability/alerts/' + ), serviceName: 'bar', environment: 'env-bar', reason: 'Error count is 3 in the last 5 mins for bar. Alert when > 2.', diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_error_rate/register_transaction_error_rate_rule_type.test.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_error_rate/register_transaction_error_rate_rule_type.test.ts index f28493338ad0d..38de7d48cce4c 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_error_rate/register_transaction_error_rate_rule_type.test.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_error_rate/register_transaction_error_rate_rule_type.test.ts @@ -121,6 +121,9 @@ describe('Transaction error rate alert', () => { ); expect(scheduleActions).toHaveBeenCalledWith('threshold_met', { + alertDetailsUrl: expect.stringContaining( + 'http://localhost:5601/eyr/app/observability/alerts/' + ), serviceName: 'foo', transactionType: 'type-foo', environment: 'env-foo', From 3a4ed3dc583292d1aa26414135dbe9794f6d05fe Mon Sep 17 00:00:00 2001 From: Dario Gieselaar Date: Mon, 14 Nov 2022 10:31:21 +0100 Subject: [PATCH 07/17] Expand types of createLifecycleRuleTypeFactory --- x-pack/plugins/alerting/server/types.ts | 1 - .../anomaly/register_anomaly_rule_type.ts | 28 +++++++++---------- .../create_lifecycle_rule_type_factory.ts | 16 +++++++---- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/x-pack/plugins/alerting/server/types.ts b/x-pack/plugins/alerting/server/types.ts index 481bdf257c204..2fea8791e4a41 100644 --- a/x-pack/plugins/alerting/server/types.ts +++ b/x-pack/plugins/alerting/server/types.ts @@ -79,7 +79,6 @@ export interface RuleExecutorServices< scopedClusterClient: IScopedClusterClient; searchSourceClient: ISearchStartSearchSource; uiSettingsClient: IUiSettingsClient; - getAlertUuid?: (alertId: string) => string | null; shouldStopExecution: () => boolean; shouldWriteAlerts: () => boolean; } diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts index 8bd22a773f79f..2f91506ee7607 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts @@ -4,9 +4,14 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import datemath from '@kbn/datemath'; import { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; import { schema } from '@kbn/config-schema'; +import { KibanaRequest } from '@kbn/core/server'; +import datemath from '@kbn/datemath'; +import type { ESSearchResponse } from '@kbn/es-types'; +import { getAlertDetailsUrl } from '@kbn/infra-plugin/server/lib/alerting/common/utils'; +import { ProcessorEvent } from '@kbn/observability-plugin/common'; +import { termQuery } from '@kbn/observability-plugin/server'; import { ALERT_EVALUATION_THRESHOLD, ALERT_EVALUATION_VALUE, @@ -14,20 +19,9 @@ import { ALERT_SEVERITY, ALERT_UUID, } from '@kbn/rule-data-utils'; -import { compact } from 'lodash'; -import type { ESSearchResponse } from '@kbn/es-types'; -import { KibanaRequest } from '@kbn/core/server'; -import { termQuery } from '@kbn/observability-plugin/server'; -import { getAlertDetailsUrl } from '@kbn/infra-plugin/server/lib/alerting/common/utils'; import { createLifecycleRuleTypeFactory } from '@kbn/rule-registry-plugin/server'; import { addSpaceIdToPath } from '@kbn/spaces-plugin/common'; -import { ProcessorEvent } from '@kbn/observability-plugin/common'; -import { - ApmRuleType, - RULE_TYPES_CONFIG, - ANOMALY_ALERT_SEVERITY_TYPES, - formatAnomalyReason, -} from '../../../../../common/rules/apm_rule_types'; +import { compact } from 'lodash'; import { getSeverity } from '../../../../../common/anomaly_detection'; import { ApmMlDetectorType, @@ -44,6 +38,12 @@ import { getEnvironmentLabel, } from '../../../../../common/environment_filter_values'; import { ANOMALY_SEVERITY } from '../../../../../common/ml_constants'; +import { + ANOMALY_ALERT_SEVERITY_TYPES, + ApmRuleType, + formatAnomalyReason, + RULE_TYPES_CONFIG, +} from '../../../../../common/rules/apm_rule_types'; import { asMutableArray } from '../../../../../common/utils/as_mutable_array'; import { getAlertUrlTransaction } from '../../../../../common/utils/formatters'; import { getMLJobs } from '../../../service_map/get_service_anomalies'; @@ -112,7 +112,7 @@ export function registerAnomalyRuleType({ return {}; } - const { getAlertUuid, savedObjectsClient, scopedClusterClient } = + const { savedObjectsClient, scopedClusterClient, getAlertUuid } = services; const ruleParams = params; diff --git a/x-pack/plugins/rule_registry/server/utils/create_lifecycle_rule_type_factory.ts b/x-pack/plugins/rule_registry/server/utils/create_lifecycle_rule_type_factory.ts index 650b9563ec82e..b50912eda89b0 100644 --- a/x-pack/plugins/rule_registry/server/utils/create_lifecycle_rule_type_factory.ts +++ b/x-pack/plugins/rule_registry/server/utils/create_lifecycle_rule_type_factory.ts @@ -13,20 +13,24 @@ import { } from '@kbn/alerting-plugin/common'; import { IRuleDataClient } from '../rule_data_client'; import { AlertTypeWithExecutor } from '../types'; -import { LifecycleAlertService, createLifecycleExecutor } from './create_lifecycle_executor'; +import { createLifecycleExecutor, LifecycleAlertServices } from './create_lifecycle_executor'; import { createGetSummarizedAlertsFn } from './create_get_summarized_alerts_fn'; export const createLifecycleRuleTypeFactory = ({ logger, ruleDataClient }: { logger: Logger; ruleDataClient: IRuleDataClient }) => < TParams extends RuleTypeParams, + TAlertInstanceState extends AlertInstanceState, TAlertInstanceContext extends AlertInstanceContext, - TServices extends { - alertWithLifecycle: LifecycleAlertService, TAlertInstanceContext, string>; - } + TActionGroupIds extends string, + TServices extends LifecycleAlertServices< + TAlertInstanceState, + TAlertInstanceContext, + TActionGroupIds + > >( - type: AlertTypeWithExecutor, TParams, TAlertInstanceContext, TServices> - ): AlertTypeWithExecutor, TParams, TAlertInstanceContext, any> => { + type: AlertTypeWithExecutor + ): AlertTypeWithExecutor => { const createBoundLifecycleExecutor = createLifecycleExecutor(logger, ruleDataClient); const createGetSummarizedAlerts = createGetSummarizedAlertsFn({ ruleDataClient, From 6011172ab76c6884e73ba8ca0505138308d94089 Mon Sep 17 00:00:00 2001 From: Dario Gieselaar Date: Mon, 14 Nov 2022 10:57:14 +0100 Subject: [PATCH 08/17] Make getAlertUuid idempotent --- .../server/utils/create_lifecycle_executor.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts b/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts index 4165bbc2743d2..d03bf74247190 100644 --- a/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts +++ b/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts @@ -70,7 +70,7 @@ export interface LifecycleAlertServices< > { alertWithLifecycle: LifecycleAlertService; getAlertStartedDate: (alertId: string) => string | null; - getAlertUuid: (alertId: string) => string | null; + getAlertUuid: (alertId: string) => string; } export type LifecycleRuleExecutor< @@ -174,13 +174,14 @@ export const createLifecycleExecutor = }, getAlertStartedDate: (alertId: string) => state.trackedAlerts[alertId]?.started ?? null, getAlertUuid: (alertId: string) => { - if (!state.trackedAlerts[alertId]) { - const alertUuid = v4(); - newAlertUuids[alertId] = alertUuid; - return alertUuid; + let existingUuid = state.trackedAlerts[alertId]?.alertUuid || newAlertUuids[alertId]; + + if (!existingUuid) { + existingUuid = v4(); + newAlertUuids[alertId] = existingUuid; } - return state.trackedAlerts[alertId].alertUuid; + return existingUuid; }, }; @@ -242,7 +243,7 @@ export const createLifecycleExecutor = const { alertUuid, started } = !isNew ? state.trackedAlerts[alertId] : { - alertUuid: newAlertUuids[alertId] || v4(), + alertUuid: lifecycleAlertServices.getAlertUuid(alertId), started: commonRuleFields[TIMESTAMP], }; From 2457b869429c8efb6b2d8cf3f947043dcef45fc4 Mon Sep 17 00:00:00 2001 From: Coen Warmer Date: Mon, 14 Nov 2022 11:20:06 +0100 Subject: [PATCH 09/17] Revert changes to alerting plugin types --- x-pack/plugins/alerting/server/types.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/alerting/server/types.ts b/x-pack/plugins/alerting/server/types.ts index 2fea8791e4a41..88f5d8d1562de 100644 --- a/x-pack/plugins/alerting/server/types.ts +++ b/x-pack/plugins/alerting/server/types.ts @@ -74,13 +74,13 @@ export interface RuleExecutorServices< Context extends AlertInstanceContext = AlertInstanceContext, ActionGroupIds extends string = never > { - alertFactory: PublicAlertFactory; - savedObjectsClient: SavedObjectsClientContract; - scopedClusterClient: IScopedClusterClient; searchSourceClient: ISearchStartSearchSource; + savedObjectsClient: SavedObjectsClientContract; uiSettingsClient: IUiSettingsClient; - shouldStopExecution: () => boolean; + scopedClusterClient: IScopedClusterClient; + alertFactory: PublicAlertFactory; shouldWriteAlerts: () => boolean; + shouldStopExecution: () => boolean; } export interface RuleExecutorOptions< From aca90fca29092898a3c0b1ea903f55ddba14b0bd Mon Sep 17 00:00:00 2001 From: Coen Warmer Date: Mon, 14 Nov 2022 11:39:06 +0100 Subject: [PATCH 10/17] Pass observability plugin to registerApmRule function --- x-pack/plugins/apm/server/plugin.ts | 2 +- .../apm/server/routes/alerts/register_apm_rule_types.ts | 4 ++-- .../alerts/rule_types/anomaly/register_anomaly_rule_type.ts | 4 ++-- .../rule_types/error_count/register_error_count_rule_type.ts | 4 ++-- .../register_transaction_duration_rule_type.ts | 4 ++-- .../register_transaction_error_rate_rule_type.ts | 4 ++-- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/x-pack/plugins/apm/server/plugin.ts b/x-pack/plugins/apm/server/plugin.ts index 10aaff3d2eb7e..698abdfb14a9a 100644 --- a/x-pack/plugins/apm/server/plugin.ts +++ b/x-pack/plugins/apm/server/plugin.ts @@ -193,9 +193,9 @@ export class APMPlugin alerting: plugins.alerting, basePath: core.http.basePath, config$, - getAlertDetailsConfig: plugins.observability.getAlertDetailsConfig, logger: this.logger!.get('rule'), ml: plugins.ml, + observability: plugins.observability, ruleDataClient, }); } diff --git a/x-pack/plugins/apm/server/routes/alerts/register_apm_rule_types.ts b/x-pack/plugins/apm/server/routes/alerts/register_apm_rule_types.ts index 4edfc23f561e1..478aa20898bc1 100644 --- a/x-pack/plugins/apm/server/routes/alerts/register_apm_rule_types.ts +++ b/x-pack/plugins/apm/server/routes/alerts/register_apm_rule_types.ts @@ -8,7 +8,7 @@ import { Observable } from 'rxjs'; import { IBasePath, Logger } from '@kbn/core/server'; import { PluginSetupContract as AlertingPluginSetupContract } from '@kbn/alerting-plugin/server'; -import { ObservabilityConfig } from '@kbn/observability-plugin/server'; +import { ObservabilityPluginSetup } from '@kbn/observability-plugin/server'; import { IRuleDataClient } from '@kbn/rule-registry-plugin/server'; import { MlPluginSetup } from '@kbn/ml-plugin/server'; import { registerTransactionDurationRuleType } from './rule_types/transaction_duration/register_transaction_duration_rule_type'; @@ -21,9 +21,9 @@ export interface RegisterRuleDependencies { alerting: AlertingPluginSetupContract; basePath: IBasePath; config$: Observable; - getAlertDetailsConfig: () => ObservabilityConfig['unsafe']['alertDetails']; logger: Logger; ml?: MlPluginSetup; + observability: ObservabilityPluginSetup; ruleDataClient: IRuleDataClient; } diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts index 2f91506ee7607..258ad8d37b342 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts @@ -71,9 +71,9 @@ export function registerAnomalyRuleType({ alerting, basePath, config$, - getAlertDetailsConfig, logger, ml, + observability, ruleDataClient, }: RegisterRuleDependencies) { const createLifecycleRuleType = createLifecycleRuleTypeFactory({ @@ -92,7 +92,7 @@ export function registerAnomalyRuleType({ }, actionVariables: { context: [ - ...(getAlertDetailsConfig()?.apm.enabled + ...(observability.getAlertDetailsConfig()?.apm.enabled ? [apmActionVariables.alertDetailsUrl] : []), apmActionVariables.environment, diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.ts index 2911d24292cd4..19dae27531c0c 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.ts @@ -60,8 +60,8 @@ export function registerErrorCountRuleType({ alerting, basePath, config$, - getAlertDetailsConfig, logger, + observability, ruleDataClient, }: RegisterRuleDependencies) { const createLifecycleRuleType = createLifecycleRuleTypeFactory({ @@ -80,7 +80,7 @@ export function registerErrorCountRuleType({ }, actionVariables: { context: [ - ...(getAlertDetailsConfig()?.apm.enabled + ...(observability.getAlertDetailsConfig()?.apm.enabled ? [apmActionVariables.alertDetailsUrl] : []), apmActionVariables.environment, diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts index f52bd91eb7954..8d6ef3dd70f65 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts @@ -79,8 +79,8 @@ export function registerTransactionDurationRuleType({ alerting, ruleDataClient, config$, - getAlertDetailsConfig, logger, + observability, basePath, }: RegisterRuleDependencies) { const createLifecycleRuleType = createLifecycleRuleTypeFactory({ @@ -98,7 +98,7 @@ export function registerTransactionDurationRuleType({ }, actionVariables: { context: [ - ...(getAlertDetailsConfig()?.apm.enabled + ...(observability.getAlertDetailsConfig()?.apm.enabled ? [apmActionVariables.alertDetailsUrl] : []), apmActionVariables.environment, diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_error_rate/register_transaction_error_rate_rule_type.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_error_rate/register_transaction_error_rate_rule_type.ts index 70cf3b92c6cff..ee8eca5009f26 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_error_rate/register_transaction_error_rate_rule_type.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_error_rate/register_transaction_error_rate_rule_type.ts @@ -66,8 +66,8 @@ export function registerTransactionErrorRateRuleType({ alerting, basePath, config$, - getAlertDetailsConfig, logger, + observability, ruleDataClient, }: RegisterRuleDependencies) { const createLifecycleRuleType = createLifecycleRuleTypeFactory({ @@ -86,7 +86,7 @@ export function registerTransactionErrorRateRuleType({ }, actionVariables: { context: [ - ...(getAlertDetailsConfig()?.apm.enabled + ...(observability.getAlertDetailsConfig()?.apm.enabled ? [apmActionVariables.alertDetailsUrl] : []), apmActionVariables.environment, From c14712fef6d672a828bb1b2a53cfc6f1a538a95a Mon Sep 17 00:00:00 2001 From: Coen Warmer Date: Mon, 14 Nov 2022 12:30:24 +0100 Subject: [PATCH 11/17] Update types --- .../rule_registry/server/utils/lifecycle_alert_services.mock.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/rule_registry/server/utils/lifecycle_alert_services.mock.ts b/x-pack/plugins/rule_registry/server/utils/lifecycle_alert_services.mock.ts index a383110394da7..58037057c61ca 100644 --- a/x-pack/plugins/rule_registry/server/utils/lifecycle_alert_services.mock.ts +++ b/x-pack/plugins/rule_registry/server/utils/lifecycle_alert_services.mock.ts @@ -36,5 +36,5 @@ export const createLifecycleAlertServicesMock = < ): LifecycleAlertServices => ({ alertWithLifecycle: ({ id }) => alertServices.alertFactory.create(id), getAlertStartedDate: jest.fn((id: string) => null), - getAlertUuid: jest.fn((id: string) => null), + getAlertUuid: jest.fn((id: string) => 'mock-alert-uuid'), }); From 29d9d95c2baadcc4eda2a691908481d598ebc21e Mon Sep 17 00:00:00 2001 From: Coen Warmer Date: Mon, 14 Nov 2022 12:39:16 +0100 Subject: [PATCH 12/17] getAlertUuid function in rules will always be available --- .../alerts/rule_types/anomaly/register_anomaly_rule_type.ts | 2 +- .../rule_types/error_count/register_error_count_rule_type.ts | 2 +- .../register_transaction_duration_rule_type.ts | 2 +- .../register_transaction_error_rate_rule_type.ts | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts index 258ad8d37b342..be9ba57d6d43c 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts @@ -303,7 +303,7 @@ export function registerAnomalyRuleType({ relativeViewInAppUrl ); - const alertUuid = getAlertUuid?.(id) || ''; + const alertUuid = getAlertUuid(id); const alertDetailsUrl = getAlertDetailsUrl( basePath, diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.ts index 19dae27531c0c..0fe6c60cbf5b7 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.ts @@ -192,7 +192,7 @@ export function registerErrorCountRuleType({ relativeViewInAppUrl ); - const alertUuid = getAlertUuid?.(id) || ''; + const alertUuid = getAlertUuid(id); const alertDetailsUrl = getAlertDetailsUrl( basePath, spaceId, diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts index 8d6ef3dd70f65..1fcd0f6762104 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts @@ -255,7 +255,7 @@ export function registerTransactionDurationRuleType({ const id = `${ApmRuleType.TransactionDuration}_${environmentLabel}`; - const alertUuid = getAlertUuid?.(id) || ''; + const alertUuid = getAlertUuid(id); const alertDetailsUrl = getAlertDetailsUrl( basePath, diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_error_rate/register_transaction_error_rate_rule_type.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_error_rate/register_transaction_error_rate_rule_type.ts index ee8eca5009f26..97f07e32566fd 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_error_rate/register_transaction_error_rate_rule_type.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_error_rate/register_transaction_error_rate_rule_type.ts @@ -248,7 +248,7 @@ export function registerTransactionErrorRateRuleType({ .filter((name) => name) .join('_'); - const alertUuid = getAlertUuid?.(id) || ''; + const alertUuid = getAlertUuid(id); const alertDetailsUrl = getAlertDetailsUrl( basePath, From 21d200bd650e2131a0fe013a1a0f2358e50edf8d Mon Sep 17 00:00:00 2001 From: Coen Warmer Date: Mon, 14 Nov 2022 13:08:15 +0100 Subject: [PATCH 13/17] Update types --- x-pack/plugins/apm/server/routes/alerts/test_utils/index.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/apm/server/routes/alerts/test_utils/index.ts b/x-pack/plugins/apm/server/routes/alerts/test_utils/index.ts index 903c6e6cb0ab3..bb86322f5cebd 100644 --- a/x-pack/plugins/apm/server/routes/alerts/test_utils/index.ts +++ b/x-pack/plugins/apm/server/routes/alerts/test_utils/index.ts @@ -12,6 +12,7 @@ import { IRuleDataClient } from '@kbn/rule-registry-plugin/server'; import { ruleRegistryMocks } from '@kbn/rule-registry-plugin/server/mocks'; import { PluginSetupContract as AlertingPluginSetupContract } from '@kbn/alerting-plugin/server'; import { APMConfig, APM_SERVER_FEATURE_ID } from '../../..'; +import { ObservabilityPluginSetup } from '@kbn/observability-plugin/server'; export const createRuleTypeMocks = () => { let alertExecutor: (...args: any[]) => Promise; @@ -57,7 +58,9 @@ export const createRuleTypeMocks = () => { serverBasePath: '/eyr', } as IBasePath, config$: mockedConfig$, - getAlertDetailsConfig: jest.fn().mockReturnValue({ apm: true }), + observability: { + getAlertDetailsConfig: jest.fn().mockReturnValue({ apm: true }), + } as unknown as ObservabilityPluginSetup, logger: loggerMock, ruleDataClient: ruleRegistryMocks.createRuleDataClient( '.alerts-observability.apm.alerts' From a77f557d62e0d113ed2a430b5b523688bfaf11d7 Mon Sep 17 00:00:00 2001 From: Coen Warmer Date: Mon, 14 Nov 2022 13:17:00 +0100 Subject: [PATCH 14/17] Remove [ALERT_UUID] from alertWithLifecycle calls as uuid is already set by lifecycle executor --- .../alerts/rule_types/anomaly/register_anomaly_rule_type.ts | 1 - .../rule_types/error_count/register_error_count_rule_type.ts | 1 - .../register_transaction_duration_rule_type.ts | 1 - 3 files changed, 3 deletions(-) diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts index be9ba57d6d43c..25885451c6e8d 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts @@ -323,7 +323,6 @@ export function registerAnomalyRuleType({ [ALERT_EVALUATION_VALUE]: score, [ALERT_EVALUATION_THRESHOLD]: threshold, [ALERT_REASON]: reasonMessage, - [ALERT_UUID]: alertUuid, ...eventSourceFields, }, }) diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.ts index 0fe6c60cbf5b7..bbe49def13d6b 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.ts @@ -209,7 +209,6 @@ export function registerErrorCountRuleType({ [ALERT_EVALUATION_VALUE]: errorCount, [ALERT_EVALUATION_THRESHOLD]: ruleParams.threshold, [ALERT_REASON]: alertReason, - [ALERT_UUID]: alertUuid, ...sourceFields, }, }) diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts index 1fcd0f6762104..7045ef5874b9f 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts @@ -284,7 +284,6 @@ export function registerTransactionDurationRuleType({ [ALERT_EVALUATION_VALUE]: transactionDuration, [ALERT_EVALUATION_THRESHOLD]: thresholdMicroseconds, [ALERT_REASON]: reason, - [ALERT_UUID]: alertUuid, ...sourceFields, }, }) From 4eeaa6177de185b56837cf8e237418b3b79a3917 Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Mon, 14 Nov 2022 12:51:18 +0000 Subject: [PATCH 15/17] [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' --- .../alerts/rule_types/anomaly/register_anomaly_rule_type.ts | 1 - .../rule_types/error_count/register_error_count_rule_type.ts | 1 - .../register_transaction_duration_rule_type.ts | 1 - x-pack/plugins/apm/server/routes/alerts/test_utils/index.ts | 2 +- 4 files changed, 1 insertion(+), 4 deletions(-) diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts index 25885451c6e8d..c2e4191fc49f5 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts @@ -17,7 +17,6 @@ import { ALERT_EVALUATION_VALUE, ALERT_REASON, ALERT_SEVERITY, - ALERT_UUID, } from '@kbn/rule-data-utils'; import { createLifecycleRuleTypeFactory } from '@kbn/rule-registry-plugin/server'; import { addSpaceIdToPath } from '@kbn/spaces-plugin/common'; diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.ts index bbe49def13d6b..648aa857870cc 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/error_count/register_error_count_rule_type.ts @@ -11,7 +11,6 @@ import { ALERT_EVALUATION_THRESHOLD, ALERT_EVALUATION_VALUE, ALERT_REASON, - ALERT_UUID, } from '@kbn/rule-data-utils'; import { createLifecycleRuleTypeFactory } from '@kbn/rule-registry-plugin/server'; import { termQuery } from '@kbn/observability-plugin/server'; diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts index 7045ef5874b9f..304a752079e12 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/transaction_duration/register_transaction_duration_rule_type.ts @@ -11,7 +11,6 @@ import { ALERT_EVALUATION_THRESHOLD, ALERT_EVALUATION_VALUE, ALERT_REASON, - ALERT_UUID, } from '@kbn/rule-data-utils'; import { firstValueFrom } from 'rxjs'; import { asDuration } from '@kbn/observability-plugin/common/utils/formatters'; diff --git a/x-pack/plugins/apm/server/routes/alerts/test_utils/index.ts b/x-pack/plugins/apm/server/routes/alerts/test_utils/index.ts index bb86322f5cebd..41d7385a2c3da 100644 --- a/x-pack/plugins/apm/server/routes/alerts/test_utils/index.ts +++ b/x-pack/plugins/apm/server/routes/alerts/test_utils/index.ts @@ -11,8 +11,8 @@ import { elasticsearchServiceMock } from '@kbn/core/server/mocks'; import { IRuleDataClient } from '@kbn/rule-registry-plugin/server'; import { ruleRegistryMocks } from '@kbn/rule-registry-plugin/server/mocks'; import { PluginSetupContract as AlertingPluginSetupContract } from '@kbn/alerting-plugin/server'; -import { APMConfig, APM_SERVER_FEATURE_ID } from '../../..'; import { ObservabilityPluginSetup } from '@kbn/observability-plugin/server'; +import { APMConfig, APM_SERVER_FEATURE_ID } from '../../..'; export const createRuleTypeMocks = () => { let alertExecutor: (...args: any[]) => Promise; From f90b64569ec636fea322cc1a2c4b2e822276b364 Mon Sep 17 00:00:00 2001 From: Dario Gieselaar Date: Mon, 14 Nov 2022 14:14:00 +0100 Subject: [PATCH 16/17] Consolidate types for DefaultUptimeAlertInstance --- .../server/legacy_uptime/lib/alerts/types.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/x-pack/plugins/synthetics/server/legacy_uptime/lib/alerts/types.ts b/x-pack/plugins/synthetics/server/legacy_uptime/lib/alerts/types.ts index c57b0924ac373..c57f435b29a8a 100644 --- a/x-pack/plugins/synthetics/server/legacy_uptime/lib/alerts/types.ts +++ b/x-pack/plugins/synthetics/server/legacy_uptime/lib/alerts/types.ts @@ -5,8 +5,8 @@ * 2.0. */ import { AlertTypeWithExecutor } from '@kbn/rule-registry-plugin/server'; -import { AlertInstanceContext, RuleTypeState } from '@kbn/alerting-plugin/common'; -import { LifecycleAlertService } from '@kbn/rule-registry-plugin/server'; +import { AlertInstanceContext } from '@kbn/alerting-plugin/common'; +import { LifecycleAlertServices } from '@kbn/rule-registry-plugin/server'; import { UMServerLibs } from '../lib'; import { UptimeCorePluginsSetup, UptimeServerSetup } from '../adapters'; @@ -20,11 +20,7 @@ export type DefaultUptimeAlertInstance = AlertTy Record, Record, AlertInstanceContext, - { - alertWithLifecycle: LifecycleAlertService; - getAlertStartedDate: (alertId: string) => string | null; - getAlertUuid: (alertId: string) => string | null; - } + LifecycleAlertServices, AlertInstanceContext, TActionGroupIds> >; export type UptimeAlertTypeFactory = ( From a48b067869ebd8cb93c78fd162944574faf0bd9c Mon Sep 17 00:00:00 2001 From: Coen Warmer Date: Mon, 14 Nov 2022 16:17:19 +0100 Subject: [PATCH 17/17] Revert change in rule registration as a FTR test wants rules registered in a specific way --- .../plugins/apm/server/routes/alerts/register_apm_rule_types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/apm/server/routes/alerts/register_apm_rule_types.ts b/x-pack/plugins/apm/server/routes/alerts/register_apm_rule_types.ts index 478aa20898bc1..b2abf6b7ed126 100644 --- a/x-pack/plugins/apm/server/routes/alerts/register_apm_rule_types.ts +++ b/x-pack/plugins/apm/server/routes/alerts/register_apm_rule_types.ts @@ -28,8 +28,8 @@ export interface RegisterRuleDependencies { } export function registerApmRuleTypes(dependencies: RegisterRuleDependencies) { + registerTransactionDurationRuleType(dependencies); registerAnomalyRuleType(dependencies); registerErrorCountRuleType(dependencies); - registerTransactionDurationRuleType(dependencies); registerTransactionErrorRateRuleType(dependencies); }