Skip to content

Commit

Permalink
limit plugin to specific stages (#56)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
karopolopoulos authored Aug 18, 2020
1 parent d8c4318 commit 335de26
Show file tree
Hide file tree
Showing 14 changed files with 291 additions and 24 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand All @@ -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": {
Expand Down
43 changes: 40 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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(
Expand Down Expand Up @@ -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"
Expand All @@ -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;
}

Expand All @@ -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"
Expand Down Expand Up @@ -217,7 +244,7 @@ export default class NewRelicLambdaLayerPlugin {
return;
}

if (this.shouldSkip(funcName)) {
if (this.shouldSkipFunction(funcName)) {
return;
}

Expand Down Expand Up @@ -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 (
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/debug-log-level.output.service.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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"],
Expand All @@ -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"],
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/debug.output.service.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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"],
Expand All @@ -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"],
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/include.output.service.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/log-disabled.output.service.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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"],
Expand All @@ -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"],
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/log-level.output.service.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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"],
Expand All @@ -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"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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"],
Expand All @@ -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"],
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/provider-environment.output.service.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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"],
Expand All @@ -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"],
Expand Down
45 changes: 45 additions & 0 deletions tests/fixtures/stage-excluded.input.service.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
45 changes: 45 additions & 0 deletions tests/fixtures/stage-excluded.output.service.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
Loading

0 comments on commit 335de26

Please sign in to comment.