From 8acf8cbc6773e7d645fa91c48935f79443ec0ac2 Mon Sep 17 00:00:00 2001 From: Maryam Saeidi Date: Wed, 19 Mar 2025 17:23:26 +0100 Subject: [PATCH] [Custom threshold] Use createLazy instead of create when initializing searchSource (#213904) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary In this PR, we use a similar approach as was introduced in the ES Query rule in this [PR](https://github.com/elastic/kibana/pull/183694) for the custom threshold rule to reduce the field_caps traffic using createLazy. (Thanks @mikecote for pointing this out!) ||Screenshot| |---|---| |Create (796 ms)|![image](https://github.com/user-attachments/assets/2df8f864-bbc5-44e4-af43-7ae70f5dd2c3)| |CreateLazy (321 ms)|![image](https://github.com/user-attachments/assets/cd9a6e51-af7e-411a-ab2e-5d7a2efd3ce5)| ### 🧪 How to test - Enable APM locally ``` elastic.apm.active: true elastic.apm.transactionSampleRate: 1.0 elastic.apm.environment: username ``` - Create a custom threshold rule and check its execution in [traces](https://kibana-cloud-apm.elastic.dev/app/apm/traces?rangeFrom=now-15m&rangeTo=now) filtered for your `username` as the environment. There should be one with your rule name: The timing for `_field_caps` would be more if you replace the `createLazy` with the `create` function. (cherry picked from commit cc9494ccb057414d1eea9e27c51308854e521f05) --- .../rules/custom_threshold/custom_threshold_executor.test.ts | 2 +- .../lib/rules/custom_threshold/custom_threshold_executor.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/solutions/observability/plugins/observability/server/lib/rules/custom_threshold/custom_threshold_executor.test.ts b/x-pack/solutions/observability/plugins/observability/server/lib/rules/custom_threshold/custom_threshold_executor.test.ts index 804ae09e5ee9e..846f75ece720b 100644 --- a/x-pack/solutions/observability/plugins/observability/server/lib/rules/custom_threshold/custom_threshold_executor.test.ts +++ b/x-pack/solutions/observability/plugins/observability/server/lib/rules/custom_threshold/custom_threshold_executor.test.ts @@ -183,7 +183,7 @@ const setup = () => { ...alertsServices, getSearchSourceClient: jest.fn().mockResolvedValue({ ...searchSourceCommonMock, - create: jest.fn(() => Promise.resolve(mockedSearchSource)), + createLazy: jest.fn(() => Promise.resolve(mockedSearchSource)), }), }; diff --git a/x-pack/solutions/observability/plugins/observability/server/lib/rules/custom_threshold/custom_threshold_executor.ts b/x-pack/solutions/observability/plugins/observability/server/lib/rules/custom_threshold/custom_threshold_executor.ts index e3674a5aa9b33..feb3d17144fc6 100644 --- a/x-pack/solutions/observability/plugins/observability/server/lib/rules/custom_threshold/custom_threshold_executor.ts +++ b/x-pack/solutions/observability/plugins/observability/server/lib/rules/custom_threshold/custom_threshold_executor.ts @@ -123,7 +123,7 @@ export const createCustomThresholdExecutor = ({ ) : []; - const initialSearchSource = await searchSourceClient.create(params.searchConfiguration); + const initialSearchSource = await searchSourceClient.createLazy(params.searchConfiguration); const dataView = initialSearchSource.getField('index')!; const { id: dataViewId, timeFieldName } = dataView; const runtimeMappings = dataView.getRuntimeMappings();