-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cloudwatch): CompositeAlarm (#8498)
* fix #8462 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
1 parent
5053eb7
commit 1e6d293
Showing
12 changed files
with
627 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { IResource, Resource } from '@aws-cdk/core'; | ||
import { IAlarmAction } from './alarm-action'; | ||
|
||
/** | ||
* Interface for Alarm Rule. | ||
*/ | ||
export interface IAlarmRule { | ||
|
||
/** | ||
* serialized representation of Alarm Rule to be used when building the Composite Alarm resource. | ||
*/ | ||
renderAlarmRule(): string; | ||
|
||
} | ||
|
||
/** | ||
* Represents a CloudWatch Alarm | ||
*/ | ||
export interface IAlarm extends IAlarmRule, IResource { | ||
/** | ||
* Alarm ARN (i.e. arn:aws:cloudwatch:<region>:<account-id>:alarm:Foo) | ||
* | ||
* @attribute | ||
*/ | ||
readonly alarmArn: string; | ||
|
||
/** | ||
* Name of the alarm | ||
* | ||
* @attribute | ||
*/ | ||
readonly alarmName: string; | ||
} | ||
|
||
/** | ||
* The base class for Alarm and CompositeAlarm resources. | ||
*/ | ||
export abstract class AlarmBase extends Resource implements IAlarm { | ||
|
||
/** | ||
* @attribute | ||
*/ | ||
public abstract readonly alarmArn: string; | ||
public abstract readonly alarmName: string; | ||
|
||
protected alarmActionArns?: string[]; | ||
protected insufficientDataActionArns?: string[]; | ||
protected okActionArns?: string[]; | ||
|
||
/** | ||
* AlarmRule indicating ALARM state for Alarm. | ||
*/ | ||
public renderAlarmRule(): string { | ||
return `ALARM(${this.alarmArn})`; | ||
} | ||
|
||
/** | ||
* Trigger this action if the alarm fires | ||
* | ||
* Typically the ARN of an SNS topic or ARN of an AutoScaling policy. | ||
*/ | ||
public addAlarmAction(...actions: IAlarmAction[]) { | ||
if (this.alarmActionArns === undefined) { | ||
this.alarmActionArns = []; | ||
} | ||
|
||
this.alarmActionArns.push(...actions.map(a => a.bind(this, this).alarmActionArn)); | ||
} | ||
|
||
/** | ||
* Trigger this action if there is insufficient data to evaluate the alarm | ||
* | ||
* Typically the ARN of an SNS topic or ARN of an AutoScaling policy. | ||
*/ | ||
public addInsufficientDataAction(...actions: IAlarmAction[]) { | ||
if (this.insufficientDataActionArns === undefined) { | ||
this.insufficientDataActionArns = []; | ||
} | ||
|
||
this.insufficientDataActionArns.push(...actions.map(a => a.bind(this, this).alarmActionArn)); | ||
} | ||
|
||
/** | ||
* Trigger this action if the alarm returns from breaching state into ok state | ||
* | ||
* Typically the ARN of an SNS topic or ARN of an AutoScaling policy. | ||
*/ | ||
public addOkAction(...actions: IAlarmAction[]) { | ||
if (this.okActionArns === undefined) { | ||
this.okActionArns = []; | ||
} | ||
|
||
this.okActionArns.push(...actions.map(a => a.bind(this, this).alarmActionArn)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { IAlarm, IAlarmRule } from './alarm-base'; | ||
|
||
/** | ||
* Enumeration indicates state of Alarm used in building Alarm Rule. | ||
*/ | ||
export enum AlarmState { | ||
|
||
/** | ||
* State indicates resource is in ALARM | ||
*/ | ||
ALARM = 'ALARM', | ||
|
||
/** | ||
* State indicates resource is not in ALARM | ||
*/ | ||
OK = 'OK', | ||
|
||
/** | ||
* State indicates there is not enough data to determine is resource is in ALARM | ||
*/ | ||
INSUFFICIENT_DATA = 'INSUFFICIENT_DATA', | ||
|
||
} | ||
|
||
/** | ||
* Enumeration of supported Composite Alarms operators. | ||
*/ | ||
enum Operator { | ||
|
||
AND = 'AND', | ||
OR = 'OR', | ||
NOT = 'NOT', | ||
|
||
} | ||
|
||
/** | ||
* Class with static functions to build AlarmRule for Composite Alarms. | ||
*/ | ||
export class AlarmRule { | ||
|
||
/** | ||
* function to join all provided AlarmRules with AND operator. | ||
* | ||
* @param operands IAlarmRules to be joined with AND operator. | ||
*/ | ||
public static allOf(...operands: IAlarmRule[]): IAlarmRule { | ||
return this.concat(Operator.AND, ...operands); | ||
} | ||
|
||
/** | ||
* function to join all provided AlarmRules with OR operator. | ||
* | ||
* @param operands IAlarmRules to be joined with OR operator. | ||
*/ | ||
public static anyOf(...operands: IAlarmRule[]): IAlarmRule { | ||
return this.concat(Operator.OR, ...operands); | ||
} | ||
|
||
/** | ||
* function to wrap provided AlarmRule in NOT operator. | ||
* | ||
* @param operand IAlarmRule to be wrapped in NOT operator. | ||
*/ | ||
public static not(operand: IAlarmRule): IAlarmRule { | ||
// tslint:disable-next-line:new-parens | ||
return new class implements IAlarmRule { | ||
public renderAlarmRule(): string { | ||
return `(NOT (${operand.renderAlarmRule()}))`; | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* function to build TRUE/FALSE intent for Rule Expression. | ||
* | ||
* @param value boolean value to be used in rule expression. | ||
*/ | ||
public static fromBoolean(value: boolean): IAlarmRule { | ||
// tslint:disable-next-line:new-parens | ||
return new class implements IAlarmRule { | ||
public renderAlarmRule(): string { | ||
return `${String(value).toUpperCase()}`; | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* function to build Rule Expression for given IAlarm and AlarmState. | ||
* | ||
* @param alarm IAlarm to be used in Rule Expression. | ||
* @param alarmState AlarmState to be used in Rule Expression. | ||
*/ | ||
public static fromAlarm(alarm: IAlarm, alarmState: AlarmState): IAlarmRule { | ||
// tslint:disable-next-line:new-parens | ||
return new class implements IAlarmRule { | ||
public renderAlarmRule(): string { | ||
return `${alarmState}(${alarm.alarmArn})`; | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* function to build Rule Expression for given Alarm Rule string. | ||
* | ||
* @param alarmRule string to be used in Rule Expression. | ||
*/ | ||
public static fromString(alarmRule: string): IAlarmRule { | ||
// tslint:disable-next-line:new-parens | ||
return new class implements IAlarmRule { | ||
public renderAlarmRule(): string { | ||
return alarmRule; | ||
} | ||
}; | ||
} | ||
|
||
private static concat(operator: Operator, ...operands: IAlarmRule[]): IAlarmRule { | ||
// tslint:disable-next-line:new-parens | ||
return new class implements IAlarmRule { | ||
public renderAlarmRule(): string { | ||
const expression = operands | ||
.map(operand => `${operand.renderAlarmRule()}`) | ||
.join(` ${operator} `); | ||
return `(${expression})`; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.