Skip to content

Commit

Permalink
Merge branch 'master' into nija-at/mergify
Browse files Browse the repository at this point in the history
  • Loading branch information
Niranjan Jayakar authored Dec 9, 2020
2 parents 609735c + 3530e8c commit 475b58a
Show file tree
Hide file tree
Showing 30 changed files with 23,009 additions and 202 deletions.
41 changes: 26 additions & 15 deletions packages/@aws-cdk/aws-apigateway/lib/restapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CfnOutput, IResource as IResourceBase, Resource, Stack } from '@aws-cdk
import { Construct } from 'constructs';
import { ApiDefinition } from './api-definition';
import { ApiKey, ApiKeyOptions, IApiKey } from './api-key';
import { ApiGatewayMetrics } from './apigateway-canned-metrics.generated';
import { CfnAccount, CfnRestApi } from './apigateway.generated';
import { CorsOptions } from './cors';
import { Deployment } from './deployment';
Expand Down Expand Up @@ -397,74 +398,77 @@ export abstract class RestApiBase extends Resource implements IRestApi {
metricName,
dimensions: { ApiName: this.restApiName },
...props,
});
}).attachTo(this);
}

/**
* Metric for the number of client-side errors captured in a given period.
*
* @default - sum over 5 minutes
* Default: sum over 5 minutes
*/
public metricClientError(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('4XXError', { statistic: 'Sum', ...props });
return this.cannedMetric(ApiGatewayMetrics._4XxErrorSum, props);
}

/**
* Metric for the number of server-side errors captured in a given period.
*
* @default - sum over 5 minutes
* Default: sum over 5 minutes
*/
public metricServerError(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('5XXError', { statistic: 'Sum', ...props });
return this.cannedMetric(ApiGatewayMetrics._5XxErrorSum, props);
}

/**
* Metric for the number of requests served from the API cache in a given period.
*
* @default - sum over 5 minutes
* Default: sum over 5 minutes
*/
public metricCacheHitCount(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('CacheHitCount', { statistic: 'Sum', ...props });
return this.cannedMetric(ApiGatewayMetrics.cacheHitCountSum, props);
}

/**
* Metric for the number of requests served from the backend in a given period,
* when API caching is enabled.
*
* @default - sum over 5 minutes
* Default: sum over 5 minutes
*/
public metricCacheMissCount(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('CacheMissCount', { statistic: 'Sum', ...props });
return this.cannedMetric(ApiGatewayMetrics.cacheMissCountSum, props);
}

/**
* Metric for the total number API requests in a given period.
*
* @default - SampleCount over 5 minutes
* Default: samplecount over 5 minutes
*/
public metricCount(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('Count', { statistic: 'SampleCount', ...props });
return this.cannedMetric(ApiGatewayMetrics.countSum, {
statistic: 'SampleCount',
...props,
});
}

/**
* Metric for the time between when API Gateway relays a request to the backend
* and when it receives a response from the backend.
*
* @default - no statistic
* Default: average over 5 minutes.
*/
public metricIntegrationLatency(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('IntegrationLatency', props);
return this.cannedMetric(ApiGatewayMetrics.integrationLatencyAverage, props);
}

/**
* The time between when API Gateway receives a request from a client
* and when it returns a response to the client.
* The latency includes the integration latency and other API Gateway overhead.
*
* @default - no statistic
* Default: average over 5 minutes.
*/
public metricLatency(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('Latency', props);
return this.cannedMetric(ApiGatewayMetrics.latencyAverage, props);
}

/**
Expand Down Expand Up @@ -544,6 +548,13 @@ export abstract class RestApiBase extends Resource implements IRestApi {
}
return undefined;
}

private cannedMetric(fn: (dims: { ApiName: string }) => cloudwatch.MetricProps, props?: cloudwatch.MetricOptions) {
return new cloudwatch.Metric({
...fn({ ApiName: this.restApiName }),
...props,
}).attachTo(this);
}
}

/**
Expand Down
32 changes: 16 additions & 16 deletions packages/@aws-cdk/aws-apigatewayv2/lib/http/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Metric, MetricOptions } from '@aws-cdk/aws-cloudwatch';
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
import { Duration, IResource, Resource } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnApi, CfnApiProps } from '../apigatewayv2.generated';
Expand Down Expand Up @@ -28,43 +28,43 @@ export interface IHttpApi extends IResource {
*
* @default - average over 5 minutes
*/
metric(metricName: string, props?: MetricOptions): Metric;
metric(metricName: string, props?: cloudwatch.MetricOptions): cloudwatch.Metric;

/**
* Metric for the number of client-side errors captured in a given period.
*
* @default - sum over 5 minutes
*/
metricClientError(props?: MetricOptions): Metric;
metricClientError(props?: cloudwatch.MetricOptions): cloudwatch.Metric;

/**
* Metric for the number of server-side errors captured in a given period.
*
* @default - sum over 5 minutes
*/
metricServerError(props?: MetricOptions): Metric;
metricServerError(props?: cloudwatch.MetricOptions): cloudwatch.Metric;

/**
* Metric for the amount of data processed in bytes.
*
* @default - sum over 5 minutes
*/
metricDataProcessed(props?: MetricOptions): Metric;
metricDataProcessed(props?: cloudwatch.MetricOptions): cloudwatch.Metric;

/**
* Metric for the total number API requests in a given period.
*
* @default - SampleCount over 5 minutes
*/
metricCount(props?: MetricOptions): Metric;
metricCount(props?: cloudwatch.MetricOptions): cloudwatch.Metric;

/**
* Metric for the time between when API Gateway relays a request to the backend
* and when it receives a response from the backend.
*
* @default - no statistic
*/
metricIntegrationLatency(props?: MetricOptions): Metric;
metricIntegrationLatency(props?: cloudwatch.MetricOptions): cloudwatch.Metric;

/**
* The time between when API Gateway receives a request from a client
Expand All @@ -73,7 +73,7 @@ export interface IHttpApi extends IResource {
*
* @default - no statistic
*/
metricLatency(props?: MetricOptions): Metric;
metricLatency(props?: cloudwatch.MetricOptions): cloudwatch.Metric;

/**
* Add a new VpcLink
Expand Down Expand Up @@ -186,36 +186,36 @@ abstract class HttpApiBase extends Resource implements IHttpApi { // note that t
public abstract readonly httpApiId: string;
private vpcLinks: Record<string, VpcLink> = {};

public metric(metricName: string, props?: MetricOptions): Metric {
return new Metric({
public metric(metricName: string, props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return new cloudwatch.Metric({
namespace: 'AWS/ApiGateway',
metricName,
dimensions: { ApiId: this.httpApiId },
...props,
}).attachTo(this);
}

public metricClientError(props?: MetricOptions): Metric {
public metricClientError(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('4XXError', { statistic: 'Sum', ...props });
}

public metricServerError(props?: MetricOptions): Metric {
public metricServerError(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('5XXError', { statistic: 'Sum', ...props });
}

public metricDataProcessed(props?: MetricOptions): Metric {
public metricDataProcessed(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('DataProcessed', { statistic: 'Sum', ...props });
}

public metricCount(props?: MetricOptions): Metric {
public metricCount(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('Count', { statistic: 'SampleCount', ...props });
}

public metricIntegrationLatency(props?: MetricOptions): Metric {
public metricIntegrationLatency(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('IntegrationLatency', props);
}

public metricLatency(props?: MetricOptions): Metric {
public metricLatency(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('Latency', props);
}

Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-cloudwatch/lib/metric.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as iam from '@aws-cdk/aws-iam';
import * as cdk from '@aws-cdk/core';
import * as constructs from 'constructs';
import { Alarm, ComparisonOperator, TreatMissingData } from './alarm';
import { Dimension, IMetric, MetricAlarmConfig, MetricConfig, MetricGraphConfig, Unit } from './metric-types';
import { dispatchMetric, metricKey } from './private/metric-util';
Expand Down Expand Up @@ -272,7 +273,7 @@ export class Metric implements IMetric {
* If the scope we attach to is in an environment-agnostic stack,
* nothing is done and the same Metric object is returned.
*/
public attachTo(scope: cdk.Construct): Metric {
public attachTo(scope: constructs.IConstruct): Metric {
const stack = cdk.Stack.of(scope);

return this.with({
Expand Down
45 changes: 24 additions & 21 deletions packages/@aws-cdk/aws-codebuild/lib/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Construct } from 'constructs';
import { IArtifacts } from './artifacts';
import { BuildSpec } from './build-spec';
import { Cache } from './cache';
import { CodeBuildMetrics } from './codebuild-canned-metrics.generated';
import { CfnProject } from './codebuild.generated';
import { CodePipelineArtifacts } from './codepipeline-artifacts';
import { IFileSystemLocation } from './file-location';
Expand Down Expand Up @@ -340,11 +341,8 @@ abstract class ProjectBase extends Resource implements IProject {
*
* @default sum over 5 minutes
*/
public metricBuilds(props?: cloudwatch.MetricOptions) {
return this.metric('Builds', {
statistic: 'sum',
...props,
});
public metricBuilds(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.cannedMetric(CodeBuildMetrics.buildsSum, props);
}

/**
Expand All @@ -356,11 +354,8 @@ abstract class ProjectBase extends Resource implements IProject {
*
* @default average over 5 minutes
*/
public metricDuration(props?: cloudwatch.MetricOptions) {
return this.metric('Duration', {
statistic: 'avg',
...props,
});
public metricDuration(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.cannedMetric(CodeBuildMetrics.durationAverage, props);
}

/**
Expand All @@ -372,11 +367,8 @@ abstract class ProjectBase extends Resource implements IProject {
*
* @default sum over 5 minutes
*/
public metricSucceededBuilds(props?: cloudwatch.MetricOptions) {
return this.metric('SucceededBuilds', {
statistic: 'sum',
...props,
});
public metricSucceededBuilds(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.cannedMetric(CodeBuildMetrics.succeededBuildsSum, props);
}

/**
Expand All @@ -389,11 +381,17 @@ abstract class ProjectBase extends Resource implements IProject {
*
* @default sum over 5 minutes
*/
public metricFailedBuilds(props?: cloudwatch.MetricOptions) {
return this.metric('FailedBuilds', {
statistic: 'sum',
public metricFailedBuilds(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.cannedMetric(CodeBuildMetrics.failedBuildsSum, props);
}

private cannedMetric(
fn: (dims: { ProjectName: string }) => cloudwatch.MetricProps,
props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return new cloudwatch.Metric({
...fn({ ProjectName: this.projectName }),
...props,
});
}).attachTo(this);
}
}

Expand Down Expand Up @@ -931,11 +929,16 @@ export class Project extends ProjectBase {

const resources = Object.values(props.environmentVariables)
.filter(envVariable => envVariable.type === BuildEnvironmentVariableType.PARAMETER_STORE)
.map(envVariable =>
// If the parameter name starts with / the resource name is not separated with a double '/'
// arn:aws:ssm:region:1111111111:parameter/PARAM_NAME
(envVariable.value as string).startsWith('/')
? (envVariable.value as string).substr(1)
: envVariable.value)
.map(envVariable => Stack.of(this).formatArn({
service: 'ssm',
resource: 'parameter',
sep: ':',
resourceName: envVariable.value,
resourceName: envVariable,
}));

if (resources.length === 0) {
Expand Down
6 changes: 3 additions & 3 deletions packages/@aws-cdk/aws-codebuild/test/test.project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ export = {
},
'ENV_VAR2': {
type: codebuild.BuildEnvironmentVariableType.PARAMETER_STORE,
value: '/params/param2',
value: 'params/param2',
},
},
});
Expand All @@ -823,7 +823,7 @@ export = {
{
Ref: 'AWS::AccountId',
},
':parameter:/params/param1',
':parameter/params/param1',
],
],
},
Expand All @@ -843,7 +843,7 @@ export = {
{
Ref: 'AWS::AccountId',
},
':parameter:/params/param2',
':parameter/params/param2',
],
],
}],
Expand Down
Loading

0 comments on commit 475b58a

Please sign in to comment.