-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Metrics Alerts] Handle invalid KQL in filterQuery #119557
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
Changes from all commits
7339e3b
09e3569
acb90e2
f7087cd
2489138
95a5628
10e7bc1
1ee8a85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,11 +13,14 @@ import { | |
| } from '../../../../server/lib/alerting/inventory_metric_threshold/types'; | ||
| // eslint-disable-next-line @kbn/eslint/no-restricted-paths | ||
| import { ValidationResult } from '../../../../../triggers_actions_ui/public/types'; | ||
| import { QUERY_INVALID } from './expression'; | ||
|
|
||
| export function validateMetricThreshold({ | ||
| criteria, | ||
| filterQuery, | ||
| }: { | ||
| criteria: InventoryMetricConditions[]; | ||
| filterQuery?: string | symbol; | ||
| }): ValidationResult { | ||
| const validationResult = { errors: {} }; | ||
| const errors: { | ||
|
|
@@ -34,9 +37,17 @@ export function validateMetricThreshold({ | |
| }; | ||
| metric: string[]; | ||
| }; | ||
| } = {}; | ||
| } & { filterQuery?: string[] } = {}; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIt: Can this type live as part of the ValidationResult type? |
||
| validationResult.errors = errors; | ||
|
|
||
| if (filterQuery === QUERY_INVALID) { | ||
| errors.filterQuery = [ | ||
| i18n.translate('xpack.infra.metrics.alertFlyout.error.invalidFilterQuery', { | ||
| defaultMessage: 'Filter query is invalid.', | ||
| }), | ||
| ]; | ||
| } | ||
|
|
||
| if (!criteria || !criteria.length) { | ||
| return validationResult; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,7 @@ import { ExpressionChart } from './expression_chart'; | |
| import { useKibanaContextForPlugin } from '../../../hooks/use_kibana'; | ||
|
|
||
| const FILTER_TYPING_DEBOUNCE_MS = 500; | ||
| export const QUERY_INVALID = Symbol('QUERY_INVALID'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering if there is any benefit to having two symbols for this? |
||
|
|
||
| type Props = Omit< | ||
| AlertTypeParamsExpressionProps<AlertTypeParams & AlertParams, AlertContextMeta>, | ||
|
|
@@ -117,10 +118,14 @@ export const Expressions: React.FC<Props> = (props) => { | |
| const onFilterChange = useCallback( | ||
| (filter: any) => { | ||
| setAlertParams('filterQueryText', filter); | ||
| setAlertParams( | ||
| 'filterQuery', | ||
| convertKueryToElasticSearchQuery(filter, derivedIndexPattern) || '' | ||
| ); | ||
| try { | ||
| setAlertParams( | ||
| 'filterQuery', | ||
| convertKueryToElasticSearchQuery(filter, derivedIndexPattern, false) || '' | ||
| ); | ||
| } catch (e) { | ||
| setAlertParams('filterQuery', QUERY_INVALID); | ||
| } | ||
| }, | ||
| [setAlertParams, derivedIndexPattern] | ||
| ); | ||
|
|
@@ -281,15 +286,16 @@ export const Expressions: React.FC<Props> = (props) => { | |
| }, [alertParams.groupBy]); | ||
|
|
||
| const redundantFilterGroupBy = useMemo(() => { | ||
| if (!alertParams.filterQuery || !groupByFilterTestPatterns) return []; | ||
| const { filterQuery } = alertParams; | ||
| if (typeof filterQuery !== 'string' || !groupByFilterTestPatterns) return []; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I'd expect to compare filterQuery to QUERY_INVALID here instead (TypeScript should ensure it's a string in any other case) |
||
| return groupByFilterTestPatterns | ||
| .map(({ groupName, pattern }) => { | ||
| if (pattern.test(alertParams.filterQuery!)) { | ||
| if (pattern.test(filterQuery)) { | ||
| return groupName; | ||
| } | ||
| }) | ||
| .filter((g) => typeof g === 'string') as string[]; | ||
| }, [alertParams.filterQuery, groupByFilterTestPatterns]); | ||
| }, [alertParams, groupByFilterTestPatterns]); | ||
|
|
||
| return ( | ||
| <> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,8 @@ import { esKuery } from '../../../../../src/plugins/data/public'; | |
|
|
||
| export const convertKueryToElasticSearchQuery = ( | ||
| kueryExpression: string, | ||
| indexPattern: DataViewBase | ||
| indexPattern: DataViewBase, | ||
| swallowErrors: boolean = true | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can convert this to a small helper instead and use composition for the same effect? |
||
| ) => { | ||
| try { | ||
| return kueryExpression | ||
|
|
@@ -19,6 +20,8 @@ export const convertKueryToElasticSearchQuery = ( | |
| ) | ||
| : ''; | ||
| } catch (err) { | ||
| return ''; | ||
| if (swallowErrors) { | ||
| return ''; | ||
| } else throw err; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ import { | |
| buildNoDataAlertReason, | ||
| // buildRecoveredAlertReason, | ||
| stateToAlertMessage, | ||
| buildInvalidQueryAlertReason, | ||
| } from '../common/messages'; | ||
| import { evaluateCondition } from './evaluate_condition'; | ||
|
|
||
|
|
@@ -74,6 +75,25 @@ export const createInventoryMetricThresholdExecutor = (libs: InfraBackendLibs) = | |
| }, | ||
| }); | ||
|
|
||
| if (!params.filterQuery && params.filterQueryText) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be worth wrapping this in a function that calls out it's handling old alerts that didn't have the new guard |
||
| try { | ||
| const { fromKueryExpression } = await import('@kbn/es-query'); | ||
| fromKueryExpression(params.filterQueryText); | ||
| } catch (e) { | ||
| const actionGroupId = FIRED_ACTIONS.id; // Change this to an Error action group when able | ||
| const reason = buildInvalidQueryAlertReason(params.filterQueryText); | ||
| const alertInstance = alertInstanceFactory('*', reason); | ||
| alertInstance.scheduleActions(actionGroupId, { | ||
| group: '*', | ||
| alertState: stateToAlertMessage[AlertStates.ERROR], | ||
| reason, | ||
| timestamp: moment().toISOString(), | ||
| value: null, | ||
| metric: mapToConditionsLookup(criteria, (c) => c.metric), | ||
| }); | ||
| return {}; | ||
| } | ||
| } | ||
| const source = await libs.sources.getSourceConfiguration( | ||
| savedObjectsClient, | ||
| sourceId || 'default' | ||
|
|
||
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.
Nit: For me a name like
INVALID_QUERY_MARKERwould be more clear.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.
Would it make sense to use
unique symboltype and export that type to the other places where this is the value being passed in? So it's not "any symbol"?