Skip to content

Commit

Permalink
Adding ALERT_CONTEXT to Metric Threshold Rule AAD documents
Browse files Browse the repository at this point in the history
  • Loading branch information
simianhacker committed May 15, 2023
1 parent 12dd688 commit 3c2101a
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 24 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ const setEvaluationResults = (response: Array<Record<string, Evaluation>>) => {
jest.requireMock('./lib/evaluate_rule').evaluateRule.mockImplementation(() => response);
};

// FAILING: https://github.com/elastic/kibana/issues/155534
describe.skip('The metric threshold alert type', () => {
describe('The metric threshold alert type', () => {
describe('querying the entire infrastructure', () => {
afterAll(() => clearInstances());
const instanceID = '*';
Expand Down Expand Up @@ -296,8 +295,12 @@ describe.skip('The metric threshold alert type', () => {
},
]);
await execute(Comparator.GT, [0.75]);
expect(mostRecentAction(instanceIdA)).toBeAlertAction();
expect(mostRecentAction(instanceIdB)).toBeAlertAction();
const recentActionA = mostRecentAction(instanceIdA);
const recentActionB = mostRecentAction(instanceIdB);
expect(recentActionA).toBeAlertAction();
expect(recentActionB).toBeAlertAction();
expect(recentActionA.action).toMatchSnapshot();
expect(recentActionB.action).toMatchSnapshot();
});
test('sends an alert when only some groups pass the threshold', async () => {
setEvaluationResults([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
*/

import { i18n } from '@kbn/i18n';
import { ALERT_ACTION_GROUP, ALERT_EVALUATION_VALUES, ALERT_REASON } from '@kbn/rule-data-utils';
import {
ALERT_ACTION_GROUP,
ALERT_CONTEXT,
ALERT_EVALUATION_VALUES,
ALERT_REASON,
} from '@kbn/rule-data-utils';
import { isEqual } from 'lodash';
import {
ActionGroupIdsOf,
Expand Down Expand Up @@ -78,7 +83,8 @@ type MetricThresholdAlertFactory = (
id: string,
reason: string,
actionGroup: MetricThresholdActionGroup,
additionalContext?: AdditionalContext | null,
alertContext?: AdditionalContext | null,
rootLevelContext?: AdditionalContext | null,
evaluationValues?: Array<number | null>
) => MetricThresholdAlert;

Expand Down Expand Up @@ -116,7 +122,8 @@ export const createMetricThresholdExecutor = (libs: InfraBackendLibs) =>
id,
reason,
actionGroup,
additionalContext,
alertContext,
rootLevelContext,
evaluationValues
) =>
alertWithLifecycle({
Expand All @@ -125,7 +132,8 @@ export const createMetricThresholdExecutor = (libs: InfraBackendLibs) =>
[ALERT_REASON]: reason,
[ALERT_ACTION_GROUP]: actionGroup,
[ALERT_EVALUATION_VALUES]: evaluationValues,
...flattenAdditionalContext(additionalContext),
[ALERT_CONTEXT]: alertContext,
...flattenAdditionalContext(rootLevelContext),
},
});

Expand All @@ -148,10 +156,8 @@ export const createMetricThresholdExecutor = (libs: InfraBackendLibs) =>
const timestamp = startedAt.toISOString();
const actionGroupId = FIRED_ACTIONS_ID; // Change this to an Error action group when able
const reason = buildInvalidQueryAlertReason(params.filterQueryText);
const alert = alertFactory(UNGROUPED_FACTORY_KEY, reason, actionGroupId);
const alertUuid = getAlertUuid(UNGROUPED_FACTORY_KEY);

alert.scheduleActions(actionGroupId, {
const alertContext = {
alertDetailsUrl: getAlertDetailsUrl(libs.basePath, spaceId, alertUuid),
alertState: stateToAlertMessage[AlertStates.ERROR],
group: UNGROUPED_FACTORY_KEY,
Expand All @@ -160,7 +166,10 @@ export const createMetricThresholdExecutor = (libs: InfraBackendLibs) =>
timestamp,
value: null,
viewInAppUrl: getViewInMetricsAppUrl(libs.basePath, spaceId),
});
};
const alert = alertFactory(UNGROUPED_FACTORY_KEY, reason, actionGroupId, alertContext);

alert.scheduleActions(actionGroupId, alertContext);

return {
state: {
Expand Down Expand Up @@ -297,21 +306,14 @@ export const createMetricThresholdExecutor = (libs: InfraBackendLibs) =>
);

const evaluationValues = alertResults.reduce((acc: Array<number | null>, result) => {
acc.push(result[group].currentValue);
if (result[group]) {
acc.push(result[group].currentValue);
}
return acc;
}, []);

const alert = alertFactory(
`${group}`,
reason,
actionGroupId,
additionalContext,
evaluationValues
);
const alertUuid = getAlertUuid(group);
scheduledActionsCount++;

alert.scheduleActions(actionGroupId, {
const alertContext = {
alertDetailsUrl: getAlertDetailsUrl(libs.basePath, spaceId, alertUuid),
alertState: stateToAlertMessage[nextState],
group,
Expand Down Expand Up @@ -342,7 +344,18 @@ export const createMetricThresholdExecutor = (libs: InfraBackendLibs) =>
}),
viewInAppUrl: getViewInMetricsAppUrl(libs.basePath, spaceId),
...additionalContext,
});
};

const alert = alertFactory(
`${group}`,
reason,
actionGroupId,
alertContext,
additionalContext,
evaluationValues
);
scheduledActionsCount++;
alert.scheduleActions(actionGroupId, alertContext);
}
}

Expand Down

0 comments on commit 3c2101a

Please sign in to comment.