-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add automatic log correlation of trace identifiers for bunyan (#410)
- Loading branch information
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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') | ||
} | ||
} |
This file contains 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
This file contains 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
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() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |