Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Observability alerting] Save group information with dynamic mapping #199298

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/kbn-alerts-as-data-utils/src/field_maps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ export * from './alert_field_map';
export * from './ecs_field_map';
export * from './legacy_alert_field_map';
export * from './legacy_experimental_field_map';
export type { FieldMap, MultiField } from './types';
export type { DynamicTemplate, FieldMap, MultiField } from './types';
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ALERT_EVALUATION_THRESHOLD,
ALERT_EVALUATION_VALUE,
ALERT_EVALUATION_VALUES,
ALERT_GROUPING,
ALERT_GROUP,
ALERT_GROUP_FIELD,
ALERT_GROUP_VALUE,
Expand All @@ -31,6 +32,12 @@ export const legacyExperimentalFieldMap = {
required: false,
array: true,
},
[ALERT_GROUPING]: {
type: 'object',
dynamic: true,
array: false,
required: false,
},
[ALERT_GROUP]: {
type: 'object',
array: true,
Expand Down
14 changes: 14 additions & 0 deletions packages/kbn-alerts-as-data-utils/src/field_maps/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,17 @@ export interface FieldMap {
properties?: Record<string, { type: string }>;
};
}

export interface DynamicTemplate {
[key: string]: {
mapping: {
[key: string]: any;
};
match_mapping_type?: string | string[];
match?: string;
unmatch?: string;
match_pattern?: string;
path_match?: string;
path_unmatch?: string;
};
}
3 changes: 3 additions & 0 deletions packages/kbn-rule-data-utils/src/technical_field_names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const ALERT_EVALUATION_THRESHOLD = `${ALERT_NAMESPACE}.evaluation.threshold` as
const ALERT_EVALUATION_VALUE = `${ALERT_NAMESPACE}.evaluation.value` as const;
const ALERT_CONTEXT = `${ALERT_NAMESPACE}.context` as const;
const ALERT_EVALUATION_VALUES = `${ALERT_NAMESPACE}.evaluation.values` as const;
const ALERT_GROUPING = `${ALERT_NAMESPACE}.grouping` as const;
const ALERT_GROUP = `${ALERT_NAMESPACE}.group` as const;
const ALERT_GROUP_FIELD = `${ALERT_GROUP}.field` as const;
const ALERT_GROUP_VALUE = `${ALERT_GROUP}.value` as const;
Expand Down Expand Up @@ -134,6 +135,7 @@ const fields = {
ALERT_EVALUATION_THRESHOLD,
ALERT_EVALUATION_VALUE,
ALERT_EVALUATION_VALUES,
ALERT_GROUPING,
ALERT_GROUP,
ALERT_GROUP_FIELD,
ALERT_GROUP_VALUE,
Expand Down Expand Up @@ -209,6 +211,7 @@ export {
ALERT_EVALUATION_VALUE,
ALERT_CONTEXT,
ALERT_EVALUATION_VALUES,
ALERT_GROUPING,
ALERT_GROUP,
ALERT_GROUP_FIELD,
ALERT_GROUP_VALUE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@
*/

import { ClusterPutComponentTemplateRequest } from '@elastic/elasticsearch/lib/api/types';
import { type FieldMap } from '@kbn/alerts-as-data-utils';
import type { FieldMap, DynamicTemplate } from '@kbn/alerts-as-data-utils';
import { mappingFromFieldMap } from './mapping_from_field_map';

export interface GetComponentTemplateFromFieldMapOpts {
name: string;
fieldMap: FieldMap;
includeSettings?: boolean;
dynamic?: 'strict' | false;
dynamicTemplates?: DynamicTemplate;
}
export const getComponentTemplateFromFieldMap = ({
name,
fieldMap,
dynamic,
includeSettings,
dynamicTemplates,
}: GetComponentTemplateFromFieldMapOpts): ClusterPutComponentTemplateRequest => {
return {
name,
Expand All @@ -37,7 +39,10 @@ export const getComponentTemplateFromFieldMap = ({
: {}),
},

mappings: mappingFromFieldMap(fieldMap, dynamic ?? 'strict'),
mappings: {
...mappingFromFieldMap(fieldMap, dynamic ?? 'strict'),
dynamic_templates: dynamicTemplates,
},
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ export class AlertsService implements IAlertsService {
const componentTemplate = getComponentTemplate({
fieldMap: mappings.fieldMap,
dynamic: mappings.dynamic,
dynamicTemplates: mappings.dynamicTemplates,
context,
});
initFns.push(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { ClusterPutComponentTemplateRequest } from '@elastic/elasticsearch/lib/api/types';
import type { FieldMap } from '@kbn/alerts-as-data-utils';
import type { DynamicTemplate, FieldMap } from '@kbn/alerts-as-data-utils';
import { getComponentTemplateFromFieldMap } from '../../common';

interface GetComponentTemplateNameOpts {
Expand Down Expand Up @@ -63,6 +63,7 @@ type GetComponentTemplateOpts = GetComponentTemplateNameOpts & {
fieldMap: FieldMap;
dynamic?: 'strict' | false;
includeSettings?: boolean;
dynamicTemplates?: DynamicTemplate;
};

export const getComponentTemplate = ({
Expand All @@ -71,10 +72,12 @@ export const getComponentTemplate = ({
name,
dynamic,
includeSettings,
dynamicTemplates,
}: GetComponentTemplateOpts): ClusterPutComponentTemplateRequest =>
getComponentTemplateFromFieldMap({
name: getComponentTemplateName({ context, name }),
fieldMap,
dynamic,
includeSettings,
dynamicTemplates,
});
3 changes: 2 additions & 1 deletion x-pack/plugins/alerting/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import type { ObjectType } from '@kbn/config-schema';
import type { PublicMethodsOf } from '@kbn/utility-types';
import { SharePluginStart } from '@kbn/share-plugin/server';
import type { DefaultAlert, FieldMap } from '@kbn/alerts-as-data-utils';
import { Alert } from '@kbn/alerts-as-data-utils';
import type { Alert, DynamicTemplate } from '@kbn/alerts-as-data-utils';
import { ActionsApiRequestHandlerContext } from '@kbn/actions-plugin/server';
import { AlertsHealth } from '@kbn/alerting-types';
import { RuleTypeRegistry as OrigruleTypeRegistry } from './rule_type_registry';
Expand Down Expand Up @@ -196,6 +196,7 @@ export type GetViewInAppRelativeUrlFn<Params extends RuleTypeParams> = (
interface ComponentTemplateSpec {
dynamic?: 'strict' | false; // defaults to 'strict'
fieldMap: FieldMap;
dynamicTemplates?: DynamicTemplate[];
}

export type FormatAlert<AlertData extends RuleAlertData> = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ALERT_EVALUATION_THRESHOLD,
ALERT_REASON,
ALERT_GROUP,
ALERT_GROUPING,
} from '@kbn/rule-data-utils';
import { LocatorPublic } from '@kbn/share-plugin/common';
import { RecoveredActionGroup } from '@kbn/alerting-plugin/common';
Expand Down Expand Up @@ -247,6 +248,8 @@ export const createCustomThresholdExecutor = ({
);

const groups = groupByKeysObjectMapping[group];
const grouping: Record<string, string> = {};
groups?.forEach((groupObj) => (grouping[groupObj.field] = groupObj.value));

const { uuid, start } = alertsClient.report({
id: `${group}`,
Expand All @@ -256,6 +259,7 @@ export const createCustomThresholdExecutor = ({
[ALERT_EVALUATION_VALUES]: evaluationValues,
[ALERT_EVALUATION_THRESHOLD]: threshold,
[ALERT_GROUP]: groups,
[ALERT_GROUPING]: grouping,
...flattenAdditionalContext(additionalContext),
...getEcsGroups(groups),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,22 @@ import { CustomThresholdAlert } from './types';

export const MetricsRulesTypeAlertDefinition: IRuleTypeAlerts<CustomThresholdAlert> = {
context: THRESHOLD_RULE_REGISTRATION_CONTEXT,
mappings: { fieldMap: legacyExperimentalFieldMap },
mappings: {
// dynamic: true,
fieldMap: legacyExperimentalFieldMap,
dynamicTemplates: [
{
strings_as_keywords: {
path_match: 'kibana.alert.grouping.*',
match_mapping_type: 'string',
mapping: {
type: 'keyword',
ignore_above: 1024,
},
},
},
],
},
useEcs: true,
useLegacyAlerts: false,
shouldWrite: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export type CustomThresholdAlertState = AlertState; // no specific instance stat
export type CustomThresholdAlertContext = AlertContext & {
alertDetailsUrl: string;
group?: object;
grouping?: object;
reason?: string;
timestamp: string; // ISO string
// String type is for [NO DATA]
Expand All @@ -80,4 +81,5 @@ export type CustomThresholdAlert = Omit<
[ALERT_EVALUATION_VALUES]?: Array<number | null>;
[ALERT_EVALUATION_THRESHOLD]?: Array<number | null>;
[ALERT_GROUP]?: Group[];
[ALERT_GROUPING]?: Record<string, string>;
};