Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
*/

import numeral from '@elastic/numeral';
import { flattenObject } from '@kbn/object-utils';
import { AlertsClientError, ExecutorType, RuleExecutorOptions } from '@kbn/alerting-plugin/server';
import { ObservabilitySloAlert } from '@kbn/alerts-as-data-utils';
import { IBasePath } from '@kbn/core/server';
import { i18n } from '@kbn/i18n';
import { getEcsGroups } from '@kbn/alerting-rule-utils';
import { getEcsGroups, getFormattedGroups } from '@kbn/alerting-rule-utils';
import { getAlertDetailsUrl } from '@kbn/observability-plugin/common';
import {
ALERT_EVALUATION_THRESHOLD,
ALERT_EVALUATION_VALUE,
ALERT_GROUPING,
ALERT_GROUP,
ALERT_REASON,
} from '@kbn/rule-data-utils';
Expand Down Expand Up @@ -50,6 +52,7 @@ import {
WindowSchema,
} from './types';

// should I add ALERT_GROUPING here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the alertClient does not complain i think it's fine to keep it as it is

export type BurnRateAlert = Omit<ObservabilitySloAlert, 'kibana.alert.group'> & {
[ALERT_GROUP]?: Group[];
};
Expand Down Expand Up @@ -122,14 +125,11 @@ export const getRuleExecutor = (basePath: IBasePath) =>
window: windowDef,
} = result;

const instances = instanceId.split(',');
const groups =
instanceId !== ALL_VALUE
? [slo.groupBy].flat().reduce<Group[]>((resultGroups, groupByItem, index) => {
resultGroups.push({ field: groupByItem, value: instances[index].trim() });
return resultGroups;
}, [])
: undefined;
const groupingsFlattened = flattenObject(groupings ?? {});
const groupingsFlattenedString = Object.fromEntries(
Comment thread
benakansara marked this conversation as resolved.
Outdated
Object.entries(groupingsFlattened).map(([key, value]) => [key, String(value)])
);
const groups = getFormattedGroups(groupingsFlattenedString);

const urlQuery = instanceId === ALL_VALUE ? '' : `?instanceId=${instanceId}`;
const viewInAppUrl = addSpaceIdToPath(
Expand Down Expand Up @@ -168,13 +168,13 @@ export const getRuleExecutor = (basePath: IBasePath) =>
actionGroup,
state: {
alertState: AlertStates.ALERT,
grouping: groupings,
},
payload: {
[ALERT_REASON]: reason,
[ALERT_EVALUATION_THRESHOLD]: windowDef.burnRateThreshold,
[ALERT_EVALUATION_VALUE]: Math.min(longWindowBurnRate, shortWindowBurnRate),
[ALERT_GROUP]: groups,
[ALERT_GROUPING]: groupings, // Object, example: { host: { name: 'host-0' } }
[SLO_ID_FIELD]: slo.id,
[SLO_REVISION_FIELD]: slo.revision,
[SLO_INSTANCE_ID_FIELD]: instanceId,
Comment thread
benakansara marked this conversation as resolved.
Expand Down Expand Up @@ -226,16 +226,14 @@ export const getRuleExecutor = (basePath: IBasePath) =>
`/app/observability/slos/${slo.id}${urlQuery}`
);

const recoveredAlertState = recoveredAlert.alert.getState();

const context = {
timestamp: startedAt.toISOString(),
viewInAppUrl,
alertDetailsUrl,
sloId: slo.id,
sloName: slo.name,
sloInstanceId: alertId,
grouping: recoveredAlertState?.grouping,
grouping: recoveredAlert.hit?.[ALERT_GROUPING],
};

alertsClient.setAlertData({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,24 @@ export function sloBurnRateRuleType(
},
alerts: {
context: SLO_RULE_REGISTRATION_CONTEXT,
mappings: { fieldMap: { ...legacyExperimentalFieldMap, ...sloRuleFieldMap } },
mappings: {
fieldMap: {
...legacyExperimentalFieldMap,
...sloRuleFieldMap,
},
dynamicTemplates: [
{
strings_as_keywords: {
path_match: 'kibana.alert.grouping.*',
match_mapping_type: 'string',
mapping: {
type: 'keyword' as const,
ignore_above: 1024,
},
},
},
],
},
useEcs: true,
useLegacyAlerts: true,
shouldWrite: true,
Expand Down