Skip to content

Commit

Permalink
[Infra UI] Disable Infrastructure and Metrics alerts in Serverless
Browse files Browse the repository at this point in the history
  • Loading branch information
mykolaharmash committed Oct 6, 2023
1 parent 866edd2 commit f11efeb
Show file tree
Hide file tree
Showing 35 changed files with 231 additions and 1,652 deletions.
24 changes: 8 additions & 16 deletions test/plugin_functional/test_suites/core_plugins/rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,25 +268,17 @@ export default function ({ getService }: PluginFunctionalProviderContext) {
'xpack.index_management.enableIndexStats (any)',
'xpack.infra.sources.default.fields.message (array)',
/**
* xpack.infra.featureFlags.customThresholdAlertsEnabled is conditional based on traditional/serverless offering
* and will resolve to (boolean)
*/
'xpack.infra.featureFlags.customThresholdAlertsEnabled (any)',
/**
* xpack.infra.featureFlags.logsUIEnabled is conditional based on traditional/serverless offering
* and will resolve to (boolean)
*/
'xpack.infra.featureFlags.logsUIEnabled (any)',
/**
* xpack.infra.featureFlags.metricsExplorerEnabled is conditional based on traditional/serverless offering
* and will resolve to (boolean)
* Feature flags bellow are conditional based on traditional/serverless offering
* and will all resolve to xpack.infra.featureFlags.* (boolean)
*/
'xpack.infra.featureFlags.metricsExplorerEnabled (any)',
/**
* xpack.infra.featureFlags.osqueryEnabled is conditional based on traditional/serverless offering
* and will resolve to (boolean)
*/
'xpack.infra.featureFlags.customThresholdAlertsEnabled (any)',
'xpack.infra.featureFlags.osqueryEnabled (any)',
'xpack.infra.featureFlags.inventoryThresholdAlertRuleEnabled (any)',
'xpack.infra.featureFlags.metricThresholdAlertRuleEnabled (any)',
'xpack.infra.featureFlags.logThresholdAlertRuleEnabled (any)',
'xpack.infra.featureFlags.logsUIEnabled (any)',

'xpack.license_management.ui.enabled (boolean)',
'xpack.maps.preserveDrawingBuffer (boolean)',
'xpack.maps.showMapsInspectorAdapter (boolean)',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ describe('rule_type_registry_deprecated_consumers', () => {
"siem.newTermsRule",
"siem.notifications",
"slo.rules.burnRate",
"metrics.alert.anomaly",
"logs.alert.document.count",
"metrics.alert.inventory.threshold",
"metrics.alert.threshold",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const ruleTypeIdWithValidLegacyConsumers: Record<string, string[]> = {
'siem.newTermsRule': [ALERTS_FEATURE_ID],
'siem.notifications': [ALERTS_FEATURE_ID],
'slo.rules.burnRate': [ALERTS_FEATURE_ID],
'metrics.alert.anomaly': [ALERTS_FEATURE_ID],
'logs.alert.document.count': [ALERTS_FEATURE_ID],
'metrics.alert.inventory.threshold': [ALERTS_FEATURE_ID],
'metrics.alert.threshold': [ALERTS_FEATURE_ID],
Expand Down
3 changes: 0 additions & 3 deletions x-pack/plugins/infra/common/alerting/metrics/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@ import { InventoryItemType, SnapshotMetricType } from '../../inventory_models/ty

export const METRIC_THRESHOLD_ALERT_TYPE_ID = 'metrics.alert.threshold';
export const METRIC_INVENTORY_THRESHOLD_ALERT_TYPE_ID = 'metrics.alert.inventory.threshold';
export const METRIC_ANOMALY_ALERT_TYPE_ID = 'metrics.alert.anomaly';

export enum InfraRuleType {
MetricThreshold = 'metrics.alert.threshold',
InventoryThreshold = 'metrics.alert.inventory.threshold',
Anomaly = 'metrics.alert.anomaly',
}

export interface InfraRuleTypeParams {
[InfraRuleType.MetricThreshold]: MetricThresholdParams;
[InfraRuleType.InventoryThreshold]: InventoryMetricConditions;
[InfraRuleType.Anomaly]: MetricAnomalyParams;
}

export enum Comparator {
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/infra/common/plugin_config_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export interface InfraConfig {
logsUIEnabled: boolean;
metricsExplorerEnabled: boolean;
osqueryEnabled: boolean;
inventoryThresholdAlertRuleEnabled: boolean;
metricThresholdAlertRuleEnabled: boolean;
logThresholdAlertRuleEnabled: boolean;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@

import { i18n } from '@kbn/i18n';
import React, { useState, useCallback, useMemo } from 'react';
import {
EuiPopover,
EuiHeaderLink,
EuiContextMenu,
import { EuiPopover, EuiHeaderLink, EuiContextMenu } from '@elastic/eui';
import type {
EuiContextMenuPanelDescriptor,
EuiContextMenuPanelItemDescriptor,
} from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n-react';
import { useKibana } from '@kbn/kibana-react-plugin/public';
Expand All @@ -23,6 +22,118 @@ import { InfraClientStartDeps } from '../../../types';

type VisibleFlyoutType = 'inventory' | 'metricThreshold' | 'customThreshold';

interface ContextMenuEntries {
items: EuiContextMenuPanelItemDescriptor[];
panels: EuiContextMenuPanelDescriptor[];
}

function useInfrastructureMenu(
onCreateRuleClick: (flyoutType: VisibleFlyoutType) => void
): ContextMenuEntries {
const { featureFlags } = usePluginConfig();

return useMemo(() => {
if (!featureFlags.inventoryThresholdAlertRuleEnabled) {
return { items: [], panels: [] };
}

return {
items: [
{
'data-test-subj': 'inventory-alerts-menu-option',
name: i18n.translate('xpack.infra.alerting.infrastructureDropdownMenu', {
defaultMessage: 'Infrastructure',
}),
panel: 1,
},
],
panels: [
{
id: 1,
title: i18n.translate('xpack.infra.alerting.infrastructureDropdownTitle', {
defaultMessage: 'Infrastructure rules',
}),
items: [
{
'data-test-subj': 'inventory-alerts-create-rule',
name: i18n.translate('xpack.infra.alerting.createInventoryRuleButton', {
defaultMessage: 'Create inventory rule',
}),
onClick: () => onCreateRuleClick('inventory'),
},
],
},
],
};
}, [featureFlags.inventoryThresholdAlertRuleEnabled, onCreateRuleClick]);
}

function useMetricsMenu(
onCreateRuleClick: (flyoutType: VisibleFlyoutType) => void
): ContextMenuEntries {
const { featureFlags } = usePluginConfig();

return useMemo(() => {
if (!featureFlags.metricThresholdAlertRuleEnabled) {
return { items: [], panels: [] };
}

return {
items: [
{
'data-test-subj': 'metrics-threshold-alerts-menu-option',
name: i18n.translate('xpack.infra.alerting.metricsDropdownMenu', {
defaultMessage: 'Metrics',
}),
panel: 2,
},
],
panels: [
{
id: 2,
title: i18n.translate('xpack.infra.alerting.metricsDropdownTitle', {
defaultMessage: 'Metrics rules',
}),
items: [
{
'data-test-subj': 'metrics-threshold-alerts-create-rule',
name: i18n.translate('xpack.infra.alerting.createThresholdRuleButton', {
defaultMessage: 'Create threshold rule',
}),
onClick: () => onCreateRuleClick('metricThreshold'),
},
],
},
],
};
}, [featureFlags.metricThresholdAlertRuleEnabled, onCreateRuleClick]);
}

function useCustomThresholdMenu(
onCreateRuleClick: (flyoutType: VisibleFlyoutType) => void
): ContextMenuEntries {
const { featureFlags } = usePluginConfig();

return useMemo(() => {
if (!featureFlags.customThresholdAlertsEnabled) {
return { items: [], panels: [] };
}

return {
items: [
{
'data-test-subj': 'custom-threshold-alerts-menu-option',
name: i18n.translate('xpack.infra.alerting.customThresholdDropdownMenu', {
defaultMessage: 'Create custom threshold rule',
}),
onClick: () => onCreateRuleClick('customThreshold'),
},
],
panels: [],
};
}, [featureFlags.customThresholdAlertsEnabled, onCreateRuleClick]);
}

export const MetricsAlertDropdown = () => {
const [popoverOpen, setPopoverOpen] = useState(false);
const [visibleFlyoutType, setVisibleFlyoutType] = useState<VisibleFlyoutType | null>(null);
Expand All @@ -34,8 +145,6 @@ export const MetricsAlertDropdown = () => {
() => Boolean(uiCapabilities?.infrastructure?.save),
[uiCapabilities]
);
const { featureFlags } = usePluginConfig();

const closeFlyout = useCallback(() => setVisibleFlyoutType(null), [setVisibleFlyoutType]);

const closePopover = useCallback(() => {
Expand All @@ -45,50 +154,19 @@ export const MetricsAlertDropdown = () => {
const togglePopover = useCallback(() => {
setPopoverOpen(!popoverOpen);
}, [setPopoverOpen, popoverOpen]);
const infrastructureAlertsPanel = useMemo(
() => ({
id: 1,
title: i18n.translate('xpack.infra.alerting.infrastructureDropdownTitle', {
defaultMessage: 'Infrastructure rules',
}),
items: [
{
'data-test-subj': 'inventory-alerts-create-rule',
name: i18n.translate('xpack.infra.alerting.createInventoryRuleButton', {
defaultMessage: 'Create inventory rule',
}),
onClick: () => {
closePopover();
setVisibleFlyoutType('inventory');
},
},
],
}),
[setVisibleFlyoutType, closePopover]
);

const metricsAlertsPanel = useMemo(
() => ({
id: 2,
title: i18n.translate('xpack.infra.alerting.metricsDropdownTitle', {
defaultMessage: 'Metrics rules',
}),
items: [
{
'data-test-subj': 'metrics-threshold-alerts-create-rule',
name: i18n.translate('xpack.infra.alerting.createThresholdRuleButton', {
defaultMessage: 'Create threshold rule',
}),
onClick: () => {
closePopover();
setVisibleFlyoutType('metricThreshold');
},
},
],
}),
[setVisibleFlyoutType, closePopover]
const onCreateRuleClick = useCallback(
(flyoutType: VisibleFlyoutType) => {
closePopover();
setVisibleFlyoutType(flyoutType);
},
[closePopover]
);

const infrastructureMenu = useInfrastructureMenu(onCreateRuleClick);
const metricsMenu = useMetricsMenu(onCreateRuleClick);
const customThresholdMenu = useCustomThresholdMenu(onCreateRuleClick);

const manageRulesLinkProps = observability.useRulesLink();

const manageAlertsMenuItem = useMemo(
Expand All @@ -102,56 +180,27 @@ export const MetricsAlertDropdown = () => {
[manageRulesLinkProps]
);

const firstPanelMenuItems: EuiContextMenuPanelDescriptor['items'] = useMemo(
() =>
canCreateAlerts
? [
{
'data-test-subj': 'inventory-alerts-menu-option',
name: i18n.translate('xpack.infra.alerting.infrastructureDropdownMenu', {
defaultMessage: 'Infrastructure',
}),
panel: 1,
},
{
'data-test-subj': 'metrics-threshold-alerts-menu-option',
name: i18n.translate('xpack.infra.alerting.metricsDropdownMenu', {
defaultMessage: 'Metrics',
}),
panel: 2,
},
...(featureFlags.customThresholdAlertsEnabled
? [
{
'data-test-subj': 'custom-threshold-alerts-menu-option',
name: i18n.translate('xpack.infra.alerting.customThresholdDropdownMenu', {
defaultMessage: 'Create custom threshold rule',
}),
onClick: () => {
closePopover();
setVisibleFlyoutType('customThreshold');
},
},
]
: []),
manageAlertsMenuItem,
]
: [manageAlertsMenuItem],
[canCreateAlerts, closePopover, featureFlags.customThresholdAlertsEnabled, manageAlertsMenuItem]
);

const panels: EuiContextMenuPanelDescriptor[] = useMemo(
() =>
[
{
id: 0,
title: i18n.translate('xpack.infra.alerting.alertDropdownTitle', {
defaultMessage: 'Alerts and rules',
}),
items: firstPanelMenuItems,
},
].concat(canCreateAlerts ? [infrastructureAlertsPanel, metricsAlertsPanel] : []),
[infrastructureAlertsPanel, metricsAlertsPanel, firstPanelMenuItems, canCreateAlerts]
() => [
{
id: 0,
title: i18n.translate('xpack.infra.alerting.alertDropdownTitle', {
defaultMessage: 'Alerts and rules',
}),
items: canCreateAlerts
? [
...infrastructureMenu.items,
...metricsMenu.items,
...customThresholdMenu.items,
manageAlertsMenuItem,
]
: [manageAlertsMenuItem],
},
...(canCreateAlerts
? [...infrastructureMenu.panels, ...metricsMenu.panels, ...customThresholdMenu.panels]
: []),
],
[canCreateAlerts, infrastructureMenu, metricsMenu, customThresholdMenu, manageAlertsMenuItem]
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export function AlertFlyout({ onClose }: Props) {
onClose,
canChangeTrigger: false,
ruleTypeId: OBSERVABILITY_THRESHOLD_RULE_TYPE_ID,
metadata: {
currentOptions: {
/*
Setting the groupBy is currently required in custom threshold
rule for it to populate the rule with additional host context.
*/
groupBy: 'host.name',
},
},
});
}, [onClose, triggersActionsUI]);

Expand Down
Loading

0 comments on commit f11efeb

Please sign in to comment.