From 6afb7efbe44a682deed5b794c45f1d037f279c0d Mon Sep 17 00:00:00 2001 From: Baptiste Guerin Date: Mon, 16 Nov 2020 18:20:38 +0100 Subject: [PATCH 1/3] Add detailed metrics to HttpApi --- docs/providers/aws/events/http-api.md | 12 ++++++++++ .../package/compile/events/httpApi/index.js | 10 +++++++- .../compile/events/httpApi/index.test.js | 23 +++++++++++++++++++ lib/plugins/aws/provider/awsProvider.js | 1 + 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/docs/providers/aws/events/http-api.md b/docs/providers/aws/events/http-api.md index 9cdd65207f7..f6ed2db44bd 100644 --- a/docs/providers/aws/events/http-api.md +++ b/docs/providers/aws/events/http-api.md @@ -271,3 +271,15 @@ provider: httpApi: payload: '1.0' ``` + +### Detailed Metrics + +With HTTP API we may configure detailed metrics that can be used setup monitoring and alerting in Cloudwatch. + +Detailed Metrics can be turned on with: + +```yaml +provider: + httpApi: + detailedMetrics: true +``` diff --git a/lib/plugins/aws/package/compile/events/httpApi/index.js b/lib/plugins/aws/package/compile/events/httpApi/index.js index f1b35f14109..ffb7b7ed6a7 100644 --- a/lib/plugins/aws/package/compile/events/httpApi/index.js +++ b/lib/plugins/aws/package/compile/events/httpApi/index.js @@ -130,7 +130,11 @@ class HttpApiEvents { ApiId: { Ref: this.provider.naming.getHttpApiLogicalId() }, StageName: '$default', AutoDeploy: true, + DefaultRouteSettings: { + DetailedMetricsEnabled: this.config.detailedMetrics, + }, }; + const resource = (this.cfTemplate.Resources[this.provider.naming.getHttpApiStageLogicalId()] = { Type: 'AWS::ApiGatewayV2::Stage', Properties: properties, @@ -226,7 +230,11 @@ Object.defineProperties( const routes = new Map(); const providerConfig = this.serverless.service.provider; const userConfig = providerConfig.httpApi || {}; - this.config = { routes, id: userConfig.id }; + this.config = { + routes, + id: userConfig.id, + detailedMetrics: userConfig.detailedMetrics || false, + }; let cors = null; let shouldFillCorsMethods = false; const userCors = userConfig.cors; diff --git a/lib/plugins/aws/package/compile/events/httpApi/index.test.js b/lib/plugins/aws/package/compile/events/httpApi/index.test.js index 62b7028ad82..5670f6accb0 100644 --- a/lib/plugins/aws/package/compile/events/httpApi/index.test.js +++ b/lib/plugins/aws/package/compile/events/httpApi/index.test.js @@ -60,6 +60,10 @@ describe('HttpApiEvents', () => { expect(resource.Properties.StageName).to.equal('$default'); expect(resource.Properties.AutoDeploy).to.equal(true); }); + it('Should not have detailed metrics when not asked to', () => { + const resource = cfResources[naming.getHttpApiStageLogicalId()]; + expect(resource.Properties.DefaultRouteSettings.DetailedMetricsEnabled).to.equal(false); + }); it('Should configure output', () => { const output = cfOutputs.HttpApiUrl; expect(output).to.have.property('Value'); @@ -107,6 +111,25 @@ describe('HttpApiEvents', () => { }); }); + describe('Detailed Metrics', () => { + let cfHttpApiStage; + + before(() => + runServerless({ + fixture: 'httpApi', + configExt: { provider: { httpApi: { detailedMetrics: true } } }, + cliArgs: ['package'], + }).then(({ awsNaming, cfTemplate }) => { + const { Resources } = cfTemplate; + cfHttpApiStage = Resources[awsNaming.getHttpApiStageLogicalId()]; + }) + ); + it('Should configure detailed metrics', () => { + expect(cfHttpApiStage.Type).to.equal('AWS::ApiGatewayV2::Stage'); + expect(cfHttpApiStage.Properties.DefaultRouteSettings.DetailedMetricsEnabled).to.equal(true); + }); + }); + describe('Payload format version', () => { let cfHttpApiIntegration; diff --git a/lib/plugins/aws/provider/awsProvider.js b/lib/plugins/aws/provider/awsProvider.js index 4a3be0e00c5..a618965b021 100644 --- a/lib/plugins/aws/provider/awsProvider.js +++ b/lib/plugins/aws/provider/awsProvider.js @@ -698,6 +698,7 @@ class AwsProvider { }, name: { type: 'string' }, payload: { type: 'string' }, + detailedMetrics: { type: 'boolean' }, }, additionalProperties: false, }, From 84c3d527d346d5a246bb9d57c5070ee07be3448b Mon Sep 17 00:00:00 2001 From: Baptiste Guerin Date: Tue, 17 Nov 2020 11:51:57 +0100 Subject: [PATCH 2/3] Take PR feedbacks --- docs/providers/aws/events/http-api.md | 2 +- lib/plugins/aws/package/compile/events/httpApi/index.js | 4 ++-- lib/plugins/aws/package/compile/events/httpApi/index.test.js | 2 +- lib/plugins/aws/provider/awsProvider.js | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/providers/aws/events/http-api.md b/docs/providers/aws/events/http-api.md index f6ed2db44bd..3cf9ee13423 100644 --- a/docs/providers/aws/events/http-api.md +++ b/docs/providers/aws/events/http-api.md @@ -281,5 +281,5 @@ Detailed Metrics can be turned on with: ```yaml provider: httpApi: - detailedMetrics: true + metrics: true ``` diff --git a/lib/plugins/aws/package/compile/events/httpApi/index.js b/lib/plugins/aws/package/compile/events/httpApi/index.js index ffb7b7ed6a7..453c040317b 100644 --- a/lib/plugins/aws/package/compile/events/httpApi/index.js +++ b/lib/plugins/aws/package/compile/events/httpApi/index.js @@ -131,7 +131,7 @@ class HttpApiEvents { StageName: '$default', AutoDeploy: true, DefaultRouteSettings: { - DetailedMetricsEnabled: this.config.detailedMetrics, + DetailedMetricsEnabled: this.config.metrics, }, }; @@ -233,7 +233,7 @@ Object.defineProperties( this.config = { routes, id: userConfig.id, - detailedMetrics: userConfig.detailedMetrics || false, + metrics: userConfig.metrics || false, }; let cors = null; let shouldFillCorsMethods = false; diff --git a/lib/plugins/aws/package/compile/events/httpApi/index.test.js b/lib/plugins/aws/package/compile/events/httpApi/index.test.js index 5670f6accb0..17062cfe870 100644 --- a/lib/plugins/aws/package/compile/events/httpApi/index.test.js +++ b/lib/plugins/aws/package/compile/events/httpApi/index.test.js @@ -117,7 +117,7 @@ describe('HttpApiEvents', () => { before(() => runServerless({ fixture: 'httpApi', - configExt: { provider: { httpApi: { detailedMetrics: true } } }, + configExt: { provider: { httpApi: { metrics: true } } }, cliArgs: ['package'], }).then(({ awsNaming, cfTemplate }) => { const { Resources } = cfTemplate; diff --git a/lib/plugins/aws/provider/awsProvider.js b/lib/plugins/aws/provider/awsProvider.js index a618965b021..f283682f2ad 100644 --- a/lib/plugins/aws/provider/awsProvider.js +++ b/lib/plugins/aws/provider/awsProvider.js @@ -698,7 +698,7 @@ class AwsProvider { }, name: { type: 'string' }, payload: { type: 'string' }, - detailedMetrics: { type: 'boolean' }, + metrics: { type: 'boolean' }, }, additionalProperties: false, }, From 4c0796a68b34f8518e9d2cbdcb5b3ca26f9077c5 Mon Sep 17 00:00:00 2001 From: Baptiste Guerin Date: Tue, 17 Nov 2020 12:02:03 +0100 Subject: [PATCH 3/3] Use existing runServerless for tests --- .../compile/events/httpApi/index.test.js | 27 +++++-------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/lib/plugins/aws/package/compile/events/httpApi/index.test.js b/lib/plugins/aws/package/compile/events/httpApi/index.test.js index 17062cfe870..d3deed0ec96 100644 --- a/lib/plugins/aws/package/compile/events/httpApi/index.test.js +++ b/lib/plugins/aws/package/compile/events/httpApi/index.test.js @@ -111,25 +111,6 @@ describe('HttpApiEvents', () => { }); }); - describe('Detailed Metrics', () => { - let cfHttpApiStage; - - before(() => - runServerless({ - fixture: 'httpApi', - configExt: { provider: { httpApi: { metrics: true } } }, - cliArgs: ['package'], - }).then(({ awsNaming, cfTemplate }) => { - const { Resources } = cfTemplate; - cfHttpApiStage = Resources[awsNaming.getHttpApiStageLogicalId()]; - }) - ); - it('Should configure detailed metrics', () => { - expect(cfHttpApiStage.Type).to.equal('AWS::ApiGatewayV2::Stage'); - expect(cfHttpApiStage.Properties.DefaultRouteSettings.DetailedMetricsEnabled).to.equal(true); - }); - }); - describe('Payload format version', () => { let cfHttpApiIntegration; @@ -483,7 +464,7 @@ describe('HttpApiEvents', () => { }); }); - describe('Access logs', () => { + describe('Access logs and detailed metrics', () => { let cfResources; let naming; before(() => @@ -491,6 +472,7 @@ describe('HttpApiEvents', () => { fixture: 'httpApi', configExt: { provider: { + httpApi: { metrics: true }, logs: { httpApi: true, }, @@ -515,6 +497,11 @@ describe('HttpApiEvents', () => { expect(stageResourceProps.AccessLogSettings).to.have.property('Format'); }); + + it('Should configure detailed metrics', () => { + const resource = cfResources[naming.getHttpApiStageLogicalId()]; + expect(resource.Properties.DefaultRouteSettings.DetailedMetricsEnabled).to.equal(true); + }); }); describe('External HTTP API', () => {