Skip to content

Commit

Permalink
feat(cdk): metric functions now automatically generated (#1617)
Browse files Browse the repository at this point in the history
`cfnspec` now has a mechanism to specify what metrics exist for a particular resource
type, and `cfn2ts` will generate an augmentation file with `metricXxx()` methods
for those resources automatically.

Lambda, SNS and SQS now have their metric methods generated this way.
  • Loading branch information
rix0rrr authored Feb 6, 2019
1 parent ff99403 commit 36cfca8
Show file tree
Hide file tree
Showing 32 changed files with 485 additions and 158 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-lambda/lib/alias.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import iam = require('@aws-cdk/aws-iam');
import cdk = require('@aws-cdk/cdk');
import { FunctionBase, FunctionImportProps, IFunction } from './lambda-ref';
import { FunctionBase, FunctionImportProps, IFunction } from './function-base';
import { Version } from './lambda-version';
import { CfnAlias } from './lambda.generated';
import { Permission } from './permission';
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cdk = require('@aws-cdk/cdk');
import { IFunction } from './lambda-ref';
import { IFunction } from './function-base';
import { CfnEventSourceMapping } from './lambda.generated';

export interface EventSourceMappingProps {
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-lambda/lib/event-source.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FunctionBase } from './lambda-ref';
import { FunctionBase } from './function-base';

/**
* An abstract class which represents an AWS Lambda event source.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,6 @@ export interface IFunction extends cdk.IConstruct, events.IEventRuleTarget, logs
*/
metric(metricName: string, props?: cloudwatch.MetricCustomization): cloudwatch.Metric;

/**
* Metric for the Errors executing this Lambda
*
* @default sum over 5 minutes
*/
metricErrors(props?: cloudwatch.MetricCustomization): cloudwatch.Metric;

/**
* Metric for the Duration of this Lambda
*
Expand Down Expand Up @@ -275,54 +268,6 @@ export abstract class FunctionBase extends cdk.Construct implements IFunction {
}
}

/**
* Return the given named metric for this Lambda
*/
public metric(metricName: string, props?: cloudwatch.MetricCustomization): cloudwatch.Metric {
return new cloudwatch.Metric({
namespace: 'AWS/Lambda',
metricName,
dimensions: { FunctionName: this.functionName },
...props
});
}

/**
* Metric for the Errors executing this Lambda
*
* @default sum over 5 minutes
*/
public metricErrors(props?: cloudwatch.MetricCustomization): cloudwatch.Metric {
return this.metric('Errors', { statistic: 'sum', ...props });
}

/**
* Metric for the Duration of this Lambda
*
* @default average over 5 minutes
*/
public metricDuration(props?: cloudwatch.MetricCustomization): cloudwatch.Metric {
return this.metric('Duration', props);
}

/**
* Metric for the number of invocations of this Lambda
*
* @default sum over 5 minutes
*/
public metricInvocations(props?: cloudwatch.MetricCustomization): cloudwatch.Metric {
return this.metric('Invocations', { statistic: 'sum', ...props });
}

/**
* Metric for the number of throttled invocations of this Lambda
*
* @default sum over 5 minutes
*/
public metricThrottles(props?: cloudwatch.MetricCustomization): cloudwatch.Metric {
return this.metric('Throttles', { statistic: 'sum', ...props });
}

public logSubscriptionDestination(sourceLogGroup: logs.ILogGroup): logs.LogSubscriptionDestination {
const arn = sourceLogGroup.logGroupArn;

Expand Down Expand Up @@ -419,4 +364,4 @@ export abstract class FunctionBase extends cdk.Construct implements IFunction {
throw new Error(`Invalid principal type for Lambda permission statement: ${JSON.stringify(this.node.resolve(principal))}. ` +
'Supported: AccountPrincipal, ServicePrincipal');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import iam = require('@aws-cdk/aws-iam');
import sqs = require('@aws-cdk/aws-sqs');
import cdk = require('@aws-cdk/cdk');
import { Code } from './code';
import { FunctionBase, FunctionImportProps, IFunction } from './lambda-ref';
import { FunctionBase, FunctionImportProps, IFunction } from './function-base';
import { Version } from './lambda-version';
import { CfnFunction } from './lambda.generated';
import { ILayerVersion } from './layers';
Expand Down
6 changes: 4 additions & 2 deletions packages/@aws-cdk/aws-lambda/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from './alias';
export * from './lambda-ref';
export * from './lambda';
export * from './function-base';
export * from './function';
export * from './layers';
export * from './permission';
export * from './pipeline-action';
Expand All @@ -13,3 +13,5 @@ export * from './event-source-mapping';

// AWS::Lambda CloudFormation Resources:
export * from './lambda.generated';

import './lambda-augmentations.generated';
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-lambda/lib/lambda-version.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Construct } from '@aws-cdk/cdk';
import { IFunction } from './lambda-ref';
import { IFunction } from './function-base';
import { CfnVersion } from './lambda.generated';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-lambda/lib/pipeline-action.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import codepipeline = require('@aws-cdk/aws-codepipeline-api');
import iam = require('@aws-cdk/aws-iam');
import cdk = require('@aws-cdk/cdk');
import { IFunction } from './lambda-ref';
import { IFunction } from './function-base';

/**
* Common properties for creating a {@link PipelineInvokeAction} -
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-lambda/lib/singleton-lambda.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import iam = require('@aws-cdk/aws-iam');
import cdk = require('@aws-cdk/cdk');
import { Function as LambdaFunction, FunctionProps } from './lambda';
import { FunctionBase, FunctionImportProps, IFunction } from './lambda-ref';
import { Function as LambdaFunction, FunctionProps } from './function';
import { FunctionBase, FunctionImportProps, IFunction } from './function-base';
import { Permission } from './permission';

/**
Expand Down
21 changes: 21 additions & 0 deletions packages/@aws-cdk/aws-lambda/test/test.lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,27 @@ export = {
test.done();
},

'Can use metricErrors on a lambda Function'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const fn = new lambda.Function(stack, 'Function', {
code: lambda.Code.inline('xxx'),
handler: 'index.handler',
runtime: lambda.Runtime.NodeJS810,
});

// THEN
test.deepEqual(stack.node.resolve(fn.metricErrors()), {
dimensions: { FunctionName: { Ref: 'Function76856677' }},
namespace: 'AWS/Lambda',
metricName: 'Errors',
periodSec: 300,
statistic: 'Sum',
});

test.done();
},

'addEventSource calls bind'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down
4 changes: 3 additions & 1 deletion packages/@aws-cdk/aws-sns/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export * from './policy';
export * from './topic';
export * from './topic-ref';
export * from './topic-base';
export * from './subscription';

// AWS::SNS CloudFormation Resources:
export * from './sns.generated';

import './sns-augmentations.generated';
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-sns/lib/policy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PolicyDocument } from '@aws-cdk/aws-iam';
import { Construct } from '@aws-cdk/cdk';
import { CfnTopicPolicy } from './sns.generated';
import { ITopic } from './topic-ref';
import { ITopic } from './topic-base';

export interface TopicPolicyProps {
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-sns/lib/subscription.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Construct } from '@aws-cdk/cdk';
import { CfnSubscription } from './sns.generated';
import { ITopic } from './topic-ref';
import { ITopic } from './topic-base';

/**
* Properties for creating a new subscription
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,39 +82,6 @@ export interface ITopic extends
* Grant topic publishing permissions to the given identity
*/
grantPublish(identity?: iam.IPrincipal): void;

/**
* Construct a Metric object for the current topic for the given metric
*/
metric(metricName: string, props?: cloudwatch.MetricCustomization): cloudwatch.Metric;

/**
* Metric for the size of messages published through this topic
*
* @default average over 5 minutes
*/
metricPublishSize(props?: cloudwatch.MetricCustomization): cloudwatch.Metric;

/**
* Metric for the number of messages published through this topic
*
* @default sum over 5 minutes
*/
metricNumberOfMessagesPublished(props?: cloudwatch.MetricCustomization): cloudwatch.Metric;

/**
* Metric for the number of messages that failed to publish through this topic
*
* @default sum over 5 minutes
*/
metricNumberOfMessagesFailed(props?: cloudwatch.MetricCustomization): cloudwatch.Metric;

/**
* Metric for the number of messages that were successfully delivered through this topic
*
* @default sum over 5 minutes
*/
metricNumberOfMessagesDelivered(props?: cloudwatch.MetricCustomization): cloudwatch.Metric;
}

/**
Expand Down Expand Up @@ -329,54 +296,6 @@ export abstract class TopicBase extends cdk.Construct implements ITopic {
return this.topicArn;
}

/**
* Construct a Metric object for the current topic for the given metric
*/
public metric(metricName: string, props?: cloudwatch.MetricCustomization): cloudwatch.Metric {
return new cloudwatch.Metric({
namespace: 'AWS/SNS',
dimensions: { TopicName: this.topicName },
metricName,
...props
});
}

/**
* Metric for the size of messages published through this topic
*
* @default average over 5 minutes
*/
public metricPublishSize(props?: cloudwatch.MetricCustomization): cloudwatch.Metric {
return this.metric('PublishSize', props);
}

/**
* Metric for the number of messages published through this topic
*
* @default sum over 5 minutes
*/
public metricNumberOfMessagesPublished(props?: cloudwatch.MetricCustomization): cloudwatch.Metric {
return this.metric('NumberOfMessagesPublished', { statistic: 'sum', ...props });
}

/**
* Metric for the number of messages that failed to publish through this topic
*
* @default sum over 5 minutes
*/
public metricNumberOfMessagesFailed(props?: cloudwatch.MetricCustomization): cloudwatch.Metric {
return this.metric('NumberOfMessagesFailed', { statistic: 'sum', ...props });
}

/**
* Metric for the number of messages that were successfully delivered through this topic
*
* @default sum over 5 minutes
*/
public metricNumberOfMessagesDelivered(props?: cloudwatch.MetricCustomization): cloudwatch.Metric {
return this.metric('NumberOfMessagesDelivered', { statistic: 'sum', ...props });
}

/**
* Implements the IBucketNotificationDestination interface, allowing topics to be used
* as bucket notification destinations.
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-sns/lib/topic.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Construct, Output } from '@aws-cdk/cdk';
import { CfnTopic } from './sns.generated';
import { ITopic, TopicBase, TopicImportProps } from './topic-ref';
import { ITopic, TopicBase, TopicImportProps } from './topic-base';

/**
* Properties for a new SNS topic
Expand Down
25 changes: 25 additions & 0 deletions packages/@aws-cdk/aws-sns/test/test.sns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,31 @@ export = {
}
});

test.done();
},

'test metrics'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const topic = new sns.Topic(stack, 'Topic');

// THEN
test.deepEqual(stack.node.resolve(topic.metricNumberOfMessagesPublished()), {
dimensions: {TopicName: { 'Fn::GetAtt': [ 'TopicBFC7AF6E', 'TopicName' ] }},
namespace: 'AWS/SNS',
metricName: 'NumberOfMessagesPublished',
periodSec: 300,
statistic: 'Sum'
});

test.deepEqual(stack.node.resolve(topic.metricPublishSize()), {
dimensions: {TopicName: { 'Fn::GetAtt': [ 'TopicBFC7AF6E', 'TopicName' ] }},
namespace: 'AWS/SNS',
metricName: 'PublishSize',
periodSec: 300,
statistic: 'Average'
});

test.done();
}
};
4 changes: 3 additions & 1 deletion packages/@aws-cdk/aws-sqs/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export * from './policy';
export * from './queue';
export * from './queue-ref';
export * from './queue-base';

// AWS::SQS CloudFormation Resources:
export * from './sqs.generated';

import './sqs-augmentations.generated';
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-sqs/lib/policy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PolicyDocument } from '@aws-cdk/aws-iam';
import { Construct } from '@aws-cdk/cdk';
import { IQueue } from './queue-ref';
import { IQueue } from './queue-base';
import { CfnQueuePolicy } from './sqs.generated';

export interface QueuePolicyProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export interface IQueue extends cdk.IConstruct, s3n.IBucketNotificationDestinati
*/
readonly queueUrl: string;

/**
* The name of this queue
*/
readonly queueName: string;

/**
* If this queue is server-side encrypted, this is the KMS encryption key.
*/
Expand Down Expand Up @@ -105,6 +110,11 @@ export abstract class QueueBase extends cdk.Construct implements IQueue {
*/
public abstract readonly queueUrl: string;

/**
* The name of this queue
*/
public abstract readonly queueName: string;

/**
* If this queue is server-side encrypted, this is the KMS encryption key.
*/
Expand Down
Loading

0 comments on commit 36cfca8

Please sign in to comment.