Skip to content

feat(apigateway): add metrics for Stage and Method constructs#20617

Merged
mergify[bot] merged 7 commits intoaws:mainfrom
cecheta:metrics
Aug 3, 2022
Merged

feat(apigateway): add metrics for Stage and Method constructs#20617
mergify[bot] merged 7 commits intoaws:mainfrom
cecheta:metrics

Conversation

@cecheta
Copy link
Copy Markdown
Contributor

@cecheta cecheta commented Jun 3, 2022

This PR adds metrics for API Gateway stages and methods. The metrics have been largely copied from the existing RestApi metrics methods.

Closes #16433


All Submissions:

Adding new Unconventional Dependencies:

  • This PR adds new unconventional dependencies following the process described here

New Features

  • Have you added the new feature to an integration test?
    • Did you use yarn integ to deploy the infrastructure and generate the snapshot (i.e. yarn integ without --dry-run)?

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@gitpod-io
Copy link
Copy Markdown

gitpod-io bot commented Jun 3, 2022

@github-actions github-actions bot added effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p2 labels Jun 3, 2022
@aws-cdk-automation aws-cdk-automation requested a review from a team June 3, 2022 21:59
@comcalvi comcalvi self-assigned this Jun 15, 2022
Copy link
Copy Markdown
Contributor

@comcalvi comcalvi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! Please add an integ test using the new metric factories and update the README for this module.

@mergify mergify bot dismissed comcalvi’s stale review June 18, 2022 16:49

Pull request has been modified.

@cecheta
Copy link
Copy Markdown
Contributor Author

cecheta commented Jun 18, 2022

@comcalvi README updated and integration test added

Comment on lines +734 to +736
class ImportWithApiName extends Import {
public readonly restApiName = restApiName;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not add restApiName to Import itself, so we don't need this extra class?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was struggling with the fact that the attribute was optional, but I guess we can fall back to id

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the unnecessary class

* Import an existing RestApi that can be configured with additional Methods and Resources.
*/
public static fromRestApiAttributes(scope: Construct, id: string, attrs: RestApiAttributes): IRestApi {
const restApiName = attrs.restApiName ?? '';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to assign it ''? Line 684 of this file states that the default is ID of the RestApi construct. This is confusing since we assign it to the empty string here instead.

public readonly restApiName = restApiName;
}

if (attrs.restApiName !== undefined) return new ImportWithApiName(scope, id);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we shouldn't be referencing attrs.restApiName here, we already have restApiName above. This usage is confusing; I would delete the restApiName local variable and use attrs.restApiName instead.

/**
* Metric for the number of client-side errors captured in a given period.
*
* Default: sum over 5 minutes
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throughout this PR, default comments should read analogously to:

Suggested change
* Default: sum over 5 minutes
* @default - sum over 5 minutes

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated throughout

Comment on lines +6 to +10
const app = new cdk.App();

const stack = new cdk.Stack(app, 'restapi-metrics');

const restApi = new apigw.RestApi(stack, 'Api');

const stage = restApi.deploymentStage;

const method = restApi.root.addMethod('GET');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const app = new cdk.App();
const stack = new cdk.Stack(app, 'restapi-metrics');
const restApi = new apigw.RestApi(stack, 'Api');
const stage = restApi.deploymentStage;
const method = restApi.root.addMethod('GET');
const app = new cdk.App();
const stack = new cdk.Stack(app, 'restapi-metrics');
const restApi = new apigw.RestApi(stack, 'Api');
const stage = restApi.deploymentStage;
const method = restApi.root.addMethod('GET');

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@mergify mergify bot dismissed comcalvi’s stale review June 29, 2022 16:33

Pull request has been modified.

The APIs with the `metric` prefix can be used to get reference to specific metrics for this API. For example,
the method below refers to the client side errors metric for this API.
These metrics can be referred to using the metric APIs available on the `RestApi`, `Stage` and `Method` constructs.
Note that detailed metrics must be enabled for a stage to use the `Method` metrics.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include a link to documentation that explains how to enable detailed metrics.

Comment on lines +372 to +374
* Returns the given named metric for this API method
*/
public metric(metricName: string, stage: IStage, props?: cloudwatch.MetricOptions): cloudwatch.Metric {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this confusing; the doc string says that it returns "the given named metric", as if the metric already exists, but then the method invokes a constructor.

can we rename this to or attachMetric and change the docstring to something like "adds the named metric for this API method. It has metric name metricName and dimensions:

{please list them here in some form}

to this method."

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've mainly copied the existing code from restapi.ts, all the metric methods invoke a constructor and return a metric after calling attachTo().

The docstring seems to match metrics in other modules such as SQS and Lambda

I would think that renaming the docstring to something like "Returns a metric for this API method with the given name" would be sufficient to make it clearer

*
* @default - sum over 5 minutes
*/
public metricCacheHitCount(stage: IStage, props?: cloudwatch.MetricOptions): cloudwatch.Metric {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this abstraction on top of the canned metrics, nice work 👍

/**
* Returns the given named metric for this stage
*/
public metric(metricName: string, props?: cloudwatch.MetricOptions): cloudwatch.Metric {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar comment as above.

Additionally, having metric() next to metricClientError() is confusing; can we add a more descriptive name to metric(), to better distinguish it from the other metric*() methods in both classes?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, the naming metric() here seems to match the naming in other modules such as SQS and Lambda, maybe it is better to leave them consistent?

@mergify mergify bot dismissed comcalvi’s stale review July 18, 2022 21:00

Pull request has been modified.

@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Aug 3, 2022

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Copy Markdown
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: adf5ea4
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit 3bf1361 into aws:main Aug 3, 2022
@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Aug 3, 2022

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(apigateway): get metrics from a resource

4 participants