Skip to content

Commit

Permalink
[Monitoring] Fetch status once and change fetchStatus to support an a…
Browse files Browse the repository at this point in the history
…rray of clusters (#91749)

* Fetch status once and change fetchStatus to support an array of clusters

* Update test
  • Loading branch information
chrisronline authored Feb 18, 2021
1 parent 8c9eaa2 commit a558920
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 41 deletions.
49 changes: 17 additions & 32 deletions x-pack/plugins/monitoring/server/lib/alerts/fetch_status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,9 @@ describe('fetchStatus', () => {
});

it('should fetch from the alerts client', async () => {
const status = await fetchStatus(
alertsClient as any,
licenseService as any,
alertTypes,
defaultClusterState.clusterUuid
);
const status = await fetchStatus(alertsClient as any, licenseService as any, alertTypes, [
defaultClusterState.clusterUuid,
]);
expect(status).toEqual({
monitoring_alert_cpu_usage: {
rawAlert: { id: 1 },
Expand All @@ -99,24 +96,18 @@ describe('fetchStatus', () => {
},
];

const status = await fetchStatus(
alertsClient as any,
licenseService as any,
alertTypes,
defaultClusterState.clusterUuid
);
const status = await fetchStatus(alertsClient as any, licenseService as any, alertTypes, [
defaultClusterState.clusterUuid,
]);
expect(Object.values(status).length).toBe(1);
expect(Object.keys(status)).toEqual(alertTypes);
expect(status[alertType].states[0].state.ui.isFiring).toBe(true);
});

it('should pass in the right filter to the alerts client', async () => {
await fetchStatus(
alertsClient as any,
licenseService as any,
alertTypes,
defaultClusterState.clusterUuid
);
await fetchStatus(alertsClient as any, licenseService as any, alertTypes, [
defaultClusterState.clusterUuid,
]);
expect((alertsClient.find as jest.Mock).mock.calls[0][0].options.filter).toBe(
`alert.attributes.alertTypeId:${alertType}`
);
Expand All @@ -127,12 +118,9 @@ describe('fetchStatus', () => {
alertTypeState: null,
})) as any;

const status = await fetchStatus(
alertsClient as any,
licenseService as any,
alertTypes,
defaultClusterState.clusterUuid
);
const status = await fetchStatus(alertsClient as any, licenseService as any, alertTypes, [
defaultClusterState.clusterUuid,
]);
expect(status[alertType].states.length).toEqual(0);
});

Expand All @@ -142,12 +130,9 @@ describe('fetchStatus', () => {
data: [],
})) as any;

const status = await fetchStatus(
alertsClient as any,
licenseService as any,
alertTypes,
defaultClusterState.clusterUuid
);
const status = await fetchStatus(alertsClient as any, licenseService as any, alertTypes, [
defaultClusterState.clusterUuid,
]);
expect(status).toEqual({});
});

Expand All @@ -163,7 +148,7 @@ describe('fetchStatus', () => {
alertsClient as any,
customLicenseService as any,
[ALERT_CLUSTER_HEALTH],
defaultClusterState.clusterUuid
[defaultClusterState.clusterUuid]
);
expect(customLicenseService.getWatcherFeature).toHaveBeenCalled();
});
Expand Down Expand Up @@ -200,7 +185,7 @@ describe('fetchStatus', () => {
customAlertsClient as any,
licenseService as any,
[ALERT_CPU_USAGE, ALERT_DISK_USAGE, ALERT_MISSING_MONITORING_DATA],
defaultClusterState.clusterUuid
[defaultClusterState.clusterUuid]
);
expect(Object.keys(status)).toEqual([
ALERT_CPU_USAGE,
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/monitoring/server/lib/alerts/fetch_status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function fetchStatus(
alertsClient: AlertsClient,
licenseService: MonitoringLicenseService,
alertTypes: string[] | undefined,
clusterUuid: string,
clusterUuids: string[],
filters: CommonAlertFilter[] = []
): Promise<{ [type: string]: CommonAlertStatus }> {
const types: Array<{ type: string; result: CommonAlertStatus }> = [];
Expand Down Expand Up @@ -57,7 +57,7 @@ export async function fetchStatus(
}
for (const state of alertInstanceState.alertStates) {
const meta = instance.meta;
if (clusterUuid && state.cluster.clusterUuid !== clusterUuid) {
if (clusterUuids && !clusterUuids.includes(state.cluster.clusterUuid)) {
return accum;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ export async function getClustersFromRequest(
// add alerts data
if (isInCodePath(codePaths, [CODE_PATH_ALERTS])) {
const alertsClient = req.getAlertsClient();
const alertStatus = await fetchStatus(
alertsClient,
req.server.plugins.monitoring.info,
undefined,
clusters.map((cluster) => cluster.cluster_uuid)
);

for (const cluster of clusters) {
const verification = verifyMonitoringLicense(req.server);
if (!verification.enabled) {
Expand Down Expand Up @@ -154,12 +161,20 @@ export async function getClustersFromRequest(
if (prodLicenseInfo.clusterAlerts.enabled) {
try {
cluster.alerts = {
list: await fetchStatus(
alertsClient,
req.server.plugins.monitoring.info,
undefined,
cluster.cluster_uuid
),
list: Object.keys(alertStatus).reduce((accum, alertName) => {
const value = alertStatus[alertName];
if (value.states && value.states.length) {
accum[alertName] = {
...value,
states: value.states.filter(
(state) => state.state.cluster.clusterUuid === cluster.cluster_uuid
),
};
} else {
accum[alertName] = value;
}
return accum;
}, {}),
alertsMeta: {
enabled: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function alertStatusRoute(server: any, npRoute: RouteDependencies) {
alertsClient,
npRoute.licenseService,
alertTypeIds,
clusterUuid,
[clusterUuid],
filters as CommonAlertFilter[]
);
return response.ok({ body: status });
Expand Down

0 comments on commit a558920

Please sign in to comment.