Last active
January 21, 2019 19:01
-
-
Save mlabouardy/8a0feeaf43e410257027b0a847b74bf3 to your computer and use it in GitHub Desktop.
Ship Kinesis events to Logstash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Zlib = require('zlib'); | |
const Logstash = require('logstash-client'); | |
const lambdaVersion = function (logStream) { | |
let start = logStream.indexOf('['); | |
let end = logStream.indexOf(']'); | |
return logStream.substring(start+1, end); | |
} | |
exports.handler = async (event, context) => { | |
const logstashClient = new Logstash({ | |
type: 'udp', | |
host: process.env.LOGSTASH_HOST, | |
port: process.env.LOGSTASH_PORT | |
}); | |
event.Records.forEach(record => { | |
const payload = new Buffer(record.kinesis.data, 'base64'); | |
const json = (Zlib.gunzipSync(payload)).toString('utf8'); | |
const data = JSON.parse(json); | |
const functionName = data.logGroup.split('/').reverse()[0]; | |
const functionVersion = lambdaVersion(data.logStream); | |
const awsRegion = record.awsRegion; | |
data.logEvents.forEach((event) => { | |
if (!(event.message.startsWith('START RequestId') || | |
event.message.startsWith('END RequestId') || | |
event.message.startsWith('REPORT RequestId'))) { | |
logstashClient.send({ | |
message: event.extractedFields.event, | |
function: functionName, | |
version: functionVersion, | |
region: awsRegion, | |
}); | |
} | |
}) | |
}) | |
return 'done'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment