Skip to content

Commit 04e640b

Browse files
Add log stream name prefix filtering, expecially for AWS Batch logs
1 parent da8becc commit 04e640b

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

cloudwatchlogs-with-dlq/Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ The following AWS Lambda environment variables are supported in both the lambda
2121
* SOURCE_NAME_OVERRIDE (OPTIONAL) - Override _sourceName metadata field within SumoLogic.
2222
* INCLUDE_LOG_INFO (OPTIONAL) - Set it to true when loggroup/logstream values needs to be included in logs. Default is false
2323
* LOG_FORMAT - Default is Others. One can choose VPC-JSON for VPC flow logs in json format and VPC-RAW for only RAW message line
24+
* LOG_STREAM_PREFIX (OPTIONAL) - Comma separated list of logStream name prefixes to filter by logStream, expecially for AWS Batch logs
2425

2526
### Configuring Lambda for VPC Flow Logs
2627
The following AWS Lambda environment variables are supported in both the lambda functions for VPC flow logs.

cloudwatchlogs-with-dlq/cloudwatchlogs_lambda.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ function getConfig(env) {
8282
"compressData": env.COMPRESS_DATA || true,
8383
"vpcCIDRPrefix": env.VPC_CIDR_PREFIX || '',
8484
"includeLogInfo": ("INCLUDE_LOG_INFO" in env) ? env.INCLUDE_LOG_INFO === "true" : false,
85-
"includeSecurityGroupInfo": ("INCLUDE_SECURITY_GROUP_INFO" in env) ? env.INCLUDE_SECURITY_GROUP_INFO === "true" : false
85+
"includeSecurityGroupInfo": ("INCLUDE_SECURITY_GROUP_INFO" in env) ? env.INCLUDE_SECURITY_GROUP_INFO === "true" : false,
86+
// Regex to filter by logStream name prefixes
87+
"logStreamPrefixRegex": ("LOG_STREAM_PREFIX" in env)
88+
? new RegExp('^(' + env.LOG_STREAM_PREFIX.replace(/,/g, '|') + ')', 'i')
89+
: ''
8690
};
8791
if (!config.SumoURL) {
8892
return new Error('Undefined SUMO_ENDPOINT environment variable');
@@ -134,6 +138,8 @@ exports.processLogs = function (env, eventAwslogsData, callback) {
134138
var records = [];
135139
if (awslogsData.messageType === 'CONTROL_MESSAGE') {
136140
console.log('Skipping Control Message');
141+
} else if(config.logStreamPrefixRegex && !awslogsData.logStream.match(config.logStreamPrefixRegex)){
142+
console.log('Skipping Non-Applicable Log Stream');
137143
} else {
138144
records = createRecords(config, awslogsData.logEvents, awslogsData);
139145
console.log(records.length + " Records Found");

cloudwatchlogs/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Sumo Logic Functions for AWS CloudWatch Logs
1+
# Sumo Logic Functions for AWS CloudWatch Logs
22

33
AWS Lambda function to collector logs from CloudWatch Logs and post them to [SumoLogic](http://www.sumologic.com) via a [HTTP collector endpoint](http://help.sumologic.com/Send_Data/Sources/02Sources_for_Hosted_Collectors/HTTP_Source)
44

@@ -20,7 +20,7 @@ First create an [HTTP collector endpoint](http://help.sumologic.com/Send_Data/So
2020
* Copy code from cloudwatchlogs_lambda.js into the Lambda function code.
2121
* Add Environment variables (See below)
2222
5. Scroll down to the `Lambda function handle and role` section, make sure you set the right values that match the function. For role, you can just use the basic execution role. Click next.
23-
6. Finally click on "Create function" to create the function.
23+
6. Finally click on "Create function" to create the function.
2424
7. (Optional) Test this new function with sample AWS CloudWatch Logs template provided by AWS
2525

2626
## Create Stream from CloudWatch Logs
@@ -41,6 +41,7 @@ The following AWS Lambda environment variables are supported
4141
* `SOURCE_CATEGORY_OVERRIDE` (OPTIONAL) - Override _sourceCategory metadata field within SumoLogic. If `none` will not be overridden
4242
* `SOURCE_HOST_OVERRIDE` (OPTIONAL) - Override _sourceHost metadata field within SumoLogic. If `none` will not be overridden
4343
* `SOURCE_NAME_OVERRIDE` (OPTIONAL) - Override _sourceName metadata field within SumoLogic. If `none` will not be overridden
44+
* `LOG_STREAM_PREFIX` (OPTIONAL) - Comma separated list of logStream name prefixes to filter by logStream, expecially for AWS Batch logs
4445

4546
# Dynamic Metadata Fields
4647

@@ -52,7 +53,7 @@ For example:
5253

5354
```
5455
exports.handler = (event, context, callback) => {
55-
56+
5657
var serverIp = '123.123.123.123'
5758
5859
console.log(JSON.stringify({
@@ -62,7 +63,7 @@ exports.handler = (event, context, callback) => {
6263
'source': 'other_source',
6364
'host': serverIp
6465
}
65-
66+
6667
}));
6768
console.log('some other log message with default sourceCategory');
6869
};

cloudwatchlogs/cloudwatchlogs_lambda.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ var encoding = process.env.ENCODING || 'utf-8'; // default is utf-8
2121
// Include logStream and logGroup as json fields within the message. Required for SumoLogic AWS Lambda App
2222
var includeLogInfo = false; // default is false
2323

24+
// Regex to filter by logStream name prefixes
25+
var logStreamPrefixRegex = process.env.LOG_STREAM_PREFIX
26+
? new RegExp('^(' + process.env.LOG_STREAM_PREFIX.replace(/,/g, '|') + ')', 'i')
27+
: '';
28+
2429
// Regex used to detect logs coming from lambda functions.
2530
// The regex will parse out the requestID and strip the timestamp
2631
// Example: 2016-11-10T23:11:54.523Z 108af3bb-a79b-11e6-8bd7-91c363cc05d9 some message
@@ -158,6 +163,9 @@ exports.handler = function (event, context, callback) {
158163
if (awslogsData.messageType === 'CONTROL_MESSAGE') {
159164
console.log('Control message');
160165
callback(null, 'Success');
166+
} else if(logStreamPrefixRegex && !awslogsData.logStream.match(logStreamPrefixRegex)){
167+
console.log('Skipping Non-Applicable Log Stream');
168+
return callback(null, 'Success');
161169
}
162170

163171
var lastRequestID = null;

0 commit comments

Comments
 (0)