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 all 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
30 changes: 23 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ export default class NewRelicLambdaLayerPlugin {

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

let cloudWatchFilterString = ""
if(!cloudWatchFilter.find( filter => filter == "*")) {
cloudWatchFilter = cloudWatchFilter.map(el => `?\"${el}\"`);
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 +96,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 +287,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 +331,8 @@ export default class NewRelicLambdaLayerPlugin {
return;
}



const existingFilters = subscriptionFilters.filter(
filter => filter.filterName === "NewRelicLogStreaming"
);
Expand All @@ -332,18 +344,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 +377,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