diff --git a/lib/e2e/index.spec.js b/lib/e2e/index.spec.js new file mode 100644 index 0000000..30bfac2 --- /dev/null +++ b/lib/e2e/index.spec.js @@ -0,0 +1,39 @@ +var stream = require('stream'); + +var cls = require('continuation-local-storage'); +var expect = require('chai').expect; +var sinon = require('sinon'); + +var collectorConfig = require('../config'); +var wraps = require('../wraps'); + +describe('Trace', function () { + + before(function () { + sinon.sandbox.stub(collectorConfig, 'appName', 'test'); + sinon.sandbox.stub(collectorConfig, 'reporterType', 'logstash'); + sinon.sandbox.stub(collectorConfig, 'reporterConfig', + '{"type":"tcp","host":"localhost","port":12201}'); + }); + + after(function () { + wraps.uninstrument(); + }); + + it('binds the stream core module', function () { + var originalCreateNamespace = cls.createNamespace; + var bindEmitterSpy; + + var sandbox = this.sandbox; + + sinon.sandbox.stub(cls, 'createNamespace', function (str) { + var ns = originalCreateNamespace(str); + bindEmitterSpy = sandbox.spy(ns, 'bindEmitter'); + return ns; + }); + + require('../'); + + expect(bindEmitterSpy).to.have.been.calledWith(stream.prototype); + }); +}); diff --git a/lib/index.spec.js b/lib/index.spec.js index 6a09b1a..9701081 100644 --- a/lib/index.spec.js +++ b/lib/index.spec.js @@ -32,7 +32,7 @@ describe('The Trace module', function () { }); after(function () { - wraps.uninstrumentNatives(); + wraps.uninstrument(); }); it('exposes methods', function () { diff --git a/lib/wraps/http.Server.prototype.js b/lib/wraps/http.Server.prototype.js index a6abca3..017d188 100644 --- a/lib/wraps/http.Server.prototype.js +++ b/lib/wraps/http.Server.prototype.js @@ -75,11 +75,8 @@ function wrapListener(listener, collector) { originalWriteHead.apply(response, arguments); }; - function addSession () { - session.bindEmitter(response); - session.bindEmitter(request); + function addSession() { session.set('request-id', requestId); - return listener.apply(this, arguments); } diff --git a/lib/wraps/http.request.js b/lib/wraps/http.request.js index a5823fa..db89153 100644 --- a/lib/wraps/http.request.js +++ b/lib/wraps/http.request.js @@ -68,7 +68,6 @@ function wrapRequest (original, collector) { */ var returned; returned = original.apply(this, arguments); - session.bindEmitter(returned); returned.on('error', function (err) { var collectorDataBag = { diff --git a/lib/wraps/http.request.spec.js b/lib/wraps/http.request.spec.js index 4a05eb8..ccbe845 100644 --- a/lib/wraps/http.request.spec.js +++ b/lib/wraps/http.request.spec.js @@ -14,7 +14,7 @@ var dummyCollector = { } }; -describe('The wrapper module', function () { +describe('The http.request wrapper module', function () { before(function () { require('continuation-local-storage').createNamespace('trace'); diff --git a/lib/wraps/index.js b/lib/wraps/index.js index 7e134ed..b408220 100644 --- a/lib/wraps/index.js +++ b/lib/wraps/index.js @@ -1,8 +1,9 @@ var http = require('http'); var https = require('https'); +var stream = require('stream'); +var getNamespace = require('continuation-local-storage').getNamespace; var Shimmer = require('./shimmer'); -var thirdParty = require('./third-party'); function instrument (collector) { Shimmer.wrap(http.Server.prototype, 'http.Server.prototype', ['on', 'addListener'], @@ -41,15 +42,15 @@ function instrument (collector) { return require('./process._fatalException')(original, collector); }); - thirdParty.instrument(); + getNamespace('trace').bindEmitter(stream.prototype); } -function uninstrumentNatives () { +function uninstrument () { Shimmer.unwrap(https.Server.prototype, 'https.Server.prototype', 'on'); Shimmer.unwrap(https.Server.prototype, 'https.Server.prototype', 'addListener'); - Shimmer.unwrap(http.Server.prototype, 'https.Server.prototype', 'addListener'); - Shimmer.unwrap(http.Server.prototype, 'https.Server.prototype', 'addListener'); + Shimmer.unwrap(http.Server.prototype, 'http.Server.prototype', 'addListener'); + Shimmer.unwrap(http.Server.prototype, 'http.Server.prototype', 'addListener'); Shimmer.unwrap(http, 'http', 'request'); @@ -59,4 +60,4 @@ function uninstrumentNatives () { } module.exports.instrument = instrument; -module.exports.uninstrumentNatives = uninstrumentNatives; +module.exports.uninstrument = uninstrument; diff --git a/lib/wraps/process._fatalException.spec.js b/lib/wraps/process._fatalException.spec.js index 2be8a7b..f2077b0 100644 --- a/lib/wraps/process._fatalException.spec.js +++ b/lib/wraps/process._fatalException.spec.js @@ -17,7 +17,7 @@ function startFailingServer () { return child; } -describe('The stacktrace wrapper module', function () { +describe('The process._fatalException wrapper module', function () { var spawned; diff --git a/lib/wraps/third-party/index.js b/lib/wraps/third-party/index.js deleted file mode 100644 index ebba39b..0000000 --- a/lib/wraps/third-party/index.js +++ /dev/null @@ -1,42 +0,0 @@ -var Shimmer = require('../shimmer'); -var getNamespace = require('continuation-local-storage').getNamespace; - -function wrapSuperagent() { - var session = getNamespace('trace'); - var superagent; - - try { - superagent = require('superagent'); - } catch (ex) { - return; - } - - Shimmer.wrap(superagent.Request.prototype, 'superagent.Request.prototype', 'end', - function (original) { - return function (fn) { - fn = session.bind(fn); - - return original.call(this, fn); - }; - }); -} - -function wrapRequest() { - var session = getNamespace('trace'); - var request; - - try { - request = require('request'); - } catch (ex) { - return; - } - - session.bindEmitter(request.Request.prototype); -} - -function instrument() { - wrapSuperagent(); - wrapRequest(); -} - -module.exports.instrument = instrument;