Skip to content

Commit d63ec07

Browse files
jcgerkibanamachinecesco-f
authored
[ResponseOps][Rules] Add custom query description field to rules (#240956)
## Summary closes #240081 ## QA create a rule with type: - Synthetics monitor status (xpack.synthetics.alerts.monitorStatus) - Synthetics TLS certificate (xpack.synthetics.alerts.tls) check the rule detail page, it should now include the query <img width="376" height="275" alt="Screenshot 2025-10-28 at 11 09 50" src="https://github.com/user-attachments/assets/1b560098-42a2-4c7f-903a-56a5d1198e6f" /> --------- Co-authored-by: kibanamachine <[email protected]> Co-authored-by: Francesco Fagnani <[email protected]>
1 parent d8f35f1 commit d63ec07

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { RULE_PREBUILD_DESCRIPTION_FIELDS } from '@kbn/triggers-actions-ui-plugin/public';
9+
import type { Rule, PrebuildFieldsMap } from '@kbn/triggers-actions-ui-plugin/public/types';
10+
import type { HttpSetup } from '@kbn/core/public';
11+
import { getDescriptionFields } from './get_description_fields';
12+
import type { TLSRuleParams } from '@kbn/response-ops-rule-params/synthetics_tls';
13+
14+
describe('synthetics getDescriptionFields', () => {
15+
const indexField = { type: 'indexPattern', value: 'synthetics-*' };
16+
const mockPrebuildField = jest.fn();
17+
const mockPrebuildFields = {
18+
[RULE_PREBUILD_DESCRIPTION_FIELDS.CUSTOM_QUERY]: mockPrebuildField,
19+
[RULE_PREBUILD_DESCRIPTION_FIELDS.INDEX_PATTERN]: jest.fn().mockReturnValue(indexField),
20+
} as unknown as PrebuildFieldsMap;
21+
22+
beforeEach(() => {
23+
jest.clearAllMocks();
24+
});
25+
26+
it('should return empty array when rule is not provided', () => {
27+
const result = getDescriptionFields({
28+
rule: undefined as unknown as Rule<TLSRuleParams>,
29+
prebuildFields: mockPrebuildFields,
30+
http: {} as HttpSetup,
31+
});
32+
33+
expect(result).toEqual([]);
34+
});
35+
36+
it('should return empty array when prebuildFields is not provided', () => {
37+
const mockRule = {
38+
params: {
39+
kqlQuery: '_id: *',
40+
},
41+
} as unknown as Rule<TLSRuleParams>;
42+
43+
const result = getDescriptionFields({
44+
rule: mockRule,
45+
prebuildFields: undefined,
46+
http: {} as HttpSetup,
47+
});
48+
49+
expect(result).toEqual([]);
50+
});
51+
52+
it('should return empty array when both rule and prebuildFields are not provided', () => {
53+
const result = getDescriptionFields({
54+
rule: undefined as unknown as Rule<TLSRuleParams>,
55+
prebuildFields: undefined,
56+
http: {} as HttpSetup,
57+
});
58+
59+
expect(result).toEqual([]);
60+
});
61+
62+
it('should return the custom query', () => {
63+
const mockRule = {
64+
params: {
65+
kqlQuery: '_id: *',
66+
},
67+
} as unknown as Rule<TLSRuleParams>;
68+
69+
const mockReturnValue = { type: 'customQuery', value: '_id: *' };
70+
mockPrebuildField.mockReturnValue(mockReturnValue);
71+
72+
const result = getDescriptionFields({
73+
rule: mockRule,
74+
prebuildFields: mockPrebuildFields,
75+
http: {} as HttpSetup,
76+
});
77+
78+
expect(mockPrebuildField).toHaveBeenCalledWith('_id: *');
79+
expect(result).toEqual([indexField, mockReturnValue]);
80+
});
81+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import type { TLSRuleParams } from '@kbn/response-ops-rule-params/synthetics_tls';
9+
import type { GetDescriptionFieldsFn } from '@kbn/triggers-actions-ui-plugin/public/types';
10+
import type { SyntheticsMonitorStatusRuleParams } from '@kbn/response-ops-rule-params/synthetics_monitor_status';
11+
import { SYNTHETICS_INDEX_PATTERN } from '../../../../../common/constants/ui';
12+
13+
export const getDescriptionFields: GetDescriptionFieldsFn<
14+
TLSRuleParams | SyntheticsMonitorStatusRuleParams
15+
> = ({ rule, prebuildFields }) => {
16+
if (!rule || !prebuildFields) {
17+
return [];
18+
}
19+
20+
const fields = [prebuildFields.indexPattern([SYNTHETICS_INDEX_PATTERN])];
21+
22+
if (rule.params.kqlQuery) {
23+
fields.push(prebuildFields.customQuery(rule.params.kqlQuery));
24+
}
25+
26+
return fields;
27+
};

x-pack/solutions/observability/plugins/synthetics/public/apps/synthetics/lib/alert_types/monitor_status.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { getSyntheticsErrorRouteFromMonitorId } from '../../../../../common/util
1616
import { STATE_ID } from '../../../../../common/field_names';
1717
import { SyntheticsMonitorStatusTranslations } from '../../../../../common/rules/synthetics/translations';
1818
import type { AlertTypeInitializer } from './types';
19+
import { getDescriptionFields } from './get_description_fields';
1920

2021
const { defaultActionMessage, defaultRecoveryMessage, description } =
2122
SyntheticsMonitorStatusTranslations;
@@ -51,4 +52,5 @@ export const initMonitorStatusAlertType: AlertTypeInitializer = ({
5152
}),
5253
};
5354
},
55+
getDescriptionFields,
5456
});

x-pack/solutions/observability/plugins/synthetics/public/apps/synthetics/lib/alert_types/tls.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { TlsTranslations } from '../../../../../common/rules/synthetics/translat
1515
import { CERTIFICATES_ROUTE } from '../../../../../common/constants/ui';
1616

1717
import type { AlertTypeInitializer } from './types';
18+
import { getDescriptionFields } from './get_description_fields';
1819

1920
let validateFunc: (ruleParams: any) => ValidationResult;
2021

@@ -55,4 +56,5 @@ export const initTlsAlertType: AlertTypeInitializer = ({
5556
reason: fields[ALERT_REASON] || '',
5657
link: `/app/synthetics${CERTIFICATES_ROUTE}`,
5758
}),
59+
getDescriptionFields,
5860
});

0 commit comments

Comments
 (0)