diff --git a/x-pack/plugins/uptime/common/constants/ui.ts b/x-pack/plugins/uptime/common/constants/ui.ts index 29df2614d0617..659d5727abc0c 100644 --- a/x-pack/plugins/uptime/common/constants/ui.ts +++ b/x-pack/plugins/uptime/common/constants/ui.ts @@ -17,6 +17,8 @@ export const STEP_DETAIL_ROUTE = '/journey/:checkGroupId/step/:stepIndex'; export const SYNTHETIC_CHECK_STEPS_ROUTE = '/journey/:checkGroupId/steps'; +export const MAPPING_ERROR_ROUTE = '/mapping-error'; + export enum STATUS { UP = 'up', DOWN = 'down', diff --git a/x-pack/plugins/uptime/public/components/overview/monitor_list/monitor_list_container.tsx b/x-pack/plugins/uptime/public/components/overview/monitor_list/monitor_list_container.tsx index 835a89e8f7272..726ef59827f9e 100644 --- a/x-pack/plugins/uptime/public/components/overview/monitor_list/monitor_list_container.tsx +++ b/x-pack/plugins/uptime/public/components/overview/monitor_list/monitor_list_container.tsx @@ -13,6 +13,7 @@ import { MonitorListComponent } from './monitor_list'; import { useUrlParams } from '../../../hooks'; import { UptimeRefreshContext } from '../../../contexts'; import { getConnectorsAction, getMonitorAlertsAction } from '../../../state/alerts/alerts'; +import { useMappingCheck } from '../../../hooks/use_mapping_check'; export interface MonitorListProps { filters?: string; @@ -41,6 +42,7 @@ export const MonitorList: React.FC = (props) => { const { lastRefresh } = useContext(UptimeRefreshContext); const monitorList = useSelector(monitorListSelector); + useMappingCheck(monitorList.error); useEffect(() => { dispatch( diff --git a/x-pack/plugins/uptime/public/hooks/use_mapping_check.test.ts b/x-pack/plugins/uptime/public/hooks/use_mapping_check.test.ts new file mode 100644 index 0000000000000..5f17e65d102b4 --- /dev/null +++ b/x-pack/plugins/uptime/public/hooks/use_mapping_check.test.ts @@ -0,0 +1,53 @@ +/* + * 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 { shouldRedirect } from './use_mapping_check'; + +describe('useMappingCheck', () => { + describe('should redirect', () => { + it('returns true for appropriate error', () => { + const error = { + request: {}, + response: {}, + body: { + statusCode: 400, + error: 'Bad Request', + message: + '[search_phase_execution_exception: [illegal_argument_exception] Reason: Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [monitor.id] in order to load field data by uninverting the inverted index. Note that this can use significant memory.]: all shards failed', + }, + name: 'Error', + req: {}, + res: {}, + }; + expect(shouldRedirect(error)).toBe(true); + }); + + it('returns false for undefined', () => { + expect(shouldRedirect(undefined)).toBe(false); + }); + + it('returns false for missing body', () => { + expect(shouldRedirect({})).toBe(false); + }); + + it('returns false for incorrect error string', () => { + expect(shouldRedirect({ body: { error: 'not the right type' } })).toBe(false); + }); + + it('returns false for missing body message', () => { + expect(shouldRedirect({ body: { error: 'Bad Request' } })).toBe(false); + }); + + it('returns false for incorrect error message', () => { + expect( + shouldRedirect({ + body: { error: 'Bad Request', message: 'Not the correct kind of error message' }, + }) + ); + }); + }); +}); diff --git a/x-pack/plugins/uptime/public/hooks/use_mapping_check.ts b/x-pack/plugins/uptime/public/hooks/use_mapping_check.ts new file mode 100644 index 0000000000000..d8a7e0fac4065 --- /dev/null +++ b/x-pack/plugins/uptime/public/hooks/use_mapping_check.ts @@ -0,0 +1,43 @@ +/* + * 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 { useEffect } from 'react'; +import { useHistory } from 'react-router-dom'; +import { MAPPING_ERROR_ROUTE } from '../../common/constants'; + +interface EsBadRequestError { + body?: { + error?: string; + message?: string; + }; +} + +function contains(message: string, phrase: string) { + return message.indexOf(phrase) !== -1; +} + +export function shouldRedirect(error?: EsBadRequestError) { + if (!error || !error.body || error.body.error !== 'Bad Request' || !error.body.message) { + return false; + } + const { message } = error.body; + return ( + contains(message, 'search_phase_execution_exception') || + contains(message, 'Please use a keyword field instead.') || + contains(message, 'set fielddata=true') + ); +} + +export function useMappingCheck(error?: EsBadRequestError) { + const history = useHistory(); + + useEffect(() => { + if (shouldRedirect(error)) { + history.push(MAPPING_ERROR_ROUTE); + } + }, [error, history]); +} diff --git a/x-pack/plugins/uptime/public/hooks/use_telemetry.ts b/x-pack/plugins/uptime/public/hooks/use_telemetry.ts index 05fbd349b8f0f..f5abdb473fb0d 100644 --- a/x-pack/plugins/uptime/public/hooks/use_telemetry.ts +++ b/x-pack/plugins/uptime/public/hooks/use_telemetry.ts @@ -12,6 +12,7 @@ import { API_URLS } from '../../common/constants'; export enum UptimePage { Overview = 'Overview', + MappingError = 'MappingError', Monitor = 'Monitor', Settings = 'Settings', Certificates = 'Certificates', diff --git a/x-pack/plugins/uptime/public/pages/index.ts b/x-pack/plugins/uptime/public/pages/index.ts index 5624f61c3abb5..352ceb39123e8 100644 --- a/x-pack/plugins/uptime/public/pages/index.ts +++ b/x-pack/plugins/uptime/public/pages/index.ts @@ -5,6 +5,7 @@ * 2.0. */ +export { MappingErrorPage } from './mapping_error'; export { MonitorPage } from './monitor'; export { StepDetailPage } from './synthetics/step_detail_page'; export { SettingsPage } from './settings'; diff --git a/x-pack/plugins/uptime/public/pages/mapping_error.tsx b/x-pack/plugins/uptime/public/pages/mapping_error.tsx new file mode 100644 index 0000000000000..9c234700136b0 --- /dev/null +++ b/x-pack/plugins/uptime/public/pages/mapping_error.tsx @@ -0,0 +1,78 @@ +/* + * 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 { EuiCode, EuiEmptyPrompt, EuiLink, EuiTitle } from '@elastic/eui'; +import { i18n } from '@kbn/i18n'; +import { FormattedMessage } from '@kbn/i18n/react'; + +import React from 'react'; + +import { useKibana } from '../../../../../src/plugins/kibana_react/public'; +import { useBreadcrumbs } from '../hooks/use_breadcrumbs'; +import { useTrackPageview } from '../../../observability/public'; + +export const MappingErrorPage = () => { + useTrackPageview({ app: 'uptime', path: 'mapping-error' }); + useTrackPageview({ app: 'uptime', path: 'mapping-error', delay: 15000 }); + + const docLinks = useKibana().services.docLinks; + + useBreadcrumbs([ + { + text: i18n.translate('xpack.uptime.mappingErrorRoute.breadcrumb', { + defaultMessage: 'Mapping error', + }), + }, + ]); + + return ( + +

+ +

+ + } + body={ +
+

+ setup }} + /> +

+ {docLinks && ( +

+ + docs + + ), + }} + /> +

+ )} +
+ } + /> + ); +}; diff --git a/x-pack/plugins/uptime/public/routes.tsx b/x-pack/plugins/uptime/public/routes.tsx index e151e19180dd4..9f7310b43e556 100644 --- a/x-pack/plugins/uptime/public/routes.tsx +++ b/x-pack/plugins/uptime/public/routes.tsx @@ -11,13 +11,14 @@ import { FormattedMessage } from '@kbn/i18n/react'; import { i18n } from '@kbn/i18n'; import { CERTIFICATES_ROUTE, + MAPPING_ERROR_ROUTE, MONITOR_ROUTE, OVERVIEW_ROUTE, SETTINGS_ROUTE, STEP_DETAIL_ROUTE, SYNTHETIC_CHECK_STEPS_ROUTE, } from '../common/constants'; -import { MonitorPage, StepDetailPage, NotFoundPage, SettingsPage } from './pages'; +import { MappingErrorPage, MonitorPage, StepDetailPage, NotFoundPage, SettingsPage } from './pages'; import { CertificatesPage } from './pages/certificates'; import { UptimePage, useUptimeTelemetry } from './hooks'; import { OverviewPageComponent } from './pages/overview'; @@ -142,6 +143,26 @@ const Routes: RouteProps[] = [ rightSideItems: [], }, }, + { + title: i18n.translate('xpack.uptime.mappingErrorRoute.title', { + defaultMessage: 'Synthetics | mapping error', + }), + path: MAPPING_ERROR_ROUTE, + component: MappingErrorPage, + dataTestSubj: 'uptimeMappingErrorPage', + telemetryId: UptimePage.MappingError, + pageHeader: { + pageTitle: ( +
+ +
+ ), + rightSideItems: [], + }, + }, ]; const RouteInit: React.FC> = ({ diff --git a/x-pack/plugins/uptime/server/rest_api/monitors/monitor_list.ts b/x-pack/plugins/uptime/server/rest_api/monitors/monitor_list.ts index d28645bcb21a1..36bc5a80ef47a 100644 --- a/x-pack/plugins/uptime/server/rest_api/monitors/monitor_list.ts +++ b/x-pack/plugins/uptime/server/rest_api/monitors/monitor_list.ts @@ -27,7 +27,7 @@ export const createMonitorListRoute: UMRestApiRouteFactory = (libs) => ({ options: { tags: ['access:uptime-read'], }, - handler: async ({ uptimeEsClient, request }): Promise => { + handler: async ({ uptimeEsClient, request, response }): Promise => { const { dateRangeStart, dateRangeEnd, filters, pagination, statusFilter, pageSize, query } = request.query; @@ -35,20 +35,29 @@ export const createMonitorListRoute: UMRestApiRouteFactory = (libs) => ({ ? JSON.parse(decodeURIComponent(pagination)) : CONTEXT_DEFAULTS.CURSOR_PAGINATION; - const result = await libs.requests.getMonitorStates({ - uptimeEsClient, - dateRangeStart, - dateRangeEnd, - pagination: decodedPagination, - pageSize, - filters, - query, - // this is added to make typescript happy, - // this sort of reassignment used to be further downstream but I've moved it here - // because this code is going to be decomissioned soon - statusFilter: statusFilter || undefined, - }); + try { + const result = await libs.requests.getMonitorStates({ + uptimeEsClient, + dateRangeStart, + dateRangeEnd, + pagination: decodedPagination, + pageSize, + filters, + query, + statusFilter, + }); - return result; + return result; + } catch (e) { + /** + * This particular error is usually indicative of a mapping problem within the user's + * indices. It's relevant for the UI because we will be able to provide the user with a + * tailored message to help them remediate this problem on their own with minimal effort. + */ + if (e.name === 'ResponseError') { + return response.badRequest({ body: e }); + } + throw e; + } }, }); diff --git a/x-pack/test/functional/apps/uptime/certificates.ts b/x-pack/test/functional/apps/uptime/certificates.ts index 70affdf836072..610f07c183782 100644 --- a/x-pack/test/functional/apps/uptime/certificates.ts +++ b/x-pack/test/functional/apps/uptime/certificates.ts @@ -9,19 +9,27 @@ import { FtrProviderContext } from '../../ftr_provider_context'; import { makeCheck } from '../../../api_integration/apis/uptime/rest/helper/make_checks'; import { getSha256 } from '../../../api_integration/apis/uptime/rest/helper/make_tls'; +const BLANK_INDEX_PATH = 'x-pack/test/functional/es_archives/uptime/blank'; + export default ({ getPageObjects, getService }: FtrProviderContext) => { const { uptime } = getPageObjects(['uptime']); const uptimeService = getService('uptime'); + const esArchiver = getService('esArchiver'); const es = getService('es'); describe('certificates', function () { describe('empty certificates', function () { before(async () => { + await esArchiver.load(BLANK_INDEX_PATH); await makeCheck({ es }); await uptime.goToRoot(true); }); + after(async () => { + await esArchiver.unload(BLANK_INDEX_PATH); + }); + it('go to certs page', async () => { await uptimeService.common.waitUntilDataIsLoaded(); await uptimeService.cert.hasViewCertButton(); @@ -34,10 +42,15 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => { describe('with certs', function () { before(async () => { + await esArchiver.load(BLANK_INDEX_PATH); await makeCheck({ es, tls: true }); await uptime.goToRoot(true); }); + after(async () => { + await esArchiver.unload(BLANK_INDEX_PATH); + }); + beforeEach(async () => { await makeCheck({ es, tls: true }); }); diff --git a/x-pack/test/functional/apps/uptime/index.ts b/x-pack/test/functional/apps/uptime/index.ts index 2db29bfca4054..ef5a4e576da88 100644 --- a/x-pack/test/functional/apps/uptime/index.ts +++ b/x-pack/test/functional/apps/uptime/index.ts @@ -80,5 +80,9 @@ export default ({ loadTestFile, getService }: FtrProviderContext) => { loadTestFile(require.resolve('./ml_anomaly')); loadTestFile(require.resolve('./feature_controls')); }); + + describe('mappings error state', () => { + loadTestFile(require.resolve('./missing_mappings')); + }); }); }; diff --git a/x-pack/test/functional/apps/uptime/missing_mappings.ts b/x-pack/test/functional/apps/uptime/missing_mappings.ts new file mode 100644 index 0000000000000..2483aa45ecef9 --- /dev/null +++ b/x-pack/test/functional/apps/uptime/missing_mappings.ts @@ -0,0 +1,26 @@ +/* + * 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 { FtrProviderContext } from '../../ftr_provider_context'; +import { makeCheck } from '../../../api_integration/apis/uptime/rest/helper/make_checks'; + +export default ({ getPageObjects, getService }: FtrProviderContext) => { + const { common } = getPageObjects(['common']); + const uptimeService = getService('uptime'); + + const es = getService('es'); + describe('missing mappings', function () { + before(async () => { + await makeCheck({ es }); + await common.navigateToApp('uptime'); + }); + + it('redirects to mappings error page', async () => { + await uptimeService.common.hasMappingsError(); + }); + }); +}; diff --git a/x-pack/test/functional/services/uptime/common.ts b/x-pack/test/functional/services/uptime/common.ts index 7d47bcf985943..e3c73a1e1ca97 100644 --- a/x-pack/test/functional/services/uptime/common.ts +++ b/x-pack/test/functional/services/uptime/common.ts @@ -115,5 +115,8 @@ export function UptimeCommonProvider({ getService, getPageObjects }: FtrProvider await testSubjects.missingOrFail('data-missing'); }); }, + async hasMappingsError() { + return testSubjects.exists('xpack.uptime.mappingsErrorPage'); + }, }; }