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 when #376

Merged
merged 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/plugins/index.js
Expand Up @@ -17,5 +17,6 @@ module.exports = {
'mysql2': require('./mysql2'),
'pg': require('./pg'),
'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)
})
})
})
})
})
})