-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Synthetics] Improve project monitors creation performance (#140525)
- Loading branch information
Showing
17 changed files
with
1,195 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
x-pack/plugins/synthetics/server/routes/monitor_cruds/bulk_cruds/add_monitor_bulk.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* 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 { SavedObjectsClientContract, KibanaRequest, SavedObject } from '@kbn/core/server'; | ||
import pMap from 'p-map'; | ||
import { SavedObjectsBulkResponse } from '@kbn/core-saved-objects-api-server'; | ||
import { v4 as uuidV4 } from 'uuid'; | ||
import { formatTelemetryEvent, sendTelemetryEvents } from '../../telemetry/monitor_upgrade_sender'; | ||
import { deleteMonitor } from '../delete_monitor'; | ||
import { UptimeServerSetup } from '../../../legacy_uptime/lib/adapters'; | ||
import { formatSecrets } from '../../../synthetics_service/utils'; | ||
import { syntheticsMonitorType } from '../../../../common/types/saved_objects'; | ||
import { | ||
ConfigKey, | ||
EncryptedSyntheticsMonitor, | ||
MonitorFields, | ||
PrivateLocation, | ||
ServiceLocationErrors, | ||
SyntheticsMonitor, | ||
} from '../../../../common/runtime_types'; | ||
import { SyntheticsMonitorClient } from '../../../synthetics_service/synthetics_monitor/synthetics_monitor_client'; | ||
|
||
export const createNewSavedObjectMonitorBulk = async ({ | ||
soClient, | ||
monitorsToCreate, | ||
}: { | ||
soClient: SavedObjectsClientContract; | ||
monitorsToCreate: Array<{ id: string; monitor: MonitorFields }>; | ||
}) => { | ||
const newMonitors = monitorsToCreate.map(({ id, monitor }) => ({ | ||
id, | ||
type: syntheticsMonitorType, | ||
attributes: formatSecrets({ | ||
...monitor, | ||
revision: 1, | ||
}), | ||
})); | ||
|
||
return await soClient.bulkCreate<EncryptedSyntheticsMonitor>(newMonitors); | ||
}; | ||
|
||
export const syncNewMonitorBulk = async ({ | ||
normalizedMonitors, | ||
server, | ||
syntheticsMonitorClient, | ||
soClient, | ||
request, | ||
privateLocations, | ||
spaceId, | ||
}: { | ||
normalizedMonitors: SyntheticsMonitor[]; | ||
server: UptimeServerSetup; | ||
syntheticsMonitorClient: SyntheticsMonitorClient; | ||
soClient: SavedObjectsClientContract; | ||
request: KibanaRequest; | ||
privateLocations: PrivateLocation[]; | ||
spaceId: string; | ||
}) => { | ||
let newMonitors: SavedObjectsBulkResponse<EncryptedSyntheticsMonitor> | null = null; | ||
|
||
const monitorsToCreate = normalizedMonitors.map((monitor) => ({ | ||
id: uuidV4(), | ||
monitor: monitor as MonitorFields, | ||
})); | ||
|
||
try { | ||
const [createdMonitors, { syncErrors }] = await Promise.all([ | ||
createNewSavedObjectMonitorBulk({ | ||
monitorsToCreate, | ||
soClient, | ||
}), | ||
syntheticsMonitorClient.addMonitors( | ||
monitorsToCreate, | ||
request, | ||
soClient, | ||
privateLocations, | ||
spaceId | ||
), | ||
]); | ||
|
||
newMonitors = createdMonitors; | ||
|
||
sendNewMonitorTelemetry(server, newMonitors.saved_objects, syncErrors); | ||
|
||
return { errors: syncErrors, newMonitors: newMonitors.saved_objects }; | ||
} catch (e) { | ||
await rollBackNewMonitorBulk( | ||
monitorsToCreate, | ||
server, | ||
soClient, | ||
syntheticsMonitorClient, | ||
request | ||
); | ||
|
||
throw e; | ||
} | ||
}; | ||
|
||
const rollBackNewMonitorBulk = async ( | ||
monitorsToCreate: Array<{ id: string; monitor: MonitorFields }>, | ||
server: UptimeServerSetup, | ||
soClient: SavedObjectsClientContract, | ||
syntheticsMonitorClient: SyntheticsMonitorClient, | ||
request: KibanaRequest | ||
) => { | ||
try { | ||
await pMap( | ||
monitorsToCreate, | ||
async (monitor) => | ||
deleteMonitor({ | ||
server, | ||
request, | ||
savedObjectsClient: soClient, | ||
monitorId: monitor.id, | ||
syntheticsMonitorClient, | ||
}), | ||
{ concurrency: 100 } | ||
); | ||
} catch (e) { | ||
// ignore errors here | ||
server.logger.error(e); | ||
} | ||
}; | ||
|
||
const sendNewMonitorTelemetry = ( | ||
server: UptimeServerSetup, | ||
monitors: Array<SavedObject<EncryptedSyntheticsMonitor>>, | ||
errors?: ServiceLocationErrors | null | ||
) => { | ||
for (const monitor of monitors) { | ||
sendTelemetryEvents( | ||
server.logger, | ||
server.telemetry, | ||
formatTelemetryEvent({ | ||
errors, | ||
monitor, | ||
isInlineScript: Boolean((monitor.attributes as MonitorFields)[ConfigKey.SOURCE_INLINE]), | ||
kibanaVersion: server.kibanaVersion, | ||
}) | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.