generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 106
Add alert insight to alerts card on overview page #1248
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
83f5cea
add alert insight to alerts card
Hailong-am b61d00e
fix ut
Hailong-am 9abff38
try to fix the cypress performance
Hailong-am d4dbf89
increase memory for cypress run
Hailong-am 6009a5e
support data source disabled case
Hailong-am 4c97323
cypress
Hailong-am 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 hidden or 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 hidden or 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,211 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import moment from 'moment'; | ||
| import { escape } from 'lodash'; | ||
| import { OPERATORS_PPL_QUERY_MAP } from '../../pages/CreateMonitor/containers/CreateMonitor/utils/whereFilters'; | ||
| import { | ||
| BUCKET_UNIT_PPL_UNIT_MAP, | ||
| DEFAULT_ACTIVE_ALERTS_AI_TOP_N, | ||
| DEFAULT_DSL_QUERY_DATE_FORMAT, | ||
| DEFAULT_LOG_PATTERN_SAMPLE_SIZE, | ||
| DEFAULT_LOG_PATTERN_TOP_N, | ||
| DEFAULT_PPL_QUERY_DATE_FORMAT, | ||
| PERIOD_END_PLACEHOLDER, | ||
| PPL_SEARCH_PATH, | ||
| } from '../../pages/Dashboard/utils/constants'; | ||
| import { MONITOR_TYPE, SEARCH_TYPE } from '../../utils/constants'; | ||
| import { getTime } from '../../pages/MonitorDetails/components/MonitorOverview/utils/getOverviewStats'; | ||
| import { | ||
| filterActiveAlerts, | ||
| findLongestStringField, | ||
| searchQuery, | ||
| } from '../../pages/Dashboard/utils/helpers'; | ||
| import { getApplication, getAssistantDashboards, getClient } from '../../services'; | ||
| import { dataSourceEnabled } from '../../pages/utils/helpers'; | ||
|
|
||
| export interface AlertInsightProps { | ||
| alert: any; | ||
| alertId: string; | ||
| isAgentConfigured: boolean; | ||
| children: React.ReactElement; | ||
| datasourceId?: string; | ||
| } | ||
|
|
||
| export const AlertInsight: React.FC<AlertInsightProps> = (props: AlertInsightProps) => { | ||
| const { alert, children, isAgentConfigured, alertId, datasourceId } = props; | ||
| const httpClient = getClient(); | ||
| const dataSourceQuery = dataSourceEnabled() | ||
| ? { query: { dataSourceId: datasourceId || '' } } | ||
| : undefined; | ||
|
|
||
| const contextProvider = async () => { | ||
| // 1. get monitor definition | ||
| const monitorResp = await httpClient.get( | ||
| `../api/alerting/monitors/${alert.monitor_id}`, | ||
| dataSourceQuery | ||
| ); | ||
| const monitorDefinition = monitorResp.resp; | ||
| // 2. If the monitor is created via visual editor, translate ui_metadata dsl filter to ppl filter | ||
| let formikToPPLFilters = []; | ||
| let pplBucketValue = 1; | ||
| let pplBucketUnitOfTime = 'HOUR'; | ||
| let pplTimeField = ''; | ||
| const isVisualEditorMonitor = | ||
| monitorDefinition?.ui_metadata?.search?.searchType === SEARCH_TYPE.GRAPH; | ||
| if (isVisualEditorMonitor) { | ||
| const uiFilters = monitorDefinition?.ui_metadata?.search?.filters || []; | ||
| formikToPPLFilters = uiFilters.map((filter) => | ||
| OPERATORS_PPL_QUERY_MAP[filter.operator].query(filter) | ||
| ); | ||
| pplBucketValue = monitorDefinition?.ui_metadata?.search?.bucketValue || 1; | ||
| pplBucketUnitOfTime = | ||
| BUCKET_UNIT_PPL_UNIT_MAP[monitorDefinition?.ui_metadata?.search?.bucketUnitOfTime] || | ||
| 'HOUR'; | ||
| pplTimeField = monitorDefinition?.ui_metadata?.search?.timeField; | ||
| } | ||
| delete monitorDefinition.ui_metadata; | ||
| delete monitorDefinition.data_sources; | ||
|
|
||
| // 3. get data triggers the alert and fetch log patterns | ||
| let monitorDefinitionStr = JSON.stringify(monitorDefinition); | ||
| let alertTriggeredByValue = ''; | ||
| let dsl = ''; | ||
| let index = ''; | ||
| let topNLogPatternData = ''; | ||
| if ( | ||
| monitorResp.resp.monitor_type === MONITOR_TYPE.QUERY_LEVEL || | ||
| monitorResp.resp.monitor_type === MONITOR_TYPE.BUCKET_LEVEL | ||
| ) { | ||
| // 3.1 preprocess index, only support first index use case | ||
| const search = monitorResp.resp.inputs[0].search; | ||
| index = String(search.indices).split(',')[0]?.trim() || ''; | ||
| // 3.2 preprocess dsl query with right time range | ||
| let query = JSON.stringify(search.query); | ||
| // Only keep the query part | ||
| dsl = JSON.stringify({ query: search.query.query }); | ||
| let latestAlertTriggerTime = ''; | ||
| if (query.indexOf(PERIOD_END_PLACEHOLDER) !== -1) { | ||
| query = query.replaceAll(PERIOD_END_PLACEHOLDER, alert.last_notification_time); | ||
| latestAlertTriggerTime = moment | ||
| .utc(alert.last_notification_time) | ||
| .format(DEFAULT_DSL_QUERY_DATE_FORMAT); | ||
| dsl = dsl.replaceAll(PERIOD_END_PLACEHOLDER, latestAlertTriggerTime); | ||
| // as we changed the format, remove it | ||
| dsl = dsl.replaceAll('"format":"epoch_millis",', ''); | ||
| monitorDefinitionStr = monitorDefinitionStr.replaceAll( | ||
| PERIOD_END_PLACEHOLDER, | ||
| getTime(alert.last_notification_time) // human-readable time format for summary | ||
| ); | ||
| // as we changed the format, remove it | ||
| monitorDefinitionStr = monitorDefinitionStr.replaceAll('"format":"epoch_millis",', ''); | ||
| } | ||
| // 3.3 preprocess ppl query base with concatenated filters | ||
| const pplAlertTriggerTime = moment | ||
| .utc(alert.last_notification_time) | ||
| .format(DEFAULT_PPL_QUERY_DATE_FORMAT); | ||
| const basePPL = | ||
| `source=${index} | ` + | ||
| `where ${pplTimeField} >= TIMESTAMPADD(${pplBucketUnitOfTime}, -${pplBucketValue}, '${pplAlertTriggerTime}') and ` + | ||
| `${pplTimeField} <= TIMESTAMP('${pplAlertTriggerTime}')`; | ||
| const basePPLWithFilters = formikToPPLFilters.reduce((acc, filter) => { | ||
| return `${acc} | where ${filter}`; | ||
| }, basePPL); | ||
| const firstSamplePPL = `${basePPLWithFilters} | head 1`; | ||
|
|
||
| if (index) { | ||
| // 3.4 dsl query result with aggregation results | ||
| const alertData = await searchQuery( | ||
| httpClient, | ||
| `${index}/_search`, | ||
| 'GET', | ||
| dataSourceQuery, | ||
| query | ||
| ); | ||
| alertTriggeredByValue = JSON.stringify( | ||
| alertData.body.aggregations?.metric?.value || alertData.body.hits.total.value | ||
| ); | ||
|
|
||
| if (isVisualEditorMonitor) { | ||
| // 3.5 find the log pattern field by longest length in the first sample data | ||
| const firstSampleData = await searchQuery( | ||
| httpClient, | ||
| PPL_SEARCH_PATH, | ||
| 'POST', | ||
| dataSourceQuery, | ||
| JSON.stringify({ query: firstSamplePPL }) | ||
| ); | ||
| const patternField = findLongestStringField(firstSampleData); | ||
|
|
||
| // 3.6 log pattern query to get top N log patterns | ||
| if (patternField) { | ||
| const topNLogPatternPPL = | ||
| `${basePPLWithFilters} | patterns ${patternField} | ` + | ||
| `stats count() as count, take(${patternField}, ${DEFAULT_LOG_PATTERN_SAMPLE_SIZE}) by patterns_field | ` + | ||
| `sort - count | head ${DEFAULT_LOG_PATTERN_TOP_N}`; | ||
| const logPatternData = await searchQuery( | ||
| httpClient, | ||
| PPL_SEARCH_PATH, | ||
| 'POST', | ||
| dataSourceQuery, | ||
| JSON.stringify({ query: topNLogPatternPPL }) | ||
| ); | ||
| topNLogPatternData = escape(JSON.stringify(logPatternData?.body?.datarows || '')); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // 3.6 only keep top N active alerts and replace time with human-readable timezone format | ||
| const activeAlerts = filterActiveAlerts(alert.alerts || [alert]) | ||
| .slice(0, DEFAULT_ACTIVE_ALERTS_AI_TOP_N) | ||
| .map((activeAlert) => ({ | ||
| ...activeAlert, | ||
| start_time: getTime(activeAlert.start_time), | ||
| last_notification_time: getTime(activeAlert.last_notification_time), | ||
| })); | ||
| // Reduce llm input token size by taking topN active alerts | ||
| const filteredAlert = { | ||
| ...alert, | ||
| alerts: activeAlerts, | ||
| start_time: getTime(alert.start_time), | ||
| last_notification_time: getTime(alert.last_notification_time), | ||
| }; | ||
|
|
||
| // 4. build the context | ||
| return { | ||
| context: ` | ||
| Here is the detail information about alert ${alert.trigger_name} | ||
| ### Monitor definition\n ${monitorDefinitionStr}\n | ||
| ### Active Alert\n ${JSON.stringify(filteredAlert)}\n | ||
| ### Value triggers this alert\n ${alertTriggeredByValue}\n | ||
| ### Alert query DSL ${dsl} \n`, | ||
| additionalInfo: { | ||
| monitorType: monitorResp.resp.monitor_type, | ||
| dsl, | ||
| index, | ||
| topNLogPatternData, | ||
| isVisualEditorMonitor, | ||
| }, | ||
| dataSourceId: dataSourceQuery?.query?.dataSourceId, | ||
| }; | ||
| }; | ||
|
|
||
| const assistantEnabled = getApplication().capabilities?.assistant?.enabled === true; | ||
| const assistantFeatureStatus = getAssistantDashboards().getFeatureStatus(); | ||
| if (assistantFeatureStatus.alertInsight && assistantEnabled && isAgentConfigured) { | ||
| getAssistantDashboards().registerIncontextInsight([ | ||
| { | ||
| key: alertId, | ||
| type: 'generate', | ||
| suggestions: [`Please summarize this alert`], | ||
| contextProvider, | ||
| }, | ||
| ]); | ||
|
|
||
| return getAssistantDashboards().renderIncontextInsight({ children }); | ||
| } | ||
| return children; | ||
| }; | ||
This file contains hidden or 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,8 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { AlertInsight } from './AlertInsight'; | ||
|
|
||
| export { AlertInsight }; |
This file contains hidden or 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 hidden or 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.
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.
For my own understanding, what's the process for other monitor types?
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.
Other monitor types like
per cluster metricsdon't have a query dsl associated with it, so we don't have the process to query data.we may have a follow up to review
Per documentandComposite monitormonitor types