Skip to content

Commit

Permalink
update cpu usage action messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
neptunian committed Jun 21, 2021
1 parent 7099ff7 commit c2ac780
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 65 deletions.
23 changes: 12 additions & 11 deletions x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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.',
Expand Down Expand Up @@ -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',
});
});
Expand Down Expand Up @@ -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',
});
});
Expand Down
109 changes: 55 additions & 54 deletions x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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,
});
}
}

0 comments on commit c2ac780

Please sign in to comment.