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 instrumentation for Q #206

Closed
wants to merge 2 commits into from
Closed
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
24 changes: 24 additions & 0 deletions lib/instrumentation/q.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

var wrap = require('../shimmer').wrapMethod

module.exports = initialize

function initialize(agent, Q) {
if (Q.nextTick) {
// The wrap() call for nextTick wipes the sub-function. Save a reference
// now so it can be restored later
var savedRunAfter = Q.nextTick.runAfter

wrap(Q, 'Q', 'nextTick', function wrapUninstrumented(original, method) {
return agent.tracer.wrapFunctionFirstNoSegment(original, method)
})

if (savedRunAfter) {
Q.nextTick.runAfter = savedRunAfter;
wrap(Q.nextTick, 'Q.nextTick', 'runAfter', function wrapUninstrumented(original, method) {
return agent.tracer.wrapFunctionFirstNoSegment(original, method)
})
}
}
}
1 change: 1 addition & 0 deletions lib/instrumentations.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = function instrumentations() {
'node-cassandra-cql',
'cassandra-driver',
'pg',
'q',
'redis',
'restify',
'oracle'
Expand Down
121 changes: 121 additions & 0 deletions test/integration/instrumentation/q.tap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
'use strict'

var test = require('tap').test
var helper = require('../../lib/agent_helper')

function QContext(test, agent) {
this.agent = agent;
this.test = test;
}

QContext.prototype.assertTransaction = function assertTransaction(transaction) {
this.test.equal(this.agent.getTransaction(), transaction)
this.test.equal(this.agent.getTransaction().trace.root.children.length, 0)
}

test('Q.ninvoke', function testQNInvoke(t) {
var agent = setupAgent(t)
var Q = require('q')
var qContext = new QContext(t, agent)

var firstTest = Q.defer()
var secondTest = Q.defer()

helper.runInTransaction(agent, function transactionWrapper(transaction) {
Q.ninvoke(function anonymous() {
qContext.assertTransaction(transaction)
firstTest.resolve()
})
})

helper.runInTransaction(agent, function transactionWrapper(transaction) {
Q.ninvoke(function anonymous() {
qContext.assertTransaction(transaction)
secondTest.resolve()
})
})

Q.all([firstTest, secondTest])
.then(function done() {
t.end()
})
})

test('Q.then', function testQNInvoke(t) {
var agent = setupAgent(t)
var Q = require('q')
var qContext = new QContext(t, agent)

var firstTest = Q.defer()
var secondTest = Q.defer()

helper.runInTransaction(agent, function transactionWrapper(transaction) {
Q(true).then(function anonymous() {
qContext.assertTransaction(transaction)
firstTest.resolve()
})
})

helper.runInTransaction(agent, function transactionWrapper(transaction) {
Q(true).then(function anonymous() {
qContext.assertTransaction(transaction)
secondTest.resolve()
})
})

Q.all([firstTest, secondTest])
.then(function done() {
t.end()
})
})

test('Q.then rejections', function testQNInvoke(t) {
var agent = setupAgent(t)
var Q = require('q')
var qContext = new QContext(t, agent)

var firstTest = Q.defer()
var secondTest = Q.defer()

helper.runInTransaction(agent, function transactionWrapper(transaction) {
var thrownError = new Error('Unhandled error');
process.on('unhandledRejection', function rejectionHandler(error) {
if (error === thrownError) {
qContext.assertTransaction(transaction)
firstTest.resolve()
}
})

Q(true).then(function anonymous() {
throw thrownError
})
})

helper.runInTransaction(agent, function transactionWrapper(transaction) {
var thrownError = new Error('Unhandled error');
process.on('unhandledRejection', function rejectionHandler(error) {
if (error === thrownError) {
qContext.assertTransaction(transaction)
secondTest.resolve()
}
})

Q(true).then(function anonymous() {
throw thrownError
})
})

Q.all([firstTest, secondTest])
.then(function done() {
t.end()
})
})

function setupAgent(t) {
var agent = helper.instrumentMockedAgent()
t.tearDown(function tearDown() {
helper.unloadAgent(agent)
})

return agent
}