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

Added Heroku log formatting for JSON log messages #266

Merged
merged 5 commits into from
Nov 27, 2020
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
1 change: 1 addition & 0 deletions bin/logagent.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ var moduleAlias = {
sql: '../lib/plugins/output-filter/sql.js',
'access-watch': '../lib/plugins/output-filter/access-watch.js',
'cloudfoundry-format': '../lib/plugins/output-filter/cloudfoundry-format.js',
'heroku-format': '../lib/plugins/output-filter/heroku-format.js',
'hash-fields': '../lib/plugins/output-filter/hash-fields.js',
'aes-encrypt-fields': '../lib/plugins/output-filter/aes-encrypt-fields.js',
'ip-truncate-fields': '../lib/plugins/output-filter/ip-truncate-fields.js',
Expand Down
20 changes: 20 additions & 0 deletions config/examples/heroku-elasticsearch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Global options
options:
# includeOriginalLine: true
# debug: true

input:
cloudFoundry:
module: input-heroku
port: 9999

outputFilter:
heroku-format:
module: heroku-format

output:
elasticsearch:
module: elasticsearch
url: https://logsene-receiver.sematext.com
# debug: true

5 changes: 5 additions & 0 deletions config/examples/heroku-stdout-yml.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ parser:
}
}
}

outputFilter:
heroku-format:
module: heroku-format

output:
# print parsed logs in YAML format to stdout if supress is set false
stdout: yaml # use 'pretty' for pretty json and 'ldjson' for line delimited json (default)
Expand Down
60 changes: 1 addition & 59 deletions lib/plugins/input/heroku.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,6 @@ const extractTokenRegEx = /([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9
const tokenFormatRegEx = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/
const TokenBlacklist = require('../../util/token-blacklist.js')

function jsonParse (text) {
try {
return JSON.parse(text)
} catch (err) {
return null
}
}

function extractJson (line, source) {
var parsed = {}
if (/^\[{0,1}\{.*\}]{0,1}$/.test(line)) {
parsed = jsonParse(line)
if (!parsed) {
return null
}
return parsed
}
}

function InputHeroku (config, eventEmitter) {
this.config = config
this.eventEmitter = eventEmitter
Expand Down Expand Up @@ -79,44 +60,6 @@ InputHeroku.prototype.getHttpServer = function (aport, handler) {
}
}

function filterHerokuMessage (data, context) {
if (data) {
data._type = context.sourceName.replace('_' + context.index, '')
data.logSource = ('' + data.logSource).replace('_' + context.index, '')
var msg = {
message: data.message,
app: data.app,
host: data.host,
process_type: data.process_type,
originalLine: data.originalLine,
severity: data.severity,
facility: data.facility
}
msg.json = extractJson(msg.message)
var optionalFields = [
'method',
'path',
'host',
'request_id',
'fwd',
'dyno',
'connect',
'service',
'status',
'bytes'
]
optionalFields.forEach(function (f) {
if (data[f]) {
msg[f] = data[f]
}
})
if (!data['@timestamp']) {
msg['@timestamp'] = new Date()
}
return msg
}
}

InputHeroku.prototype.herokuHandler = function (req, res) {
try {
var self = this
Expand Down Expand Up @@ -166,8 +109,7 @@ InputHeroku.prototype.herokuHandler = function (req, res) {
}
self.eventEmitter.emit('data.raw', line, {
sourceName: 'heroku_' + token,
index: token,
filter: filterHerokuMessage
index: token
})
})
res.end('ok\n')
Expand Down
3 changes: 1 addition & 2 deletions lib/plugins/output-filter/cloudfoundry-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ function filterCloudFoundryMessage (data, context) {
}

function parseCloudFoundryTags (tags) {
console.log(tags)
try {
const parsedTags2dArr = tags
.split(' ')
Expand All @@ -53,7 +52,7 @@ function parseCloudFoundryTags (tags) {

return parsedTags
} catch (error) {
console.log(error)
console.error(error)
return tags
}
}
Expand Down
76 changes: 76 additions & 0 deletions lib/plugins/output-filter/heroku-format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
function jsonParse (text) {
try {
return JSON.parse(text)
} catch (err) {
return null
}
}

function extractJson (line, source) {
let parsed = {}
if (/^\[{0,1}\{.*\}]{0,1}$/.test(line)) {
parsed = jsonParse(line)
if (!parsed) {
return null
}
return parsed
}
}

function filterHerokuMessage (data, context) {
if (data) {
data._type = context.sourceName.replace('_' + context.index, '')
data.logSource = ('' + data.logSource).replace('_' + context.index, '')
const msg = {
message: data.message,
app: data.app,
host: data.host,
process_type: data.process_type,
originalLine: data.originalLine,
severity: data.severity,
facility: data.facility
}

const optionalFields = [
'method',
'path',
'host',
'request_id',
'fwd',
'dyno',
'connect',
'service',
'status',
'bytes'
]
optionalFields.forEach(function (f) {
if (data[f]) {
msg[f] = data[f]
}
})
if (!data['@timestamp']) {
msg['@timestamp'] = new Date()
}

const json = extractJson(msg.message)
if (json && json.message) {
delete msg.message
}

return {
...msg,
...json
}
}
}

module.exports = function (context, config, eventEmitter, log, callback) {
try {
const parsedLog = filterHerokuMessage(log, context)

return callback(null, parsedLog)
} catch (err) {
console.log(err)
return callback(null, log)
}
}