Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloud watch filter #11

Merged
merged 3 commits into from
Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ custom:
layerArn: arn:aws:lambda:us-east-1:451483290750:layer:NewRelicPython37:2
```

#### `cloudWatchFilter` (optional)

Provide a list of quoted filter terms for the CloudWatch log subscription to the newrelic-log-ingestion Lambda. Combines all terms into an OR filter. Defaults to "NR_LAMBDA_MONITORING" if not set. Use "*" to capture all logs

```yaml
custom:
newRelic:
cloudWatchFilter:
- "NR_LAMBDA_MONITORING"
- "trace this"
- "ERROR"
```

#### `prepend` (optional)

Whether or not to prepend the IOpipe layer. Defaults to `false` which appends the layer.
Expand Down
32 changes: 25 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ export default class NewRelicLambdaLayerPlugin {

public async addLogSubscriptions() {
const funcs = this.functions;
let { cloudWatchFilter = [ "NR_LAMBDA_MONITORING" ] } = this.config;

if(cloudWatchFilter.length == 1 && cloudWatchFilter[0] == "*") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why delete the first item here? Wouldn't it be better to iterate over the array looking for * and if found just set the filter pattern to ""?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The thinking here was if you wanted all the logs, you wouldn't provide a list of filters, just the one. i will make the change as suggested.

delete cloudWatchFilter[0];
}
else if(cloudWatchFilter.length > 1) {
cloudWatchFilter = cloudWatchFilter.map(el => `?\"${el}\"`);
}

const cloudWatchFilterString = cloudWatchFilter.join(" ");
this.serverless.cli.log(`log filter: ${cloudWatchFilterString}`);

Object.keys(funcs).forEach(async funcName => {
const { exclude = [] } = this.config;
if (_.isArray(exclude) && exclude.indexOf(funcName) !== -1) {
Expand All @@ -86,7 +98,7 @@ export default class NewRelicLambdaLayerPlugin {
);

const funcDef = funcs[funcName];
await this.ensureLogSubscription(funcDef.name);
await this.ensureLogSubscription(funcDef.name, cloudWatchFilterString);
});
}

Expand Down Expand Up @@ -277,7 +289,7 @@ export default class NewRelicLambdaLayerPlugin {
return pkg;
}

private async ensureLogSubscription(funcName: string) {
private async ensureLogSubscription(funcName: string, cloudWatchFilterString: string) {
try {
await this.awsProvider.request("Lambda", "getFunction", {
FunctionName: funcName
Expand Down Expand Up @@ -321,6 +333,8 @@ export default class NewRelicLambdaLayerPlugin {
return;
}



const existingFilters = subscriptionFilters.filter(
filter => filter.filterName === "NewRelicLogStreaming"
);
Expand All @@ -332,18 +346,18 @@ export default class NewRelicLambdaLayerPlugin {

await Promise.all(
existingFilters
.filter(filter => filter.filterPattern !== "NR_LAMBDA_MONITORING")
.filter(filter => filter.filterPattern !== cloudWatchFilterString)
.map(async filter => this.removeSubscriptionFilter(funcName))
.map(async filter =>
this.addSubscriptionFilter(funcName, destinationArn)
this.addSubscriptionFilter(funcName, destinationArn, cloudWatchFilterString)
)
);
} else {
this.serverless.cli.log(
`Adding New Relic log subscription to ${funcName}`
);

await this.addSubscriptionFilter(funcName, destinationArn);
await this.addSubscriptionFilter(funcName, destinationArn, cloudWatchFilterString);
}
}

Expand All @@ -365,13 +379,17 @@ export default class NewRelicLambdaLayerPlugin {

private async addSubscriptionFilter(
funcName: string,
destinationArn: string
destinationArn: string,
cloudWatchFilterString: string
) {



return this.awsProvider
.request("CloudWatchLogs", "putSubscriptionFilter", {
destinationArn,
filterName: "NewRelicLogStreaming",
filterPattern: "NR_LAMBDA_MONITORING",
filterPattern: cloudWatchFilterString,
logGroupName: `/aws/lambda/${funcName}`
})
.catch(err => {
Expand Down