Skip to content

Commit 3342b20

Browse files
shahzad31kibanamachine
authored andcommitted
[Synthetics] Use runWithCache for fleet bulk operations !! (elastic#238326)
## Summary Use runWithCache for fleet bulk operations !! This was causing too many extra operations being called unecessarily with bulk operations, for example having to update 1000 update monitors , it was causing 1000s of epr requests for package info for synthetics integration, ### Testing Creating/updating private location monitors should work as expected and also injecting global params via task. --------- Co-authored-by: kibanamachine <[email protected]>
1 parent bf1244d commit 3342b20

File tree

10 files changed

+282
-187
lines changed

10 files changed

+282
-187
lines changed

x-pack/platform/plugins/shared/fleet/server/mocks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ export const createFleetStartContractMock = (): DeeplyMockedKeys<FleetStartContr
381381
createFleetActionsClient: jest.fn((_) => fleetActionsClient),
382382
getPackageSpecTagId: jest.fn(getPackageSpecTagId),
383383
createOutputClient: jest.fn(async (_) => createOutputClientMock()),
384+
runWithCache: (async (cb: any) => await cb()) as any,
384385
};
385386

386387
return startContract;

x-pack/platform/plugins/shared/fleet/server/plugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ import {
6969
LEGACY_AGENT_POLICY_SAVED_OBJECT_TYPE,
7070
} from '../common/constants';
7171

72+
import { runWithCache } from './services/epm/packages/cache';
73+
7274
import { getFilesClientFactory } from './services/files/get_files_client_factory';
7375

7476
import type { MessageSigningServiceInterface } from './services/security';
@@ -256,6 +258,7 @@ export interface FleetStartContract {
256258
* Services for Fleet's package policies
257259
*/
258260
packagePolicyService: typeof packagePolicyService;
261+
runWithCache: typeof runWithCache;
259262
agentPolicyService: AgentPolicyServiceInterface;
260263
cloudConnectorService: CloudConnectorServiceInterface;
261264
/**
@@ -925,6 +928,7 @@ export class FleetPlugin
925928
return new OutputClient(soClient, authz);
926929
},
927930
cloudConnectorService,
931+
runWithCache,
928932
};
929933
}
930934

x-pack/solutions/observability/packages/synthetics-test-data/src/e2e/tasks/read_kibana_config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ export const readKibanaConfig = () => {
1919

2020
return (yaml.load(
2121
fs.readFileSync(fs.existsSync(kibanaDevConfig) ? kibanaDevConfig : kibanaConfig, 'utf8')
22-
) || {}) as Record<string, string>;
22+
) || {}) as Record<string, any>;
2323
};

x-pack/solutions/observability/plugins/synthetics/scripts/tasks/generate_monitors.ts

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,35 @@
77

88
import axios from 'axios';
99
import moment from 'moment';
10+
import { readKibanaConfig } from '@kbn/observability-synthetics-test-data';
1011

11-
const UP_MONITORS = 0;
12+
const UP_MONITORS = 10;
1213
const DOWN_MONITORS = 10;
1314

15+
function getAuthFromKibanaConfig() {
16+
const config = readKibanaConfig();
17+
let username = config.elasticsearch?.username;
18+
if (username === 'kibana_system_user') {
19+
username = 'elastic';
20+
}
21+
const password = config.elasticsearch?.password;
22+
return { username, password };
23+
}
24+
1425
export const generateMonitors = async () => {
26+
const policy = (await createTestAgentPolicy()) as { data: { item: { id: string } } };
27+
const location = await createPrivateLocation(policy.data.item.id);
28+
1529
// eslint-disable-next-line no-console
1630
console.log(`Generating ${UP_MONITORS} up monitors`);
1731
for (let i = 0; i < UP_MONITORS; i++) {
18-
await createMonitor(getHttpMonitor());
32+
await createMonitor(getHttpMonitor({ privateLocation: location }));
1933
}
2034

2135
// eslint-disable-next-line no-console
2236
console.log(`Generating ${DOWN_MONITORS} down monitors`);
2337
for (let i = 0; i < DOWN_MONITORS; i++) {
24-
await createMonitor(getHttpMonitor(true));
38+
await createMonitor(getHttpMonitor({ isDown: true, privateLocation: location }));
2539
}
2640
};
2741

@@ -31,7 +45,30 @@ const createMonitor = async (monitor: any) => {
3145
data: monitor,
3246
method: 'post',
3347
url: 'http://127.0.0.1:5601/test/api/synthetics/monitors',
34-
auth: { username: 'elastic', password: 'jdpAyka8HBiq81dFAIB86Nkp' },
48+
auth: getAuthFromKibanaConfig(),
49+
headers: { 'kbn-xsrf': 'true', 'elastic-api-version': '2023-10-31' },
50+
})
51+
.catch((error) => {
52+
// eslint-disable-next-line no-console
53+
console.error(error);
54+
});
55+
};
56+
57+
const createTestAgentPolicy = async () => {
58+
const data = {
59+
name: 'Test agent policy ' + moment().format('LTS'),
60+
description: '',
61+
namespace: 'default',
62+
monitoring_enabled: ['logs', 'metrics', 'traces'],
63+
inactivity_timeout: 1209600,
64+
is_protected: false,
65+
};
66+
return await axios
67+
.request({
68+
data,
69+
method: 'post',
70+
url: 'http://127.0.0.1:5601/test/api/fleet/agent_policies',
71+
auth: getAuthFromKibanaConfig(),
3572
headers: { 'kbn-xsrf': 'true', 'elastic-api-version': '2023-10-31' },
3673
})
3774
.catch((error) => {
@@ -40,7 +77,40 @@ const createMonitor = async (monitor: any) => {
4077
});
4178
};
4279

43-
const getHttpMonitor = (isDown?: boolean) => {
80+
const createPrivateLocation = async (policyId: string) => {
81+
const data = {
82+
label: 'Test private location ' + moment().format('LTS'),
83+
agentPolicyId: policyId,
84+
geo: { lat: 0, lon: 0 },
85+
spaces: ['*'],
86+
};
87+
88+
return (
89+
(await axios
90+
.request({
91+
data,
92+
method: 'post',
93+
url: 'http://127.0.0.1:5601/test/api/synthetics/private_locations',
94+
auth: getAuthFromKibanaConfig(),
95+
headers: { 'kbn-xsrf': 'true', 'elastic-api-version': '2023-10-31' },
96+
})
97+
.catch((error) => {
98+
// eslint-disable-next-line no-console
99+
console.error(error);
100+
})) as any
101+
).data;
102+
};
103+
104+
const getHttpMonitor = ({
105+
isDown,
106+
privateLocation,
107+
}: {
108+
isDown?: boolean;
109+
privateLocation?: {
110+
id: string;
111+
label: string;
112+
};
113+
}) => {
44114
return {
45115
type: 'http',
46116
form_monitor_type: 'http',
@@ -56,6 +126,9 @@ const getHttpMonitor = (isDown?: boolean) => {
56126
{ id: 'us_central_staging', label: 'US Central Staging', isServiceManaged: true },
57127
{ id: 'us_central', label: 'North America - US Central', isServiceManaged: true },
58128
{ id: 'us_central_qa', label: 'US Central QA', isServiceManaged: true },
129+
...(privateLocation
130+
? [{ id: privateLocation.id, label: privateLocation.label, isServiceManaged: false }]
131+
: []),
59132
],
60133
namespace: 'default',
61134
origin: 'ui',

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ describe('SyntheticsPrivateLocation', () => {
6767
bulkCreate: jest.fn(),
6868
getByIDs: jest.fn(),
6969
},
70+
runWithCache: async (cb: any) => await cb(),
7071
},
7172
spaces: {
7273
spacesService: {

0 commit comments

Comments
 (0)