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

add automatic log correlation of trace identifiers for bunyan #410

Merged
merged 1 commit into from
Jan 11, 2019
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
25 changes: 25 additions & 0 deletions src/plugins/bunyan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict'

const tx = require('./util/log')

function createWrapEmit (tracer, config) {
return function wrapEmit (emit) {
return function emitWithTrace (rec, noemit) {
tx.correlate(tracer, rec)

return emit.apply(this, arguments)
}
}
}

module.exports = {
name: 'bunyan',
versions: ['1 - 2'],
patch (Logger, tracer, config) {
if (!config.correlate) return
this.wrap(Logger.prototype, '_emit', createWrapEmit(tracer, config))
},
unpatch (Logger) {
this.unwrap(Logger.prototype, '_emit')
}
}
1 change: 1 addition & 0 deletions src/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
'amqp10': require('./amqp10'),
'amqplib': require('./amqplib'),
'bluebird': require('./bluebird'),
'bunyan': require('./bunyan'),
'dns': require('./dns'),
'elasticsearch': require('./elasticsearch'),
'express': require('./express'),
Expand Down
87 changes: 87 additions & 0 deletions test/plugins/bunyan.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
'use strict'

const Writable = require('stream').Writable
const agent = require('./agent')
const plugin = require('../../src/plugins/bunyan')

wrapIt()

describe('Plugin', () => {
let logger
let tracer
let stream
let span

function setup (version) {
const bunyan = require(`../../versions/bunyan@${version}`).get()

span = tracer.startSpan('test')

stream = new Writable()
stream._write = () => {}

sinon.spy(stream, 'write')

logger = bunyan.createLogger({ name: 'test', stream })
}

describe('bunyan', () => {
withVersions(plugin, 'bunyan', version => {
beforeEach(() => {
tracer = require('../..')
})

afterEach(() => {
return agent.close()
})

describe('without configuration', () => {
beforeEach(() => {
return agent.load(plugin, 'bunyan')
.then(() => {
setup(version)
})
})

it('should not alter the default behavior', () => {
tracer.scopeManager().activate(span)

logger.info('message')

expect(stream.write).to.have.been.called

const record = JSON.parse(stream.write.firstCall.args[0].toString())

expect(record).to.not.include({
'dd.trace_id': span.context().toTraceId(),
'dd.span_id': span.context().toSpanId()
})
})
})

describe('with configuration', () => {
beforeEach(() => {
return agent.load(plugin, 'bunyan', { correlate: true })
.then(() => {
setup(version)
})
})

it('should add the trace identifiers to logger instances', () => {
tracer.scopeManager().activate(span)

logger.info('message')

expect(stream.write).to.have.been.called

const record = JSON.parse(stream.write.firstCall.args[0].toString())

expect(record).to.include({
'dd.trace_id': span.context().toTraceId(),
'dd.span_id': span.context().toSpanId()
})
})
})
})
})
})