From ebd47eb42e797005bb8e9ea3c5b427c97626286d Mon Sep 17 00:00:00 2001 From: Shahzad Date: Tue, 18 Jul 2023 18:09:11 +0200 Subject: [PATCH] [Synthetics] Handle a case where settings were never saved (#161834) Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> --- .../server/legacy_uptime/lib/lib.test.ts | 44 +++++++++++++++++-- .../uptime/server/legacy_uptime/lib/lib.ts | 26 +++++++++-- x-pack/plugins/uptime/tsconfig.json | 1 + 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/x-pack/plugins/uptime/server/legacy_uptime/lib/lib.test.ts b/x-pack/plugins/uptime/server/legacy_uptime/lib/lib.test.ts index 13a151da2e635..de101bed4e037 100644 --- a/x-pack/plugins/uptime/server/legacy_uptime/lib/lib.test.ts +++ b/x-pack/plugins/uptime/server/legacy_uptime/lib/lib.test.ts @@ -8,7 +8,8 @@ import { UptimeEsClient } from './lib'; import { savedObjectsClientMock, uiSettingsServiceMock } from '@kbn/core/server/mocks'; import { elasticsearchClientMock } from '@kbn/core-elasticsearch-client-server-mocks'; -import { savedObjectsAdapter } from './saved_objects'; +import { SavedObjectsErrorHelpers } from '@kbn/core-saved-objects-server'; +import { settingsObjectId, umDynamicSettings } from './saved_objects/uptime_settings'; describe('UptimeEsClient', () => { let uptimeEsClient: UptimeEsClient; @@ -140,9 +141,44 @@ describe('UptimeEsClient', () => { }); describe('heartbeatIndices', () => { it('appends synthetics-* in index for legacy alerts', async () => { - savedObjectsAdapter.getUptimeDynamicSettings = jest.fn().mockResolvedValue({ - heartbeatIndices: 'heartbeat-8*,heartbeat-7*', - syntheticsIndexRemoved: true, + savedObjectsClient.get = jest.fn().mockResolvedValue({ + attributes: { + heartbeatIndices: 'heartbeat-8*,heartbeat-7*', + syntheticsIndexRemoved: true, + }, + }); + uptimeEsClient = new UptimeEsClient(savedObjectsClient, esClient, { isLegacyAlert: true }); + + const mockSearchParams = { + body: { + query: { + match_all: {}, + }, + }, + }; + + await uptimeEsClient.search({ + body: { + query: { + match_all: {}, + }, + }, + }); + + expect(esClient.search).toHaveBeenCalledWith( + { + index: 'heartbeat-8*,heartbeat-7*,synthetics-*', + ...mockSearchParams, + }, + { meta: true } + ); + }); + it('appends synthetics-* in index for legacy alerts when settings are never saved', async () => { + savedObjectsClient.get = jest.fn().mockImplementation(() => { + throw SavedObjectsErrorHelpers.createGenericNotFoundError( + umDynamicSettings.name, + settingsObjectId + ); }); uptimeEsClient = new UptimeEsClient(savedObjectsClient, esClient, { isLegacyAlert: true }); diff --git a/x-pack/plugins/uptime/server/legacy_uptime/lib/lib.ts b/x-pack/plugins/uptime/server/legacy_uptime/lib/lib.ts index e00104974014f..32c8e085cdf48 100644 --- a/x-pack/plugins/uptime/server/legacy_uptime/lib/lib.ts +++ b/x-pack/plugins/uptime/server/legacy_uptime/lib/lib.ts @@ -10,6 +10,7 @@ import { SavedObjectsClientContract, KibanaRequest, CoreRequestHandlerContext, + SavedObjectsErrorHelpers, } from '@kbn/core/server'; import chalk from 'chalk'; import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; @@ -18,9 +19,11 @@ import { RequestStatus } from '@kbn/inspector-plugin/common'; import { InspectResponse } from '@kbn/observability-plugin/typings/common'; import { enableInspectEsQueries } from '@kbn/observability-plugin/common'; import { getInspectResponse } from '@kbn/observability-shared-plugin/common'; +import { DYNAMIC_SETTINGS_DEFAULT_ATTRIBUTES } from '../../constants/settings'; +import { DynamicSettingsAttributes } from '../../runtime_types/settings'; +import { settingsObjectId, umDynamicSettings } from './saved_objects/uptime_settings'; import { API_URLS } from '../../../common/constants'; import { UptimeServerSetup } from './adapters'; -import { savedObjectsAdapter } from './saved_objects/saved_objects'; export type { UMServerLibs } from '../uptime_server'; @@ -202,14 +205,31 @@ export class UptimeEsClient { // if isLegacyAlert appends synthetics-* if it's not already there let indices = ''; let syntheticsIndexRemoved = false; + let settingsChangedByUser = true; + let settings: DynamicSettingsAttributes = DYNAMIC_SETTINGS_DEFAULT_ATTRIBUTES; if (this.heartbeatIndices) { indices = this.heartbeatIndices; } else { - const settings = await savedObjectsAdapter.getUptimeDynamicSettings(this.savedObjectsClient); + try { + const obj = await this.savedObjectsClient.get( + umDynamicSettings.name, + settingsObjectId + ); + settings = obj.attributes; + } catch (getErr) { + if (SavedObjectsErrorHelpers.isNotFoundError(getErr)) { + settingsChangedByUser = false; + } + } + indices = settings?.heartbeatIndices || ''; syntheticsIndexRemoved = settings.syntheticsIndexRemoved ?? false; } - if (this.isLegacyAlert && !indices.includes('synthetics-') && syntheticsIndexRemoved) { + if ( + this.isLegacyAlert && + !indices.includes('synthetics-') && + (syntheticsIndexRemoved || !settingsChangedByUser) + ) { indices = indices + ',synthetics-*'; } return indices; diff --git a/x-pack/plugins/uptime/tsconfig.json b/x-pack/plugins/uptime/tsconfig.json index 7c27b64878253..56621a40dddfe 100644 --- a/x-pack/plugins/uptime/tsconfig.json +++ b/x-pack/plugins/uptime/tsconfig.json @@ -72,6 +72,7 @@ "@kbn/core-http-server", "@kbn/core-http-router-server-internal", "@kbn/actions-plugin", + "@kbn/core-saved-objects-server", ], "exclude": [ "target/**/*",