Skip to content

Commit

Permalink
[ResponseOps] Lazy load dataViews and wrappedSearchSourceClient servi…
Browse files Browse the repository at this point in the history
…ces when running alerting rules (#189929)

Resolves #184322

## Summary

This PR updates `getExecutorServices` to allow alerting rules to only
load the dataViews and wrappedSearchSourceClient services when needed. I
updated the rule types dependent on dataViews and/or
wrappedSearchSourceClient.


### Checklist

- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

### To verify

- Verify that the dataviews and searchsource are only loaded when
needed. I think the best way to verify this is create an Index threshold
rule and make sure that the `dataViews` and `searchSourceClient`
services are not created.
- Verify that the updated rules work correctly. I updated the following
rule types:
Custom threshold
SLO burn rate
ES query
Indicator match
  • Loading branch information
doakalexi committed Aug 19, 2024
1 parent 1d5a0e1 commit cbaa3a8
Show file tree
Hide file tree
Showing 21 changed files with 148 additions and 123 deletions.
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
41 changes: 21 additions & 20 deletions x-pack/plugins/alerting/server/task_runner/get_executor_services.ts
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

0 comments on commit cbaa3a8

Please sign in to comment.