Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ResponseOps] Lazy load dataViews and wrappedSearchSourceClient services when running alerting rules #189929

Merged
Merged
4 changes: 2 additions & 2 deletions x-pack/plugins/alerting/server/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ const createRuleExecutorServicesMock = <
shouldWriteAlerts: () => true,
shouldStopExecution: () => true,
search: createAbortableSearchServiceMock(),
searchSourceClient: searchSourceCommonMock,
getSearchSourceClient: jest.fn().mockResolvedValue(searchSourceCommonMock),
ruleMonitoringService: createRuleMonitoringServiceMock(),
share: createShareStartMock(),
dataViews: dataViewPluginMocks.createStartContract(),
getDataViews: jest.fn().mockResolvedValue(dataViewPluginMocks.createStartContract()),
};
};
export type RuleExecutorServicesMock = ReturnType<typeof createRuleExecutorServicesMock>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export class AdHocTaskRunner implements CancellableTask {
taskInstance: this.taskInstance,
});

const executorServices = await getExecutorServices({
const executorServices = getExecutorServices({
context: this.context,
fakeRequest,
abortController: this.searchAbortController,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ interface GetExecutorServicesOpts {
}

export interface ExecutorServices {
dataViews: DataViewsContract;
ruleMonitoringService: PublicRuleMonitoringService;
ruleResultService: PublicRuleResultService;
savedObjectsClient: SavedObjectsClientContract;
uiSettingsClient: IUiSettingsClient;
wrappedScopedClusterClient: WrappedScopedClusterClient;
wrappedSearchSourceClient: WrappedSearchSourceClient;
getDataViews: () => Promise<DataViewsContract>;
getWrappedSearchSourceClient: () => Promise<WrappedSearchSourceClient>;
}

export const getExecutorServices = async (opts: GetExecutorServicesOpts) => {
export const getExecutorServices = (opts: GetExecutorServicesOpts) => {
const { context, abortController, fakeRequest, logger, ruleData, ruleTaskTimeout } = opts;

const wrappedClientOptions = {
Expand All @@ -66,34 +66,35 @@ export const getExecutorServices = async (opts: GetExecutorServicesOpts) => {
scopedClusterClient,
});

const searchSourceClient = await withAlertingSpan('alerting:get-search-source-client', () =>
context.data.search.searchSource.asScoped(fakeRequest)
);
const wrappedSearchSourceClient = wrapSearchSourceClient({
...wrappedClientOptions,
searchSourceClient,
});

const savedObjectsClient = context.savedObjects.getScopedClient(fakeRequest, {
includedHiddenTypes: [RULE_SAVED_OBJECT_TYPE, 'action'],
});

const dataViews = await await withAlertingSpan('alerting:get-data-views-factory', () =>
context.dataViews.dataViewsServiceFactory(
savedObjectsClient,
scopedClusterClient.asInternalUser
)
);

const uiSettingsClient = context.uiSettings.asScopedToClient(savedObjectsClient);

return {
dataViews,
ruleMonitoringService: opts.ruleMonitoringService.getLastRunMetricsSetters(),
ruleResultService: opts.ruleResultService.getLastRunSetters(),
savedObjectsClient,
uiSettingsClient,
wrappedScopedClusterClient,
wrappedSearchSourceClient,
getDataViews: async () => {
const dataViews = await withAlertingSpan('alerting:get-data-views-factory', () =>
context.dataViews.dataViewsServiceFactory(
savedObjectsClient,
scopedClusterClient.asInternalUser
)
);
return dataViews;
},
getWrappedSearchSourceClient: async () => {
const searchSourceClient = await withAlertingSpan('alerting:get-search-source-client', () =>
context.data.search.searchSource.asScoped(fakeRequest)
);
return wrapSearchSourceClient({
...wrappedClientOptions,
searchSourceClient,
});
},
};
};
Loading