Skip to content

Commit 8ae82ad

Browse files
committed
Do not create default alerts if connectors are not defined.
1 parent 9c49859 commit 8ae82ad

File tree

6 files changed

+46
-38
lines changed

6 files changed

+46
-38
lines changed

x-pack/solutions/observability/plugins/synthetics/server/routes/default_alerts/default_alert_service.test.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
SYNTHETICS_STATUS_RULE,
1212
SYNTHETICS_TLS_RULE,
1313
} from '../../../common/constants/synthetics_alerts';
14-
import { DefaultAlertService } from './default_alert_service';
14+
import { DefaultRuleService } from './default_alert_service';
1515
import { DYNAMIC_SETTINGS_DEFAULTS } from '../../constants/settings';
1616

1717
describe('DefaultAlertService', () => {
@@ -24,7 +24,7 @@ describe('DefaultAlertService', () => {
2424
const soResponse = { attributes: { ...expectedSettings } };
2525
it('returns settings if already set', async () => {
2626
const soClient = { get: jest.fn() } as any;
27-
const service = new DefaultAlertService({} as any, {} as any, soClient);
27+
const service = new DefaultRuleService({} as any, {} as any, soClient);
2828
service.settings = expectedSettings;
2929
const settings = await service.getSettings();
3030
expect(settings).toEqual(expectedSettings);
@@ -33,7 +33,7 @@ describe('DefaultAlertService', () => {
3333

3434
it('fetches settings if not set', async () => {
3535
const soClient = { get: jest.fn() } as any;
36-
const service = new DefaultAlertService({} as any, {} as any, soClient);
36+
const service = new DefaultRuleService({} as any, {} as any, soClient);
3737
soClient.get.mockResolvedValueOnce(soResponse);
3838
const settings = await service.getSettings();
3939
expect(settings).toEqual({
@@ -51,7 +51,7 @@ describe('DefaultAlertService', () => {
5151

5252
it('sets up status and tls rules', async () => {
5353
const soClient = { get: jest.fn() } as any;
54-
const service = new DefaultAlertService({} as any, {} as any, soClient);
54+
const service = new DefaultRuleService({} as any, {} as any, soClient);
5555
service.getSettings = jest.fn().mockResolvedValue({
5656
certAgeThreshold: 50,
5757
certExpirationThreshold: 10,
@@ -66,7 +66,7 @@ describe('DefaultAlertService', () => {
6666
service.setupTlsRule = setupTlsRule;
6767
setupStatusRule.mockResolvedValueOnce({ status: 'fulfilled', value: {} });
6868
setupTlsRule.mockResolvedValueOnce({ status: 'fulfilled', value: {} });
69-
const result = await service.setupDefaultAlerts();
69+
const result = await service.setupDefaultRules();
7070
expect(setupStatusRule).toHaveBeenCalledTimes(1);
7171
expect(setupTlsRule).toHaveBeenCalledTimes(1);
7272
expect(result).toEqual({
@@ -76,7 +76,7 @@ describe('DefaultAlertService', () => {
7676
});
7777
it('returns null rules if value is falsy', async () => {
7878
const soClient = { get: jest.fn() } as any;
79-
const service = new DefaultAlertService({} as any, {} as any, soClient);
79+
const service = new DefaultRuleService({} as any, {} as any, soClient);
8080
service.getSettings = jest.fn().mockResolvedValue({
8181
certAgeThreshold: 50,
8282
certExpirationThreshold: 10,
@@ -91,7 +91,7 @@ describe('DefaultAlertService', () => {
9191
service.setupTlsRule = setupTlsRule;
9292
setupStatusRule.mockResolvedValueOnce(undefined);
9393
setupTlsRule.mockResolvedValueOnce(undefined);
94-
const result = await service.setupDefaultAlerts();
94+
const result = await service.setupDefaultRules();
9595
expect(setupStatusRule).toHaveBeenCalledTimes(1);
9696
expect(setupTlsRule).toHaveBeenCalledTimes(1);
9797
expect(result).toEqual({
@@ -106,22 +106,22 @@ describe('DefaultAlertService', () => {
106106
const server = {
107107
alerting: { getConfig: () => ({ minimumScheduleInterval: { value: '30s' } }) },
108108
} as any;
109-
const service = new DefaultAlertService({} as any, server, {} as any);
109+
const service = new DefaultRuleService({} as any, server, {} as any);
110110
expect(service.getMinimumRuleInterval()).toBe('1m');
111111
});
112112

113113
it('returns minimum interval if greater than 1m', () => {
114114
const server = {
115115
alerting: { getConfig: () => ({ minimumScheduleInterval: { value: '5m' } }) },
116116
} as any;
117-
const service = new DefaultAlertService({} as any, server, {} as any);
117+
const service = new DefaultRuleService({} as any, server, {} as any);
118118
expect(service.getMinimumRuleInterval()).toBe('5m');
119119
});
120120
});
121121

122122
describe('setupStatusRule', () => {
123123
it('creates status rule if enabled', async () => {
124-
const service = new DefaultAlertService({} as any, {} as any, {} as any);
124+
const service = new DefaultRuleService({} as any, {} as any, {} as any);
125125
service.getMinimumRuleInterval = jest.fn().mockReturnValue('1m');
126126
service.createDefaultRuleIfNotExist = jest.fn();
127127
service.settings = { defaultStatusRuleEnabled: true } as any;
@@ -137,7 +137,7 @@ describe('DefaultAlertService', () => {
137137
});
138138

139139
it('does not create status rule if disabled', async () => {
140-
const service = new DefaultAlertService({} as any, {} as any, {} as any);
140+
const service = new DefaultRuleService({} as any, {} as any, {} as any);
141141
service.getMinimumRuleInterval = jest.fn().mockReturnValue('1m');
142142
service.createDefaultRuleIfNotExist = jest.fn();
143143
service.settings = { defaultStatusRuleEnabled: false } as any;
@@ -149,7 +149,7 @@ describe('DefaultAlertService', () => {
149149

150150
describe('setupTlsRule', () => {
151151
it('creates tls rule if enabled', async () => {
152-
const service = new DefaultAlertService({} as any, {} as any, {} as any);
152+
const service = new DefaultRuleService({} as any, {} as any, {} as any);
153153
service.getMinimumRuleInterval = jest.fn().mockReturnValue('1m');
154154
service.createDefaultRuleIfNotExist = jest.fn();
155155
service.settings = { defaultTlsRuleEnabled: true } as any;
@@ -165,7 +165,7 @@ describe('DefaultAlertService', () => {
165165
});
166166

167167
it('does not create tls rule if disabled', async () => {
168-
const service = new DefaultAlertService({} as any, {} as any, {} as any);
168+
const service = new DefaultRuleService({} as any, {} as any, {} as any);
169169
service.getMinimumRuleInterval = jest.fn().mockReturnValue('1m');
170170
service.createDefaultRuleIfNotExist = jest.fn();
171171
service.settings = { defaultTLSRuleEnabled: false } as any;
@@ -209,7 +209,7 @@ describe('DefaultAlertService', () => {
209209
describe('getExistingAlert', () => {
210210
it('returns rule if exists', async () => {
211211
const { getRulesClient, mockRule } = setUpExistingRules();
212-
const service = new DefaultAlertService(
212+
const service = new DefaultRuleService(
213213
{ alerting: { getRulesClient } } as any,
214214
{} as any,
215215
{} as any
@@ -222,7 +222,7 @@ describe('DefaultAlertService', () => {
222222
const find = jest.fn().mockResolvedValue({ data: [] });
223223
const getRulesClient = jest.fn();
224224
getRulesClient.mockReturnValue({ find });
225-
const service = new DefaultAlertService(
225+
const service = new DefaultRuleService(
226226
{ alerting: { getRulesClient } } as any,
227227
{} as any,
228228
{} as any
@@ -234,7 +234,7 @@ describe('DefaultAlertService', () => {
234234
describe('createDefaultAlertIfNotExist', () => {
235235
it('returns rule if exists', async () => {
236236
const { getRulesClient, mockRule } = setUpExistingRules();
237-
const service = new DefaultAlertService(
237+
const service = new DefaultRuleService(
238238
{ alerting: { getRulesClient } } as any,
239239
{} as any,
240240
{} as any
@@ -265,7 +265,7 @@ describe('DefaultAlertService', () => {
265265
});
266266
const getRulesClient = jest.fn();
267267
getRulesClient.mockReturnValue({ find, create });
268-
const service = new DefaultAlertService(
268+
const service = new DefaultRuleService(
269269
{ actions: { getActionsClient }, alerting: { getRulesClient } } as any,
270270
{} as any,
271271
{} as any
@@ -330,7 +330,7 @@ describe('DefaultAlertService', () => {
330330
schedule: { interval: '1m' },
331331
params: { param: 'value' },
332332
});
333-
const service = new DefaultAlertService(context as any, server as any, {} as any);
333+
const service = new DefaultRuleService(context as any, server as any, {} as any);
334334
service.settings = { defaultConnectors: ['slack', 'email'] } as any;
335335
const result = await service.updateStatusRule(true);
336336
expect(result).toEqual({
@@ -364,7 +364,7 @@ describe('DefaultAlertService', () => {
364364
} as any;
365365
const bulkDeleteRules = jest.fn();
366366
const { getRulesClient } = setUpExistingRules(undefined, { bulkDeleteRules });
367-
const service = new DefaultAlertService(
367+
const service = new DefaultRuleService(
368368
{ alerting: { getRulesClient } } as any,
369369
server as any,
370370
{} as any
@@ -381,7 +381,7 @@ describe('DefaultAlertService', () => {
381381
describe('updateTlsRule', () => {
382382
it('updates the rule if it is enabled', async () => {
383383
const { context, server } = setUpUpdateTest();
384-
const service = new DefaultAlertService(context as any, server as any, {} as any);
384+
const service = new DefaultRuleService(context as any, server as any, {} as any);
385385
service.settings = { defaultConnectors: ['slack', 'email'] } as any;
386386
const result = await service.updateTlsRule(true);
387387
expect(result).toEqual({
@@ -397,7 +397,7 @@ describe('DefaultAlertService', () => {
397397

398398
it('creates the rule if it does not exist', async () => {
399399
const { context, server } = setUpUpdateTest();
400-
const service = new DefaultAlertService(context as any, server as any, {} as any);
400+
const service = new DefaultRuleService(context as any, server as any, {} as any);
401401
service.settings = { defaultConnectors: ['slack', 'email'] } as any;
402402
const getExistingAlertMock = jest.fn().mockResolvedValue(undefined);
403403
service.getExistingAlert = getExistingAlertMock;
@@ -423,7 +423,7 @@ describe('DefaultAlertService', () => {
423423
} as any;
424424
const bulkDeleteRules = jest.fn();
425425
const { getRulesClient } = setUpExistingRules(undefined, { bulkDeleteRules });
426-
const service = new DefaultAlertService(
426+
const service = new DefaultRuleService(
427427
{ alerting: { getRulesClient } } as any,
428428
server as any,
429429
{} as any
@@ -445,7 +445,7 @@ describe('DefaultAlertService', () => {
445445
getActionsClient.mockReturnValue({
446446
getAll,
447447
});
448-
const service = new DefaultAlertService(
448+
const service = new DefaultRuleService(
449449
{ actions: { getActionsClient } } as any,
450450
{} as any,
451451
{ get: jest.fn() } as any

x-pack/solutions/observability/plugins/synthetics/server/routes/default_alerts/default_alert_service.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import type { SavedObjectsClientContract } from '@kbn/core-saved-objects-api-server';
99
import { parseDuration } from '@kbn/alerting-plugin/server';
1010
import type { FindActionResult } from '@kbn/actions-plugin/server';
11+
import { isEmpty } from 'lodash';
1112
import { getSyntheticsDynamicSettings } from '../../saved_objects/synthetics_settings';
1213
import type { DynamicSettingsAttributes } from '../../runtime_types/settings';
1314
import { populateAlertActions } from '../../../common/rules/alert_actions';
@@ -22,7 +23,7 @@ import {
2223
SYNTHETICS_TLS_RULE,
2324
} from '../../../common/constants/synthetics_alerts';
2425
import type { DefaultRuleType } from '../../../common/types/default_alerts';
25-
export class DefaultAlertService {
26+
export class DefaultRuleService {
2627
context: UptimeRequestHandlerContext;
2728
soClient: SavedObjectsClientContract;
2829
server: SyntheticsServerSetup;
@@ -45,8 +46,15 @@ export class DefaultAlertService {
4546
return this.settings;
4647
}
4748

48-
async setupDefaultAlerts() {
49+
async setupDefaultRules() {
4950
this.settings = await this.getSettings();
51+
if (isEmpty(this.settings?.defaultConnectors)) {
52+
this.server.logger.debug(`Default connectors are not set. Skipping default rule setup.`);
53+
return {
54+
statusRule: null,
55+
tlsRule: null,
56+
};
57+
}
5058

5159
const [statusRule, tlsRule] = await Promise.allSettled([
5260
this.setupStatusRule(),
@@ -152,7 +160,7 @@ export class DefaultAlertService {
152160
async updateStatusRule(enabled?: boolean) {
153161
const minimumRuleInterval = this.getMinimumRuleInterval();
154162
if (enabled) {
155-
return this.upsertDefaultAlert(
163+
return this.upsertDefaultRule(
156164
SYNTHETICS_STATUS_RULE,
157165
`Synthetics status internal rule`,
158166
minimumRuleInterval
@@ -168,7 +176,7 @@ export class DefaultAlertService {
168176
async updateTlsRule(enabled?: boolean) {
169177
const minimumRuleInterval = this.getMinimumRuleInterval();
170178
if (enabled) {
171-
return this.upsertDefaultAlert(
179+
return this.upsertDefaultRule(
172180
SYNTHETICS_TLS_RULE,
173181
`Synthetics internal TLS rule`,
174182
minimumRuleInterval
@@ -181,7 +189,7 @@ export class DefaultAlertService {
181189
}
182190
}
183191

184-
async upsertDefaultAlert(ruleType: DefaultRuleType, name: string, interval: string) {
192+
async upsertDefaultRule(ruleType: DefaultRuleType, name: string, interval: string) {
185193
const rulesClient = await (await this.context.alerting)?.getRulesClient();
186194

187195
const alert = await this.getExistingAlert(ruleType);

x-pack/solutions/observability/plugins/synthetics/server/routes/default_alerts/enable_default_alert.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* 2.0.
66
*/
77

8-
import { DefaultAlertService } from './default_alert_service';
8+
import { DefaultRuleService } from './default_alert_service';
99
import type { SyntheticsRestApiRouteFactory } from '../types';
1010
import { SYNTHETICS_API_URLS } from '../../../common/constants';
1111
import type { DEFAULT_ALERT_RESPONSE } from '../../../common/types/default_alerts';
@@ -15,8 +15,8 @@ export const enableDefaultAlertingRoute: SyntheticsRestApiRouteFactory = () => (
1515
path: SYNTHETICS_API_URLS.ENABLE_DEFAULT_ALERTING,
1616
validate: {},
1717
handler: async ({ context, server, savedObjectsClient }): Promise<DEFAULT_ALERT_RESPONSE> => {
18-
const defaultAlertService = new DefaultAlertService(context, server, savedObjectsClient);
18+
const defaultAlertService = new DefaultRuleService(context, server, savedObjectsClient);
1919

20-
return defaultAlertService.setupDefaultAlerts();
20+
return defaultAlertService.setupDefaultRules();
2121
},
2222
});

x-pack/solutions/observability/plugins/synthetics/server/routes/default_alerts/get_default_alert.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
SYNTHETICS_STATUS_RULE,
1010
SYNTHETICS_TLS_RULE,
1111
} from '../../../common/constants/synthetics_alerts';
12-
import { DefaultAlertService } from './default_alert_service';
12+
import { DefaultRuleService } from './default_alert_service';
1313
import type { SyntheticsRestApiRouteFactory } from '../types';
1414
import { SYNTHETICS_API_URLS } from '../../../common/constants';
1515
import type { DEFAULT_ALERT_RESPONSE } from '../../../common/types/default_alerts';
@@ -19,7 +19,7 @@ export const getDefaultAlertingRoute: SyntheticsRestApiRouteFactory = () => ({
1919
path: SYNTHETICS_API_URLS.ENABLE_DEFAULT_ALERTING,
2020
validate: {},
2121
handler: async ({ context, server, savedObjectsClient }): Promise<DEFAULT_ALERT_RESPONSE> => {
22-
const defaultAlertService = new DefaultAlertService(context, server, savedObjectsClient);
22+
const defaultAlertService = new DefaultRuleService(context, server, savedObjectsClient);
2323
const statusRule = defaultAlertService.getExistingAlert(SYNTHETICS_STATUS_RULE);
2424
const tlsRule = defaultAlertService.getExistingAlert(SYNTHETICS_TLS_RULE);
2525
const [status, tls] = await Promise.all([statusRule, tlsRule]);

x-pack/solutions/observability/plugins/synthetics/server/routes/default_alerts/update_default_alert.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import { getSyntheticsDynamicSettings } from '../../saved_objects/synthetics_settings';
9-
import { DefaultAlertService } from './default_alert_service';
9+
import { DefaultRuleService } from './default_alert_service';
1010
import type { SyntheticsRestApiRouteFactory } from '../types';
1111
import { SYNTHETICS_API_URLS } from '../../../common/constants';
1212
import type { DEFAULT_ALERT_RESPONSE } from '../../../common/types/default_alerts';
@@ -16,7 +16,7 @@ export const updateDefaultAlertingRoute: SyntheticsRestApiRouteFactory = () => (
1616
path: SYNTHETICS_API_URLS.ENABLE_DEFAULT_ALERTING,
1717
validate: {},
1818
handler: async ({ context, server, savedObjectsClient }): Promise<DEFAULT_ALERT_RESPONSE> => {
19-
const defaultAlertService = new DefaultAlertService(context, server, savedObjectsClient);
19+
const defaultAlertService = new DefaultRuleService(context, server, savedObjectsClient);
2020
const { defaultTLSRuleEnabled, defaultStatusRuleEnabled } = await getSyntheticsDynamicSettings(
2121
savedObjectsClient
2222
);

x-pack/solutions/observability/plugins/synthetics/server/routes/monitor_cruds/add_monitor/add_monitor_api.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import {
3737
DEFAULT_NAMESPACE_STRING,
3838
} from '../../../../common/constants/monitor_defaults';
3939
import { triggerTestNow } from '../../synthetics_service/test_now_monitor';
40-
import { DefaultAlertService } from '../../default_alerts/default_alert_service';
40+
import { DefaultRuleService } from '../../default_alerts/default_alert_service';
4141
import type { RouteContext } from '../../types';
4242
import { formatTelemetryEvent, sendTelemetryEvents } from '../../telemetry/monitor_upgrade_sender';
4343
import { formatKibanaNamespace } from '../../../../common/formatters';
@@ -235,9 +235,9 @@ export class AddEditMonitorAPI {
235235

236236
try {
237237
// we do this async, so we don't block the user, error handling will be done on the UI via separate api
238-
const defaultAlertService = new DefaultAlertService(context, server, savedObjectsClient);
238+
const defaultAlertService = new DefaultRuleService(context, server, savedObjectsClient);
239239
defaultAlertService
240-
.setupDefaultAlerts()
240+
.setupDefaultRules()
241241
.then(() => {
242242
server.logger.debug(`Successfully created default alert for monitor: ${name}`);
243243
})

0 commit comments

Comments
 (0)