Skip to content

Commit

Permalink
Merge pull request #235 from sematext/vercel-integration-updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Adnan Rahić authored May 20, 2020
2 parents 9032064 + 6a8da01 commit 37532b4
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 10 deletions.
3 changes: 2 additions & 1 deletion config/examples/vercel-input-es-output.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ input:
module: input-vercel
port: 8400
useIndexFromUrlPath: true
clientSecret: <CLIENT_SECRET>
# workers: 4


outputFilter:
vercel-format:
Expand All @@ -17,4 +19,3 @@ output:
sematext-cloud:
module: elasticsearch
url: https://logs-token-receiver.apps.test.sematext.com
debug: false
28 changes: 27 additions & 1 deletion lib/plugins/input/vercel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const consoleLogger = require('../../util/logger.js')
const http = require('http')
const crypto = require('crypto')
const throng = require('throng')
const extractTokenRegEx = /([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/
const tokenFormatRegEx = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/
Expand Down Expand Up @@ -91,6 +92,7 @@ class Vercel {
const log = docs[i]
log['@timestamp'] = new Date(log.timestamp)
const { timestamp, ...filteredLog } = log

self.emitEvent(filteredLog, token)
}
} else {
Expand All @@ -99,6 +101,19 @@ class Vercel {
}
}

verifySignature (req, body) {
const signature = crypto
.createHmac('sha1', this.config.clientSecret)
.update(body)
.digest('hex')

if (this.config.debug) {
consoleLogger.log("Vercel signature didn't match")
}

return signature === req.headers['x-zeit-signature']
}

HttpHandler (req, res) {
try {
const self = this
Expand Down Expand Up @@ -130,8 +145,19 @@ class Vercel {
})
req.on('end', function endHandler () {
try {
// verify log messages are from Vercel
if (!self.verifySignature(req, bodyIn)) {
res.writeHead(403, { 'Content-Type': 'text/plain' })
res.end("Vercel signature didn't match")
return
}

self.parseBody(bodyIn, token)
} catch (err) {
if (self.config.debug) {
consoleLogger.error('Error in Vercel HttpHandler: ' + err)
}

res.writeHead(500, { 'Content-Type': 'text/plain' })
res.end(`Invalid json input: ${err}\n`)
return
Expand All @@ -143,7 +169,7 @@ class Vercel {
} catch (err) {
res.statusCode = 500
res.end()
consoleLogger.error('Error in Kubernetes Audit HttpHandler: ' + err)
consoleLogger.error('Error in Vercel HttpHandler: ' + err)
}
}

Expand Down
73 changes: 65 additions & 8 deletions lib/plugins/output-filter/vercel-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ const initDurationRegex = /Init\sDuration:\s([\d+]+\.[\d+]+)\sms/
function formatVercelLogsOutput (context, config, eventEmitter, log, callback) {
try {
const parsedLog = parseVercelLog(log)
const error = parseVercelError(log.message)
const logToSend = { ...parsedLog, ...error }

if (config.debug) {
console.log(parsedLog)
console.log(logToSend)
}

if (parsedLog) {
callback(null, parsedLog)
if (logToSend) {
callback(null, logToSend)
}
} catch (e) {
if (config.debug) {
console.log(e, log)
}

callback(e, log)
}
}
Expand All @@ -30,15 +36,15 @@ function parseVercelLog (log) {
const filtered = splitN.filter(f => f.startsWith('REPORT'))
const splitT = filtered.pop().split('\t')
const lambdaVals = splitT.splice(1, 5)
const duration = lambdaVals[0].match(durationRegex)[1]
const billedDuration = lambdaVals[1].match(billedDurationRegex)[1]
const memorySize = lambdaVals[2].match(memorySizeRegex)[1]
const maxMemoryUsed = lambdaVals[3].match(maxMemoryUsedRegex)[1]

const duration = +lambdaVals[0].match(durationRegex)[1]
const billedDuration = +lambdaVals[1].match(billedDurationRegex)[1]
const memorySize = +lambdaVals[2].match(memorySizeRegex)[1]
const maxMemoryUsed = +lambdaVals[3].match(maxMemoryUsedRegex)[1]
const initDuration = {}
if (lambdaVals[4] && lambdaVals[4].length) {
initDuration.coldStart = true
initDuration.initDuration = lambdaVals[4].match(initDurationRegex)[1]
initDuration.initDuration = +lambdaVals[4].match(initDurationRegex)[1]
}

return {
Expand All @@ -52,4 +58,55 @@ function parseVercelLog (log) {
}
}

function parseVercelError (message) {
try {
const errorLine = message
.split('\n')
.filter(line => line.length)
.filter(
line =>
!(
line.startsWith('REPORT') ||
line.startsWith('END') ||
line.startsWith('START')
)
)
.map(line => removeErrorLineStart(line))
.shift()

const { errorType, errorMessage, stack } = tryParseErrorLine(errorLine)

return {
error: {
type: errorType,
message: errorMessage,
stack: stack.join('\n')
}
}
} catch (error) {
// return empty object it is not an error message
return {}
}
}

function removeErrorLineStart (line) {
const res = line.replace(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/, '')
return res.replace(
/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i,
''
)
}

function tryParseErrorLine (line) {
const json = line.match(/{[\s\S]*}/)
if (json === null) {
return line
}
try {
return JSON.parse(json[0])
} catch (e) {
return line
}
}

module.exports = formatVercelLogsOutput

0 comments on commit 37532b4

Please sign in to comment.