From 4873fa18d70142c5c2e6633d51648c45f6f481cb Mon Sep 17 00:00:00 2001 From: Antonio Date: Wed, 8 Jan 2025 10:50:26 +0100 Subject: [PATCH] [ResponseOps][Rules] Move metric rule params schema to package (#205492) Connected with #195189 ## Summary - Moved params of duration metric inventory threshold rule type to `/response-ops/rule_params/metric_inventory_threshold/` - Moved params of metric threshold rule type to `/response-ops/rule_params/metric_threshold/` **I did NOT move the corresponding type to the rule_params package due to the recursive imports it would create.** --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> --- .../response-ops/rule_params/common/utils.ts | 41 ++++++++ .../metric_inventory_threshold/index.ts | 11 +++ .../metric_inventory_threshold/latest.ts | 10 ++ .../metric_inventory_threshold/v1.ts | 91 +++++++++++++++++ .../rule_params/metric_threshold/index.ts | 11 +++ .../rule_params/metric_threshold/latest.ts | 10 ++ .../rule_params/metric_threshold/v1.ts | 99 +++++++++++++++++++ .../response-ops/rule_params/tsconfig.json | 6 +- .../infra/server/lib/alerting/common/utils.ts | 27 +---- ...er_inventory_metric_threshold_rule_type.ts | 53 ++-------- .../register_metric_threshold_rule_type.ts | 76 +------------- .../observability/plugins/infra/tsconfig.json | 3 +- 12 files changed, 292 insertions(+), 146 deletions(-) create mode 100644 src/platform/packages/shared/response-ops/rule_params/common/utils.ts create mode 100644 src/platform/packages/shared/response-ops/rule_params/metric_inventory_threshold/index.ts create mode 100644 src/platform/packages/shared/response-ops/rule_params/metric_inventory_threshold/latest.ts create mode 100644 src/platform/packages/shared/response-ops/rule_params/metric_inventory_threshold/v1.ts create mode 100644 src/platform/packages/shared/response-ops/rule_params/metric_threshold/index.ts create mode 100644 src/platform/packages/shared/response-ops/rule_params/metric_threshold/latest.ts create mode 100644 src/platform/packages/shared/response-ops/rule_params/metric_threshold/v1.ts diff --git a/src/platform/packages/shared/response-ops/rule_params/common/utils.ts b/src/platform/packages/shared/response-ops/rule_params/common/utils.ts new file mode 100644 index 0000000000000..521621620dc29 --- /dev/null +++ b/src/platform/packages/shared/response-ops/rule_params/common/utils.ts @@ -0,0 +1,41 @@ +/* + * 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", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import { schema } from '@kbn/config-schema'; +import { isEmpty } from 'lodash'; + +export const oneOfLiterals = (arrayOfLiterals: Readonly) => + schema.string({ + validate: (value) => + arrayOfLiterals.includes(value) ? undefined : `must be one of ${arrayOfLiterals.join(' | ')}`, + }); + +export const validateIsStringElasticsearchJSONFilter = (value: string) => { + if (value === '') { + // Allow clearing the filter. + return; + } + + const errorMessage = 'filterQuery must be a valid Elasticsearch filter expressed in JSON'; + try { + const parsedValue = JSON.parse(value); + if (!isEmpty(parsedValue.bool)) { + return undefined; + } + return errorMessage; + } catch (e) { + return errorMessage; + } +}; + +export type TimeUnitChar = 's' | 'm' | 'h' | 'd'; + +export enum LEGACY_COMPARATORS { + OUTSIDE_RANGE = 'outside', +} diff --git a/src/platform/packages/shared/response-ops/rule_params/metric_inventory_threshold/index.ts b/src/platform/packages/shared/response-ops/rule_params/metric_inventory_threshold/index.ts new file mode 100644 index 0000000000000..779a8d97d2905 --- /dev/null +++ b/src/platform/packages/shared/response-ops/rule_params/metric_inventory_threshold/index.ts @@ -0,0 +1,11 @@ +/* + * 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", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +export { metricInventoryThresholdRuleParamsSchema } from './latest'; +export { metricInventoryThresholdRuleParamsSchema as metricInventoryThresholdRuleParamsSchemaV1 } from './v1'; diff --git a/src/platform/packages/shared/response-ops/rule_params/metric_inventory_threshold/latest.ts b/src/platform/packages/shared/response-ops/rule_params/metric_inventory_threshold/latest.ts new file mode 100644 index 0000000000000..f278309c22b03 --- /dev/null +++ b/src/platform/packages/shared/response-ops/rule_params/metric_inventory_threshold/latest.ts @@ -0,0 +1,10 @@ +/* + * 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", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +export * from './v1'; diff --git a/src/platform/packages/shared/response-ops/rule_params/metric_inventory_threshold/v1.ts b/src/platform/packages/shared/response-ops/rule_params/metric_inventory_threshold/v1.ts new file mode 100644 index 0000000000000..59b74200f2f12 --- /dev/null +++ b/src/platform/packages/shared/response-ops/rule_params/metric_inventory_threshold/v1.ts @@ -0,0 +1,91 @@ +/* + * 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", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import { schema, Type } from '@kbn/config-schema'; +import { COMPARATORS } from '@kbn/alerting-comparators'; + +import { + LEGACY_COMPARATORS, + TimeUnitChar, + oneOfLiterals, + validateIsStringElasticsearchJSONFilter, +} from '../common/utils'; + +const SNAPSHOT_CUSTOM_AGGREGATIONS = ['avg', 'max', 'min', 'rate'] as const; +type SnapshotCustomAggregation = (typeof SNAPSHOT_CUSTOM_AGGREGATIONS)[number]; + +const SnapshotMetricTypeKeysArray = [ + 'count', + 'cpuV2', + 'cpu', + 'diskLatency', + 'diskSpaceUsage', + 'load', + 'memory', + 'memoryFree', + 'memoryTotal', + 'normalizedLoad1m', + 'tx', + 'rx', + 'txV2', + 'rxV2', + 'logRate', + 'diskIOReadBytes', + 'diskIOWriteBytes', + 's3TotalRequests', + 's3NumberOfObjects', + 's3BucketSize', + 's3DownloadBytes', + 's3UploadBytes', + 'rdsConnections', + 'rdsQueriesExecuted', + 'rdsActiveTransactions', + 'rdsLatency', + 'sqsMessagesVisible', + 'sqsMessagesDelayed', + 'sqsMessagesSent', + 'sqsMessagesEmpty', + 'sqsOldestMessage', + 'custom', +]; +type SnapshotMetricTypeKeys = (typeof SNAPSHOT_CUSTOM_AGGREGATIONS)[number]; + +const comparators = Object.values({ ...COMPARATORS, ...LEGACY_COMPARATORS }); + +export const metricInventoryThresholdRuleParamsSchema = schema.object( + { + criteria: schema.arrayOf( + schema.object({ + threshold: schema.arrayOf(schema.number()), + comparator: oneOfLiterals(comparators) as Type, + timeUnit: schema.string() as Type, + timeSize: schema.number(), + metric: oneOfLiterals(SnapshotMetricTypeKeysArray) as Type, + warningThreshold: schema.maybe(schema.arrayOf(schema.number())), + warningComparator: schema.maybe(oneOfLiterals(comparators)), + customMetric: schema.maybe( + schema.object({ + type: schema.literal('custom'), + id: schema.string(), + field: schema.string(), + aggregation: oneOfLiterals( + SNAPSHOT_CUSTOM_AGGREGATIONS + ) as Type, + label: schema.maybe(schema.string()), + }) + ), + }) + ), + nodeType: schema.string(), + filterQuery: schema.maybe(schema.string({ validate: validateIsStringElasticsearchJSONFilter })), + sourceId: schema.string(), + alertOnNoData: schema.maybe(schema.boolean()), + }, + { unknowns: 'allow' } +); diff --git a/src/platform/packages/shared/response-ops/rule_params/metric_threshold/index.ts b/src/platform/packages/shared/response-ops/rule_params/metric_threshold/index.ts new file mode 100644 index 0000000000000..a3110ba721272 --- /dev/null +++ b/src/platform/packages/shared/response-ops/rule_params/metric_threshold/index.ts @@ -0,0 +1,11 @@ +/* + * 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", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +export { metricThresholdRuleParamsSchema } from './latest'; +export { metricThresholdRuleParamsSchema as metricThresholdRuleParamsSchemaV1 } from './v1'; diff --git a/src/platform/packages/shared/response-ops/rule_params/metric_threshold/latest.ts b/src/platform/packages/shared/response-ops/rule_params/metric_threshold/latest.ts new file mode 100644 index 0000000000000..f278309c22b03 --- /dev/null +++ b/src/platform/packages/shared/response-ops/rule_params/metric_threshold/latest.ts @@ -0,0 +1,10 @@ +/* + * 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", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +export * from './v1'; diff --git a/src/platform/packages/shared/response-ops/rule_params/metric_threshold/v1.ts b/src/platform/packages/shared/response-ops/rule_params/metric_threshold/v1.ts new file mode 100644 index 0000000000000..8c1790cc16238 --- /dev/null +++ b/src/platform/packages/shared/response-ops/rule_params/metric_threshold/v1.ts @@ -0,0 +1,99 @@ +/* + * 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", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import { schema } from '@kbn/config-schema'; +import { COMPARATORS } from '@kbn/alerting-comparators'; + +import { + LEGACY_COMPARATORS, + oneOfLiterals, + validateIsStringElasticsearchJSONFilter, +} from '../common/utils'; + +const METRIC_EXPLORER_AGGREGATIONS = [ + 'avg', + 'max', + 'min', + 'cardinality', + 'rate', + 'count', + 'sum', + 'p95', + 'p99', + 'custom', +] as const; + +const comparator = Object.values({ ...COMPARATORS, ...LEGACY_COMPARATORS }); + +const baseCriterion = { + threshold: schema.arrayOf(schema.number()), + comparator: oneOfLiterals(comparator), + timeUnit: schema.string(), + timeSize: schema.number(), + warningThreshold: schema.maybe(schema.arrayOf(schema.number())), + warningComparator: schema.maybe(oneOfLiterals(comparator)), +}; + +const nonCountCriterion = schema.object({ + ...baseCriterion, + metric: schema.string(), + aggType: oneOfLiterals(METRIC_EXPLORER_AGGREGATIONS), + customMetrics: schema.never(), + equation: schema.never(), + label: schema.never(), +}); + +const countCriterion = schema.object({ + ...baseCriterion, + aggType: schema.literal('count'), + metric: schema.never(), + customMetrics: schema.never(), + equation: schema.never(), + label: schema.never(), +}); + +const customCriterion = schema.object({ + ...baseCriterion, + aggType: schema.literal('custom'), + metric: schema.never(), + customMetrics: schema.arrayOf( + schema.oneOf([ + schema.object({ + name: schema.string(), + aggType: oneOfLiterals(['avg', 'sum', 'max', 'min', 'cardinality']), + field: schema.string(), + filter: schema.never(), + }), + schema.object({ + name: schema.string(), + aggType: schema.literal('count'), + filter: schema.maybe(schema.string()), + field: schema.never(), + }), + ]) + ), + equation: schema.maybe(schema.string()), + label: schema.maybe(schema.string()), +}); + +export const metricThresholdRuleParamsSchema = schema.object( + { + criteria: schema.arrayOf(schema.oneOf([countCriterion, nonCountCriterion, customCriterion])), + groupBy: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])), + filterQuery: schema.maybe( + schema.string({ + validate: validateIsStringElasticsearchJSONFilter, + }) + ), + sourceId: schema.string(), + alertOnNoData: schema.maybe(schema.boolean()), + alertOnGroupDisappear: schema.maybe(schema.boolean()), + }, + { unknowns: 'allow' } +); diff --git a/src/platform/packages/shared/response-ops/rule_params/tsconfig.json b/src/platform/packages/shared/response-ops/rule_params/tsconfig.json index 47b4f90bb2813..c8de98d29c211 100644 --- a/src/platform/packages/shared/response-ops/rule_params/tsconfig.json +++ b/src/platform/packages/shared/response-ops/rule_params/tsconfig.json @@ -6,5 +6,9 @@ }, "include": ["**/*.ts"], "exclude": ["target/**/*"], - "kbn_references": ["@kbn/config-schema", "@kbn/ml-anomaly-utils"] + "kbn_references": [ + "@kbn/config-schema", + "@kbn/ml-anomaly-utils", + "@kbn/alerting-comparators", + ] } diff --git a/x-pack/solutions/observability/plugins/infra/server/lib/alerting/common/utils.ts b/x-pack/solutions/observability/plugins/infra/server/lib/alerting/common/utils.ts index f87c5197373d7..c26d866218d7b 100644 --- a/x-pack/solutions/observability/plugins/infra/server/lib/alerting/common/utils.ts +++ b/x-pack/solutions/observability/plugins/infra/server/lib/alerting/common/utils.ts @@ -5,8 +5,7 @@ * 2.0. */ -import { isEmpty, isError } from 'lodash'; -import { schema } from '@kbn/config-schema'; +import { isError } from 'lodash'; import type { Logger, LogMeta } from '@kbn/logging'; import type { ElasticsearchClient } from '@kbn/core/server'; import type { ObservabilityConfig } from '@kbn/observability-plugin/server'; @@ -52,30 +51,6 @@ const SUPPORTED_ES_FIELD_TYPES = [ ES_FIELD_TYPES.BOOLEAN, ]; -export const oneOfLiterals = (arrayOfLiterals: Readonly) => - schema.string({ - validate: (value) => - arrayOfLiterals.includes(value) ? undefined : `must be one of ${arrayOfLiterals.join(' | ')}`, - }); - -export const validateIsStringElasticsearchJSONFilter = (value: string) => { - if (value === '') { - // Allow clearing the filter. - return; - } - - const errorMessage = 'filterQuery must be a valid Elasticsearch filter expressed in JSON'; - try { - const parsedValue = JSON.parse(value); - if (!isEmpty(parsedValue.bool)) { - return undefined; - } - return errorMessage; - } catch (e) { - return errorMessage; - } -}; - export const UNGROUPED_FACTORY_KEY = '*'; export const createScopedLogger = ( diff --git a/x-pack/solutions/observability/plugins/infra/server/lib/alerting/inventory_metric_threshold/register_inventory_metric_threshold_rule_type.ts b/x-pack/solutions/observability/plugins/infra/server/lib/alerting/inventory_metric_threshold/register_inventory_metric_threshold_rule_type.ts index 2a84ef327b9cd..cc07cb10bd725 100644 --- a/x-pack/solutions/observability/plugins/infra/server/lib/alerting/inventory_metric_threshold/register_inventory_metric_threshold_rule_type.ts +++ b/x-pack/solutions/observability/plugins/infra/server/lib/alerting/inventory_metric_threshold/register_inventory_metric_threshold_rule_type.ts @@ -5,8 +5,6 @@ * 2.0. */ -import type { Type } from '@kbn/config-schema'; -import { schema } from '@kbn/config-schema'; import { i18n } from '@kbn/i18n'; import { DEFAULT_APP_CATEGORIES } from '@kbn/core/server'; import type { @@ -14,13 +12,7 @@ import type { AlertingServerSetup, } from '@kbn/alerting-plugin/server'; import { observabilityPaths } from '@kbn/observability-plugin/common'; -import type { TimeUnitChar } from '@kbn/observability-plugin/common/utils/formatters/duration'; -import type { InventoryItemType, SnapshotMetricType } from '@kbn/metrics-data-access-plugin/common'; -import { SnapshotMetricTypeKeys } from '@kbn/metrics-data-access-plugin/common'; -import { COMPARATORS } from '@kbn/alerting-comparators'; -import { LEGACY_COMPARATORS } from '@kbn/observability-plugin/common/utils/convert_legacy_outside_comparator'; -import type { SnapshotCustomAggregation } from '../../../../common/http_api'; -import { SNAPSHOT_CUSTOM_AGGREGATIONS } from '../../../../common/http_api'; +import { metricInventoryThresholdRuleParamsSchema } from '@kbn/response-ops-rule-params/metric_inventory_threshold'; import type { InfraConfig } from '../../../../common/plugin_config_types'; import { METRIC_INVENTORY_THRESHOLD_ALERT_TYPE_ID } from '../../../../common/alerting/metrics'; import type { InfraBackendLibs, InfraLocators } from '../../infra_types'; @@ -42,7 +34,6 @@ import { valueActionVariableDescription, viewInAppUrlActionVariableDescription, } from '../common/messages'; -import { oneOfLiterals, validateIsStringElasticsearchJSONFilter } from '../common/utils'; import { createInventoryMetricThresholdExecutor, FIRED_ACTIONS, @@ -52,26 +43,6 @@ import { import { MetricsRulesTypeAlertDefinition } from '../register_rule_types'; import { O11Y_AAD_FIELDS } from '../../../../common/constants'; -const comparators = Object.values({ ...COMPARATORS, ...LEGACY_COMPARATORS }); -const condition = schema.object({ - threshold: schema.arrayOf(schema.number()), - comparator: oneOfLiterals(comparators) as Type, - timeUnit: schema.string() as Type, - timeSize: schema.number(), - metric: oneOfLiterals(Object.keys(SnapshotMetricTypeKeys)) as Type, - warningThreshold: schema.maybe(schema.arrayOf(schema.number())), - warningComparator: schema.maybe(oneOfLiterals(comparators)) as Type, - customMetric: schema.maybe( - schema.object({ - type: schema.literal('custom'), - id: schema.string(), - field: schema.string(), - aggregation: oneOfLiterals(SNAPSHOT_CUSTOM_AGGREGATIONS) as Type, - label: schema.maybe(schema.string()), - }) - ), -}); - const groupActionVariableDescription = i18n.translate( 'xpack.infra.inventory.alerting.groupActionVariableDescription', { @@ -89,31 +60,18 @@ export function registerInventoryThresholdRuleType( return; } - const paramsSchema = schema.object( - { - criteria: schema.arrayOf(condition), - nodeType: schema.string() as Type, - filterQuery: schema.maybe( - schema.string({ validate: validateIsStringElasticsearchJSONFilter }) - ), - sourceId: schema.string(), - alertOnNoData: schema.maybe(schema.boolean()), - }, - { unknowns: 'allow' } - ); - alertingPlugin.registerType({ id: METRIC_INVENTORY_THRESHOLD_ALERT_TYPE_ID, name: i18n.translate('xpack.infra.metrics.inventory.alertName', { defaultMessage: 'Inventory', }), validate: { - params: paramsSchema, + params: metricInventoryThresholdRuleParamsSchema, }, schemas: { params: { type: 'config-schema', - schema: paramsSchema, + schema: metricInventoryThresholdRuleParamsSchema, }, }, defaultActionGroupId: FIRED_ACTIONS_ID, @@ -123,6 +81,11 @@ export function registerInventoryThresholdRuleType( producer: 'infrastructure', minimumLicenseRequired: 'basic', isExportable: true, + /* + * The schema defined in response-ops-rule-params cannot import all types from the plugins + * so the executor will expect slight differences with the type InventoryMetricThresholdParams. + */ + // @ts-ignore executor: createInventoryMetricThresholdExecutor(libs, locators), actionVariables: { context: [ diff --git a/x-pack/solutions/observability/plugins/infra/server/lib/alerting/metric_threshold/register_metric_threshold_rule_type.ts b/x-pack/solutions/observability/plugins/infra/server/lib/alerting/metric_threshold/register_metric_threshold_rule_type.ts index 1eed9f7ef3e5c..5a0353c03c753 100644 --- a/x-pack/solutions/observability/plugins/infra/server/lib/alerting/metric_threshold/register_metric_threshold_rule_type.ts +++ b/x-pack/solutions/observability/plugins/infra/server/lib/alerting/metric_threshold/register_metric_threshold_rule_type.ts @@ -6,18 +6,16 @@ */ import { DEFAULT_APP_CATEGORIES } from '@kbn/core/server'; -import { schema } from '@kbn/config-schema'; import { i18n } from '@kbn/i18n'; import type { GetViewInAppRelativeUrlFnOpts, AlertingServerSetup, } from '@kbn/alerting-plugin/server'; import { observabilityPaths } from '@kbn/observability-plugin/common'; -import { COMPARATORS } from '@kbn/alerting-comparators'; -import { LEGACY_COMPARATORS } from '@kbn/observability-plugin/common/utils/convert_legacy_outside_comparator'; +import { metricThresholdRuleParamsSchema } from '@kbn/response-ops-rule-params/metric_threshold'; + import type { InfraConfig } from '../../../../common/plugin_config_types'; import { METRIC_THRESHOLD_ALERT_TYPE_ID } from '../../../../common/alerting/metrics'; -import { METRIC_EXPLORER_AGGREGATIONS } from '../../../../common/http_api'; import type { InfraBackendLibs, InfraLocators } from '../../infra_types'; import { alertDetailUrlActionVariableDescription, @@ -38,7 +36,6 @@ import { valueActionVariableDescription, viewInAppUrlActionVariableDescription, } from '../common/messages'; -import { oneOfLiterals, validateIsStringElasticsearchJSONFilter } from '../common/utils'; import { createMetricThresholdExecutor, FIRED_ACTIONS, @@ -57,57 +54,6 @@ export function registerMetricThresholdRuleType( if (!featureFlags.metricThresholdAlertRuleEnabled) { return; } - const comparator = Object.values({ ...COMPARATORS, ...LEGACY_COMPARATORS }); - const baseCriterion = { - threshold: schema.arrayOf(schema.number()), - comparator: oneOfLiterals(comparator), - timeUnit: schema.string(), - timeSize: schema.number(), - warningThreshold: schema.maybe(schema.arrayOf(schema.number())), - warningComparator: schema.maybe(oneOfLiterals(comparator)), - }; - - const nonCountCriterion = schema.object({ - ...baseCriterion, - metric: schema.string(), - aggType: oneOfLiterals(METRIC_EXPLORER_AGGREGATIONS), - customMetrics: schema.never(), - equation: schema.never(), - label: schema.never(), - }); - - const countCriterion = schema.object({ - ...baseCriterion, - aggType: schema.literal('count'), - metric: schema.never(), - customMetrics: schema.never(), - equation: schema.never(), - label: schema.never(), - }); - - const customCriterion = schema.object({ - ...baseCriterion, - aggType: schema.literal('custom'), - metric: schema.never(), - customMetrics: schema.arrayOf( - schema.oneOf([ - schema.object({ - name: schema.string(), - aggType: oneOfLiterals(['avg', 'sum', 'max', 'min', 'cardinality']), - field: schema.string(), - filter: schema.never(), - }), - schema.object({ - name: schema.string(), - aggType: schema.literal('count'), - filter: schema.maybe(schema.string()), - field: schema.never(), - }), - ]) - ), - equation: schema.maybe(schema.string()), - label: schema.maybe(schema.string()), - }); const groupActionVariableDescription = i18n.translate( 'xpack.infra.metrics.alerting.groupActionVariableDescription', @@ -124,23 +70,7 @@ export function registerMetricThresholdRuleType( }), fieldsForAAD: O11Y_AAD_FIELDS, validate: { - params: schema.object( - { - criteria: schema.arrayOf( - schema.oneOf([countCriterion, nonCountCriterion, customCriterion]) - ), - groupBy: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])), - filterQuery: schema.maybe( - schema.string({ - validate: validateIsStringElasticsearchJSONFilter, - }) - ), - sourceId: schema.string(), - alertOnNoData: schema.maybe(schema.boolean()), - alertOnGroupDisappear: schema.maybe(schema.boolean()), - }, - { unknowns: 'allow' } - ), + params: metricThresholdRuleParamsSchema, }, defaultActionGroupId: FIRED_ACTIONS.id, actionGroups: [FIRED_ACTIONS, WARNING_ACTIONS, NO_DATA_ACTIONS], diff --git a/x-pack/solutions/observability/plugins/infra/tsconfig.json b/x-pack/solutions/observability/plugins/infra/tsconfig.json index 3a8b68772fed1..38788900217b7 100644 --- a/x-pack/solutions/observability/plugins/infra/tsconfig.json +++ b/x-pack/solutions/observability/plugins/infra/tsconfig.json @@ -116,7 +116,8 @@ "@kbn/core-plugins-server", "@kbn/config", "@kbn/observability-utils-common", - "@kbn/charts-theme" + "@kbn/charts-theme", + "@kbn/response-ops-rule-params" ], "exclude": ["target/**/*"] }