-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(*): use async_hooks for Node.js 8.1.0 and above
Fixes #75
- Loading branch information
Showing
6 changed files
with
171 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict' | ||
|
||
const asyncHooks = require('async_hooks') | ||
|
||
module.exports = function (ins) { | ||
const asyncHook = asyncHooks.createHook({init, before, after, destroy}) | ||
const initState = new Map() | ||
const beforeState = new Map() | ||
|
||
asyncHook.enable() | ||
|
||
function init (asyncId, type, triggerAsyncId, resource) { | ||
// We don't care about the TIMERWRAP, as it will only init once for each | ||
// timer that shares the timeout value. Instead we rely on the Timeout | ||
// type, which will init for each scheduled timer. | ||
if (type === 'TIMERWRAP') return | ||
|
||
initState.set(asyncId, ins.currentTransaction) | ||
} | ||
|
||
function before (asyncId) { | ||
if (!initState.has(asyncId)) return // in case type === TIMERWRAP | ||
beforeState.set(asyncId, ins.currentTransaction) | ||
ins.currentTransaction = initState.get(asyncId) | ||
} | ||
|
||
function after (asyncId) { | ||
if (!initState.has(asyncId)) return // in case type === TIMERWRAP | ||
ins.currentTransaction = beforeState.get(asyncId) | ||
} | ||
|
||
function destroy (asyncId) { | ||
if (!initState.has(asyncId)) return // in case type === TIMERWRAP | ||
initState.delete(asyncId) | ||
beforeState.delete(asyncId) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict' | ||
|
||
var agent = require('../..').start({ | ||
appName: 'test', | ||
captureExceptions: false | ||
}) | ||
var ins = agent._instrumentation | ||
|
||
var test = require('tape') | ||
|
||
test('setTimeout', function (t) { | ||
t.plan(2) | ||
twice(function () { | ||
var trans = agent.startTransaction() | ||
setTimeout(function () { | ||
t.equal(ins.currentTransaction, trans) | ||
}, 50) | ||
}) | ||
}) | ||
|
||
test('setInterval', function (t) { | ||
t.plan(2) | ||
twice(function () { | ||
var trans = agent.startTransaction() | ||
var timer = setInterval(function () { | ||
clearInterval(timer) | ||
t.equal(ins.currentTransaction, trans) | ||
}, 50) | ||
}) | ||
}) | ||
|
||
test('setImmediate', function (t) { | ||
t.plan(2) | ||
twice(function () { | ||
var trans = agent.startTransaction() | ||
setImmediate(function () { | ||
t.equal(ins.currentTransaction, trans) | ||
}) | ||
}) | ||
}) | ||
|
||
test('process.nextTick', function (t) { | ||
t.plan(2) | ||
twice(function () { | ||
var trans = agent.startTransaction() | ||
process.nextTick(function () { | ||
t.equal(ins.currentTransaction, trans) | ||
}) | ||
}) | ||
}) | ||
|
||
function twice (fn) { | ||
setImmediate(fn) | ||
setImmediate(fn) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'use strict' | ||
|
||
var agent = require('../..').start({ | ||
appName: 'test', | ||
captureExceptions: false | ||
}) | ||
|
||
var http = require('http') | ||
var send = require('send') | ||
var test = require('tape') | ||
|
||
// run it 5 times in case of false positives due to race conditions | ||
times(5, function (n, done) { | ||
test('https://github.com/elastic/apm-agent-nodejs/issues/75 ' + n, function (t) { | ||
resetAgent(function (endpoint, headers, data, cb) { | ||
t.equal(data.transactions.length, 2, 'should create transactions') | ||
data.transactions.forEach(function (trans) { | ||
t.equal(trans.traces.length, 1, 'transaction should have one trace') | ||
t.equal(trans.traces[0].name, trans.id, 'trace should belong to transaction') | ||
}) | ||
server.close() | ||
t.end() | ||
done() | ||
}) | ||
|
||
var server = http.createServer(function (req, res) { | ||
var trace = agent.buildTrace() | ||
trace.start(agent._instrumentation.currentTransaction.id) | ||
setTimeout(function () { | ||
trace.end() | ||
send(req, __filename).pipe(res) | ||
}, 50) | ||
}) | ||
|
||
var requestNo = 0 | ||
|
||
server.listen(function () { | ||
request() | ||
request() | ||
}) | ||
|
||
function request () { | ||
var port = server.address().port | ||
http.get('http://localhost:' + port, function (res) { | ||
res.on('end', function () { | ||
if (++requestNo === 2) { | ||
agent._instrumentation._queue._flush() | ||
} | ||
}) | ||
res.resume() | ||
}) | ||
} | ||
}) | ||
}) | ||
|
||
function times (max, fn) { | ||
var n = 0 | ||
run() | ||
function run () { | ||
if (++n > max) return | ||
fn(n, run) | ||
} | ||
} | ||
|
||
function resetAgent (cb) { | ||
agent._instrumentation.currentTransaction = null | ||
agent._instrumentation._queue._clear() | ||
agent._httpClient = { request: cb || function () {} } | ||
agent.captureError = function (err) { throw err } | ||
} |