Skip to content

Commit

Permalink
Fixed resolve state and throttle
Browse files Browse the repository at this point in the history
  • Loading branch information
igoristic committed Sep 22, 2020
1 parent 663e7dd commit b32226a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 20 deletions.
9 changes: 5 additions & 4 deletions x-pack/plugins/monitoring/server/alerts/base_alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export class BaseAlert {
);

const data = await this.fetchData(params, callCluster, clusters, uiSettings, availableCcs);
this.processData(data, clusters, services, logger);
return await this.processData(data, clusters, services, logger, state);
}

protected async fetchData(
Expand All @@ -252,12 +252,13 @@ export class BaseAlert {
throw new Error('Child classes must implement `fetchData`');
}

protected processData(
protected async processData(
data: AlertData[],
clusters: AlertCluster[],
services: AlertServices,
logger: Logger
) {
logger: Logger,
instanceState: unknown
): Promise<void | Record<string, any>> {
for (const item of data) {
const cluster = clusters.find((c: AlertCluster) => c.clusterUuid === item.clusterUuid);
if (!cluster) {
Expand Down
6 changes: 5 additions & 1 deletion x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,11 @@ export class CpuUsageAlert extends BaseAlert {
}
}

protected processData(data: AlertData[], clusters: AlertCluster[], services: AlertServices) {
protected async processData(
data: AlertData[],
clusters: AlertCluster[],
services: AlertServices
) {
const currentUTC = +new Date();
for (const cluster of clusters) {
const nodes = data.filter((node) => node.clusterUuid === cluster.clusterUuid);
Expand Down
87 changes: 72 additions & 15 deletions x-pack/plugins/monitoring/server/alerts/disk_usage_alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { IUiSettingsClient } from 'kibana/server';
import { IUiSettingsClient, Logger } from 'kibana/server';
import { i18n } from '@kbn/i18n';
import { BaseAlert } from './base_alert';
import {
Expand Down Expand Up @@ -207,10 +207,6 @@ export class DiskUsageAlert extends BaseAlert {
item: AlertData | null,
cluster: AlertCluster
) {
if (!alertStates.length) {
return;
}

const ccs = alertStates.find((state) => state.ccs)?.ccs;
const firingNodes = alertStates.filter(
(alertState) => alertState.ui.isFiring
Expand Down Expand Up @@ -297,8 +293,58 @@ export class DiskUsageAlert extends BaseAlert {
}
}

protected processData(data: AlertData[], clusters: AlertCluster[], services: AlertServices) {
const currentUTC = +new Date();
private executeDeltas(
services: AlertServices,
cluster: AlertCluster,
newAlertStates: AlertDiskUsageState[],
oldAlertStates: AlertDiskUsageState[]
) {
const deltaFiringStates = [];
const deltaResolvedStates = [];
const deltaInstanceIdPrefix: string = `.monitoring:${this.type}:${
cluster.clusterUuid
}:${Date.now()}:`;

for (const newAlertState of newAlertStates) {
const relatedOldState = oldAlertStates.find(
(oldState) =>
oldState.nodeId === newAlertState.nodeId &&
oldState.ui.isFiring !== newAlertState.ui.isFiring &&
oldState.ui.resolvedMS !== newAlertState.ui.resolvedMS
);
if (!relatedOldState) {
if (newAlertState.ui.isFiring) {
deltaFiringStates.push(newAlertState);
} else if (newAlertState.ui.resolvedMS) {
deltaResolvedStates.push(newAlertState);
}
}
}

if (deltaFiringStates.length + deltaResolvedStates.length === newAlertStates.length) {
/** No delta changes, so we do nothing */
return;
}

if (deltaFiringStates.length) {
const instance = services.alertInstanceFactory(`${deltaInstanceIdPrefix}:firing`);
this.executeActions(instance, { alertStates: deltaFiringStates }, null, cluster);
}

if (deltaResolvedStates.length) {
const instance = services.alertInstanceFactory(`${deltaInstanceIdPrefix}:resolved`);
this.executeActions(instance, { alertStates: deltaResolvedStates }, null, cluster);
}
}

protected async processData(
data: AlertData[],
clusters: AlertCluster[],
services: AlertServices,
logger: Logger,
state: any
) {
const currentUTC = Date.now();
for (const cluster of clusters) {
const nodes = data.filter((node) => node.clusterUuid === cluster.clusterUuid);
if (!nodes.length) {
Expand All @@ -307,9 +353,9 @@ export class DiskUsageAlert extends BaseAlert {

const instanceId = `.monitoring:${this.type}:${cluster.clusterUuid}`;
const instance = services.alertInstanceFactory(instanceId);
const state = instance.getState() as AlertInstanceState;
const newAlertStates: AlertInstanceState['alertStates'] = [];
const oldAlertStates = (state?.alertStates || []) as AlertDiskUsageState[];
const instanceState = instance.getState() as AlertInstanceState;
const newAlertStates: AlertDiskUsageState[] = [];
const oldAlertStates = (instanceState?.alertStates || []) as AlertDiskUsageState[];

for (const node of nodes) {
const stat = node.meta as AlertDiskUsageState;
Expand All @@ -334,13 +380,24 @@ export class DiskUsageAlert extends BaseAlert {
}

nodeState.ui.message = this.getUiMessage(nodeState, node);
}

const alertInstanceState = { alertStates: newAlertStates };
instance.replaceState(alertInstanceState);
if (newAlertStates.length && !instance.hasScheduledActions()) {
this.executeActions(instance, alertInstanceState, null, cluster);
}
/**
* Addresses lost delta triggers if executed between throttle states, context:
* https://github.com/elastic/kibana/pull/75419#discussion_r490497639. This is
* a temporary solution until: https://github.com/elastic/kibana/issues/49405 is implemented
*/
this.executeDeltas(services, cluster, newAlertStates, oldAlertStates);

const alertInstanceState = { alertStates: newAlertStates };
instance.replaceState(alertInstanceState);
if (newAlertStates.length && !instance.hasScheduledActions()) {
this.executeActions(instance, alertInstanceState, null, cluster);
state.lastExecutedAction = currentUTC;
}
}

state.lastChecked = currentUTC;
return state;
}
}

0 comments on commit b32226a

Please sign in to comment.