Skip to content

Commit e095c3d

Browse files
[9.2] [Synthetics] Sync task de-dupe cache init !! (#241094) (#241470)
# Backport This will backport the following commits from `main` to `9.2`: - [[Synthetics] Sync task de-dupe cache init !! (#241094)](#241094) <!--- Backport version: 9.6.6 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Shahzad","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-10-31T15:03:22Z","message":"[Synthetics] Sync task de-dupe cache init !! (#241094)\n\n## Summary\n\nRun with cache was called being twice in the code flow, this make sure\nto do it only once.\n\n\n### After\nThere should be only one span for Find latest synthetics package\n<img width=\"1724\" height=\"894\" alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/79e90175-3179-4212-bd7d-0fe5220dcbca\"\n/>\n\n### Before\n\n<img width=\"2556\" height=\"1186\" alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/fa7079e0-c86d-4383-b266-ad1bf0914666\"\n/>","sha":"db6da914f49e2a3d2d1231a71415357bc6829041","branchLabelMapping":{"^v9.3.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:obs-ux-management","backport:version","author:obs-ux-management","v9.3.0","v8.19.7","v9.1.7","v9.2.1"],"title":"[Synthetics] Sync task de-dupe cache init !!","number":241094,"url":"https://github.com/elastic/kibana/pull/241094","mergeCommit":{"message":"[Synthetics] Sync task de-dupe cache init !! (#241094)\n\n## Summary\n\nRun with cache was called being twice in the code flow, this make sure\nto do it only once.\n\n\n### After\nThere should be only one span for Find latest synthetics package\n<img width=\"1724\" height=\"894\" alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/79e90175-3179-4212-bd7d-0fe5220dcbca\"\n/>\n\n### Before\n\n<img width=\"2556\" height=\"1186\" alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/fa7079e0-c86d-4383-b266-ad1bf0914666\"\n/>","sha":"db6da914f49e2a3d2d1231a71415357bc6829041"}},"sourceBranch":"main","suggestedTargetBranches":["8.19","9.1","9.2"],"targetPullRequestStates":[{"branch":"main","label":"v9.3.0","branchLabelMappingKey":"^v9.3.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/241094","number":241094,"mergeCommit":{"message":"[Synthetics] Sync task de-dupe cache init !! (#241094)\n\n## Summary\n\nRun with cache was called being twice in the code flow, this make sure\nto do it only once.\n\n\n### After\nThere should be only one span for Find latest synthetics package\n<img width=\"1724\" height=\"894\" alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/79e90175-3179-4212-bd7d-0fe5220dcbca\"\n/>\n\n### Before\n\n<img width=\"2556\" height=\"1186\" alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/fa7079e0-c86d-4383-b266-ad1bf0914666\"\n/>","sha":"db6da914f49e2a3d2d1231a71415357bc6829041"}},{"branch":"8.19","label":"v8.19.7","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"9.1","label":"v9.1.7","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"9.2","label":"v9.2.1","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Shahzad <[email protected]>
1 parent 3d5590e commit e095c3d

File tree

3 files changed

+154
-159
lines changed

3 files changed

+154
-159
lines changed

x-pack/solutions/observability/plugins/synthetics/server/synthetics_route_wrapper.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const syntheticsRouteWrapper: SyntheticsRouteWrapper = (
6666
const spaceId = server.spaces?.spacesService.getSpaceId(request) ?? DEFAULT_SPACE_ID;
6767

6868
try {
69-
const res = await syntheticsRoute.handler({
69+
const data = {
7070
syntheticsEsClient,
7171
savedObjectsClient,
7272
context,
@@ -76,7 +76,10 @@ export const syntheticsRouteWrapper: SyntheticsRouteWrapper = (
7676
spaceId,
7777
syntheticsMonitorClient,
7878
monitorConfigRepository,
79-
});
79+
};
80+
81+
const res = await server.fleet.runWithCache(() => syntheticsRoute.handler(data));
82+
8083
if (isKibanaResponse(res)) {
8184
return res;
8285
}

x-pack/solutions/observability/plugins/synthetics/server/synthetics_service/private_location/synthetics_private_location.ts

Lines changed: 147 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -148,74 +148,72 @@ export class SyntheticsPrivateLocation {
148148
if (configs.length === 0) {
149149
return { created: [], failed: [] };
150150
}
151-
return this.runWithCache(async () => {
152-
const newPolicies: NewPackagePolicyWithId[] = [];
153-
const newPolicyTemplate = await this.buildNewPolicy();
151+
const newPolicies: NewPackagePolicyWithId[] = [];
152+
const newPolicyTemplate = await this.buildNewPolicy();
154153

155-
for (const { config, globalParams } of configs) {
156-
try {
157-
const { locations } = config;
158-
const fleetManagedLocations = locations.filter((loc) => !loc.isServiceManaged);
159-
160-
for (const privateLocation of fleetManagedLocations) {
161-
const location = privateLocations?.find((loc) => loc.id === privateLocation.id)!;
162-
if (!location) {
163-
throw new Error(
164-
`Unable to find Synthetics private location for agentId ${privateLocation.id}`
165-
);
166-
}
154+
for (const { config, globalParams } of configs) {
155+
try {
156+
const { locations } = config;
157+
const fleetManagedLocations = locations.filter((loc) => !loc.isServiceManaged);
167158

168-
const newPolicy = await this.generateNewPolicy(
169-
config,
170-
location,
171-
newPolicyTemplate,
172-
spaceId,
173-
globalParams,
174-
maintenanceWindows,
175-
testRunId,
176-
runOnce
159+
for (const privateLocation of fleetManagedLocations) {
160+
const location = privateLocations?.find((loc) => loc.id === privateLocation.id)!;
161+
if (!location) {
162+
throw new Error(
163+
`Unable to find Synthetics private location for agentId ${privateLocation.id}`
177164
);
165+
}
178166

179-
if (!newPolicy) {
180-
throw new Error(
181-
`Unable to create Synthetics package policy for monitor ${
182-
config[ConfigKey.NAME]
183-
} with private location ${location.label}`
184-
);
185-
}
186-
if (newPolicy) {
187-
if (testRunId) {
188-
newPolicies.push(newPolicy as NewPackagePolicyWithId);
189-
} else {
190-
newPolicies.push({
191-
...newPolicy,
192-
id: this.getPolicyId(config, location.id, spaceId),
193-
});
194-
}
167+
const newPolicy = await this.generateNewPolicy(
168+
config,
169+
location,
170+
newPolicyTemplate,
171+
spaceId,
172+
globalParams,
173+
maintenanceWindows,
174+
testRunId,
175+
runOnce
176+
);
177+
178+
if (!newPolicy) {
179+
throw new Error(
180+
`Unable to create Synthetics package policy for monitor ${
181+
config[ConfigKey.NAME]
182+
} with private location ${location.label}`
183+
);
184+
}
185+
if (newPolicy) {
186+
if (testRunId) {
187+
newPolicies.push(newPolicy as NewPackagePolicyWithId);
188+
} else {
189+
newPolicies.push({
190+
...newPolicy,
191+
id: this.getPolicyId(config, location.id, spaceId),
192+
});
195193
}
196194
}
197-
} catch (e) {
198-
this.server.logger.error(e);
199-
throw e;
200-
}
201-
}
202-
203-
if (newPolicies.length === 0) {
204-
throw new Error('Failed to build package policies for all monitors');
205-
}
206-
207-
try {
208-
const result = await this.createPolicyBulk(newPolicies);
209-
if (result?.created && result?.created?.length > 0 && testRunId) {
210-
// ignore await here, we don't want to wait for this to finish
211-
void scheduleCleanUpTask(this.server);
212195
}
213-
return result;
214196
} catch (e) {
215197
this.server.logger.error(e);
216198
throw e;
217199
}
218-
});
200+
}
201+
202+
if (newPolicies.length === 0) {
203+
throw new Error('Failed to build package policies for all monitors');
204+
}
205+
206+
try {
207+
const result = await this.createPolicyBulk(newPolicies);
208+
if (result?.created && result?.created?.length > 0 && testRunId) {
209+
// ignore await here, we don't want to wait for this to finish
210+
void scheduleCleanUpTask(this.server);
211+
}
212+
return result;
213+
} catch (e) {
214+
this.server.logger.error(e);
215+
throw e;
216+
}
219217
}
220218

221219
async inspectPackagePolicy({
@@ -276,90 +274,88 @@ export class SyntheticsPrivateLocation {
276274
};
277275
}
278276

279-
return this.runWithCache(async () => {
280-
const [newPolicyTemplate, existingPolicies] = await Promise.all([
281-
this.buildNewPolicy(),
282-
this.getExistingPolicies(
283-
configs.map(({ config }) => config),
284-
allPrivateLocations,
285-
spaceId
286-
),
287-
]);
288-
289-
const policiesToUpdate: NewPackagePolicyWithId[] = [];
290-
const policiesToCreate: NewPackagePolicyWithId[] = [];
291-
const policiesToDelete: string[] = [];
292-
293-
for (const { config, globalParams } of configs) {
294-
const { locations } = config;
277+
const [newPolicyTemplate, existingPolicies] = await Promise.all([
278+
this.buildNewPolicy(),
279+
this.getExistingPolicies(
280+
configs.map(({ config }) => config),
281+
allPrivateLocations,
282+
spaceId
283+
),
284+
]);
295285

296-
const monitorPrivateLocations = locations.filter((loc) => !loc.isServiceManaged);
286+
const policiesToUpdate: NewPackagePolicyWithId[] = [];
287+
const policiesToCreate: NewPackagePolicyWithId[] = [];
288+
const policiesToDelete: string[] = [];
297289

298-
for (const privateLocation of allPrivateLocations) {
299-
const hasLocation = monitorPrivateLocations?.some((loc) => loc.id === privateLocation.id);
300-
const currId = this.getPolicyId(config, privateLocation.id, spaceId);
301-
const hasPolicy = existingPolicies?.some((policy) => policy.id === currId);
302-
try {
303-
if (hasLocation) {
304-
const newPolicy = await this.generateNewPolicy(
305-
config,
306-
privateLocation,
307-
newPolicyTemplate,
308-
spaceId,
309-
globalParams,
310-
maintenanceWindows
311-
);
312-
313-
if (!newPolicy) {
314-
throwAddEditError(hasPolicy, privateLocation.label);
315-
}
290+
for (const { config, globalParams } of configs) {
291+
const { locations } = config;
316292

317-
if (hasPolicy) {
318-
policiesToUpdate.push({ ...newPolicy, id: currId } as NewPackagePolicyWithId);
319-
} else {
320-
policiesToCreate.push({ ...newPolicy, id: currId } as NewPackagePolicyWithId);
321-
}
322-
} else if (hasPolicy) {
323-
policiesToDelete.push(currId);
293+
const monitorPrivateLocations = locations.filter((loc) => !loc.isServiceManaged);
294+
295+
for (const privateLocation of allPrivateLocations) {
296+
const hasLocation = monitorPrivateLocations?.some((loc) => loc.id === privateLocation.id);
297+
const currId = this.getPolicyId(config, privateLocation.id, spaceId);
298+
const hasPolicy = existingPolicies?.some((policy) => policy.id === currId);
299+
try {
300+
if (hasLocation) {
301+
const newPolicy = await this.generateNewPolicy(
302+
config,
303+
privateLocation,
304+
newPolicyTemplate,
305+
spaceId,
306+
globalParams,
307+
maintenanceWindows
308+
);
309+
310+
if (!newPolicy) {
311+
throwAddEditError(hasPolicy, privateLocation.label);
312+
}
313+
314+
if (hasPolicy) {
315+
policiesToUpdate.push({ ...newPolicy, id: currId } as NewPackagePolicyWithId);
316+
} else {
317+
policiesToCreate.push({ ...newPolicy, id: currId } as NewPackagePolicyWithId);
324318
}
325-
} catch (e) {
326-
this.server.logger.error(e);
327-
throwAddEditError(hasPolicy, privateLocation.label, config[ConfigKey.NAME]);
319+
} else if (hasPolicy) {
320+
policiesToDelete.push(currId);
328321
}
322+
} catch (e) {
323+
this.server.logger.error(e);
324+
throwAddEditError(hasPolicy, privateLocation.label, config[ConfigKey.NAME]);
329325
}
330326
}
327+
}
331328

332-
this.server.logger.debug(
333-
`[editingMonitors] Creating ${policiesToCreate.length} policies, updating ${policiesToUpdate.length} policies, and deleting ${policiesToDelete.length} policies`
334-
);
329+
this.server.logger.debug(
330+
`[editingMonitors] Creating ${policiesToCreate.length} policies, updating ${policiesToUpdate.length} policies, and deleting ${policiesToDelete.length} policies`
331+
);
335332

336-
const [_createResponse, failedUpdatesRes, _deleteResponse] = await Promise.all([
337-
this.createPolicyBulk(policiesToCreate),
338-
this.updatePolicyBulk(policiesToUpdate),
339-
this.deletePolicyBulk(policiesToDelete),
340-
]);
333+
const [_createResponse, failedUpdatesRes, _deleteResponse] = await Promise.all([
334+
this.createPolicyBulk(policiesToCreate),
335+
this.updatePolicyBulk(policiesToUpdate),
336+
this.deletePolicyBulk(policiesToDelete),
337+
]);
341338

342-
const failedUpdates = failedUpdatesRes?.map(({ packagePolicy, error }) => {
343-
const policyConfig = configs.find(({ config }) => {
344-
const { locations } = config;
339+
const failedUpdates = failedUpdatesRes?.map(({ packagePolicy, error }) => {
340+
const policyConfig = configs.find(({ config }) => {
341+
const { locations } = config;
345342

346-
const monitorPrivateLocations = locations.filter((loc) => !loc.isServiceManaged);
347-
for (const privateLocation of monitorPrivateLocations) {
348-
const currId = this.getPolicyId(config, privateLocation.id, spaceId);
349-
return currId === packagePolicy.id;
350-
}
351-
});
352-
return {
353-
error,
354-
packagePolicy,
355-
config: policyConfig?.config,
356-
};
343+
const monitorPrivateLocations = locations.filter((loc) => !loc.isServiceManaged);
344+
for (const privateLocation of monitorPrivateLocations) {
345+
const currId = this.getPolicyId(config, privateLocation.id, spaceId);
346+
return currId === packagePolicy.id;
347+
}
357348
});
358-
359349
return {
360-
failedUpdates,
350+
error,
351+
packagePolicy,
352+
config: policyConfig?.config,
361353
};
362354
});
355+
356+
return {
357+
failedUpdates,
358+
};
363359
}
364360

365361
async getExistingPolicies(
@@ -436,39 +432,37 @@ export class SyntheticsPrivateLocation {
436432
}
437433

438434
async deleteMonitors(configs: HeartbeatConfig[], spaceId: string) {
439-
return this.runWithCache(async () => {
440-
const soClient = this.server.coreStart.savedObjects.createInternalRepository();
441-
const esClient = this.server.coreStart.elasticsearch.client.asInternalUser;
435+
const soClient = this.server.coreStart.savedObjects.createInternalRepository();
436+
const esClient = this.server.coreStart.elasticsearch.client.asInternalUser;
442437

443-
const policyIdsToDelete = [];
444-
for (const config of configs) {
445-
const { locations } = config;
438+
const policyIdsToDelete = [];
439+
for (const config of configs) {
440+
const { locations } = config;
446441

447-
const monitorPrivateLocations = locations.filter((loc) => !loc.isServiceManaged);
442+
const monitorPrivateLocations = locations.filter((loc) => !loc.isServiceManaged);
448443

449-
for (const privateLocation of monitorPrivateLocations) {
450-
policyIdsToDelete.push(this.getPolicyId(config, privateLocation.id, spaceId));
451-
}
444+
for (const privateLocation of monitorPrivateLocations) {
445+
policyIdsToDelete.push(this.getPolicyId(config, privateLocation.id, spaceId));
452446
}
453-
if (policyIdsToDelete.length > 0) {
454-
const result = await this.server.fleet.packagePolicyService.delete(
455-
soClient,
456-
esClient,
457-
policyIdsToDelete,
458-
{
459-
force: true,
460-
asyncDeploy: true,
461-
}
462-
);
463-
const failedPolicies = result?.filter((policy) => {
464-
return !policy.success && policy?.statusCode !== 404;
465-
});
466-
if (failedPolicies?.length === policyIdsToDelete.length) {
467-
throw new Error(deletePolicyError(configs[0][ConfigKey.NAME]));
447+
}
448+
if (policyIdsToDelete.length > 0) {
449+
const result = await this.server.fleet.packagePolicyService.delete(
450+
soClient,
451+
esClient,
452+
policyIdsToDelete,
453+
{
454+
force: true,
455+
asyncDeploy: true,
468456
}
469-
return result;
457+
);
458+
const failedPolicies = result?.filter((policy) => {
459+
return !policy.success && policy?.statusCode !== 404;
460+
});
461+
if (failedPolicies?.length === policyIdsToDelete.length) {
462+
throw new Error(deletePolicyError(configs[0][ConfigKey.NAME]));
470463
}
471-
});
464+
return result;
465+
}
472466
}
473467

474468
async getAgentPolicies() {
@@ -481,10 +475,6 @@ export class SyntheticsPrivateLocation {
481475
}
482476
return undefined;
483477
}
484-
485-
async runWithCache<T>(fn: () => Promise<T>): Promise<T> {
486-
return await this.server.fleet.runWithCache(fn);
487-
}
488478
}
489479

490480
const throwAddEditError = (hasPolicy: boolean, location?: string, name?: string) => {

x-pack/solutions/observability/plugins/synthetics/server/tasks/sync_private_locations_monitors_task.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ export class SyncPrivateLocationMonitorsTask {
132132
soClient,
133133
encryptedSavedObjects,
134134
});
135+
} else {
136+
this.debugLog(`No private locations found, skipping sync`);
135137
}
136138
this.debugLog(`Sync of private location monitors succeeded`);
137139
} else {

0 commit comments

Comments
 (0)