Skip to content

Commit

Permalink
[Monitoring] update SM rule template variables (#104176) (#104534)
Browse files Browse the repository at this point in the history
* continue to support shardIndices template variable

* add unit test for large shard size alert

* change template variables in CCR rule

* add CCR rule unit test

* fix internationalization

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
neptunian and kibanamachine authored Jul 6, 2021
1 parent 805b834 commit 71e1e96
Show file tree
Hide file tree
Showing 6 changed files with 409 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { CCRReadExceptionsAlert } from './ccr_read_exceptions_alert';
import { ALERT_CCR_READ_EXCEPTIONS } from '../../common/constants';
import { fetchCCRReadExceptions } from '../lib/alerts/fetch_ccr_read_exceptions';
import { fetchClusters } from '../lib/alerts/fetch_clusters';
import { elasticsearchServiceMock } from 'src/core/server/mocks';

type ICCRReadExceptionsAlertMock = CCRReadExceptionsAlert & {
defaultParams: {
duration: string;
};
} & {
actionVariables: Array<{
name: string;
description: string;
}>;
};

const RealDate = Date;

jest.mock('../lib/alerts/fetch_ccr_read_exceptions', () => ({
fetchCCRReadExceptions: jest.fn(),
}));
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 },
metricbeat: { index: 'metricbeat-*' },
container: { elasticsearch: { enabled: false } },
},
},
},
},
}));

describe('CCRReadExceptionsAlert', () => {
it('should have defaults', () => {
const alert = new CCRReadExceptionsAlert() as ICCRReadExceptionsAlertMock;
expect(alert.alertOptions.id).toBe(ALERT_CCR_READ_EXCEPTIONS);
expect(alert.alertOptions.name).toBe('CCR read exceptions');
expect(alert.alertOptions.throttle).toBe('6h');
expect(alert.alertOptions.defaultParams).toStrictEqual({
duration: '1h',
});
expect(alert.alertOptions.actionVariables).toStrictEqual([
{
name: 'remoteCluster',
description: 'The remote cluster experiencing CCR read exceptions.',
},
{
name: 'followerIndex',
description: 'The follower index reporting CCR read exceptions.',
},
{
name: 'internalShortMessage',
description: 'The short internal message generated by Elastic.',
},
{
name: 'internalFullMessage',
description: 'The full internal message generated by Elastic.',
},
{ name: 'state', description: 'The current state of the alert.' },
{ name: 'clusterName', description: 'The cluster to which the node(s) belongs.' },
{ name: 'action', description: 'The recommended action for this alert.' },
{
name: 'actionPlain',
description: 'The recommended action for this alert, without any markdown.',
},
]);
});
describe('execute', () => {
const FakeDate = function () {};
FakeDate.prototype.valueOf = () => 1;

const clusterUuid = 'abc123';
const clusterName = 'testCluster';
const nodeId = 'myNodeId';
const nodeName = 'myNodeName';
const remoteCluster = 'BcK-0pmsQniyPQfZuauuXw_remote_cluster_1';
const followerIndex = '.follower_index_1';
const leaderIndex = '.leader_index_1';
const readExceptions = [
{
exception: {
type: 'read_exceptions_type_1',
reason: 'read_exceptions_reason_1',
},
},
];
const stat = {
remoteCluster,
followerIndex,
leaderIndex,
read_exceptions: readExceptions,
clusterUuid,
nodeId,
nodeName,
};

const replaceState = jest.fn();
const scheduleActions = jest.fn();
const getState = jest.fn();
const executorOptions = {
services: {
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient(),
alertInstanceFactory: jest.fn().mockImplementation(() => {
return {
replaceState,
scheduleActions,
getState,
};
}),
},
state: {},
};

beforeEach(() => {
Date = FakeDate as DateConstructor;
(fetchCCRReadExceptions as jest.Mock).mockImplementation(() => {
return [stat];
});
(fetchClusters as jest.Mock).mockImplementation(() => {
return [{ clusterUuid, clusterName }];
});
});

afterEach(() => {
Date = RealDate;
replaceState.mockReset();
scheduleActions.mockReset();
getState.mockReset();
});

it('should fire actions', async () => {
const alert = new CCRReadExceptionsAlert() as ICCRReadExceptionsAlertMock;
const type = alert.getAlertType();
await type.executor({
...executorOptions,
params: alert.alertOptions.defaultParams,
} as any);
expect(scheduleActions).toHaveBeenCalledWith('default', {
internalFullMessage: `CCR read exceptions alert is firing for the following remote cluster: ${remoteCluster}. Current 'follower_index' index affected: ${followerIndex}. [View CCR stats](http://localhost:5601/app/monitoring#/elasticsearch/ccr?_g=(cluster_uuid:${clusterUuid}))`,
internalShortMessage: `CCR read exceptions alert is firing for the following remote cluster: ${remoteCluster}. Verify follower and leader index relationships on the affected remote cluster.`,
action: `[View CCR stats](http://localhost:5601/app/monitoring#/elasticsearch/ccr?_g=(cluster_uuid:${clusterUuid}))`,
actionPlain:
'Verify follower and leader index relationships on the affected remote cluster.',
clusterName,
state: 'firing',
remoteCluster,
remoteClusters: remoteCluster,
followerIndex,
followerIndices: followerIndex,
});
});

it('should handle ccs', async () => {
const ccs = 'testCluster';
(fetchCCRReadExceptions as jest.Mock).mockImplementation(() => {
return [
{
...stat,
ccs,
},
];
});
const alert = new CCRReadExceptionsAlert() as ICCRReadExceptionsAlertMock;
const type = alert.getAlertType();
await type.executor({
...executorOptions,
params: alert.alertOptions.defaultParams,
} as any);
expect(scheduleActions).toHaveBeenCalledWith('default', {
internalFullMessage: `CCR read exceptions alert is firing for the following remote cluster: ${remoteCluster}. Current 'follower_index' index affected: ${followerIndex}. [View CCR stats](http://localhost:5601/app/monitoring#/elasticsearch/ccr?_g=(cluster_uuid:${clusterUuid},ccs:testCluster))`,
internalShortMessage: `CCR read exceptions alert is firing for the following remote cluster: ${remoteCluster}. Verify follower and leader index relationships on the affected remote cluster.`,
action: `[View CCR stats](http://localhost:5601/app/monitoring#/elasticsearch/ccr?_g=(cluster_uuid:${clusterUuid},ccs:testCluster))`,
actionPlain:
'Verify follower and leader index relationships on the affected remote cluster.',
clusterName,
state: 'firing',
remoteCluster,
remoteClusters: remoteCluster,
followerIndex,
followerIndices: followerIndex,
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@ export class CCRReadExceptionsAlert extends BaseAlert {
},
actionVariables: [
{
name: 'remoteClusters',
name: 'remoteCluster',
description: i18n.translate(
'xpack.monitoring.alerts.ccrReadExceptions.actionVariables.remoteClusters',
'xpack.monitoring.alerts.ccrReadExceptions.actionVariables.remoteCluster',
{
defaultMessage: 'List of remote clusters that are experiencing CCR read exceptions.',
defaultMessage: 'The remote cluster experiencing CCR read exceptions.',
}
),
},
{
name: 'followerIndices',
name: 'followerIndex',
description: i18n.translate(
'xpack.monitoring.alerts.ccrReadExceptions.actionVariables.followerIndices',
'xpack.monitoring.alerts.ccrReadExceptions.actionVariables.followerIndex',
{
defaultMessage: 'List of follower indices reporting CCR read exceptions.',
defaultMessage: 'The follower index reporting CCR read exceptions.',
}
),
},
Expand Down Expand Up @@ -229,12 +229,11 @@ export class CCRReadExceptionsAlert extends BaseAlert {
item: AlertData | null,
cluster: AlertCluster
) {
const remoteClustersList = alertStates
.map((alertState) => (alertState.meta as CCRReadExceptionsUIMeta).remoteCluster)
.join(', ');
const followerIndicesList = alertStates
.map((alertState) => (alertState.meta as CCRReadExceptionsUIMeta).followerIndex)
.join(', ');
if (alertStates.length === 0) {
return;
}
const CCRReadExceptionsMeta = alertStates[0].meta as CCRReadExceptionsUIMeta;
const { remoteCluster, followerIndex } = CCRReadExceptionsMeta;

const shortActionText = i18n.translate(
'xpack.monitoring.alerts.ccrReadExceptions.shortAction',
Expand All @@ -258,21 +257,21 @@ export class CCRReadExceptionsAlert extends BaseAlert {
const internalShortMessage = i18n.translate(
'xpack.monitoring.alerts.ccrReadExceptions.firing.internalShortMessage',
{
defaultMessage: `CCR read exceptions alert is firing for the following remote cluster: {remoteClustersList}. {shortActionText}`,
defaultMessage: `CCR read exceptions alert is firing for the following remote cluster: {remoteCluster}. {shortActionText}`,
values: {
remoteClustersList,
remoteCluster,
shortActionText,
},
}
);
const internalFullMessage = i18n.translate(
'xpack.monitoring.alerts.ccrReadExceptions.firing.internalFullMessage',
{
defaultMessage: `CCR read exceptions alert is firing for the following remote cluster: {remoteClustersList}. Current 'follower_index' index affected: {followerIndicesList}. {action}`,
defaultMessage: `CCR read exceptions alert is firing for the following remote cluster: {remoteCluster}. Current 'follower_index' index affected: {followerIndex}. {action}`,
values: {
action,
remoteClustersList,
followerIndicesList,
remoteCluster,
followerIndex,
},
}
);
Expand All @@ -281,8 +280,14 @@ export class CCRReadExceptionsAlert extends BaseAlert {
internalShortMessage,
internalFullMessage,
state: AlertingDefaults.ALERT_STATE.firing,
remoteClusters: remoteClustersList,
followerIndices: followerIndicesList,
remoteCluster,
followerIndex,
/* continue to send "remoteClusters" and "followerIndices" values for users still using it though
we have replaced it with "remoteCluster" and "followerIndex" in the template due to alerts per index instead of all indices
see https://github.com/elastic/kibana/issues/100136#issuecomment-865229431
*/
remoteClusters: remoteCluster,
followerIndices: followerIndex,
clusterName: cluster.clusterName,
action,
actionPlain: shortActionText,
Expand Down
Loading

0 comments on commit 71e1e96

Please sign in to comment.