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

Inject trace ids into paperplane logs #633

Merged
merged 3 commits into from
Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
69 changes: 49 additions & 20 deletions packages/datadog-plugin-paperplane/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,53 @@ const wrapRoutes = tracer => routes => handlers => {
return routes(traced)
}

function patch (paperplane, tracer, config) {
config = web.normalizeConfig(config)

if (tracer._logInjection) {
this.wrap(paperplane, 'logger', wrapLogger(tracer))
module.exports = [
{
name: 'paperplane',
versions: ['>=2.3.2'],
rochdev marked this conversation as resolved.
Show resolved Hide resolved
file: 'lib/logger.js',
patch (exports, tracer) {
if (tracer._logInjection) {
this.wrap(exports, 'logger', wrapLogger(tracer))
}
},
unpatch (exports) {
this.unwrap(exports, 'logger')
}
},
{
name: 'paperplane',
versions: ['>=2.3.2'],
file: 'lib/mount.js',
patch (exports, tracer, config) {
config = web.normalizeConfig(config)
this.wrap(exports, 'mount', wrapMount(tracer, config))
},
unpatch (exports) {
this.unwrap(exports, 'mount')
}
},
{
name: 'paperplane',
versions: ['>=2.3.2'],
file: 'lib/routes.js',
patch (exports, tracer) {
this.wrap(exports, 'routes', wrapRoutes(tracer))
},
unpatch (exports) {
this.unwrap(exports, 'routes')
}
},
{
name: 'paperplane',
versions: ['2.3.0 - 2.3.1'],
rochdev marked this conversation as resolved.
Show resolved Hide resolved
patch (paperplane, tracer, config) {
config = web.normalizeConfig(config)
this.wrap(paperplane, 'mount', wrapMount(tracer, config))
this.wrap(paperplane, 'routes', wrapRoutes(tracer))
},
unpatch (paperplane) {
this.unwrap(paperplane, ['mount', 'routes'])
}
}

this.wrap(paperplane, 'mount', wrapMount(tracer, config))
this.wrap(paperplane, 'routes', wrapRoutes(tracer))
}

function unpatch (paperplane) {
this.unwrap(paperplane, ['logger', 'mount', 'routes'])
}

module.exports = {
name: 'paperplane',
versions: ['>=2.3.1'],
patch,
unpatch
}
]
113 changes: 59 additions & 54 deletions packages/datadog-plugin-paperplane/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const axios = require('axios')
const getPort = require('get-port')
const http = require('http')
const semver = require('semver')

const agent = require('../../dd-trace/test/plugins/agent')
const plugin = require('../src')
Expand Down Expand Up @@ -428,23 +429,25 @@ describe('Plugin', () => {
})
})

it('should not alter the behavior of `logger`', done => {
span = tracer.startSpan('test')
if (semver.intersects(version, '>=2.3.1')) {
it('should not alter the behavior of `logger`', done => {
span = tracer.startSpan('test')

tracer.scope().activate(span, () => {
/* eslint-disable no-console */
paperplane.logger({ message: ':datadoge:' })
tracer.scope().activate(span, () => {
/* eslint-disable no-console */
paperplane.logger({ message: ':datadoge:' })

expect(console.info).to.have.been.called
expect(console.info).to.have.been.called

const record = JSON.parse(console.info.firstCall.args[0])
const record = JSON.parse(console.info.firstCall.args[0])

expect(record).to.not.have.property('dd')
expect(record).to.have.property('message', ':datadoge:')
done()
/* eslint-enable no-console */
expect(record).to.not.have.property('dd')
expect(record).to.have.property('message', ':datadoge:')
done()
/* eslint-enable no-console */
})
})
})
}
})

describe('with configuration', () => {
Expand Down Expand Up @@ -556,65 +559,67 @@ describe('Plugin', () => {
})
})

it('should add the trace ids to logs', done => {
span = tracer.startSpan('test')
if (semver.intersects(version, '>=2.3.2')) {
it('should add the trace ids to logs', done => {
span = tracer.startSpan('test')

tracer.scope().activate(span, () => {
/* eslint-disable no-console */
paperplane.logger({ message: ':datadoge:' })
tracer.scope().activate(span, () => {
/* eslint-disable no-console */
paperplane.logger({ message: ':datadoge:' })

expect(console.info).to.have.been.called
expect(console.info).to.have.been.called

const record = JSON.parse(console.info.firstCall.args[0])
const record = JSON.parse(console.info.firstCall.args[0])

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

expect(record).to.have.property('message', ':datadoge:')
done()
/* eslint-enable no-console */
expect(record).to.have.property('message', ':datadoge:')
done()
/* eslint-enable no-console */
})
})
})

it('should add the trace ids to error logs', done => {
span = tracer.startSpan('test')
it('should add the trace ids to error logs', done => {
span = tracer.startSpan('test')

tracer.scope().activate(span, () => {
/* eslint-disable no-console */
const err = new Error('Bad things happened')
paperplane.logger(err)
tracer.scope().activate(span, () => {
/* eslint-disable no-console */
const err = new Error('Bad things happened')
paperplane.logger(err)

expect(console.info).to.have.been.called
expect(console.info).to.have.been.called

const record = JSON.parse(console.info.firstCall.args[0])
const record = JSON.parse(console.info.firstCall.args[0])

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

expect(record).to.have.property('message', 'Bad things happened')
expect(record).to.have.property('name', 'Error')
expect(record).to.have.property('stack')
done()
/* eslint-enable no-console */
expect(record).to.have.property('message', 'Bad things happened')
expect(record).to.have.property('name', 'Error')
expect(record).to.have.property('stack')
done()
/* eslint-enable no-console */
})
})
})

it('should not alter logs with no active span', () => {
/* eslint-disable no-console */
paperplane.logger({ message: ':datadoge:' })
it('should not alter logs with no active span', () => {
/* eslint-disable no-console */
paperplane.logger({ message: ':datadoge:' })

expect(console.info).to.have.been.called
expect(console.info).to.have.been.called

const record = JSON.parse(console.info.firstCall.args[0])
const record = JSON.parse(console.info.firstCall.args[0])

expect(record).to.not.have.property('dd')
expect(record).to.have.property('message', ':datadoge:')
/* eslint-enable no-console */
})
expect(record).to.not.have.property('dd')
expect(record).to.have.property('message', ':datadoge:')
/* eslint-enable no-console */
})
}
})
})
})
Expand Down