Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Introduced Duration class #2857

Merged
merged 8 commits into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'origin/master' into rmuller/duration
  • Loading branch information
RomainMuller committed Jun 20, 2019
commit dfdd3a3eef93cc006b9a522bcc01376811d593a4
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-apigateway/lib/stage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Construct, Duration, Resource, Stack, toSeconds } from '@aws-cdk/cdk';
import { Construct, Duration, Resource, Stack } from '@aws-cdk/cdk';
import { CfnStage } from './apigateway.generated';
import { Deployment } from './deployment';
import { IRestApi } from './restapi';
Expand Down
11 changes: 10 additions & 1 deletion packages/@aws-cdk/aws-apigateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,16 @@
"awslint": {
"exclude": [
"from-method:@aws-cdk/aws-apigateway.Resource",
"duration-prop-type:@aws-cdk/aws-apigateway.QuotaSettings.period"
"duration-prop-type:@aws-cdk/aws-apigateway.QuotaSettings.period",
"from-method:@aws-cdk/aws-apigateway.ApiKey",
"ref-via-interface:@aws-cdk/aws-apigateway.ApiKeyProps.resources",
"props-physical-name:@aws-cdk/aws-apigateway.DeploymentProps",
"props-physical-name:@aws-cdk/aws-apigateway.MethodProps",
"props-physical-name:@aws-cdk/aws-apigateway.ProxyResourceProps",
"props-physical-name:@aws-cdk/aws-apigateway.ResourceProps",
"props-physical-name:@aws-cdk/aws-apigateway.StageProps",
"props-physical-name:@aws-cdk/aws-apigateway.UsagePlanProps",
"props-physical-name:@aws-cdk/aws-apigateway.LambdaRestApiProps"
]
},
"stability": "experimental"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ export class StepScalingPolicy extends cdk.Construct {

this.lowerAlarm = new cloudwatch.Alarm(this, 'LowerAlarm', {
// Recommended by AutoScaling
metric: props.metric.with({ period: cdk.Duration.minutes(1) }),
metric: props.metric,
period: cdk.Duration.minutes(1), // Recommended by AutoScaling
alarmDescription: 'Lower threshold scaling alarm',
comparisonOperator: cloudwatch.ComparisonOperator.LESS_THAN_OR_EQUAL_TO_THRESHOLD,
evaluationPeriods: 1,
Expand Down Expand Up @@ -132,7 +133,8 @@ export class StepScalingPolicy extends cdk.Construct {

this.upperAlarm = new cloudwatch.Alarm(this, 'UpperAlarm', {
// Recommended by AutoScaling
metric: props.metric.with({ period: cdk.Duration.minutes(1) }),
metric: props.metric,
period: cdk.Duration.minutes(1), // Recommended by AutoScaling
alarmDescription: 'Upper threshold scaling alarm',
comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
evaluationPeriods: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,15 @@ function renderCustomMetric(metric?: cloudwatch.IMetric): CfnScalingPolicy.Custo
* One of the predefined autoscaling metrics
*/
export enum PredefinedMetric {
DynamoDBReadCapacityUtilization = 'DynamoDBReadCapacityUtilization',
DynamoDBWriteCapacityUtilization = 'DynamoDBWriteCapacityUtilization',
ALBRequestCountPerTarget = 'ALBRequestCountPerTarget',
RDSReaderAverageCPUUtilization = 'RDSReaderAverageCPUUtilization',
RDSReaderAverageDatabaseConnections = 'RDSReaderAverageDatabaseConnections',
EC2SpotFleetRequestAverageCPUUtilization = 'EC2SpotFleetRequestAverageCPUUtilization',
EC2SpotFleetRequestAverageNetworkIn = 'EC2SpotFleetRequestAverageNetworkIn',
EC2SpotFleetRequestAverageNetworkOut = 'EC2SpotFleetRequestAverageNetworkOut',
SageMakerVariantInvocationsPerInstance = 'SageMakerVariantInvocationsPerInstance',
ECSServiceAverageCPUUtilization = 'ECSServiceAverageCPUUtilization',
ECSServiceAverageMemoryUtilization = 'ECSServiceAverageMemoryUtilization',
DYNAMODB_READ_CAPACITY_UTILIZATION = 'DynamoDBReadCapacityUtilization',
DYANMODB_WRITE_CAPACITY_UTILIZATION = 'DynamoDBWriteCapacityUtilization',
ALB_REQUEST_COUNT_PER_TARGET = 'ALBRequestCountPerTarget',
RDS_READER_AVERAGE_CPU_UTILIZATION = 'RDSReaderAverageCPUUtilization',
RDS_READER_AVERAGE_DATABASE_CONNECTIONS = 'RDSReaderAverageDatabaseConnections',
EC2_SPOT_FLEET_REQUEST_AVERAGE_CPU_UTILIZATION = 'EC2SpotFleetRequestAverageCPUUtilization',
EC2_SPOT_FLEET_REQUEST_AVERAGE_NETWORK_IN = 'EC2SpotFleetRequestAverageNetworkIn',
EC2_SPOT_FLEET_REQUEST_AVERAGE_NETWORK_OUT = 'EC2SpotFleetRequestAverageNetworkOut',
SAGEMAKER_VARIANT_INVOCATIONS_PER_INSTANCE = 'SageMakerVariantInvocationsPerInstance',
ECS_SERVICE_AVERAGE_CPU_UTILIZATION = 'ECSServiceAverageCPUUtilization',
ECS_SERVICE_AVERAGE_MEMORY_UTILIZATION = 'ECSServiceAverageMemoryUtilization',
}
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements

const { subnetIds } = props.vpc.selectSubnets(props.vpcSubnets);
const asgProps: CfnAutoScalingGroupProps = {
cooldown: props.cooldown !== undefined ? `${props.cooldown}` : undefined,
cooldown: props.cooldown !== undefined ? props.cooldown.toSeconds().toString() : undefined,
minSize: minCapacity.toString(),
maxSize: maxCapacity.toString(),
desiredCapacity: desiredCapacity.toString(),
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-autoscaling/lib/lifecycle-hook.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import iam = require('@aws-cdk/aws-iam');
import { Construct, Duration, IResource, Resource, toSeconds } from '@aws-cdk/cdk';
import { Construct, Duration, IResource, PhysicalName, Resource } from '@aws-cdk/cdk';
import { IAutoScalingGroup } from './auto-scaling-group';
import { CfnLifecycleHook } from './autoscaling.generated';
import { ILifecycleHookTarget } from './lifecycle-hook-target';
Expand Down Expand Up @@ -106,7 +106,7 @@ export class LifecycleHook extends Resource implements ILifecycleHook {
autoScalingGroupName: props.autoScalingGroup.autoScalingGroupName,
defaultResult: props.defaultResult,
heartbeatTimeout: props.heartbeatTimeout && props.heartbeatTimeout.toSeconds(),
lifecycleHookName: props.lifecycleHookName,
lifecycleHookName: this.physicalName.value,
lifecycleTransition: props.lifecycleTransition,
notificationMetadata: props.notificationMetadata,
notificationTargetArn: targetProps.notificationTargetArn,
Expand Down
6 changes: 4 additions & 2 deletions packages/@aws-cdk/aws-autoscaling/lib/step-scaling-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ export class StepScalingPolicy extends cdk.Construct {

this.lowerAlarm = new cloudwatch.Alarm(this, 'LowerAlarm', {
// Recommended by AutoScaling
metric: props.metric.with({ period: cdk.Duration.minutes(1) }),
metric: props.metric,
period: cdk.Duration.minutes(1), // Recommended by AutoScaling
alarmDescription: 'Lower threshold scaling alarm',
comparisonOperator: cloudwatch.ComparisonOperator.LESS_THAN_OR_EQUAL_TO_THRESHOLD,
evaluationPeriods: 1,
Expand Down Expand Up @@ -133,7 +134,8 @@ export class StepScalingPolicy extends cdk.Construct {

this.upperAlarm = new cloudwatch.Alarm(this, 'UpperAlarm', {
// Recommended by AutoScaling
metric: props.metric.with({ period: cdk.Duration.minutes(1) }),
metric: props.metric,
period: cdk.Duration.minutes(1), // Recommended by AutoScaling
alarmDescription: 'Upper threshold scaling alarm',
comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
evaluationPeriods: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class TargetTrackingScalingPolicy extends cdk.Construct {
throw new Error(`Exactly one of 'customMetric' or 'predefinedMetric' must be specified.`);
}

if (props.predefinedMetric === PredefinedMetric.ALBRequestCountPerTarget && !props.resourceLabel) {
if (props.predefinedMetric === PredefinedMetric.ALB_REQUEST_COUNT_PER_TARGET && !props.resourceLabel) {
throw new Error('When tracking the ALBRequestCountPerTarget metric, the ALB identifier must be supplied in resourceLabel');
}

Expand Down
10 changes: 6 additions & 4 deletions packages/@aws-cdk/aws-cloudfront/lib/web_distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,10 +606,12 @@ export class CloudFrontWebDistribution extends cdk.Construct implements IDistrib
? {
httpPort: originConfig.customOriginSource.httpPort || 80,
httpsPort: originConfig.customOriginSource.httpsPort || 443,
originKeepaliveTimeout: originConfig.customOriginSource.originKeepaliveTimeout && originConfig.customOriginSource.originKeepaliveTimeout.toSeconds() || 5,
originReadTimeout: originConfig.customOriginSource.originReadTimeout && originConfig.customOriginSource.originReadTimeout.toSeconds() || 30,
originProtocolPolicy: originConfig.customOriginSource.originProtocolPolicy || OriginProtocolPolicy.HttpsOnly,
originSslProtocols: originConfig.customOriginSource.allowedOriginSSLVersions || [OriginSslPolicy.TLSv1_2]
originKeepaliveTimeout: originConfig.customOriginSource.originKeepaliveTimeout
&& originConfig.customOriginSource.originKeepaliveTimeout.toSeconds() || 5,
originReadTimeout: originConfig.customOriginSource.originReadTimeout
&& originConfig.customOriginSource.originReadTimeout.toSeconds() || 30,
originProtocolPolicy: originConfig.customOriginSource.originProtocolPolicy || OriginProtocolPolicy.HTTPS_ONLY,
originSslProtocols: originConfig.customOriginSource.allowedOriginSSLVersions || [OriginSslPolicy.TLS_V1_2]
}
: undefined
};
Expand Down
69 changes: 10 additions & 59 deletions packages/@aws-cdk/aws-cloudwatch/lib/alarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class Alarm extends Resource implements IAlarm {
...dropUndef(config),
...dropUndef({
// Alarm overrides
period: props.periodSec,
period: props.period && props.period.toSeconds(),
statistic: props.statistic && normalizeStatistic(props.statistic),
})
});
Expand All @@ -167,7 +167,7 @@ export class Alarm extends Resource implements IAlarm {
this.metric = props.metric;
this.annotation = {
// tslint:disable-next-line:max-line-length
label: `${this.metric.label || this.metric.metricName} ${OPERATOR_SYMBOLS[comparisonOperator]} ${props.threshold} for ${props.evaluationPeriods} datapoints within ${describePeriod(props.evaluationPeriods * props.metric.period.toSeconds())}`,
label: `${this.metric} ${OPERATOR_SYMBOLS[comparisonOperator]} ${props.threshold} for ${props.evaluationPeriods} datapoints within ${describePeriod(props.evaluationPeriods * config.period)}`,
value: props.threshold,
};
}
Expand Down Expand Up @@ -244,61 +244,12 @@ function describePeriod(seconds: number) {
return seconds + ' seconds';
}

/**
* Return the JSON structure which represents the given metric in an alarm.
*/
function metricJson(metric: Metric): AlarmMetricJson {
const stat = parseStatistic(metric.statistic);

const dims = metric.dimensionsAsList();

return {
dimensions: dims.length > 0 ? dims : undefined,
namespace: metric.namespace,
metricName: metric.metricName,
period: metric.period.toSeconds(),
statistic: stat.type === 'simple' ? stat.statistic : undefined,
extendedStatistic: stat.type === 'percentile' ? 'p' + stat.percentile : undefined,
unit: metric.unit
};
}

/**
* Properties used to construct the Metric identifying part of an Alarm
*/
interface AlarmMetricJson {
/**
* The dimensions to apply to the alarm
*/
readonly dimensions?: Dimension[];

/**
* Namespace of the metric
*/
readonly namespace: string;

/**
* Name of the metric
*/
readonly metricName: string;

/**
* How many seconds to aggregate over
*/
readonly period: number;

/**
* Simple aggregation function to use
*/
readonly statistic?: Statistic;

/**
* Percentile aggregation function to use
*/
readonly extendedStatistic?: string;

/**
* The unit of the alarm
*/
readonly unit?: Unit;
function dropUndef<T extends object>(x: T): T {
const ret: any = {};
for (const [key, value] of Object.entries(x)) {
if (value !== undefined) {
ret[key] = value;
}
}
return ret;
}
8 changes: 4 additions & 4 deletions packages/@aws-cdk/aws-cloudwatch/lib/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,10 @@ function metricJson(metric: IMetric, yAxis: string): any[] {
// Options
ret.push({
yAxis,
label: metric.label,
color: metric.color,
period: metric.period.toSeconds(),
stat: stat.type === 'simple' ? stat.statistic : 'p' + stat.percentile.toString(),
label: config.label,
color: config.color,
period: config.period,
stat: config.statistic,
});

return ret;
Expand Down
113 changes: 2 additions & 111 deletions packages/@aws-cdk/aws-cloudwatch/lib/metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export class Metric implements IMetric {
dimensions: dims.length > 0 ? dims : undefined,
namespace: this.namespace,
metricName: this.metricName,
period: this.periodSec,
period: this.period.toSeconds(),
statistic: stat.type === 'simple' ? stat.statistic : undefined,
extendedStatistic: stat.type === 'percentile' ? 'p' + stat.percentile : undefined,
unit: this.unit
Expand All @@ -195,7 +195,7 @@ export class Metric implements IMetric {
dimensions: this.dimensionsAsList(),
namespace: this.namespace,
metricName: this.metricName,
period: this.periodSec,
period: this.period.toSeconds(),
statistic: this.statistic,
unit: this.unit,
color: this.color,
Expand Down Expand Up @@ -223,115 +223,6 @@ export class Metric implements IMetric {
}
}

/**
* Metric dimension
*/
export interface Dimension {
/**
* Name of the dimension
*/
readonly name: string;

/**
* Value of the dimension
*/
readonly value: any;
}

/**
* Statistic to use over the aggregation period
*/
export enum Statistic {
SampleCount = 'SampleCount',
Average = 'Average',
Sum = 'Sum',
Minimum = 'Minimum',
Maximum = 'Maximum',
}

/**
* Unit for metric
*/
export enum Unit {
Seconds = 'Seconds',
Microseconds = 'Microseconds',
Milliseconds = 'Milliseconds',
Bytes_ = 'Bytes',
Kilobytes = 'Kilobytes',
Megabytes = 'Megabytes',
Gigabytes = 'Gigabytes',
Terabytes = 'Terabytes',
Bits = 'Bits',
Kilobits = 'Kilobits',
Megabits = 'Megabits',
Gigabits = 'Gigabits',
Terabits = 'Terabits',
Percent = 'Percent',
Count = 'Count',
BytesPerSecond = 'Bytes/Second',
KilobytesPerSecond = 'Kilobytes/Second',
MegabytesPerSecond = 'Megabytes/Second',
GigabytesPerSecond = 'Gigabytes/Second',
TerabytesPerSecond = 'Terabytes/Second',
BitsPerSecond = 'Bits/Second',
KilobitsPerSecond = 'Kilobits/Second',
MegabitsPerSecond = 'Megabits/Second',
GigabitsPerSecond = 'Gigabits/Second',
TerabitsPerSecond = 'Terabits/Second',
CountPerSecond = 'Count/Second',
None = 'None'
}

/**
* Properties of a metric that can be changed
*/
export interface MetricOptions {
/**
* Dimensions of the metric
*
* @default - No dimensions.
*/
readonly dimensions?: DimensionHash;

/**
* The period over which the specified statistic is applied.
*
* @default Duration.minutes(5)
*/
readonly period?: cdk.Duration;

/**
* What function to use for aggregating.
*
* Can be one of the following:
*
* - "Minimum" | "min"
* - "Maximum" | "max"
* - "Average" | "avg"
* - "Sum" | "sum"
* - "SampleCount | "n"
* - "pNN.NN"
*
* @default Average
*/
readonly statistic?: string;

/**
* Unit for the metric that is associated with the alarm
*/
readonly unit?: Unit;

/**
* Label for this metric when added to a Graph in a Dashboard
*/
readonly label?: string;

/**
* Color for this metric when added to a Graph in a Dashboard
*/
readonly color?: string;
}

/**
* Properties needed to make an alarm from a metric
*/
Expand Down
8 changes: 7 additions & 1 deletion packages/@aws-cdk/aws-cloudwatch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,14 @@
"@aws-cdk/aws-iam": "^0.35.0",
"@aws-cdk/cdk": "^0.35.0"
},
"awslint": {
"exclude": [
"duration-prop-type:@aws-cdk/aws-cloudwatch.MetricAlarmConfig.period",
"duration-prop-type:@aws-cdk/aws-cloudwatch.MetricGraphConfig.period"
]
},
"engines": {
"node": ">= 8.10.0"
},
"stability": "experimental"
}
}
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.