Skip to content

Commit 77cebee

Browse files
[Synthetics] adjust monitor status rule location logic (#156432)
## Summary Resolves #156223 In previous stack versions, the label was not saved to the saved object. This ensures that alerting works both for those early versions as well current versions, by adjusting the logic to consider both the location.label and location.id in alerting logic. ### Testing 1. Create a new oblt-cli cluster and connect to the synthetics service, locally or via the dev environment 2. Checkout v8.5.3 3. Create a monitor. Make sure it's down and wait for the first down check 4. Check out this branch 5. Navigate to Observabillity alerts. Ensure that an alert is generated for the monitor. You may have to wait a few minutes. --------- Co-authored-by: Shahzad <shahzad31comp@gmail.com>
1 parent cb2e28d commit 77cebee

2 files changed

Lines changed: 101 additions & 4 deletions

File tree

x-pack/plugins/synthetics/server/alert_rules/status_rule/status_rule_executor.test.ts

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,37 @@
44
* 2.0; you may not use this file except in compliance with the Elastic License
55
* 2.0.
66
*/
7-
8-
import { StatusRuleExecutor } from './status_rule_executor';
7+
import moment from 'moment';
98
import { loggerMock } from '@kbn/logging-mocks';
109
import { savedObjectsClientMock } from '@kbn/core-saved-objects-api-server-mocks';
10+
import { StatusRuleExecutor } from './status_rule_executor';
1111
import { UptimeServerSetup } from '../../legacy_uptime/lib/adapters';
1212
import { mockEncryptedSO } from '../../synthetics_service/utils/mocks';
1313
import { elasticsearchClientMock } from '@kbn/core-elasticsearch-client-server-mocks';
1414
import { SyntheticsMonitorClient } from '../../synthetics_service/synthetics_monitor/synthetics_monitor_client';
1515
import { SyntheticsService } from '../../synthetics_service/synthetics_service';
16-
import moment from 'moment';
1716
import * as monitorUtils from '../../saved_objects/synthetics_monitor/get_all_monitors';
17+
import * as locationsUtils from '../../synthetics_service/get_all_locations';
18+
import type { PublicLocation } from '../../../common/runtime_types';
1819

1920
describe('StatusRuleExecutor', () => {
2021
const mockEsClient = elasticsearchClientMock.createElasticsearchClient();
2122
const logger = loggerMock.create();
2223
const soClient = savedObjectsClientMock.create();
24+
jest.spyOn(locationsUtils, 'getAllLocations').mockResolvedValue({
25+
// @ts-ignore
26+
publicLocations: [
27+
{
28+
id: 'us_central_qa',
29+
label: 'US Central QA',
30+
},
31+
{
32+
id: 'us_central_dev',
33+
label: 'US Central DEV',
34+
},
35+
] as unknown as PublicLocation,
36+
privateLocations: [],
37+
});
2338

2439
const serverMock: UptimeServerSetup = {
2540
logger,
@@ -64,6 +79,7 @@ describe('StatusRuleExecutor', () => {
6479
soClient,
6580
});
6681
});
82+
6783
it('marks deleted configs as expected', async () => {
6884
jest.spyOn(monitorUtils, 'getAllMonitors').mockResolvedValue(testMonitors);
6985
const statusRule = new StatusRuleExecutor(
@@ -127,6 +143,84 @@ describe('StatusRuleExecutor', () => {
127143
},
128144
});
129145
});
146+
147+
it('does not mark deleted config when monitor does not contain location label', async () => {
148+
jest.spyOn(monitorUtils, 'getAllMonitors').mockResolvedValue([
149+
{
150+
...testMonitors[0],
151+
attributes: {
152+
...testMonitors[0].attributes,
153+
locations: [
154+
{
155+
geo: { lon: -95.86, lat: 41.25 },
156+
isServiceManaged: true,
157+
id: 'us_central_qa',
158+
},
159+
],
160+
},
161+
},
162+
]);
163+
const statusRule = new StatusRuleExecutor(
164+
moment().toDate(),
165+
{},
166+
soClient,
167+
mockEsClient,
168+
serverMock,
169+
monitorClient
170+
);
171+
172+
const { downConfigs } = await statusRule.getDownChecks({});
173+
174+
expect(downConfigs).toEqual({});
175+
176+
const staleDownConfigs = await statusRule.markDeletedConfigs({
177+
id1: {
178+
location: 'us-east-1',
179+
configId: 'id1',
180+
status: 'down',
181+
timestamp: '2021-06-01T00:00:00.000Z',
182+
monitorQueryId: 'test',
183+
ping: {} as any,
184+
},
185+
'2548dab3-4752-4b4d-89a2-ae3402b6fb04-us_central_dev': {
186+
location: 'US Central DEV',
187+
configId: '2548dab3-4752-4b4d-89a2-ae3402b6fb04',
188+
status: 'down',
189+
timestamp: '2021-06-01T00:00:00.000Z',
190+
monitorQueryId: 'test',
191+
ping: {} as any,
192+
},
193+
'2548dab3-4752-4b4d-89a2-ae3402b6fb04-us_central_qa': {
194+
location: 'US Central QA',
195+
configId: '2548dab3-4752-4b4d-89a2-ae3402b6fb04',
196+
status: 'down',
197+
timestamp: '2021-06-01T00:00:00.000Z',
198+
monitorQueryId: 'test',
199+
ping: {} as any,
200+
},
201+
});
202+
203+
expect(staleDownConfigs).toEqual({
204+
id1: {
205+
configId: 'id1',
206+
isDeleted: true,
207+
location: 'us-east-1',
208+
monitorQueryId: 'test',
209+
ping: {},
210+
status: 'down',
211+
timestamp: '2021-06-01T00:00:00.000Z',
212+
},
213+
'2548dab3-4752-4b4d-89a2-ae3402b6fb04-us_central_dev': {
214+
configId: '2548dab3-4752-4b4d-89a2-ae3402b6fb04',
215+
isLocationRemoved: true,
216+
location: 'US Central DEV',
217+
monitorQueryId: 'test',
218+
ping: {},
219+
status: 'down',
220+
timestamp: '2021-06-01T00:00:00.000Z',
221+
},
222+
});
223+
});
130224
});
131225

132226
const testMonitors = [

x-pack/plugins/synthetics/server/alert_rules/status_rule/status_rule_executor.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,10 @@ export class StatusRuleExecutor {
195195
delete downConfigs[locPlusId];
196196
} else {
197197
const { locations } = monitor.attributes;
198-
if (!locations.some((l) => l.label === downConfig.location)) {
198+
const isLocationRemoved = !locations.some(
199+
(l) => l.id === this.getLocationId(downConfig.location)
200+
);
201+
if (isLocationRemoved) {
199202
staleDownConfigs[locPlusId] = { ...downConfig, isLocationRemoved: true };
200203
delete downConfigs[locPlusId];
201204
}

0 commit comments

Comments
 (0)