forked from cdklabs/cdk-watchful
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add state machine support * reorder how things are added
- Loading branch information
1 parent
085636c
commit 238e2af
Showing
11 changed files
with
417 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,37 @@ | ||
import { Metric, Statistic } from '@aws-cdk/aws-cloudwatch'; | ||
import { Duration } from '@aws-cdk/core'; | ||
|
||
const enum Metrics { | ||
ExecutionsStarted = 'ExecutionsStarted', | ||
ExecutionsSucceeded = 'ExecutionsSucceeded', | ||
ExecutionsFailed = 'ExecutionsFailed', | ||
ExecutionsAborted = 'ExecutionsAborted', | ||
ExecutionThrottled ='ExecutionThrottled', | ||
ExecutionsTimedOut = 'ExecutionsTimedOut' | ||
} | ||
|
||
const Namespace = 'AWS/States'; | ||
|
||
export class StateMachineMetricFactory { | ||
metricExecutions(stateMachineArn: string) { | ||
return { | ||
total: this.metric(Metrics.ExecutionsStarted, stateMachineArn).with({ label: 'Total', statistic: Statistic.SUM }), | ||
succeeded: this.metric(Metrics.ExecutionsSucceeded, stateMachineArn).with({ label: 'Failed Succeeded', statistic: Statistic.SUM }), | ||
failed: this.metric(Metrics.ExecutionsFailed, stateMachineArn).with({ label: 'Failed Executions', statistic: Statistic.SUM }), | ||
aborted: this.metric(Metrics.ExecutionsAborted, stateMachineArn).with({ label: 'Aborted Executions', statistic: Statistic.SUM }), | ||
throttled: this.metric(Metrics.ExecutionThrottled, stateMachineArn).with({ label: 'Executions Throttled', statistic: Statistic.SUM }), | ||
timedOut: this.metric(Metrics.ExecutionsTimedOut, stateMachineArn).with({ label: 'Executions TimedOut', statistic: Statistic.SUM }), | ||
}; | ||
} | ||
|
||
protected metric(metric: Metrics, stateMachineArn: string) { | ||
return new Metric({ | ||
metricName: metric, | ||
namespace: Namespace, | ||
period: Duration.minutes(1), | ||
dimensions: { | ||
StateMachineArn: stateMachineArn, | ||
}, | ||
}); | ||
} | ||
} |
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,74 @@ | ||
import { ComparisonOperator, GraphWidget } from '@aws-cdk/aws-cloudwatch'; | ||
import { StateMachine } from '@aws-cdk/aws-stepfunctions'; | ||
import { Construct, Duration } from '@aws-cdk/core'; | ||
import { IWatchful } from './api'; | ||
import { StateMachineMetricFactory } from './monitoring/aws/state-machine/metrics'; | ||
|
||
export interface WatchStateMachineOptions { | ||
/** | ||
* Alarm when execution failures reach this threshold over 1 minute. | ||
* | ||
* @default 1 any execution failure will trigger the alarm | ||
*/ | ||
readonly metricFailedThreshold?: number; | ||
} | ||
|
||
export interface WatchStateMachineProps extends WatchStateMachineOptions { | ||
readonly title: string; | ||
readonly watchful: IWatchful; | ||
readonly stateMachine: StateMachine; | ||
} | ||
|
||
export class WatchStateMachine extends Construct { | ||
private readonly title: string; | ||
private readonly watchful: IWatchful; | ||
private readonly stateMachine: StateMachine; | ||
private readonly metricFailedThreshold: number; | ||
|
||
private readonly metrics: StateMachineMetricFactory; | ||
|
||
constructor(scope: Construct, id: string, props: WatchStateMachineProps) { | ||
super(scope, id); | ||
|
||
this.title = props.title; | ||
this.watchful = props.watchful; | ||
this.stateMachine = props.stateMachine; | ||
|
||
this.metricFailedThreshold = props.metricFailedThreshold ?? 1; | ||
|
||
this.metrics = new StateMachineMetricFactory(); | ||
|
||
this.createLinks(); | ||
this.createExecutionMetrics(); | ||
} | ||
|
||
private createExecutionMetrics() { | ||
const execMetrics = this.metrics.metricExecutions(this.stateMachine.stateMachineArn); | ||
const { failed } = execMetrics; | ||
failed.createAlarm(this, 'ExecutionFailures', { | ||
alarmDescription: `at ${this.metricFailedThreshold}`, | ||
threshold: this.metricFailedThreshold, | ||
period: Duration.minutes(5), | ||
comparisonOperator: ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD, | ||
evaluationPeriods: 1, | ||
statistic: 'sum', | ||
}); | ||
|
||
this.watchful.addWidgets(new GraphWidget({ | ||
title: 'Overall Execution/min', | ||
width: 12, | ||
stacked: false, | ||
left: Object.values(execMetrics), | ||
leftAnnotations: [{ value: this.metricFailedThreshold, color: '#ff0000', label: 'Execution Failure Alarm' }], | ||
})); | ||
} | ||
|
||
private createLinks() { | ||
this.watchful.addSection(this.title, { | ||
links: [{ | ||
title: 'Amazon State Machine', | ||
url: `https://console.aws.amazon.com/states/home?region=${this.stateMachine.stack.region}#/statemachines/view/${this.stateMachine.stateMachineArn}`, | ||
}], | ||
}); | ||
} | ||
} |
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.