[Synthetics] Handle a case where settings were never saved (#161834)

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Shahzad 2023-07-18 18:09:11 +02:00 committed by GitHub
parent e66ebdbd24
commit 41a8a1db01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 7 deletions

View file

@ -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 });

View file

@ -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<DynamicSettingsAttributes>(
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;

View file

@ -72,6 +72,7 @@
"@kbn/core-http-server",
"@kbn/core-http-router-server-internal",
"@kbn/actions-plugin",
"@kbn/core-saved-objects-server",
],
"exclude": [
"target/**/*",