[Actionable Observability] Add context.alertDetailsUrl to connector template for Logs threshold rule (#144623)

This commit is contained in:
Coen Warmer 2022-11-07 18:25:25 +01:00 committed by GitHub
parent c5bcfd6762
commit 8d5ba8706f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 28 deletions

View file

@ -22,6 +22,7 @@ import {
RuleExecutorServices,
RuleTypeState,
} from '@kbn/alerting-plugin/server';
import { addSpaceIdToPath } from '@kbn/spaces-plugin/common';
import {
RuleParams,
@ -47,7 +48,7 @@ import { decodeOrThrow } from '../../../../common/runtime_types';
import { getLogsAppAlertUrl } from '../../../../common/formatters/alert_link';
import { getIntervalInSeconds } from '../../../../common/utils/get_interval_in_seconds';
import { InfraBackendLibs } from '../../infra_types';
import { UNGROUPED_FACTORY_KEY } from '../common/utils';
import { getAlertDetailsUrl, UNGROUPED_FACTORY_KEY } from '../common/utils';
import {
getReasonMessageForGroupedCountAlert,
getReasonMessageForGroupedRatioAlert,
@ -101,13 +102,14 @@ export const createLogThresholdExecutor = (libs: InfraBackendLibs) =>
LogThresholdAlertState,
LogThresholdAlertContext,
LogThresholdActionGroups
>(async ({ services, params, startedAt }) => {
>(async ({ services, params, spaceId, startedAt }) => {
const {
alertFactory: { alertLimit },
alertWithLifecycle,
savedObjectsClient,
scopedClusterClient,
getAlertStartedDate,
getAlertUuid,
} = services;
const { basePath } = libs;
@ -124,17 +126,30 @@ export const createLogThresholdExecutor = (libs: InfraBackendLibs) =>
if (actions && actions.length > 0) {
const indexedStartedAt = getAlertStartedDate(id) ?? startedAt.toISOString();
const relativeViewInAppUrl = getLogsAppAlertUrl(new Date(indexedStartedAt).getTime());
const viewInAppUrl = basePath.publicBaseUrl
? new URL(basePath.prepend(relativeViewInAppUrl), basePath.publicBaseUrl).toString()
: relativeViewInAppUrl;
const viewInAppUrl = addSpaceIdToPath(
basePath.publicBaseUrl,
spaceId,
relativeViewInAppUrl
);
const sharedContext = {
timestamp: startedAt.toISOString(),
viewInAppUrl,
};
actions.forEach((actionSet) => {
const { actionGroup, context } = actionSet;
alert.scheduleActions(actionGroup, { ...sharedContext, ...context });
const alertInstanceId = (context.group || id) as string;
const alertUuid = getAlertUuid(alertInstanceId);
alert.scheduleActions(actionGroup, {
...sharedContext,
...context,
alertDetailsUrl: getAlertDetailsUrl(libs.basePath, spaceId, alertUuid),
});
});
}
@ -179,13 +194,15 @@ export const createLogThresholdExecutor = (libs: InfraBackendLibs) =>
const { getRecoveredAlerts } = services.alertFactory.done();
const recoveredAlerts = getRecoveredAlerts();
processRecoveredAlerts(
recoveredAlerts,
startedAt,
getAlertStartedDate,
processRecoveredAlerts({
basePath,
validatedParams
);
getAlertStartedDate,
getAlertUuid,
recoveredAlerts,
spaceId,
startedAt,
validatedParams,
});
} catch (e) {
throw new Error(e);
}
@ -876,22 +893,33 @@ type LogThresholdRecoveredAlert = {
getId: () => string;
} & LogThresholdAlert;
const processRecoveredAlerts = (
recoveredAlerts: LogThresholdRecoveredAlert[],
startedAt: Date,
getAlertStartedDate: (alertId: string) => string | null,
basePath: IBasePath,
validatedParams: RuleParams
) => {
const processRecoveredAlerts = ({
basePath,
getAlertStartedDate,
getAlertUuid,
recoveredAlerts,
spaceId,
startedAt,
validatedParams,
}: {
basePath: IBasePath;
getAlertStartedDate: (alertId: string) => string | null;
getAlertUuid: (alertId: string) => string | null;
recoveredAlerts: LogThresholdRecoveredAlert[];
spaceId: string;
startedAt: Date;
validatedParams: RuleParams;
}) => {
for (const alert of recoveredAlerts) {
const recoveredAlertId = alert.getId();
const indexedStartedAt = getAlertStartedDate(recoveredAlertId) ?? startedAt.toISOString();
const relativeViewInAppUrl = getLogsAppAlertUrl(new Date(indexedStartedAt).getTime());
const viewInAppUrl = basePath.publicBaseUrl
? new URL(basePath.prepend(relativeViewInAppUrl), basePath.publicBaseUrl).toString()
: relativeViewInAppUrl;
const alertUuid = getAlertUuid(recoveredAlertId);
const viewInAppUrl = addSpaceIdToPath(basePath.publicBaseUrl, spaceId, relativeViewInAppUrl);
const baseContext = {
alertDetailsUrl: getAlertDetailsUrl(basePath, spaceId, alertUuid),
group: hasGroupBy(validatedParams) ? recoveredAlertId : null,
timestamp: startedAt.toISOString(),
viewInAppUrl,
@ -899,21 +927,21 @@ const processRecoveredAlerts = (
if (isRatioRuleParams(validatedParams)) {
const { criteria } = validatedParams;
const context = {
alert.setContext({
...baseContext,
numeratorConditions: createConditionsMessageForCriteria(getNumerator(criteria)),
denominatorConditions: createConditionsMessageForCriteria(getDenominator(criteria)),
isRatio: true,
};
alert.setContext(context);
});
} else {
const { criteria } = validatedParams;
const context = {
alert.setContext({
...baseContext,
conditions: createConditionsMessageForCriteria(criteria),
isRatio: false,
};
alert.setContext(context);
});
}
}
};

View file

@ -14,6 +14,8 @@ import {
} from '../../../../common/alerting/logs/log_threshold';
import { InfraBackendLibs } from '../../infra_types';
import { decodeOrThrow } from '../../../../common/runtime_types';
import { getAlertDetailsPageEnabledForApp } from '../common/utils';
import { alertDetailUrlActionVariableDescription } from '../common/messages';
const timestampActionVariableDescription = i18n.translate(
'xpack.infra.logs.alerting.threshold.timestampActionVariableDescription',
@ -96,6 +98,8 @@ export async function registerLogThresholdRuleType(
);
}
const config = libs.getAlertDetailsConfig();
alertingPlugin.registerType({
id: LOG_DOCUMENT_COUNT_RULE_TYPE_ID,
name: i18n.translate('xpack.infra.logs.alertName', {
@ -127,6 +131,9 @@ export async function registerLogThresholdRuleType(
name: 'denominatorConditions',
description: denominatorConditionsActionVariableDescription,
},
...(getAlertDetailsPageEnabledForApp(config, 'logs')
? [{ name: 'alertDetailsUrl', description: alertDetailUrlActionVariableDescription }]
: []),
{
name: 'viewInAppUrl',
description: viewInAppUrlActionVariableDescription,