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 support for Q #375

Merged
merged 2 commits into from
Nov 13, 2018
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 src/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
'mysql': require('./mysql'),
'mysql2': require('./mysql2'),
'pg': require('./pg'),
'q': require('./q'),
'redis': require('./redis'),
'restify': require('./restify')
}
16 changes: 16 additions & 0 deletions src/plugins/q.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

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

module.exports = [
{
name: 'q',
versions: ['1'],
patch (Q, tracer, config) {
this.wrap(Q.makePromise.prototype, 'then', tx.createWrapThen(tracer, config))
},
unpatch (Q) {
this.unwrap(Q.makePromise.prototype, 'then')
}
}
]
85 changes: 85 additions & 0 deletions test/plugins/q.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
'use strict'

const agent = require('./agent')
const plugin = require('../../src/plugins/q')

wrapIt()

describe('Plugin', () => {
let Q
let tracer

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

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

describe('without configuration', () => {
beforeEach(() => {
return agent.load(plugin, 'q')
.then(() => {
Q = require(`../../versions/q@${version}`).get()
})
})

it('should run the then() callback in context where then() was called', () => {
if (process.env.DD_CONTEXT_PROPAGATION === 'false') return

const span = {}
const deferred = Q.defer()
const promise = deferred.promise

setImmediate(() => {
tracer.scopeManager().activate({})
deferred.resolve()
})

tracer.scopeManager().activate(span)

return promise
.then(() => {
tracer.scopeManager().activate({})
})
.then(() => {
const scope = tracer.scopeManager().active()

expect(scope).to.not.be.null
expect(scope.span()).to.equal(span)
})
})

it('should run the catch() callback in context where catch() was called', () => {
if (process.env.DD_CONTEXT_PROPAGATION === 'false') return

const span = {}
const deferred = Q.defer()
const promise = deferred.promise

setImmediate(() => {
tracer.scopeManager().activate({})
deferred.reject(new Error())
})

tracer.scopeManager().activate(span)

return promise
.catch(err => {
tracer.scopeManager().activate({})
throw err
})
.catch(() => {
const scope = tracer.scopeManager().active()

expect(scope).to.not.be.null
expect(scope.span()).to.equal(span)
})
})
})
})
})
})