-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Alerting] Preconfigured alert history index connector #94909
Merged
Merged
Changes from 60 commits
Commits
Show all changes
62 commits
Select commit
Hold shift + click to select a range
f055c2a
Adding preconfigured alert history index
ymao1 3ac2315
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 59146f6
Adding functions to build alert history document
ymao1 97d9815
Adding functions to build alert history document
ymao1 3ecfa2a
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 1b028a7
Moving index template creation to plugin start
ymao1 f2b34d6
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 b03e052
Adding unit tests
ymao1 61948fd
Adding unit tests
ymao1 3fa6846
Adding unit tests
ymao1 b9ad6c3
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 4b1b787
Simplifying
ymao1 957c333
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 4167d49
Revert "Merge branch 'master' of https://github.com/elastic/kibana in…
ymao1 9b4eda6
Reverting some changes
ymao1 6c30518
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 3a25517
Reverting some changes
ymao1 24f2f2d
Adding index override
ymao1 1ce3a36
Updating UI with index override
ymao1 22d5a59
Only allow indexOverride for preconfigured alert history connector
ymao1 2554887
Handling preconfigured connector id clashes
ymao1 2af2999
Cleanup
ymao1 ac4248d
UI unit tests
ymao1 92531a7
Fixing default schema shown in UI
ymao1 fd94545
Fixing functional tests
ymao1 c8b44ee
Adding functional test
ymao1 5a88ab5
Fixing functional tests
ymao1 f75a958
Adding docs and link to docs
ymao1 6d769e8
Adding config to docker allowlist
ymao1 7363eb3
Fixing wrong typescript operator
ymao1 c5060ee
Changing default for config to false
ymao1 0d4acb0
Cleanup
ymao1 56848f9
Adding note about index privileges to docs
ymao1 bbe5fd6
Fixing i18n
ymao1 85ccb96
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 7444b08
PR fixes
ymao1 776e8df
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 af35a12
PR fixes
ymao1 6f420b1
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 8bc04c3
Merge branch 'master' into alerting/default-es-index-schema
kibanamachine 07eb28d
PR fixes
ymao1 79b282c
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 f3fea18
PR fixes - wording
ymao1 19f3673
PR fixes
ymao1 dcbf451
Fixing unit and functional tests
ymao1 f689618
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 75281b0
Fixing types check
ymao1 030bf55
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 5aedddb
ES -> Elasticsearch
ymao1 75dcb88
Moving files
ymao1 095b495
Adding kibana- to beginning of prefix
ymao1 094aa60
Namespacing alert data within schema with kibana
ymao1 da82858
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 9e42c60
Fix i18n
ymao1 3f6c078
Updating docs
ymao1 55e75a5
Fixing unit tests
ymao1 38e5519
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 1a17d94
Fixing doc links
ymao1 8923efa
Merging in master
ymao1 45278e2
Fixing types check
ymao1 fa502cc
PR fixes
ymao1 b23fca9
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
122 changes: 122 additions & 0 deletions
122
x-pack/plugins/actions/common/alert_history_schema.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,122 @@ | ||
/* | ||
* 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 { buildAlertHistoryDocument } from './alert_history_schema'; | ||
|
||
function getVariables(overrides = {}) { | ||
return { | ||
date: '2021-01-01T00:00:00.000Z', | ||
rule: { | ||
id: 'rule-id', | ||
name: 'rule-name', | ||
type: 'rule-type', | ||
spaceId: 'space-id', | ||
}, | ||
context: { | ||
contextVar1: 'contextValue1', | ||
contextVar2: 'contextValue2', | ||
}, | ||
params: { | ||
ruleParam: 1, | ||
ruleParamString: 'another param', | ||
}, | ||
tags: ['abc', 'def'], | ||
alert: { | ||
id: 'alert-id', | ||
actionGroup: 'action-group-id', | ||
actionGroupName: 'Action Group', | ||
}, | ||
...overrides, | ||
}; | ||
} | ||
|
||
describe('buildAlertHistoryDocument', () => { | ||
it('handles empty variables', () => { | ||
expect(buildAlertHistoryDocument({})).toBeNull(); | ||
}); | ||
|
||
it('returns null if rule type is not defined', () => { | ||
expect(buildAlertHistoryDocument(getVariables({ rule: { type: undefined } }))).toBeNull(); | ||
}); | ||
|
||
it('returns null if alert variables are not defined', () => { | ||
expect(buildAlertHistoryDocument(getVariables({ alert: undefined }))).toBeNull(); | ||
}); | ||
|
||
it('returns null if rule variables are not defined', () => { | ||
expect(buildAlertHistoryDocument(getVariables({ rule: undefined }))).toBeNull(); | ||
}); | ||
|
||
it('includes @timestamp field if date is null', () => { | ||
const alertHistoryDoc = buildAlertHistoryDocument(getVariables({ date: undefined })); | ||
expect(alertHistoryDoc).not.toBeNull(); | ||
expect(alertHistoryDoc!['@timestamp']).toBeTruthy(); | ||
}); | ||
|
||
it(`doesn't include context if context is empty`, () => { | ||
const alertHistoryDoc = buildAlertHistoryDocument(getVariables({ context: {} })); | ||
expect(alertHistoryDoc).not.toBeNull(); | ||
expect(alertHistoryDoc!.kibana?.alert?.context).toBeFalsy(); | ||
}); | ||
|
||
it(`doesn't include params if params is empty`, () => { | ||
const alertHistoryDoc = buildAlertHistoryDocument(getVariables({ params: {} })); | ||
expect(alertHistoryDoc).not.toBeNull(); | ||
expect(alertHistoryDoc!.rule?.params).toBeFalsy(); | ||
}); | ||
|
||
it(`doesn't include tags if tags is empty array`, () => { | ||
const alertHistoryDoc = buildAlertHistoryDocument(getVariables({ tags: [] })); | ||
expect(alertHistoryDoc).not.toBeNull(); | ||
expect(alertHistoryDoc!.tags).toBeFalsy(); | ||
}); | ||
|
||
it(`included message if context contains message`, () => { | ||
const alertHistoryDoc = buildAlertHistoryDocument( | ||
getVariables({ | ||
context: { contextVar1: 'contextValue1', contextVar2: 'contextValue2', message: 'hello!' }, | ||
}) | ||
); | ||
expect(alertHistoryDoc).not.toBeNull(); | ||
expect(alertHistoryDoc!.message).toEqual('hello!'); | ||
}); | ||
|
||
it('builds alert history document from variables', () => { | ||
expect(buildAlertHistoryDocument(getVariables())).toEqual({ | ||
'@timestamp': '2021-01-01T00:00:00.000Z', | ||
kibana: { | ||
alert: { | ||
actionGroup: 'action-group-id', | ||
actionGroupName: 'Action Group', | ||
context: { | ||
'rule-type': { | ||
contextVar1: 'contextValue1', | ||
contextVar2: 'contextValue2', | ||
}, | ||
}, | ||
id: 'alert-id', | ||
}, | ||
}, | ||
event: { | ||
kind: 'alert', | ||
}, | ||
rule: { | ||
id: 'rule-id', | ||
name: 'rule-name', | ||
params: { | ||
'rule-type': { | ||
ruleParam: 1, | ||
ruleParamString: 'another param', | ||
}, | ||
}, | ||
space: 'space-id', | ||
type: 'rule-type', | ||
}, | ||
tags: ['abc', 'def'], | ||
}); | ||
}); | ||
}); |
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,90 @@ | ||
/* | ||
* 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 { isEmpty } from 'lodash'; | ||
|
||
export const ALERT_HISTORY_PREFIX = 'kibana-alert-history-'; | ||
export const AlertHistoryDefaultIndexName = `${ALERT_HISTORY_PREFIX}default`; | ||
export const AlertHistoryEsIndexConnectorId = 'preconfigured-alert-history-es-index'; | ||
|
||
export const buildAlertHistoryDocument = (variables: Record<string, unknown>) => { | ||
const { date, alert: alertVariables, context, params, tags, rule: ruleVariables } = variables as { | ||
date: string; | ||
alert: Record<string, unknown>; | ||
context: Record<string, unknown>; | ||
params: Record<string, unknown>; | ||
rule: Record<string, unknown>; | ||
tags: string[]; | ||
}; | ||
|
||
if (!alertVariables || !ruleVariables) { | ||
return null; | ||
} | ||
|
||
const { actionGroup, actionGroupName, id: alertId } = alertVariables as { | ||
actionGroup: string; | ||
actionGroupName: string; | ||
id: string; | ||
}; | ||
|
||
const { id: ruleId, name, spaceId, type } = ruleVariables as { | ||
id: string; | ||
name: string; | ||
spaceId: string; | ||
type: string; | ||
}; | ||
|
||
if (!type) { | ||
// can't build the document without a type | ||
return null; | ||
} | ||
|
||
const ruleType = type.replace(/\./g, '__'); | ||
|
||
const rule = { | ||
...(ruleId ? { id: ruleId } : {}), | ||
...(name ? { name } : {}), | ||
...(!isEmpty(params) ? { params: { [ruleType]: params } } : {}), | ||
...(spaceId ? { space: spaceId } : {}), | ||
...(type ? { type } : {}), | ||
}; | ||
const alert = { | ||
...(alertId ? { id: alertId } : {}), | ||
...(!isEmpty(context) ? { context: { [ruleType]: context } } : {}), | ||
...(actionGroup ? { actionGroup } : {}), | ||
...(actionGroupName ? { actionGroupName } : {}), | ||
}; | ||
|
||
const alertHistoryDoc = { | ||
'@timestamp': date ? date : new Date().toISOString(), | ||
...(tags && tags.length > 0 ? { tags } : {}), | ||
...(context?.message ? { message: context.message } : {}), | ||
...(!isEmpty(rule) ? { rule } : {}), | ||
...(!isEmpty(alert) ? { kibana: { alert } } : {}), | ||
}; | ||
|
||
return !isEmpty(alertHistoryDoc) ? { ...alertHistoryDoc, event: { kind: 'alert' } } : null; | ||
}; | ||
|
||
export const AlertHistoryDocumentTemplate = Object.freeze( | ||
buildAlertHistoryDocument({ | ||
rule: { | ||
id: '{{rule.id}}', | ||
name: '{{rule.name}}', | ||
type: '{{rule.type}}', | ||
spaceId: '{{rule.spaceId}}', | ||
}, | ||
context: '{{context}}', | ||
params: '{{params}}', | ||
tags: '{{rule.tags}}', | ||
alert: { | ||
id: '{{alert.id}}', | ||
actionGroup: '{{alert.actionGroup}}', | ||
actionGroupName: '{{alert.actionGroupName}}', | ||
}, | ||
}) | ||
); |
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
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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This title is a little hard to read. Is the word "preconfigured" needed?
Alert history {es} index connector