Skip to content

Commit

Permalink
add support for when (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
rochdev committed Nov 13, 2018
1 parent 80aed2b commit 6c35df0
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/plugins/index.js
Expand Up @@ -18,5 +18,6 @@ module.exports = {
'pg': require('./pg'),
'q': require('./q'),
'redis': require('./redis'),
'restify': require('./restify')
'restify': require('./restify'),
'when': require('./when')
}
17 changes: 17 additions & 0 deletions src/plugins/when.js
@@ -0,0 +1,17 @@
'use strict'

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

module.exports = [
{
name: 'when',
file: 'lib/Promise.js',
versions: ['3'],
patch (Promise, tracer, config) {
this.wrap(Promise.prototype, 'then', tx.createWrapThen(tracer, config))
},
unpatch (Promise) {
this.unwrap(Promise.prototype, 'then')
}
}
]
111 changes: 111 additions & 0 deletions test/plugins/when.spec.js
@@ -0,0 +1,111 @@
'use strict'

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

wrapIt()

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

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

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

describe('without configuration', () => {
beforeEach(() => {
return agent.load(plugin, 'when')
.then(() => {
when = require(`../../versions/when@${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 = when.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 = when.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)
})
})

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

const span = {}
const deferred = when.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)
})
})
})
})
})
})

0 comments on commit 6c35df0

Please sign in to comment.