-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
805b834
commit 71e1e96
Showing
6 changed files
with
409 additions
and
27 deletions.
There are no files selected for viewing
201 changes: 201 additions & 0 deletions
201
x-pack/plugins/monitoring/server/alerts/ccr_read_exceptions_alert.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.