diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/register_metric_threshold_alert_type.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/register_metric_threshold_alert_type.ts index 8b66dbe82b79d..caa0e8211eae6 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/register_metric_threshold_alert_type.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/register_metric_threshold_alert_type.ts @@ -42,6 +42,20 @@ const getCurrentValueFromAggregations = (aggregations: Aggregation) => { return value; }; +const getParsedFilterQuery: (filterQuery: string) => Record = filterQuery => { + if (!filterQuery) return {}; + try { + return JSON.parse(filterQuery).bool; + } catch (e) { + return { + query_string: { + query: filterQuery, + analyze_wildcard: true, + }, + }; + } +}; + const getMetric: ( services: AlertServices, params: MetricExpressionParams, @@ -97,20 +111,7 @@ const getMetric: ( } : baseAggs; - const parsedFilterQuery = filterQuery - ? (() => { - try { - return JSON.parse(filterQuery).bool; - } catch (e) { - return { - query_string: { - query: filterQuery, - analyze_wildcard: true, - }, - }; - } - })() - : {}; + const parsedFilterQuery = getParsedFilterQuery(filterQuery); const searchBody = { query: {