From 335de26248f848e2bdcaaf96328bfbbf0e66368d Mon Sep 17 00:00:00 2001 From: Evan Karopoulos Date: Wed, 19 Aug 2020 02:30:12 +1000 Subject: [PATCH] limit plugin to specific stages (#56) * added test files for stage functionality * added stage functionality * updated version and added to README.md * updated all output files to reflect latest layer verison * updated README.md --- README.md | 11 +++ package.json | 4 +- src/index.ts | 43 +++++++++- .../debug-log-level.output.service.json | 6 +- tests/fixtures/debug.output.service.json | 6 +- tests/fixtures/include.output.service.json | 2 +- .../fixtures/log-disabled.output.service.json | 6 +- tests/fixtures/log-level.output.service.json | 6 +- ...-environment-log-level.output.service.json | 6 +- .../provider-environment.output.service.json | 6 +- .../stage-excluded.input.service.json | 45 ++++++++++ .../stage-excluded.output.service.json | 45 ++++++++++ .../stage-included.input.service.json | 45 ++++++++++ .../stage-included.output.service.json | 84 +++++++++++++++++++ 14 files changed, 291 insertions(+), 24 deletions(-) create mode 100644 tests/fixtures/stage-excluded.input.service.json create mode 100644 tests/fixtures/stage-excluded.output.service.json create mode 100644 tests/fixtures/stage-included.input.service.json create mode 100644 tests/fixtures/stage-included.output.service.json diff --git a/README.md b/README.md index 65f30a43..71513afe 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,17 @@ Logging configuration is considered in the following order: 3. custom newRelic `logLevel` property 4. custom newRelic `debug` flag +#### `stages` (optional) + +An array of stages that the plugin will be included for. If this key is not specified then all stages will be included. + +```yaml +custom: + newRelic: + stages: + - prod +``` + #### `include` (optional) An array of functions to include for automatic wrapping. (You can set `include` or `exclude` options, but not both.) diff --git a/package.json b/package.json index 26252bf0..d7f8c573 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serverless-newrelic-lambda-layers", - "version": "0.1.18", + "version": "0.2.18", "description": "Serverless plugin for NewRelic APM AWS Lambda layers.", "main": "dist/index.js", "files": [ @@ -13,7 +13,7 @@ "test": "jest", "test:watch": "jest --watchAll", "lint": "tslint -c tslint.json 'src/**/*.ts'", - "generate:test:case": "yaml2json examples/nodejs/serverless.yml > tests/fixnpmtures/example.service.input.json" + "generate:test:case": "yaml2json examples/nodejs/serverless.yml > tests/fixtures/example.service.input.json" }, "husky": { "hooks": { diff --git a/src/index.ts b/src/index.ts index e170d51a..ba722eac 100644 --- a/src/index.ts +++ b/src/index.ts @@ -40,6 +40,14 @@ export default class NewRelicLambdaLayerPlugin { return _.get(this.serverless, "service.custom.newRelic", {}); } + get stage() { + return ( + (this.options && this.options.stage) || + (this.serverless.service.provider && + this.serverless.service.provider.stage) + ); + } + get prependLayer() { return typeof this.config.prepend === "boolean" && this.config.prepend; } @@ -61,6 +69,13 @@ export default class NewRelicLambdaLayerPlugin { } public async run() { + if (this.shouldSkipPlugin()) { + this.serverless.cli.log( + `Skipping plugin serverless-newrelic-lambda-layers for stage ${this.stage}` + ); + return; + } + const version = this.serverless.getVersion(); if (semver.lt(version, "1.34.0")) { this.serverless.cli.log( @@ -104,10 +119,18 @@ export default class NewRelicLambdaLayerPlugin { } public cleanup() { + if (this.shouldSkipPlugin()) { + return; + } + this.removeNodeHelper(); } public async addLogSubscriptions() { + if (this.shouldSkipPlugin()) { + return; + } + if (this.autoSubscriptionDisabled) { this.serverless.cli.log( "Skipping adding log subscription. Explicitly disabled" @@ -134,7 +157,7 @@ export default class NewRelicLambdaLayerPlugin { const promises = []; for (const funcName of Object.keys(funcs)) { - if (this.shouldSkip(funcName)) { + if (this.shouldSkipFunction(funcName)) { return; } @@ -152,6 +175,10 @@ export default class NewRelicLambdaLayerPlugin { } public async removeLogSubscriptions() { + if (this.shouldSkipPlugin()) { + return; + } + if (this.autoSubscriptionDisabled) { this.serverless.cli.log( "Skipping removing log subscription. Explicitly disabled" @@ -217,7 +244,7 @@ export default class NewRelicLambdaLayerPlugin { return; } - if (this.shouldSkip(funcName)) { + if (this.shouldSkipFunction(funcName)) { return; } @@ -277,7 +304,17 @@ export default class NewRelicLambdaLayerPlugin { funcDef.package = this.updatePackageExcludes(runtime, pkg); } - private shouldSkip(funcName) { + private shouldSkipPlugin() { + if ( + !this.config.stages || + (this.config.stages && this.config.stages.includes(this.stage)) + ) { + return false; + } + return true; + } + + private shouldSkipFunction(funcName) { const { include = [], exclude = [] } = this.config; if ( diff --git a/tests/fixtures/debug-log-level.output.service.json b/tests/fixtures/debug-log-level.output.service.json index 45c69ba9..b9c33582 100644 --- a/tests/fixtures/debug-log-level.output.service.json +++ b/tests/fixtures/debug-log-level.output.service.json @@ -29,7 +29,7 @@ "events": [{ "schedule": "rate(5 minutes)" }], "handler": "newrelic-wrapper-helper.handler", "layers": [ - "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:9" + "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:17" ], "package": { "exclude": ["./**", "!newrelic-wrapper-helper.js"], @@ -51,7 +51,7 @@ "events": [{ "schedule": "rate(5 minutes)" }], "handler": "newrelic-lambda-wrapper.handler", "layers": [ - "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:9" + "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:19" ], "package": { "exclude": ["./**", "!newrelic-wrapper-helper.js"], @@ -73,7 +73,7 @@ "events": [{ "schedule": "rate(5 minutes)" }], "handler": "newrelic-lambda-wrapper.handler", "layers": [ - "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:7" + "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:17" ], "package": { "exclude": ["./**", "!newrelic-wrapper-helper.js"], diff --git a/tests/fixtures/debug.output.service.json b/tests/fixtures/debug.output.service.json index 0e4af69b..2ca070f4 100644 --- a/tests/fixtures/debug.output.service.json +++ b/tests/fixtures/debug.output.service.json @@ -28,7 +28,7 @@ "events": [{ "schedule": "rate(5 minutes)" }], "handler": "newrelic-wrapper-helper.handler", "layers": [ - "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:9" + "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:17" ], "package": { "exclude": ["./**", "!newrelic-wrapper-helper.js"], @@ -50,7 +50,7 @@ "events": [{ "schedule": "rate(5 minutes)" }], "handler": "newrelic-lambda-wrapper.handler", "layers": [ - "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:9" + "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:19" ], "package": { "exclude": ["./**", "!newrelic-wrapper-helper.js"], @@ -72,7 +72,7 @@ "events": [{ "schedule": "rate(5 minutes)" }], "handler": "newrelic-lambda-wrapper.handler", "layers": [ - "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:7" + "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:17" ], "package": { "exclude": ["./**", "!newrelic-wrapper-helper.js"], diff --git a/tests/fixtures/include.output.service.json b/tests/fixtures/include.output.service.json index 539afd85..cc7946e5 100644 --- a/tests/fixtures/include.output.service.json +++ b/tests/fixtures/include.output.service.json @@ -27,7 +27,7 @@ "events": [{ "schedule": "rate(5 minutes)" }], "handler": "newrelic-wrapper-helper.handler", "layers": [ - "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:9" + "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:17" ], "package": { "exclude": ["./**", "!newrelic-wrapper-helper.js"], diff --git a/tests/fixtures/log-disabled.output.service.json b/tests/fixtures/log-disabled.output.service.json index 45afcc33..0c67b630 100644 --- a/tests/fixtures/log-disabled.output.service.json +++ b/tests/fixtures/log-disabled.output.service.json @@ -28,7 +28,7 @@ "events": [{ "schedule": "rate(5 minutes)" }], "handler": "newrelic-wrapper-helper.handler", "layers": [ - "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:9" + "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:17" ], "package": { "exclude": ["./**", "!newrelic-wrapper-helper.js"], @@ -47,7 +47,7 @@ "events": [{ "schedule": "rate(5 minutes)" }], "handler": "newrelic-lambda-wrapper.handler", "layers": [ - "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:9" + "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:19" ], "package": { "exclude": ["./**", "!newrelic-wrapper-helper.js"], @@ -66,7 +66,7 @@ "events": [{ "schedule": "rate(5 minutes)" }], "handler": "newrelic-lambda-wrapper.handler", "layers": [ - "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:7" + "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:17" ], "package": { "exclude": ["./**", "!newrelic-wrapper-helper.js"], diff --git a/tests/fixtures/log-level.output.service.json b/tests/fixtures/log-level.output.service.json index 70f3aa67..d05852e5 100644 --- a/tests/fixtures/log-level.output.service.json +++ b/tests/fixtures/log-level.output.service.json @@ -28,7 +28,7 @@ "events": [{ "schedule": "rate(5 minutes)" }], "handler": "newrelic-wrapper-helper.handler", "layers": [ - "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:9" + "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:17" ], "package": { "exclude": ["./**", "!newrelic-wrapper-helper.js"], @@ -50,7 +50,7 @@ "events": [{ "schedule": "rate(5 minutes)" }], "handler": "newrelic-lambda-wrapper.handler", "layers": [ - "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:9" + "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:19" ], "package": { "exclude": ["./**", "!newrelic-wrapper-helper.js"], @@ -72,7 +72,7 @@ "events": [{ "schedule": "rate(5 minutes)" }], "handler": "newrelic-lambda-wrapper.handler", "layers": [ - "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:7" + "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:17" ], "package": { "exclude": ["./**", "!newrelic-wrapper-helper.js"], diff --git a/tests/fixtures/provider-environment-log-level.output.service.json b/tests/fixtures/provider-environment-log-level.output.service.json index 56feebd1..25229b21 100644 --- a/tests/fixtures/provider-environment-log-level.output.service.json +++ b/tests/fixtures/provider-environment-log-level.output.service.json @@ -31,7 +31,7 @@ "events": [{ "schedule": "rate(5 minutes)" }], "handler": "newrelic-wrapper-helper.handler", "layers": [ - "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:9" + "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:17" ], "package": { "exclude": ["./**", "!newrelic-wrapper-helper.js"], @@ -53,7 +53,7 @@ "events": [{ "schedule": "rate(5 minutes)" }], "handler": "newrelic-lambda-wrapper.handler", "layers": [ - "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:9" + "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:19" ], "package": { "exclude": ["./**", "!newrelic-wrapper-helper.js"], @@ -75,7 +75,7 @@ "events": [{ "schedule": "rate(5 minutes)" }], "handler": "newrelic-lambda-wrapper.handler", "layers": [ - "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:7" + "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:17" ], "package": { "exclude": ["./**", "!newrelic-wrapper-helper.js"], diff --git a/tests/fixtures/provider-environment.output.service.json b/tests/fixtures/provider-environment.output.service.json index eb8d5dff..321993ea 100644 --- a/tests/fixtures/provider-environment.output.service.json +++ b/tests/fixtures/provider-environment.output.service.json @@ -30,7 +30,7 @@ "events": [{ "schedule": "rate(5 minutes)" }], "handler": "newrelic-wrapper-helper.handler", "layers": [ - "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:9" + "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:17" ], "package": { "exclude": ["./**", "!newrelic-wrapper-helper.js"], @@ -52,7 +52,7 @@ "events": [{ "schedule": "rate(5 minutes)" }], "handler": "newrelic-lambda-wrapper.handler", "layers": [ - "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:9" + "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:19" ], "package": { "exclude": ["./**", "!newrelic-wrapper-helper.js"], @@ -74,7 +74,7 @@ "events": [{ "schedule": "rate(5 minutes)" }], "handler": "newrelic-lambda-wrapper.handler", "layers": [ - "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:7" + "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:17" ], "package": { "exclude": ["./**", "!newrelic-wrapper-helper.js"], diff --git a/tests/fixtures/stage-excluded.input.service.json b/tests/fixtures/stage-excluded.input.service.json new file mode 100644 index 00000000..b860d054 --- /dev/null +++ b/tests/fixtures/stage-excluded.input.service.json @@ -0,0 +1,45 @@ +{ + "service": "newrelic-lambda-layers-nodejs-example", + "provider": { + "name": "aws", + "stage": "dev", + "region": "us-east-1", + "stackTags": { + "environment": "us-testing", + "owning_team": "LAMBDA", + "product": "aws-lambda" + }, + "tags": { + "environment": "us-testing", + "owning_team": "LAMBDA", + "product": "aws-lambda" + } + }, + "plugins": ["serverless-newrelic-lambda-layers"], + "custom": { + "newRelic": { + "accountId": "${env:NEW_RELIC_ACCOUNT_ID}", + "stages": ["prod"] + } + }, + "functions": { + "layer-nodejs810": { + "events": [{ "schedule": "rate(5 minutes)" }], + "handler": "handler.handler", + "package": { "exclude": ["./**"], "include": ["handler.js"] }, + "runtime": "nodejs8.10" + }, + "layer-nodejs10x": { + "events": [{ "schedule": "rate(5 minutes)" }], + "handler": "handler.handler", + "package": { "exclude": ["./**"], "include": ["handler.js"] }, + "runtime": "nodejs10.x" + }, + "layer-nodejs12x": { + "events": [{ "schedule": "rate(5 minutes)" }], + "handler": "handler.handler", + "package": { "exclude": ["./**"], "include": ["handler.js"] }, + "runtime": "nodejs12.x" + } + } +} diff --git a/tests/fixtures/stage-excluded.output.service.json b/tests/fixtures/stage-excluded.output.service.json new file mode 100644 index 00000000..b860d054 --- /dev/null +++ b/tests/fixtures/stage-excluded.output.service.json @@ -0,0 +1,45 @@ +{ + "service": "newrelic-lambda-layers-nodejs-example", + "provider": { + "name": "aws", + "stage": "dev", + "region": "us-east-1", + "stackTags": { + "environment": "us-testing", + "owning_team": "LAMBDA", + "product": "aws-lambda" + }, + "tags": { + "environment": "us-testing", + "owning_team": "LAMBDA", + "product": "aws-lambda" + } + }, + "plugins": ["serverless-newrelic-lambda-layers"], + "custom": { + "newRelic": { + "accountId": "${env:NEW_RELIC_ACCOUNT_ID}", + "stages": ["prod"] + } + }, + "functions": { + "layer-nodejs810": { + "events": [{ "schedule": "rate(5 minutes)" }], + "handler": "handler.handler", + "package": { "exclude": ["./**"], "include": ["handler.js"] }, + "runtime": "nodejs8.10" + }, + "layer-nodejs10x": { + "events": [{ "schedule": "rate(5 minutes)" }], + "handler": "handler.handler", + "package": { "exclude": ["./**"], "include": ["handler.js"] }, + "runtime": "nodejs10.x" + }, + "layer-nodejs12x": { + "events": [{ "schedule": "rate(5 minutes)" }], + "handler": "handler.handler", + "package": { "exclude": ["./**"], "include": ["handler.js"] }, + "runtime": "nodejs12.x" + } + } +} diff --git a/tests/fixtures/stage-included.input.service.json b/tests/fixtures/stage-included.input.service.json new file mode 100644 index 00000000..1aab8f00 --- /dev/null +++ b/tests/fixtures/stage-included.input.service.json @@ -0,0 +1,45 @@ +{ + "service": "newrelic-lambda-layers-nodejs-example", + "provider": { + "name": "aws", + "stage": "dev", + "region": "us-east-1", + "stackTags": { + "environment": "us-testing", + "owning_team": "LAMBDA", + "product": "aws-lambda" + }, + "tags": { + "environment": "us-testing", + "owning_team": "LAMBDA", + "product": "aws-lambda" + } + }, + "plugins": ["serverless-newrelic-lambda-layers"], + "custom": { + "newRelic": { + "accountId": "${env:NEW_RELIC_ACCOUNT_ID}", + "stages": ["dev", "prod"] + } + }, + "functions": { + "layer-nodejs810": { + "events": [{ "schedule": "rate(5 minutes)" }], + "handler": "handler.handler", + "package": { "exclude": ["./**"], "include": ["handler.js"] }, + "runtime": "nodejs8.10" + }, + "layer-nodejs10x": { + "events": [{ "schedule": "rate(5 minutes)" }], + "handler": "handler.handler", + "package": { "exclude": ["./**"], "include": ["handler.js"] }, + "runtime": "nodejs10.x" + }, + "layer-nodejs12x": { + "events": [{ "schedule": "rate(5 minutes)" }], + "handler": "handler.handler", + "package": { "exclude": ["./**"], "include": ["handler.js"] }, + "runtime": "nodejs12.x" + } + } +} diff --git a/tests/fixtures/stage-included.output.service.json b/tests/fixtures/stage-included.output.service.json new file mode 100644 index 00000000..52ec818a --- /dev/null +++ b/tests/fixtures/stage-included.output.service.json @@ -0,0 +1,84 @@ +{ + "service": "newrelic-lambda-layers-nodejs-example", + "provider": { + "name": "aws", + "stage": "dev", + "region": "us-east-1", + "stackTags": { + "environment": "us-testing", + "owning_team": "LAMBDA", + "product": "aws-lambda" + }, + "tags": { + "environment": "us-testing", + "owning_team": "LAMBDA", + "product": "aws-lambda" + } + }, + "plugins": ["serverless-newrelic-lambda-layers"], + "custom": { + "newRelic": { + "accountId": "${env:NEW_RELIC_ACCOUNT_ID}", + "stages": ["dev", "prod"] + } + }, + "functions": { + "layer-nodejs810": { + "events": [{ "schedule": "rate(5 minutes)" }], + "handler": "newrelic-wrapper-helper.handler", + "layers": [ + "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:17" + ], + "package": { + "exclude": ["./**", "!newrelic-wrapper-helper.js"], + "include": ["handler.js"] + }, + "runtime": "nodejs8.10", + "environment": { + "NEW_RELIC_ACCOUNT_ID": "${env:NEW_RELIC_ACCOUNT_ID}", + "NEW_RELIC_APP_NAME": "layer-nodejs810", + "NEW_RELIC_LAMBDA_HANDLER": "handler.handler", + "NEW_RELIC_NO_CONFIG_FILE": "true", + "NEW_RELIC_TRUSTED_ACCOUNT_KEY": "${env:NEW_RELIC_ACCOUNT_ID}" + } + }, + "layer-nodejs10x": { + "events": [{ "schedule": "rate(5 minutes)" }], + "handler": "newrelic-lambda-wrapper.handler", + "layers": [ + "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:19" + ], + "package": { + "exclude": ["./**", "!newrelic-wrapper-helper.js"], + "include": ["handler.js"] + }, + "runtime": "nodejs10.x", + "environment": { + "NEW_RELIC_ACCOUNT_ID": "${env:NEW_RELIC_ACCOUNT_ID}", + "NEW_RELIC_APP_NAME": "layer-nodejs10x", + "NEW_RELIC_LAMBDA_HANDLER": "handler.handler", + "NEW_RELIC_NO_CONFIG_FILE": "true", + "NEW_RELIC_TRUSTED_ACCOUNT_KEY": "${env:NEW_RELIC_ACCOUNT_ID}" + } + }, + "layer-nodejs12x": { + "events": [{ "schedule": "rate(5 minutes)" }], + "handler": "newrelic-lambda-wrapper.handler", + "layers": [ + "arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:17" + ], + "package": { + "exclude": ["./**", "!newrelic-wrapper-helper.js"], + "include": ["handler.js"] + }, + "runtime": "nodejs12.x", + "environment": { + "NEW_RELIC_ACCOUNT_ID": "${env:NEW_RELIC_ACCOUNT_ID}", + "NEW_RELIC_APP_NAME": "layer-nodejs12x", + "NEW_RELIC_LAMBDA_HANDLER": "handler.handler", + "NEW_RELIC_NO_CONFIG_FILE": "true", + "NEW_RELIC_TRUSTED_ACCOUNT_KEY": "${env:NEW_RELIC_ACCOUNT_ID}" + } + } + } +}