Skip to content

Commit

Permalink
[monitoring] Rewrite CPU usage rule to not use histogram (elastic#116128
Browse files Browse the repository at this point in the history
)
  • Loading branch information
miltonhultgren committed Jun 8, 2023
1 parent 16b9614 commit 1057874
Show file tree
Hide file tree
Showing 3 changed files with 264 additions and 150 deletions.
5 changes: 1 addition & 4 deletions x-pack/plugins/monitoring/common/types/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,7 @@ export interface AlertNodeStats {
}

export interface AlertCpuUsageNodeStats extends AlertNodeStats {
cpuUsage: number;
containerUsage: number;
containerPeriods: number;
containerQuota: number;
cpuUsage?: number;
}

export interface AlertThreadPoolRejectionsStats {
Expand Down
23 changes: 12 additions & 11 deletions x-pack/plugins/monitoring/server/alerts/cpu_usage_rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ElasticsearchClient } from '@kbn/core/server';
import { Alert } from '@kbn/alerting-plugin/server';
import { RawAlertInstance, SanitizedRule } from '@kbn/alerting-plugin/common';
import { parseDuration } from '@kbn/alerting-plugin/common/parse_duration';
import { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/types';
import { BaseRule } from './base_rule';
import {
AlertData,
Expand Down Expand Up @@ -62,23 +63,23 @@ export class CpuUsageRule extends BaseRule {
const duration = parseDuration(params.duration);
const endMs = +new Date();
const startMs = endMs - duration;
const stats = await fetchCpuUsageNodeStats(
const filterQuery = params.filterQuery
? (JSON.parse(params.filterQuery) as QueryDslQueryContainer)
: undefined;

const stats = await fetchCpuUsageNodeStats({
esClient,
clusters,
clusterUuids: clusters.map((cluster) => cluster.clusterUuid),
startMs,
endMs,
Globals.app.config.ui.max_bucket_size,
params.filterQuery
);
return stats.map((stat) => {
if (Globals.app.config.ui.container.elasticsearch.enabled) {
stat.cpuUsage =
(stat.containerUsage / (stat.containerPeriods * stat.containerQuota * 1000)) * 100;
}
filterQuery,
});

return stats.map((stat) => {
return {
clusterUuid: stat.clusterUuid,
shouldFire: stat.cpuUsage > params.threshold!,
// Should we throw if we failed to compute the cpuUsage?
shouldFire: stat.cpuUsage ? stat.cpuUsage > params.threshold! : false,
severity: AlertSeverity.Danger,
meta: stat,
ccs: stat.ccs,
Expand Down
Loading

0 comments on commit 1057874

Please sign in to comment.