Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/ovhemert/pino-datadog
Browse files Browse the repository at this point in the history
  • Loading branch information
ovhemert committed Sep 9, 2020
2 parents 2ce363d + 5d65231 commit cb7750f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 36 deletions.
6 changes: 6 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,9 @@ Set a default service to all the logs sent to datadog
Type: `String` *(optional)*

Set a default hostname to all the logs sent to datadog

#### keepMsg

Type: `Boolean` *(optional)*

Keep the `msg` attribute in the log record. Used to allow a Datadog facet on the message.
44 changes: 13 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
"tap": "^14.10.8"
},
"dependencies": {
"axios": "^0.19.2",
"axios": "^0.20.0",
"batch2": "^1.0.6",
"commander": "^6.0.0",
"commander": "^6.1.0",
"fast-json-parse": "^1.0.3",
"pumpify": "^2.0.1",
"split2": "^3.1.1",
"split2": "^3.2.2",
"stream": "0.0.2",
"through2": "^4.0.2"
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function createWriteStreamSync (options = {}) {
const { size = 1 } = options

const parseJsonStream = streams.parseJsonStream()
const toLogEntryStream = streams.toLogEntryStream()
const toLogEntryStream = streams.toLogEntryStream(options)
const batchStream = streams.batchStream(size)

const client = new datadog.Client(options)
Expand Down
5 changes: 4 additions & 1 deletion src/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function toLogEntry (item) {
const ddtags = Object.keys(objTags).map(k => { return `${k}:${objTags[k]}` }).join(',')

const entry = Object.assign({}, item, { timestamp, status, message, host, service, ddsource, ddtags })
delete entry.time; delete entry.level; delete entry.msg; delete entry.hostname; delete entry.source; delete entry.labels; delete entry.tags
delete entry.time; delete entry.level; delete entry.hostname; delete entry.source; delete entry.labels; delete entry.tags
if (!service) { delete entry.service }
if (!ddsource) { delete entry.ddsource }
if (!ddtags) { delete entry.ddtags }
Expand All @@ -46,6 +46,9 @@ function toLogEntry (item) {
function toLogEntryStream (options = {}) {
return through2.obj(function transport (chunk, enc, cb) {
const entry = toLogEntry(chunk)
if (!options.keepMsg) {
delete entry.msg
}
cb(null, entry)
})
}
Expand Down
17 changes: 17 additions & 0 deletions test/streams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,20 @@ test('transforms pino log messages', t => {
logs.forEach(log => writeStream.write(log))
writeStream.end()
})

test('transforms pino log stream, leaves msg alone', t => {
const writeStream = tested.toLogEntryStream({ keepMsg: true })
const output = []
const logs = [
{ level: 10, time: 1532081790710, msg: 'trace message' }
]
writeStream.on('data', chunk => {
output.push(chunk)
}).on('end', () => {
t.equal(output[0].message, 'trace message')
t.equal(output[0].msg, 'trace message')
t.end()
})
logs.forEach(log => writeStream.write(log))
writeStream.end()
})

0 comments on commit cb7750f

Please sign in to comment.