Skip to content

Commit

Permalink
codebuild: Metrics (#407)
Browse files Browse the repository at this point in the history
Adds `metricXxx` methods to `BuildProjectRef` following our design
pattern for metrics.
  • Loading branch information
Elad Ben-Israel authored Jul 25, 2018
1 parent 3832a3d commit e9497f1
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
80 changes: 80 additions & 0 deletions packages/@aws-cdk/aws-codebuild/lib/project.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import cloudwatch = require('@aws-cdk/aws-cloudwatch');
import events = require('@aws-cdk/aws-events');
import iam = require('@aws-cdk/aws-iam');
import kms = require('@aws-cdk/aws-kms');
Expand Down Expand Up @@ -171,6 +172,85 @@ export abstract class BuildProjectRef extends cdk.Construct implements events.IE
return rule;
}

/**
* @returns a CloudWatch metric associated with this build project.
* @param metricName The name of the metric
* @param props Customization properties
*/
public metric(metricName: string, props: cloudwatch.MetricCustomization) {
return new cloudwatch.Metric({
namespace: 'AWS/CodeBuild',
metricName,
dimensions: { ProjectName: this.projectName },
...props
});
}

/**
* Measures the number of builds triggered.
*
* Units: Count
*
* Valid CloudWatch statistics: Sum
*
* @default sum over 5 minutes
*/
public metricBuilds(props?: cloudwatch.MetricCustomization) {
return this.metric('Builds', {
statistic: 'sum',
...props,
});
}

/**
* Measures the duration of all builds over time.
*
* Units: Seconds
*
* Valid CloudWatch statistics: Average (recommended), Maximum, Minimum
*
* @default average over 5 minutes
*/
public metricDuration(props?: cloudwatch.MetricCustomization) {
return this.metric('Duration', {
statistic: 'avg',
...props
});
}

/**
* Measures the number of successful builds.
*
* Units: Count
*
* Valid CloudWatch statistics: Sum
*
* @default sum over 5 minutes
*/
public metricSucceededBuilds(props?: cloudwatch.MetricCustomization) {
return this.metric('SucceededBuilds', {
statistic: 'sum',
...props,
});
}

/**
* Measures the number of builds that failed because of client error or
* because of a timeout.
*
* Units: Count
*
* Valid CloudWatch statistics: Sum
*
* @default sum over 5 minutes
*/
public metricFailedBuilds(props?: cloudwatch.MetricCustomization) {
return this.metric('FailedBuilds', {
statistic: 'sum',
...props,
});
}

/**
* Allows using build projects as event rule targets.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-codebuild/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"dependencies": {
"@aws-cdk/aws-codecommit": "^0.7.3-beta",
"@aws-cdk/aws-codepipeline": "^0.7.3-beta",
"@aws-cdk/aws-cloudwatch": "^0.7.3-beta",
"@aws-cdk/aws-events": "^0.7.3-beta",
"@aws-cdk/aws-iam": "^0.7.3-beta",
"@aws-cdk/aws-kms": "^0.7.3-beta",
Expand Down
22 changes: 22 additions & 0 deletions packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,28 @@ export = {
}
}));

test.done();
},

'.metricXxx() methods can be used to obtain Metrics for CodeBuild projects'(test: Test) {
const stack = new cdk.Stack();

const project = new codebuild.BuildProject(stack, 'MyBuildProject', { source: new codebuild.CodePipelineSource() });

const metricBuilds = project.metricBuilds();
test.same(metricBuilds.dimensions!.ProjectName, project.projectName);
test.deepEqual(metricBuilds.namespace, 'AWS/CodeBuild');
test.deepEqual(metricBuilds.statistic, 'sum', 'default stat is SUM');
test.deepEqual(metricBuilds.metricName, 'Builds');

const metricDuration = project.metricDuration({ label: 'hello' });

test.deepEqual(metricDuration.metricName, 'Duration');
test.deepEqual(metricDuration.label, 'hello');

test.deepEqual(project.metricFailedBuilds().metricName, 'FailedBuilds');
test.deepEqual(project.metricSucceededBuilds().metricName, 'SucceededBuilds');

test.done();
}
};

0 comments on commit e9497f1

Please sign in to comment.