Skip to content

Commit 253ccc6

Browse files
authored
Merge pull request #98 from ykitamura-mdsol/feature/log_stream_filter
Add log stream name prefix filtering, especially for AWS Batch logs
2 parents da8becc + e64128c commit 253ccc6

File tree

5 files changed

+36
-7
lines changed

5 files changed

+36
-7
lines changed

cloudwatchlogs-with-dlq/DLQLambdaCloudFormation.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
"Default": "false",
2929
"AllowedValues" : ["true" ,"false"],
3030
"Description": "Select true to get loggroup/logstream values in logs"
31+
},
32+
"LogStreamPrefix": {
33+
"Type": "String",
34+
"Description": "Enter comma separated list of logStream name prefixes to filter by logStream"
3135
}
3236
},
3337
"Mappings" : {
@@ -221,7 +225,8 @@
221225
"Variables": {
222226
"SUMO_ENDPOINT": {"Ref": "SumoEndPointURL"},
223227
"LOG_FORMAT": {"Ref": "LogFormat"},
224-
"INCLUDE_LOG_INFO": {"Ref": "IncludeLogGroupInfo"}
228+
"INCLUDE_LOG_INFO": {"Ref": "IncludeLogGroupInfo"},
229+
"LOG_STREAM_PREFIX": {"Ref": "LogStreamPrefix"}
225230

226231
}
227232
}
@@ -296,7 +301,8 @@
296301
},
297302
"NUM_OF_WORKERS": {"Ref": "NumOfWorkers"},
298303
"LOG_FORMAT": {"Ref": "LogFormat"},
299-
"INCLUDE_LOG_INFO": {"Ref": "IncludeLogGroupInfo"}
304+
"INCLUDE_LOG_INFO": {"Ref": "IncludeLogGroupInfo"},
305+
"LOG_STREAM_PREFIX": {"Ref": "LogStreamPrefix"}
300306
}
301307
}
302308
}

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, especially 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: 11 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('^(' + escapeRegExp(env.LOG_STREAM_PREFIX).replace(/,/g, '|') + ')', 'i')
89+
: ''
8690
};
8791
if (!config.SumoURL) {
8892
return new Error('Undefined SUMO_ENDPOINT environment variable');
@@ -95,6 +99,10 @@ function getConfig(env) {
9599
return config;
96100
}
97101

102+
function escapeRegExp(string) {
103+
return string.replace(/[|\\{}()[\]^$+*?.-]/g, '\\$&');
104+
}
105+
98106
function transformRecords(config, records) {
99107
return new Promise(function (resolve, reject) {
100108
if (config.LogFormat === "VPC-JSON" && config.includeSecurityGroupInfo) {
@@ -134,6 +142,8 @@ exports.processLogs = function (env, eventAwslogsData, callback) {
134142
var records = [];
135143
if (awslogsData.messageType === 'CONTROL_MESSAGE') {
136144
console.log('Skipping Control Message');
145+
} else if(config.logStreamPrefixRegex && !awslogsData.logStream.match(config.logStreamPrefixRegex)){
146+
console.log('Skipping Non-Applicable Log Stream');
137147
} else {
138148
records = createRecords(config, awslogsData.logEvents, awslogsData);
139149
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, especially 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: 11 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('^(' + escapeRegExp(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
@@ -33,6 +38,9 @@ var https = require('https');
3338
var zlib = require('zlib');
3439
var url = require('url');
3540

41+
function escapeRegExp(string) {
42+
return string.replace(/[|\\{}()[\]^$+*?.-]/g, '\\$&');
43+
}
3644

3745
function sumoMetaKey(awslogsData, message) {
3846
var sourceCategory = '';
@@ -158,6 +166,9 @@ exports.handler = function (event, context, callback) {
158166
if (awslogsData.messageType === 'CONTROL_MESSAGE') {
159167
console.log('Control message');
160168
callback(null, 'Success');
169+
} else if(logStreamPrefixRegex && !awslogsData.logStream.match(logStreamPrefixRegex)){
170+
console.log('Skipping Non-Applicable Log Stream');
171+
return callback(null, 'Success');
161172
}
162173

163174
var lastRequestID = null;

0 commit comments

Comments
 (0)