From c2ac78099a588d9393dde3f3f7124ef8a2f97632 Mon Sep 17 00:00:00 2001 From: neptunian Date: Mon, 21 Jun 2021 15:22:48 -0400 Subject: [PATCH] update cpu usage action messaging --- .../server/alerts/cpu_usage_alert.test.ts | 23 ++-- .../server/alerts/cpu_usage_alert.ts | 109 +++++++++--------- 2 files changed, 67 insertions(+), 65 deletions(-) diff --git a/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.test.ts b/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.test.ts index be10ba15d2674eb..508f2d8e23ebff2 100644 --- a/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.test.ts +++ b/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.test.ts @@ -19,11 +19,11 @@ jest.mock('../lib/alerts/fetch_cpu_usage_node_stats', () => ({ jest.mock('../lib/alerts/fetch_clusters', () => ({ fetchClusters: jest.fn(), })); - jest.mock('../static_globals', () => ({ Globals: { app: { getLogger: () => ({ debug: jest.fn() }), + url: 'http://localhost:5601', config: { ui: { ccs: { enabled: true }, @@ -43,8 +43,7 @@ describe('CpuUsageAlert', () => { expect(alert.alertOptions.throttle).toBe('1d'); expect(alert.alertOptions.defaultParams).toStrictEqual({ threshold: 85, duration: '5m' }); expect(alert.alertOptions.actionVariables).toStrictEqual([ - { name: 'nodes', description: 'The list of nodes reporting high cpu usage.' }, - { name: 'count', description: 'The number of nodes reporting high cpu usage.' }, + { name: 'node', description: 'The node reporting high cpu usage.' }, { name: 'internalShortMessage', description: 'The short internal message generated by Elastic.', @@ -192,13 +191,14 @@ describe('CpuUsageAlert', () => { ], }); expect(scheduleActions).toHaveBeenCalledWith('default', { - internalFullMessage: `CPU usage alert is firing for ${count} node(s) in cluster: ${clusterName}. [View nodes](elasticsearch/nodes)`, - internalShortMessage: `CPU usage alert is firing for ${count} node(s) in cluster: ${clusterName}. Verify CPU levels across affected nodes.`, - action: `[View nodes](elasticsearch/nodes)`, - actionPlain: 'Verify CPU levels across affected nodes.', + internalFullMessage: `CPU usage alert is firing for node ${nodeName} in cluster: ${clusterName}. [View node](http://localhost:5601/app/monitoring#/elasticsearch/nodes/${nodeId}?_g=(cluster_uuid:${clusterUuid}))`, + internalShortMessage: `CPU usage alert is firing for node ${nodeName} in cluster: ${clusterName}. Verify CPU level of node.`, + action: `[View node](http://localhost:5601/app/monitoring#/elasticsearch/nodes/${nodeId}?_g=(cluster_uuid:${clusterUuid}))`, + actionPlain: 'Verify CPU level of node.', clusterName, count, nodes: `${nodeName}:${cpuUsage}`, + node: `${nodeName}:${cpuUsage}`, state: 'firing', }); }); @@ -242,13 +242,14 @@ describe('CpuUsageAlert', () => { } as any); const count = 1; expect(scheduleActions).toHaveBeenCalledWith('default', { - internalFullMessage: `CPU usage alert is firing for ${count} node(s) in cluster: ${clusterName}. [View nodes](elasticsearch/nodes)`, - internalShortMessage: `CPU usage alert is firing for ${count} node(s) in cluster: ${clusterName}. Verify CPU levels across affected nodes.`, - action: `[View nodes](elasticsearch/nodes)`, - actionPlain: 'Verify CPU levels across affected nodes.', + internalFullMessage: `CPU usage alert is firing for node ${nodeName} in cluster: ${clusterName}. [View node](http://localhost:5601/app/monitoring#/elasticsearch/nodes/${nodeId}?_g=(cluster_uuid:${clusterUuid},ccs:${ccs}))`, + internalShortMessage: `CPU usage alert is firing for node ${nodeName} in cluster: ${clusterName}. Verify CPU level of node.`, + action: `[View node](http://localhost:5601/app/monitoring#/elasticsearch/nodes/${nodeId}?_g=(cluster_uuid:${clusterUuid},ccs:testCluster))`, + actionPlain: 'Verify CPU level of node.', clusterName, count, nodes: `${nodeName}:${cpuUsage}`, + node: `${nodeName}:${cpuUsage}`, state: 'firing', }); }); diff --git a/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.ts b/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.ts index 438d350d366f84c..897571ebe3ebf38 100644 --- a/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.ts +++ b/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.ts @@ -51,15 +51,9 @@ export class CpuUsageAlert extends BaseAlert { }, actionVariables: [ { - name: 'nodes', - description: i18n.translate('xpack.monitoring.alerts.cpuUsage.actionVariables.nodes', { - defaultMessage: 'The list of nodes reporting high cpu usage.', - }), - }, - { - name: 'count', - description: i18n.translate('xpack.monitoring.alerts.cpuUsage.actionVariables.count', { - defaultMessage: 'The number of nodes reporting high cpu usage.', + name: 'node', + description: i18n.translate('xpack.monitoring.alerts.cpuUsage.actionVariables.node', { + defaultMessage: 'The node reporting high cpu usage.', }), }, ...Object.values(AlertingDefaults.ALERT_TYPE.context), @@ -170,51 +164,58 @@ export class CpuUsageAlert extends BaseAlert { if (alertStates.length === 0) { return; } - - const firingNodes = alertStates.filter( - (alertState) => alertState.ui.isFiring - ) as AlertCpuUsageState[]; - const firingCount = firingNodes.length; - if (firingCount > 0) { - const shortActionText = i18n.translate('xpack.monitoring.alerts.cpuUsage.shortAction', { - defaultMessage: 'Verify CPU levels across affected nodes.', - }); - const fullActionText = i18n.translate('xpack.monitoring.alerts.cpuUsage.fullAction', { - defaultMessage: 'View nodes', - }); - const action = `[${fullActionText}](elasticsearch/nodes)`; - const internalShortMessage = i18n.translate( - 'xpack.monitoring.alerts.cpuUsage.firing.internalShortMessage', - { - defaultMessage: `CPU usage alert is firing for {count} node(s) in cluster: {clusterName}. {shortActionText}`, - values: { - count: firingCount, - clusterName: cluster.clusterName, - shortActionText, - }, - } - ); - const internalFullMessage = i18n.translate( - 'xpack.monitoring.alerts.cpuUsage.firing.internalFullMessage', - { - defaultMessage: `CPU usage alert is firing for {count} node(s) in cluster: {clusterName}. {action}`, - values: { - count: firingCount, - clusterName: cluster.clusterName, - action, - }, - } - ); - instance.scheduleActions('default', { - internalShortMessage, - internalFullMessage: Globals.app.isCloud ? internalShortMessage : internalFullMessage, - state: AlertingDefaults.ALERT_STATE.firing, - nodes: firingNodes.map(({ nodeName, cpuUsage }) => `${nodeName}:${cpuUsage}`).toString(), - count: firingCount, - clusterName: cluster.clusterName, - action, - actionPlain: shortActionText, - }); + const firingNode = alertStates[0] as AlertCpuUsageState; + if (!firingNode || !firingNode.ui.isFiring) { + return; } + const shortActionText = i18n.translate('xpack.monitoring.alerts.cpuUsage.shortAction', { + defaultMessage: 'Verify CPU level of node.', + }); + const fullActionText = i18n.translate('xpack.monitoring.alerts.cpuUsage.fullAction', { + defaultMessage: 'View node', + }); + const ccs = firingNode.ccs; + const globalStateLink = this.createGlobalStateLink( + `elasticsearch/nodes/${firingNode.nodeId}`, + cluster.clusterUuid, + ccs + ); + const action = `[${fullActionText}](${globalStateLink})`; + const internalShortMessage = i18n.translate( + 'xpack.monitoring.alerts.cpuUsage.firing.internalShortMessage', + { + defaultMessage: `CPU usage alert is firing for node {nodeName} in cluster: {clusterName}. {shortActionText}`, + values: { + clusterName: cluster.clusterName, + nodeName: firingNode.nodeName, + shortActionText, + }, + } + ); + const internalFullMessage = i18n.translate( + 'xpack.monitoring.alerts.cpuUsage.firing.internalFullMessage', + { + defaultMessage: `CPU usage alert is firing for node {nodeName} in cluster: {clusterName}. {action}`, + values: { + clusterName: cluster.clusterName, + nodeName: firingNode.nodeName, + action, + }, + } + ); + instance.scheduleActions('default', { + internalShortMessage, + internalFullMessage: Globals.app.isCloud ? internalShortMessage : internalFullMessage, + state: AlertingDefaults.ALERT_STATE.firing, + /* continue to send "node" and "count" values for users before https://github.com/elastic/kibana/pull/102544 + see https://github.com/elastic/kibana/issues/100136#issuecomment-865229431 + */ + nodes: `${firingNode.nodeName}:${firingNode.cpuUsage}`, + count: 1, + node: `${firingNode.nodeName}:${firingNode.cpuUsage}`, + clusterName: cluster.clusterName, + action, + actionPlain: shortActionText, + }); } }